From 8610bd7a87cfc1bdf5b98083e2055d3de70e983f Mon Sep 17 00:00:00 2001 From: greg Date: Wed, 13 Mar 2019 22:43:44 -0700 Subject: [PATCH] Port Schala to new framework Evaluating a Schala function in the REPL works again with no debug info --- schala-lang/language/src/lib.rs | 31 ++++++++++++++++++++++++++++++- schala-repl/Cargo.toml | 4 ++-- schala-repl/src/lib.rs | 2 +- schala-repl/src/repl/mod.rs | 24 +++++++++++++++++++++--- 4 files changed, 54 insertions(+), 7 deletions(-) diff --git a/schala-lang/language/src/lib.rs b/schala-lang/language/src/lib.rs index bc1fbeb..d44815a 100644 --- a/schala-lang/language/src/lib.rs +++ b/schala-lang/language/src/lib.rs @@ -23,7 +23,7 @@ use std::cell::RefCell; use std::rc::Rc; use itertools::Itertools; -use schala_repl::{ProgrammingLanguageInterface, EvalOptions, TraceArtifact, UnfinishedComputation, FinishedComputation}; +use schala_repl::{ProgrammingLanguageInterface, EvalOptions, TraceArtifact, UnfinishedComputation, FinishedComputation, ComputationRequest, ComputationResponse, GlobalOutputStats}; macro_rules! bx { ($e:expr) => { Box::new($e) } @@ -43,12 +43,14 @@ mod reduced_ast; mod eval; //trace_macros!(true); +/* #[derive(ProgrammingLanguageInterface)] #[LanguageName = "Schala"] #[SourceFileExtension = "schala"] #[PipelineSteps(load_source, tokenizing, parsing(compact,expanded,trace), symbol_table, typechecking, ast_reducing, eval)] #[DocMethod = "get_doc"] #[HandleCustomInterpreterDirectives = "handle_custom_interpreter_directives"] +*/ /// All bits of state necessary to parse and execute a Schala program are stored in this struct. /// `state` represents the execution state for the AST-walking interpreter, the other fields /// should be self-explanatory. @@ -218,3 +220,30 @@ impl SourceReference { self.lines.as_ref().and_then(|x| x.get(line).map(|s| s.to_string())).unwrap_or(format!("NO LINE FOUND")) } } + +impl ProgrammingLanguageInterface for Schala { + fn get_language_name(&self) -> String { format!("Schala") } + fn get_source_file_suffix(&self) -> String { format!("schala") } + + fn run_computation(&mut self, request: ComputationRequest) -> ComputationResponse { + let ComputationRequest { source, debug_requests } = request; + + load_source(&source, self, None); + let main_output: Result = tokenizing(&source, self, None) + .and_then(|tokens| parsing(tokens, self, None)) + .and_then(|ast| symbol_table(ast, self, None)) + .and_then(|ast| typechecking(ast, self, None)) + .and_then(|ast| ast_reducing(ast, self, None)) + .and_then(|reduced_ast| eval(reduced_ast, self, None)); + + ComputationResponse { + main_output, + global_output_stats: GlobalOutputStats::default(), + debug_responses: vec![], + } + } + + fn repl_request(&self, repl_request: String) -> String { + format!("Schala: can't understand {}", repl_request) + } +} diff --git a/schala-repl/Cargo.toml b/schala-repl/Cargo.toml index 9dfc308..6c9a10c 100644 --- a/schala-repl/Cargo.toml +++ b/schala-repl/Cargo.toml @@ -8,10 +8,10 @@ edition = "2018" llvm-sys = "70.0.2" take_mut = "0.2.2" itertools = "0.5.8" -getopts = "*" +getopts = "0.2.18" lazy_static = "0.2.8" maplit = "*" -colored = "1.5" +colored = "1.7" serde = "1.0.15" serde_derive = "1.0.15" serde_json = "1.0.3" diff --git a/schala-repl/src/lib.rs b/schala-repl/src/lib.rs index 4d12354..013e9ec 100644 --- a/schala-repl/src/lib.rs +++ b/schala-repl/src/lib.rs @@ -57,7 +57,7 @@ fn command_line_options() -> getopts::Options { /* --------------------------- */ pub use language::{ProgrammingLanguageInterface, EvalOptions, - ExecutionMethod, TraceArtifact, FinishedComputation, UnfinishedComputation, PassDebugOptionsDescriptor, PassDescriptor}; + ExecutionMethod, TraceArtifact, FinishedComputation, UnfinishedComputation, PassDebugOptionsDescriptor, PassDescriptor, ComputationRequest, ComputationResponse, GlobalOutputStats}; pub type PLIGenerator = Box Box + Send + Sync>; diff --git a/schala-repl/src/repl/mod.rs b/schala-repl/src/repl/mod.rs index a673066..11186ed 100644 --- a/schala-repl/src/repl/mod.rs +++ b/schala-repl/src/repl/mod.rs @@ -6,7 +6,7 @@ use std::sync::Arc; use colored::*; use itertools::Itertools; use crate::language::{ProgrammingLanguageInterface, EvalOptions, - PassDebugOptionsDescriptor}; + PassDebugOptionsDescriptor, ComputationRequest, ComputationResponse}; mod command_tree; use self::command_tree::CommandTree; @@ -96,11 +96,29 @@ impl NewRepl { } fn handle_interpreter_directive(&mut self, input: &str) -> Option { - None + Some(format!("you typed {}, which is unsupported", input)) + } + + fn get_cur_language_state(&mut self) -> &mut Box { + //TODO this is obviously not complete + &mut self.language_states[0] } fn handle_input(&mut self, input: &str) -> String { - format!("") + let ref mut language_state = self.get_cur_language_state(); + + let request = ComputationRequest { + source: input.to_string(), + debug_requests: vec![], + }; + + let ComputationResponse { main_output, global_output_stats, debug_responses } + = language_state.run_computation(request); + + match main_output { + Ok(s) => s, + Err(e) => format!("{} {}", "Error".red(), e) + } } }