diff --git a/schala-lang/language/src/ast.rs b/schala-lang/language/src/ast.rs index 1d9e6aa..29868a2 100644 --- a/schala-lang/language/src/ast.rs +++ b/schala-lang/language/src/ast.rs @@ -152,11 +152,10 @@ impl Expression { Expression { id, kind, type_anno: None } } - /* - commented out because unused + #[cfg(test)] pub fn with_anno(id: ItemId, kind: ExpressionKind, type_anno: TypeIdentifier) -> Expression { Expression { id, kind, type_anno: Some(type_anno) } } - */ } #[derive(Debug, PartialEq, Clone)] diff --git a/schala-lang/language/src/schala.rs b/schala-lang/language/src/schala.rs index 4a8e156..c7859c7 100644 --- a/schala-lang/language/src/schala.rs +++ b/schala-lang/language/src/schala.rs @@ -1,6 +1,5 @@ use stopwatch::Stopwatch; -use std::time::Duration; use std::cell::RefCell; use std::rc::Rc; use std::collections::HashSet; @@ -30,11 +29,14 @@ pub struct Schala { } impl Schala { + //TODO implement documentation for language items + /* fn handle_docs(&self, source: String) -> LangMetaResponse { LangMetaResponse::Docs { doc_string: format!("Schala item `{}` : <>", source) } } + */ } impl Schala { @@ -60,6 +62,7 @@ impl Schala { let prelude = include_str!("prelude.schala"); let mut s = Schala::new_blank_env(); + //TODO this shouldn't depend on schala-repl types let request = ComputationRequest { source: prelude, debug_requests: HashSet::default() }; let response = s.run_computation(request); if let Err(msg) = response.main_output { @@ -68,41 +71,51 @@ impl Schala { s } - fn handle_debug_immediate(&self, request: DebugAsk) -> DebugResponse { - use DebugAsk::*; - match request { - Timing => DebugResponse { ask: Timing, value: format!("Invalid") }, - ByStage { stage_name, token } => match &stage_name[..] { - "symbol-table" => { - let value = self.symbol_table.borrow().debug_symbol_table(); - DebugResponse { - ask: ByStage { stage_name: format!("symbol-table"), token }, - value - } - }, - s => { - DebugResponse { - ask: ByStage { stage_name: s.to_string(), token: None }, - value: format!("Not-implemented") - } - } - } + /// This is where the actual action of interpreting/compilation happens. + /// Note: this should eventually use a query-based system for parallelization, cf. + /// https://rustc-dev-guide.rust-lang.org/overview.html + fn run_pipeline(&mut self, source: &str) -> Result { + //TODO `run_pipeline` should return a formatted error struct + + //TODO every stage should have a common error-format that gets turned into a repl-appropriate + //string in `run_computation` + // 1st stage - tokenization + // TODO tokenize should return its own error type + let tokens = tokenizing::tokenize(source); + let token_errors: Vec = tokens.iter().filter_map(|t| t.get_error()).collect(); + if token_errors.len() > 0 { + return Err(format!("{:?}", token_errors)); } - } -} -fn tokenizing(input: &str, _handle: &mut Schala, comp: Option<&mut PassDebugArtifact>) -> Result, String> { - let tokens = tokenizing::tokenize(input); - comp.map(|comp| { - let token_string = tokens.iter().map(|t| t.to_string_with_metadata()).join(", "); - comp.add_artifact(token_string); - }); + //2nd stage - parsing + self.active_parser.add_new_tokens(tokens); + let mut ast = self.active_parser.parse() + .map_err(|err| format_parse_error(err, &self.source_reference))?; - let errors: Vec = tokens.iter().filter_map(|t| t.get_error()).collect(); - if errors.len() == 0 { - Ok(tokens) - } else { - Err(format!("{:?}", errors)) + // Symbol table + self.symbol_table.borrow_mut().add_top_level_symbols(&ast)?; + + // Scope resolution - requires mutating AST + self.resolver.resolve(&mut ast)?; + + // Typechecking + let overall_type = self.type_context.typecheck(&ast) + .map_err(|err| format!("Type error: {}", err.msg))?; + + // Reduce AST - this doesn't produce an error yet, but probably should + let symbol_table = self.symbol_table.borrow(); + let reduced_ast = reduced_ast::reduce(&ast, &symbol_table); + + // Tree-walking evaluator. TODO fix this + let evaluation_outputs = self.state.evaluate(reduced_ast, true); + let text_output: Result, String> = evaluation_outputs + .into_iter() + .collect(); + + let eval_output: String = text_output + .map(|v| { Iterator::intersperse(v.into_iter(), "\n".to_owned()).collect() })?; + + Ok(eval_output) } } @@ -259,6 +272,27 @@ impl ProgrammingLanguageInterface for Schala { "schala".to_owned() } + fn run_computation(&mut self, request: ComputationRequest) -> ComputationResponse { + let ComputationRequest { source, debug_requests } = request; + self.source_reference.load_new_source(source); + let sw = Stopwatch::start_new(); + + let main_output = self.run_pipeline(source); + + let global_output_stats = GlobalOutputStats { + total_duration: sw.elapsed(), + stage_durations: vec![] + }; + + ComputationResponse { + main_output, + global_output_stats, + debug_responses: vec![] + } + } + + // n.b. - old-style, overcomplicated `run_computation`. need to revamp the entire metadata system + /* fn run_computation(&mut self, request: ComputationRequest) -> ComputationResponse { struct PassToken<'a> { schala: &'a mut Schala, @@ -331,14 +365,12 @@ impl ProgrammingLanguageInterface for Schala { debug_responses, } } + */ fn request_meta(&mut self, request: LangMetaRequest) -> LangMetaResponse { match request { LangMetaRequest::StageNames => LangMetaResponse::StageNames(stage_names().iter().map(|s| s.to_string()).collect()), - LangMetaRequest::Docs { source } => self.handle_docs(source), - LangMetaRequest::ImmediateDebug(debug_request) => - LangMetaResponse::ImmediateDebug(self.handle_debug_immediate(debug_request)), - LangMetaRequest::Custom { .. } => LangMetaResponse::Custom { kind: format!("not-implemented"), value: format!("") } + _ => LangMetaResponse::Custom { kind: format!("not-implemented"), value: format!("") } } } }