From c674148772c3d61d3d0463accd43d65eea009f61 Mon Sep 17 00:00:00 2001 From: greg Date: Wed, 21 Feb 2018 02:31:28 -0800 Subject: [PATCH] Starting over with types --- src/schala_lang/mod.rs | 18 ++++++++++++-- src/schala_lang/typechecking.rs | 43 +++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 src/schala_lang/typechecking.rs diff --git a/src/schala_lang/mod.rs b/src/schala_lang/mod.rs index 5934b9d..96d81c7 100644 --- a/src/schala_lang/mod.rs +++ b/src/schala_lang/mod.rs @@ -2,10 +2,11 @@ use itertools::Itertools; use schala_lib::{ProgrammingLanguageInterface, EvalOptions, TraceArtifact, ReplOutput}; mod parsing; -mod type_check; +//mod type_check; +mod typechecking; mod eval; -use self::type_check::{TypeContext}; +use self::typechecking::{TypeContext}; pub struct Schala { state: eval::ReplState, @@ -61,6 +62,18 @@ impl ProgrammingLanguageInterface for Schala { } }; + match self.type_context.type_check_ast(&ast) { + Ok(ty) => { + output.add_artifact(TraceArtifact::new("type_check", format!("{:?}", ty))); + }, + Err(msg) => { + output.add_artifact(TraceArtifact::new("type_check", msg)); + output.add_output(format!("Type error")); + return output; + } + } + + /* self.type_context.add_symbols(&ast); if options.debug_symbol_table { @@ -78,6 +91,7 @@ impl ProgrammingLanguageInterface for Schala { return output; } } + */ let evaluation_outputs = self.state.evaluate(ast); let text_output: String = evaluation_outputs.into_iter().intersperse(format!("\n")).collect(); diff --git a/src/schala_lang/typechecking.rs b/src/schala_lang/typechecking.rs new file mode 100644 index 0000000..dbbecc3 --- /dev/null +++ b/src/schala_lang/typechecking.rs @@ -0,0 +1,43 @@ +use std::rc::Rc; + +use schala_lang::parsing; + +pub struct TypeContext { } + +#[derive(Debug, PartialEq)] +pub enum Type { + Unit, + Void +} + +type TypeResult = Result; + +impl TypeContext { + pub fn new() -> TypeContext { + TypeContext { } + } + pub fn type_check_ast(&mut self, ast: &parsing::AST) -> TypeResult { + use self::Type::*; + let mut ret_type = Unit; + for statement in ast.0.iter() { + ret_type = self.type_check_statement(statement)?; + } + Ok(ret_type) + } + fn type_check_statement(&mut self, statement: &parsing::Statement) -> TypeResult { + use self::parsing::Statement::*; + match statement { + &ExpressionStatement(ref expr) => self.type_check_expression(expr), + &Declaration(ref decl) => self.type_check_declaration(decl), + } + } + fn type_check_declaration(&mut self, decl: &parsing::Declaration) -> TypeResult { + use self::Type::*; + Ok(Unit) + } + fn type_check_expression(&mut self, expr: &parsing::Expression) -> TypeResult { + use self::Type::*; + Ok(Void) + } +} +