feat: use a single symbol_table in a repl session

This commit is contained in:
Araozu 2024-11-05 19:38:22 -05:00
parent 6f56ab0b4d
commit f3f6e9fd44
3 changed files with 24 additions and 6 deletions

View File

@ -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) => {

View File

@ -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;

View File

@ -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);
}