diff --git a/src/syntax/types.rs b/src/ast_types.rs similarity index 100% rename from src/syntax/types.rs rename to src/ast_types.rs diff --git a/src/main.rs b/src/main.rs index df06de2..757044c 100755 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,8 @@ mod syntax; mod lexic; mod token; mod semantic; +mod ast_types; +mod symbol_table; const VERSION: &str = "0.0.1"; diff --git a/src/repl/mod.rs b/src/repl/mod.rs index 7bd8c67..9d8bb74 100755 --- a/src/repl/mod.rs +++ b/src/repl/mod.rs @@ -1,19 +1,19 @@ use std::io::{self, Write}; +use crate::symbol_table::SymbolTable; + use super::lexic; use super::syntax; +use super::semantic; fn compile(input: &String) { let _tokens = lexic::get_tokens(input); match _tokens { Ok(tokens) => { - for token in tokens { - print!("[{:?} {}] ", token.token_type, token.value); - } - println!(""); - - let _ast = syntax::construct_ast(&Vec::new()); + let mut ast = syntax::construct_ast(&tokens).unwrap(); + let mut table = SymbolTable::new(); + let new_ast = semantic::check_ast(&mut ast, &mut table); }, Err(error) => { eprintln!("Error scanning.\n{} at pos {}", error.reason, error.position) diff --git a/src/semantic/mod.rs b/src/semantic/mod.rs index 7f79cb4..f240336 100644 --- a/src/semantic/mod.rs +++ b/src/semantic/mod.rs @@ -1,4 +1,34 @@ +use crate::symbol_table; -pub fn check_ast() { - +use super::symbol_table::SymbolTable; +use super::ast_types::ModuleAST; + +pub fn check_ast<'a>(ast: &'a mut ModuleAST, symbol_table: &'a mut SymbolTable) -> Option> { + None +} + +#[cfg(test)] +mod tests { + use super::*; + + /* + val identifier = 20 + + [Binding] + | identifier + | [Expression] + | [Number] + | 20 + + - Check [Expression] is valid + - Check type of [Expression] + - Check if `identifier` already exists in the symbol table + - Create entry in symbol table + + -> + + SymbolTable { + identifier: Num + } + */ } diff --git a/src/symbol_table.rs b/src/symbol_table.rs new file mode 100644 index 0000000..2e53bd3 --- /dev/null +++ b/src/symbol_table.rs @@ -0,0 +1,46 @@ +use std::collections::HashMap; + +// Primitive datatypes +pub const _NUMBER: &str = "Num"; +pub const _STRING: &str = "Str"; +pub const _BOOLEAN: &str = "Bool"; + +pub struct SymbolTable { + table: HashMap +} + +impl SymbolTable { + pub fn new() -> SymbolTable { + let mut symbol_table = HashMap::::new(); + + SymbolTable { + table: symbol_table, + } + } + + pub fn add(&mut self, identifier: &str, datatype: &str) { + self.table.insert(String::from(identifier), String::from(datatype)); + } + + pub fn test(&self, identifier: &str) -> bool { + return self.table.contains_key::(&String::from(identifier)); + } +} + + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn should_create() { + let mut _table = SymbolTable::new(); + } + + #[test] + fn should_add_identifier() { + let mut table = SymbolTable::new(); + table.add("identifier", _NUMBER); + assert_eq!(true, table.test("identifier")) + } +} diff --git a/src/syntax/expression.rs b/src/syntax/expression.rs index b28bc0d..59d14bd 100644 --- a/src/syntax/expression.rs +++ b/src/syntax/expression.rs @@ -1,5 +1,5 @@ use crate::token::{Token, TokenType}; -use super::types::Expression; +use super::ast_types::Expression; pub fn try_parse(tokens: &Vec, pos: usize) -> Option { diff --git a/src/syntax/mod.rs b/src/syntax/mod.rs index ed3bfb2..66e1717 100755 --- a/src/syntax/mod.rs +++ b/src/syntax/mod.rs @@ -2,9 +2,9 @@ use super::token::Token; mod expression; mod val_binding; -mod types; +use super::ast_types; -use types::ModuleAST; +use ast_types::ModuleAST; /// Constructs the Misti AST from a vector of tokens pub fn construct_ast<'a>(tokens: &'a Vec) -> Result, String> { diff --git a/src/syntax/val_binding.rs b/src/syntax/val_binding.rs index f91a3cf..dc50712 100644 --- a/src/syntax/val_binding.rs +++ b/src/syntax/val_binding.rs @@ -1,5 +1,5 @@ use crate::token::{Token, TokenType}; -use super::types::{Binding, ValBinding}; +use super::ast_types::{Binding, ValBinding}; use super::expression; // Should return a 3 state value: