Add simple symbol table
This commit is contained in:
parent
6cecd6af52
commit
1c90ee293b
@ -6,6 +6,8 @@ mod syntax;
|
||||
mod lexic;
|
||||
mod token;
|
||||
mod semantic;
|
||||
mod ast_types;
|
||||
mod symbol_table;
|
||||
|
||||
const VERSION: &str = "0.0.1";
|
||||
|
||||
|
@ -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)
|
||||
|
@ -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<ModuleAST<'a>> {
|
||||
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
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
46
src/symbol_table.rs
Normal file
46
src/symbol_table.rs
Normal file
@ -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<String, String>
|
||||
}
|
||||
|
||||
impl SymbolTable {
|
||||
pub fn new() -> SymbolTable {
|
||||
let mut symbol_table = HashMap::<String, String>::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>(&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"))
|
||||
}
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
use crate::token::{Token, TokenType};
|
||||
use super::types::Expression;
|
||||
use super::ast_types::Expression;
|
||||
|
||||
|
||||
pub fn try_parse(tokens: &Vec<Token>, pos: usize) -> Option<Expression> {
|
||||
|
@ -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<Token>) -> Result<ModuleAST<'a>, String> {
|
||||
|
@ -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:
|
||||
|
Loading…
Reference in New Issue
Block a user