From 4cf419a2e770f4971be7b03b1d73e585d973dc1b Mon Sep 17 00:00:00 2001 From: Stefano Zacchiroli Date: Tue, 4 May 2004 10:47:10 +0000 Subject: [PATCH] snapshot (notably: first working version of the console) --- helm/matita/.depend | 10 ++-- helm/matita/matita.ml | 4 +- helm/matita/matitaConsole.ml | 50 ++++++++++------ helm/matita/matitaConsole.mli | 2 + helm/matita/matitaDisambiguator.ml | 14 +---- helm/matita/matitaGui.ml | 3 +- helm/matita/matitaInterpreter.ml | 94 +++++++++++++++++++----------- helm/matita/matitaInterpreter.mli | 2 + helm/matita/matitaTypes.ml | 3 - 9 files changed, 106 insertions(+), 76 deletions(-) diff --git a/helm/matita/.depend b/helm/matita/.depend index 4576d80ba..8d7e4e7b9 100644 --- a/helm/matita/.depend +++ b/helm/matita/.depend @@ -10,8 +10,10 @@ matitaGui.cmo: matitaConsole.cmi matitaGeneratedGui.cmi matitaGtkMisc.cmi \ matitaGui.cmi matitaGui.cmx: matitaConsole.cmx matitaGeneratedGui.cmx matitaGtkMisc.cmx \ matitaGui.cmi -matitaInterpreter.cmo: matitaProof.cmi matitaTypes.cmo matitaInterpreter.cmi -matitaInterpreter.cmx: matitaProof.cmx matitaTypes.cmx matitaInterpreter.cmi +matitaInterpreter.cmo: matitaConsole.cmi matitaProof.cmi matitaTypes.cmo \ + matitaInterpreter.cmi +matitaInterpreter.cmx: matitaConsole.cmx matitaProof.cmx matitaTypes.cmx \ + matitaInterpreter.cmi matita.cmo: buildTimeConf.cmo matitaDisambiguator.cmi matitaGtkMisc.cmi \ matitaGui.cmi matitaInterpreter.cmi matitaProof.cmi matitaTypes.cmo matita.cmx: buildTimeConf.cmx matitaDisambiguator.cmx matitaGtkMisc.cmx \ @@ -22,6 +24,6 @@ matitaTypes.cmo: buildTimeConf.cmo matitaTypes.cmx: buildTimeConf.cmx matitaDisambiguator.cmi: matitaTypes.cmo matitaGtkMisc.cmi: matitaGeneratedGui.cmi matitaTypes.cmo -matitaGui.cmi: matitaGeneratedGui.cmi -matitaInterpreter.cmi: matitaTypes.cmo +matitaGui.cmi: matitaConsole.cmi matitaGeneratedGui.cmi +matitaInterpreter.cmi: matitaConsole.cmi matitaTypes.cmo matitaProof.cmi: matitaTypes.cmo diff --git a/helm/matita/matita.ml b/helm/matita/matita.ml index 8f34f5bf7..8954ec704 100644 --- a/helm/matita/matita.ml +++ b/helm/matita/matita.ml @@ -33,7 +33,9 @@ let (get_proof, set_proof, has_proof) = match !current_proof with | Some proof -> proof | None -> failwith "No current proof"), - (fun proof -> current_proof := proof), + (fun proof -> (* TODO Zack: this function should probably be smarter taking + care also of unregistering notifications subscriber and so on *) + current_proof := proof), (fun () -> !current_proof <> None)) (** {2 Settings} *) diff --git a/helm/matita/matitaConsole.ml b/helm/matita/matitaConsole.ml index a2c0515bd..9c3c5a9b5 100644 --- a/helm/matita/matitaConsole.ml +++ b/helm/matita/matitaConsole.ml @@ -33,12 +33,13 @@ let message_props = [ `STYLE `ITALIC ] let error_props = [ `WEIGHT `BOLD ] let prompt_props = [ ] +let trailing_NL_RE = Pcre.regexp "\n\\s*$" + class console ?(prompt = default_prompt) ?(phrase_sep = default_phrase_sep) ?(callback = default_callback) obj = - let ui_mark = `NAME "USER_INPUT_START" in object (self) inherit GText.view obj @@ -49,6 +50,10 @@ class console val mutable _callback = callback method set_callback f = _callback <- f + val mutable _ignore_insert_text_signal = false + method ignore_insert_text_signal ignore = + _ignore_insert_text_signal <- ignore + (* (* TODO Zack: implement history. IDEA: use CTRL-P/N a la emacs. @@ -60,49 +65,56 @@ class console initializer let buf = self#buffer in + self#set_wrap_mode `CHAR; (* create "USER_INPUT_START" mark. This mark will always point to the * beginning of user input not yet processed *) ignore (buf#create_mark ~name:"USER_INPUT_START" ~left_gravity:true buf#start_iter); - ignore (self#event#connect#key_press (fun key -> (* handle return ev. *) - if GdkEvent.Key.keyval key = GdkKeysyms._Return then begin - let insert_point = buf#get_iter_at_mark `INSERT in - if insert_point#compare buf#end_iter = 0 then (* insert pt. at end *) - let inserted_text = - buf#get_text ~start:(buf#get_iter_at_mark ui_mark) - ~stop:buf#end_iter () - in - let pat = (Pcre.quote _phrase_sep) ^ "\\s*$" in - if Pcre.pmatch ~pat inserted_text then begin (* complete phrase *) - self#lock; - _callback inserted_text - end - end; - false (* continue event processing *))) + ignore (buf#connect#after#insert_text (fun iter text -> + if (not _ignore_insert_text_signal) && + (iter#compare buf#end_iter = 0) && (* insertion at end *) + (Pcre.pmatch ~rex:trailing_NL_RE text) + then + let inserted_text = + buf#get_text + ~start:(buf#get_iter_at_mark (`NAME "USER_INPUT_START")) + ~stop:buf#end_iter () + in + let pat = (Pcre.quote _phrase_sep) ^ "\\s*$" in + if Pcre.pmatch ~pat inserted_text then begin (* complete phrase *) + self#lock; + _callback inserted_text; + self#echo_prompt () + end)) (* lock old text and bump USER_INPUT_START mark *) method private lock = let buf = self#buffer in let read_only = buf#create_tag [`EDITABLE false] in - let stop = buf#end_iter in - buf#apply_tag read_only ~start:buf#start_iter ~stop; - buf#move_mark ui_mark stop + buf#apply_tag read_only ~start:buf#start_iter ~stop:buf#end_iter; + buf#move_mark (`NAME "USER_INPUT_START") buf#end_iter method echo_prompt () = let buf = self#buffer in + self#ignore_insert_text_signal true; buf#insert ~iter:buf#end_iter ~tags:[buf#create_tag prompt_props] prompt; + self#ignore_insert_text_signal false; self#lock method echo_message msg = let buf = self#buffer in + self#ignore_insert_text_signal true; buf#insert ~iter:buf#end_iter ~tags:[buf#create_tag message_props] (msg ^ "\n"); + self#ignore_insert_text_signal false; self#lock method echo_error msg = let buf = self#buffer in + self#ignore_insert_text_signal true; buf#insert ~iter:buf#end_iter ~tags:[buf#create_tag error_props] (msg ^ "\n"); + self#ignore_insert_text_signal false; self#lock end diff --git a/helm/matita/matitaConsole.mli b/helm/matita/matitaConsole.mli index ee7b8d4fb..2cbd1ffe3 100644 --- a/helm/matita/matitaConsole.mli +++ b/helm/matita/matitaConsole.mli @@ -38,6 +38,8 @@ class console: (** override previous callback definition *) method set_callback : (string -> unit) -> unit + + method ignore_insert_text_signal: bool -> unit end (** @param prompt user prompt (default "# ") diff --git a/helm/matita/matitaDisambiguator.ml b/helm/matita/matitaDisambiguator.ml index 87215fba1..daf64884c 100644 --- a/helm/matita/matitaDisambiguator.ml +++ b/helm/matita/matitaDisambiguator.ml @@ -25,18 +25,8 @@ class parserr () = object - method parseTerm (stream: char Stream.t) = - CicTextualParser2.parse_term stream - - (* TODO Zack: implements methods below *) - method parseTactic (_: char Stream.t) : DisambiguateTypes.tactic = - MatitaTypes.not_implemented "parserr.parseTactic" - method parseTactical (_: char Stream.t) : DisambiguateTypes.tactical = - MatitaTypes.not_implemented "parserr.parseTactical" - method parseCommand (_: char Stream.t) : DisambiguateTypes.command = - MatitaTypes.not_implemented "parserr.parseCommand" - method parseScript (_: char Stream.t) : DisambiguateTypes.script = - MatitaTypes.not_implemented "parserr.parseScript" + method parseTerm = CicTextualParser2.parse_term + method parseTactical = CicTextualParser2.parse_tactical end class disambiguator diff --git a/helm/matita/matitaGui.ml b/helm/matita/matitaGui.ml index 95882a0f1..2018d7176 100644 --- a/helm/matita/matitaGui.ml +++ b/helm/matita/matitaGui.ml @@ -80,8 +80,7 @@ class gui file = [ main#saveMenuItem; main#saveAsMenuItem ]; main#helpMenu#set_right_justified true; (* console *) - console#echo_message "message"; - console#echo_error "error"; + console#echo_message "\tMatita version 0.0.1\n"; console#echo_prompt (); console#misc#grab_focus () diff --git a/helm/matita/matitaInterpreter.ml b/helm/matita/matitaInterpreter.ml index 4b87ec8f8..f5dd123f5 100644 --- a/helm/matita/matitaInterpreter.ml +++ b/helm/matita/matitaInterpreter.ml @@ -23,13 +23,22 @@ * http://helm.cs.unibo.it/ *) +open Printf + type state_tag = [ `Command | `Proof ] -class type interpreterState = - object +exception Command_not_found of string + +class virtual interpreterState ~(console: MatitaConsole.console) = + object (self) (** eval a toplevel phrase in the current state and return the new state *) - method evalPhrase: string -> state_tag + method parsePhrase s = CicTextualParser2.parse_tactical (Stream.of_string s) + + method virtual evalTactical: + (CicAst.term, string) TacticAst.tactical -> state_tag + + method evalPhrase s = self#evalTactical (self#parsePhrase s) end class commandState @@ -38,45 +47,58 @@ class commandState ~(console: MatitaConsole.console) () = - object - method evalPhrase s: state_tag = - let command = CicTextualParser2.parse_command (Stream.of_string s) in - match command with - | CommandAst.Theorem (_, _, Some name, ast, None) -> + object (self) + inherit interpreterState ~console + + method evalTactical = function +(* + | TacticAst.Command _ -> failwith "1" + | TacticAst.Tactic _ -> failwith "2" + | TacticAst.LocatedTactical _ -> failwith "3" + | TacticAst.Fail -> failwith "4" + | TacticAst.Do (_, _) -> failwith "5" + | TacticAst.IdTac -> failwith "6" + | TacticAst.Repeat _ -> failwith "7" + | TacticAst.Seq _ -> failwith "8" + | TacticAst.Then (_, _) -> failwith "9" + | TacticAst.Tries _ -> failwith "10" + | TacticAst.Try _ -> failwith "11" +*) + | TacticAst.LocatedTactical (_, tactical) -> self#evalTactical tactical + | TacticAst.Command (TacticAst.Theorem (_, Some name, ast, None)) -> let (_, metasenv, expr) = disambiguator#disambiguateTermAst ast in let _ = CicTypeChecker.type_of_aux' metasenv [] expr in let proof = MatitaProof.proof ~typ:expr ~metasenv () in proof_handler.MatitaTypes.new_proof proof; `Proof - | CommandAst.Quit _ -> + | TacticAst.Command TacticAst.Quit -> proof_handler.MatitaTypes.quit (); - `Command (* dummy answer *) - | _ -> - MatitaTypes.not_implemented (* TODO Zack *) - "MatitaInterpreter.commandState#evalPhrase: commands other than full theorem ones"; - `Proof + `Command (* dummy answer, useless *) + | TacticAst.Command TacticAst.Proof -> + (* do nothing, just for compatibility with coq syntax *) + `Command + | tactical -> + raise (Command_not_found (TacticAstPp.pp_tactical tactical)) end - (* TODO Zack FINQUI - * bisogna rivedere la grammatica di tatticali/comandi - * molti comandi (o addirittura tutti tranne Theorem) hanno senso anche nello - * stato proof, e' quindi un casino parsare la phrase. Un'idea potrebbe essere - * quella di tentare di parsare una tattica e se il parsing fallisce provare a - * parsare un comando (BLEAARGH). Oppure si puo' aggiungere una possibile entry - * nella grammatica delle tattiche che punti ad un comando (RI-BLEAARGH). - * Oppure boh ... - *) class proofState ~(disambiguator: MatitaTypes.disambiguator) ~(proof_handler: MatitaTypes.proof_handler) ~(console: MatitaConsole.console) () = + let commandState = + new commandState ~disambiguator ~proof_handler ~console () + in object - method evalPhrase (s: string): state_tag = - (* TODO Zack *) - MatitaTypes.not_implemented "MatitaInterpreter.proofState#evalPhrase"; - `Command + inherit interpreterState ~console + + method evalTactical = function + | TacticAst.Command TacticAst.Abort -> + proof_handler.MatitaTypes.set_proof None; + `Command + | tactical -> (* fallback on command state *) + commandState#evalTactical tactical end class interpreter @@ -86,17 +108,19 @@ class interpreter () = let commandState = - lazy (new commandState ~disambiguator ~proof_handler ~console ()) - in - let proofState = - lazy (new proofState ~disambiguator ~proof_handler ~console ()) + new commandState ~disambiguator ~proof_handler ~console () in + let proofState = new proofState ~disambiguator ~proof_handler ~console () in object - val mutable state = Lazy.force commandState + val mutable state = commandState method evalPhrase s = - match state#evalPhrase s with - | `Command -> state <- Lazy.force commandState - | `Proof -> state <- Lazy.force proofState + try + (match state#evalPhrase s with + | `Command -> state <- commandState + | `Proof -> state <- proofState) + with exn -> + console#echo_error (sprintf "Uncaught exception: %s" + (Printexc.to_string exn)) end diff --git a/helm/matita/matitaInterpreter.mli b/helm/matita/matitaInterpreter.mli index 1407f9c0e..a19c1c921 100644 --- a/helm/matita/matitaInterpreter.mli +++ b/helm/matita/matitaInterpreter.mli @@ -23,6 +23,8 @@ * http://helm.cs.unibo.it/ *) +exception Command_not_found of string + class interpreter: disambiguator:MatitaTypes.disambiguator -> proof_handler:MatitaTypes.proof_handler -> diff --git a/helm/matita/matitaTypes.ml b/helm/matita/matitaTypes.ml index 92fc79b7c..864e9604c 100644 --- a/helm/matita/matitaTypes.ml +++ b/helm/matita/matitaTypes.ml @@ -64,10 +64,7 @@ class type command = class type parserr = (* "parser" is a keyword :-( *) object method parseTerm: char Stream.t -> DisambiguateTypes.term - method parseTactic: char Stream.t -> DisambiguateTypes.tactic method parseTactical: char Stream.t -> DisambiguateTypes.tactical - method parseCommand: char Stream.t -> DisambiguateTypes.command - method parseScript: char Stream.t -> DisambiguateTypes.script end class type disambiguator = -- 2.39.2