From 75b1f9cce570e53a9246b565abe8099ead570824 Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Thu, 14 Oct 2021 00:03:51 -0700 Subject: [PATCH] Working on main compiler pipeline Got rid of a bunch of confusingly-designed debugging infrastruture. Need a better way to handle per-stage debug outputs (also I don't want to be using stages at all long-term) --- schala-lang/language/src/debugging.rs | 10 -- schala-lang/language/src/eval.rs | 4 - schala-lang/language/src/lib.rs | 1 - schala-lang/language/src/parsing.rs | 1 + schala-lang/language/src/schala.rs | 178 +---------------------- schala-lang/language/src/symbol_table.rs | 1 + schala-lang/language/src/typechecking.rs | 2 + 7 files changed, 10 insertions(+), 187 deletions(-) delete mode 100644 schala-lang/language/src/debugging.rs diff --git a/schala-lang/language/src/debugging.rs b/schala-lang/language/src/debugging.rs deleted file mode 100644 index d4e8d10..0000000 --- a/schala-lang/language/src/debugging.rs +++ /dev/null @@ -1,10 +0,0 @@ -use crate::ast::*; - -impl AST { - pub fn compact_debug(&self) -> String { - format!("{:?}", self) - } - pub fn expanded_debug(&self) -> String { - format!("{:#?}", self) - } -} diff --git a/schala-lang/language/src/eval.rs b/schala-lang/language/src/eval.rs index d6cba0e..441c9d7 100644 --- a/schala-lang/language/src/eval.rs +++ b/schala-lang/language/src/eval.rs @@ -20,10 +20,6 @@ impl<'a> State<'a> { State { values } } - pub fn debug_print(&self) -> String { - format!("Values: {:?}", self.values) - } - fn new_frame(&'a self, items: &'a Vec, bound_vars: &BoundVars) -> State<'a> { let mut inner_state = State { values: self.values.new_scope(None), diff --git a/schala-lang/language/src/lib.rs b/schala-lang/language/src/lib.rs index 21d2a13..39736f2 100644 --- a/schala-lang/language/src/lib.rs +++ b/schala-lang/language/src/lib.rs @@ -24,7 +24,6 @@ macro_rules! bx { mod util; #[macro_use] mod typechecking; -mod debugging; mod tokenizing; mod ast; diff --git a/schala-lang/language/src/parsing.rs b/schala-lang/language/src/parsing.rs index 0d00048..525d01d 100644 --- a/schala-lang/language/src/parsing.rs +++ b/schala-lang/language/src/parsing.rs @@ -267,6 +267,7 @@ impl Parser { self.program() } + #[allow(dead_code)] pub fn format_parse_trace(&self) -> String { let mut buf = String::new(); buf.push_str("Parse productions:\n"); diff --git a/schala-lang/language/src/schala.rs b/schala-lang/language/src/schala.rs index c7859c7..7d4dc4d 100644 --- a/schala-lang/language/src/schala.rs +++ b/schala-lang/language/src/schala.rs @@ -4,12 +4,10 @@ use std::cell::RefCell; use std::rc::Rc; use std::collections::HashSet; -use itertools::Itertools; use schala_repl::{ProgrammingLanguageInterface, ComputationRequest, ComputationResponse, -LangMetaRequest, LangMetaResponse, GlobalOutputStats, -DebugResponse, DebugAsk}; -use crate::{ast, reduced_ast, tokenizing, parsing, eval, typechecking, symbol_table, source_map}; +LangMetaRequest, LangMetaResponse, GlobalOutputStats}; +use crate::{reduced_ast, tokenizing, parsing, eval, typechecking, symbol_table, source_map}; pub type SymbolTableHandle = Rc>; pub type SourceMapHandle = Rc>; @@ -99,8 +97,9 @@ impl Schala { self.resolver.resolve(&mut ast)?; // Typechecking - let overall_type = self.type_context.typecheck(&ast) - .map_err(|err| format!("Type error: {}", err.msg))?; + // TODO typechecking not working + 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(); @@ -119,31 +118,6 @@ impl Schala { } } -fn parsing(input: Vec, handle: &mut Schala, comp: Option<&mut PassDebugArtifact>) -> Result { - use ParsingDebugType::*; - - let ref mut parser = handle.active_parser; - parser.add_new_tokens(input); - let ast = parser.parse(); - - comp.map(|comp| { - let debug_format = comp.parsing.as_ref().unwrap_or(&CompactAST); - let debug_info = match debug_format { - CompactAST => match ast{ - Ok(ref ast) => ast.compact_debug(), - Err(_) => "Error - see output".to_string(), - }, - ExpandedAST => match ast{ - Ok(ref ast) => ast.expanded_debug(), - Err(_) => "Error - see output".to_string(), - }, - Trace => parser.format_parse_trace(), - }; - comp.add_artifact(debug_info); - }); - ast.map_err(|err| format_parse_error(err, &handle.source_reference)) -} - fn format_parse_error(error: parsing::ParseError, source_reference: &SourceReference) -> String { let line_num = error.token.location.line_num; let ch = error.token.location.char_num; @@ -167,52 +141,6 @@ fn format_parse_error(error: parsing::ParseError, source_reference: &SourceRefer ) } -fn symbol_table(input: ast::AST, handle: &mut Schala, comp: Option<&mut PassDebugArtifact>) -> Result { - let () = handle.symbol_table.borrow_mut().add_top_level_symbols(&input)?; - comp.map(|comp| { - let debug = handle.symbol_table.borrow().debug_symbol_table(); - comp.add_artifact(debug); - }); - Ok(input) -} - -fn scope_resolution(mut input: ast::AST, handle: &mut Schala, _com: Option<&mut PassDebugArtifact>) -> Result { - let () = handle.resolver.resolve(&mut input)?; - Ok(input) -} - -fn typechecking(input: ast::AST, handle: &mut Schala, comp: Option<&mut PassDebugArtifact>) -> Result { - let result = handle.type_context.typecheck(&input); - - comp.map(|comp| { - comp.add_artifact(match result { - Ok(ty) => ty.to_string(), - Err(err) => format!("Type error: {}", err.msg) - }); - }); - - Ok(input) -} - -fn ast_reducing(input: ast::AST, handle: &mut Schala, comp: Option<&mut PassDebugArtifact>) -> Result { - let ref symbol_table = handle.symbol_table.borrow(); - let output = reduced_ast::reduce(&input, symbol_table); - comp.map(|comp| comp.add_artifact(format!("{:?}", output))); - Ok(output) -} - -fn eval(input: reduced_ast::ReducedAST, handle: &mut Schala, comp: Option<&mut PassDebugArtifact>) -> Result { - comp.map(|comp| comp.add_artifact(handle.state.debug_print())); - let evaluation_outputs = handle.state.evaluate(input, true); - let text_output: Result, String> = evaluation_outputs - .into_iter() - .collect(); - - let eval_output: Result = text_output - .map(|v| { Iterator::intersperse(v.into_iter(), "\n".to_owned()).collect() }); - eval_output -} - /// Represents lines of source code struct SourceReference { lines: Option> @@ -232,24 +160,6 @@ impl SourceReference { } } -enum ParsingDebugType { - CompactAST, - ExpandedAST, - Trace -} - -#[derive(Default)] -struct PassDebugArtifact { - parsing: Option, - artifacts: Vec - -} -impl PassDebugArtifact { - fn add_artifact(&mut self, artifact: String) { - self.artifacts.push(artifact) - } -} - fn stage_names() -> Vec<&'static str> { vec![ "tokenizing", @@ -273,7 +183,7 @@ impl ProgrammingLanguageInterface for Schala { } fn run_computation(&mut self, request: ComputationRequest) -> ComputationResponse { - let ComputationRequest { source, debug_requests } = request; + let ComputationRequest { source, debug_requests: _ } = request; self.source_reference.load_new_source(source); let sw = Stopwatch::start_new(); @@ -291,82 +201,6 @@ impl ProgrammingLanguageInterface for Schala { } } - // 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, - stage_durations: &'a mut Vec<(String, Duration)>, - sw: &'a Stopwatch, - debug_requests: &'a HashSet, - debug_responses: &'a mut Vec, - } - - fn output_wrapper(n: usize, func: F, input: Input, token: &mut PassToken) -> Result - where F: Fn(Input, &mut Schala, Option<&mut PassDebugArtifact>) -> Result - { - let stage_names = stage_names(); - let cur_stage_name = stage_names[n]; - let ask = token.debug_requests.iter().find(|ask| ask.is_for_stage(cur_stage_name)); - - let parsing = match ask { - Some(DebugAsk::ByStage { token, .. }) if cur_stage_name == "parsing" => Some( - token.as_ref().map(|token| match &token[..] { - "compact" => ParsingDebugType::CompactAST, - "expanded" => ParsingDebugType::ExpandedAST, - "trace" => ParsingDebugType::Trace, - _ => ParsingDebugType::CompactAST, - }).unwrap_or(ParsingDebugType::CompactAST) - ), - _ => None, - }; - - let mut debug_artifact = ask.map(|_| PassDebugArtifact { - parsing, ..Default::default() - }); - - let output = func(input, token.schala, debug_artifact.as_mut()); - - //TODO I think this is not counting the time since the *previous* stage - token.stage_durations.push((cur_stage_name.to_string(), token.sw.elapsed())); - if let Some(artifact) = debug_artifact { - for value in artifact.artifacts.into_iter() { - let resp = DebugResponse { ask: ask.unwrap().clone(), value }; - token.debug_responses.push(resp); - } - } - output - } - - let ComputationRequest { source, debug_requests } = request; - self.source_reference.load_new_source(source); - let sw = Stopwatch::start_new(); - let mut stage_durations = Vec::new(); - let mut debug_responses = Vec::new(); - let mut tok = PassToken { schala: self, stage_durations: &mut stage_durations, sw: &sw, debug_requests: &debug_requests, debug_responses: &mut debug_responses }; - - let main_output: Result = Ok(source) - .and_then(|source| output_wrapper(0, tokenizing, source, &mut tok)) - .and_then(|tokens| output_wrapper(1, parsing, tokens, &mut tok)) - .and_then(|ast| output_wrapper(2, symbol_table, ast, &mut tok)) - .and_then(|ast| output_wrapper(3, scope_resolution, ast, &mut tok)) - .and_then(|ast| output_wrapper(4, typechecking, ast, &mut tok)) - .and_then(|ast| output_wrapper(5, ast_reducing, ast, &mut tok)) - .and_then(|reduced_ast| output_wrapper(6, eval, reduced_ast, &mut tok)); - - let total_duration = sw.elapsed(); - let global_output_stats = GlobalOutputStats { - total_duration, stage_durations - }; - - ComputationResponse { - main_output, - global_output_stats, - 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()), diff --git a/schala-lang/language/src/symbol_table.rs b/schala-lang/language/src/symbol_table.rs index ff0efb9..64662e8 100644 --- a/schala-lang/language/src/symbol_table.rs +++ b/schala-lang/language/src/symbol_table.rs @@ -242,6 +242,7 @@ impl SymbolTable { } Ok(()) } + #[allow(dead_code)] pub fn debug_symbol_table(&self) -> String { let mut output = format!("Symbol table\n"); let mut sorted_symbols: Vec<(&FullyQualifiedSymbolName, &Symbol)> = self.symbol_path_to_symbol.iter().collect(); diff --git a/schala-lang/language/src/typechecking.rs b/schala-lang/language/src/typechecking.rs index 9db2fc2..7665b59 100644 --- a/schala-lang/language/src/typechecking.rs +++ b/schala-lang/language/src/typechecking.rs @@ -77,6 +77,7 @@ pub enum TypeConst { } impl TypeConst { + #[allow(dead_code)] pub fn to_string(&self) -> String { use self::TypeConst::*; match self { @@ -107,6 +108,7 @@ macro_rules! ty { //TODO find a better way to capture the to/from string logic impl Type { + #[allow(dead_code)] pub fn to_string(&self) -> String { use self::Type::*; match self {