diff --git a/src/repl/mod.rs b/src/repl/mod.rs index ec44d89..747f189 100755 --- a/src/repl/mod.rs +++ b/src/repl/mod.rs @@ -1,7 +1,9 @@ -use std::io::{self, Write}; +use ::std::io::{self, Write}; use crate::codegen::Transpilable; use crate::error_handling::PrintableError; +use crate::semantic::std; +use crate::semantic::symbol_table::SymbolTable; use super::lexic; use super::syntax; @@ -12,6 +14,8 @@ use crate::php_ast::transformers::PHPTransformable; pub fn run() -> io::Result<()> { let stdin = io::stdin(); let mut buffer = String::new(); + let mut repl_symbol_table = SymbolTable::new(); + std::populate(&mut repl_symbol_table); println!("REPL: Enter expressions to evaluate. Type Ctrl-D to exit."); loop { @@ -26,7 +30,7 @@ pub fn run() -> io::Result<()> { break Ok(()); } Ok(_) => { - compile(&buffer); + compile(&buffer, &mut repl_symbol_table); } Err(error) => { eprintln!("Error reading stdin."); @@ -37,7 +41,7 @@ pub fn run() -> io::Result<()> { } /// Full pipeline from THP source code to PHP output -fn compile(input: &String) { +fn compile(input: &String, symbol_table: &mut SymbolTable) { // // Lexical analysis // @@ -63,7 +67,7 @@ fn compile(input: &String) { // // Semantic analysis // - let res1 = crate::semantic::check_semantics(&ast); + let res1 = crate::semantic::check_semantics_with(&ast, symbol_table); match res1 { Ok(_) => {} Err(error) => { diff --git a/src/semantic/mod.rs b/src/semantic/mod.rs index 6331d61..683e099 100644 --- a/src/semantic/mod.rs +++ b/src/semantic/mod.rs @@ -2,11 +2,12 @@ use crate::{error_handling::MistiError, syntax::ast::ModuleAST}; mod checks; mod impls; -mod std; -mod symbol_table; +pub mod std; +pub mod symbol_table; mod types; use impls::SemanticCheck; +use symbol_table::SymbolTable; // What to do? // 1. Create a mutable symbol table @@ -25,6 +26,15 @@ pub fn check_semantics(ast: &ModuleAST) -> Result<(), MistiError> { ast.check_semantics(&global_scope) } +/// Checks that the AST is semantically correct. +/// Accepts a handle to a symbol table to operate with. +pub fn check_semantics_with( + ast: &ModuleAST, + symbol_table: &mut SymbolTable, +) -> Result<(), MistiError> { + ast.check_semantics(&symbol_table) +} + #[cfg(test)] mod tests { use crate::semantic::types::Type; diff --git a/src/semantic/std.rs b/src/semantic/std.rs index a499851..e89e0d4 100644 --- a/src/semantic/std.rs +++ b/src/semantic/std.rs @@ -19,4 +19,8 @@ pub fn populate(table: &mut SymbolTable) { // + operator (Int, Int) -> Int let plus_op = Type::Function(vec![INT.into(), INT.into()], INT.into()); table.insert("+".into(), plus_op); + + // - operator (Int, Int) -> Int + let plus_op = Type::Function(vec![INT.into(), INT.into()], INT.into()); + table.insert("-".into(), plus_op); }