Simplify symbol table code
This commit is contained in:
parent
4bcbf1854a
commit
30a54d997c
@ -10,37 +10,38 @@ struct PathSpecifier {
|
|||||||
constant: bool,
|
constant: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct SymbolTable {
|
pub struct TypeContext {
|
||||||
map: HashMap<PathSpecifier, Expression>,
|
symbol_table: HashMap<PathSpecifier, Expression>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SymbolTable {
|
impl TypeContext {
|
||||||
fn new() -> SymbolTable {
|
pub fn new() -> TypeContext {
|
||||||
SymbolTable { map: HashMap::new() }
|
TypeContext { symbol_table: HashMap::new() }
|
||||||
}
|
}
|
||||||
|
pub fn add_symbols(&mut self, ast: &AST) {
|
||||||
fn add_symbols(&mut self, ast: &AST) {
|
|
||||||
use self::Declaration::*;
|
use self::Declaration::*;
|
||||||
|
|
||||||
for statement in ast.0.iter() {
|
for statement in ast.0.iter() {
|
||||||
match statement {
|
match *statement {
|
||||||
&Statement::ExpressionStatement(_) => (),
|
Statement::ExpressionStatement(_) => (),
|
||||||
&Statement::Declaration(ref d) => {
|
Statement::Declaration(ref decl) => {
|
||||||
match d {
|
match *decl {
|
||||||
&FuncSig(_) => (),
|
FuncSig(_) => (),
|
||||||
&FuncDecl(_, _) => (),
|
Impl { .. } => (),
|
||||||
&TypeDecl { .. } => (),
|
TypeDecl { .. } => (),
|
||||||
&TypeAlias { .. } => (),
|
TypeAlias { .. } => (),
|
||||||
&Binding {ref name, ref constant, ref expr} => {
|
Binding {ref name, ref constant, ref expr} => {
|
||||||
let spec = PathSpecifier {
|
let spec = PathSpecifier {
|
||||||
name: name.clone(),
|
name: name.clone(),
|
||||||
kind: "binding",
|
kind: "binding",
|
||||||
constant: *constant
|
constant: *constant
|
||||||
};
|
};
|
||||||
let binding_contents = (*expr).clone();
|
let binding_contents = (*expr).clone();
|
||||||
self.map.insert(spec, binding_contents);
|
self.symbol_table.insert(spec, binding_contents);
|
||||||
|
},
|
||||||
|
FuncDecl(ref signature, ref type_anno) => {
|
||||||
|
|
||||||
},
|
},
|
||||||
&Impl { .. } => (),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -51,25 +52,11 @@ impl SymbolTable {
|
|||||||
use self::UVar::*;
|
use self::UVar::*;
|
||||||
Some(Univ(Function(Box::new(Univ(Integer)), Box::new(Univ(Boolean)))))
|
Some(Univ(Function(Box::new(Univ(Integer)), Box::new(Univ(Boolean)))))
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
pub struct TypeContext {
|
|
||||||
symbol_table: SymbolTable,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TypeContext {
|
|
||||||
pub fn new() -> TypeContext {
|
|
||||||
TypeContext { symbol_table: SymbolTable::new() }
|
|
||||||
}
|
|
||||||
pub fn add_symbols(&mut self, ast: &AST) {
|
|
||||||
self.symbol_table.add_symbols(ast)
|
|
||||||
}
|
|
||||||
pub fn debug_symbol_table(&self) -> String {
|
pub fn debug_symbol_table(&self) -> String {
|
||||||
format!("Symbol table:\n {:?}", self.symbol_table.map)
|
format!("Symbol table:\n {:?}", self.symbol_table)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Clone)]
|
#[derive(Debug, PartialEq, Clone)]
|
||||||
pub enum TypeVariable {
|
pub enum TypeVariable {
|
||||||
Univ(UVar),
|
Univ(UVar),
|
||||||
@ -149,10 +136,8 @@ impl TypeContext {
|
|||||||
},
|
},
|
||||||
(&IntLiteral(_), _) => Univ(UVar::Integer),
|
(&IntLiteral(_), _) => Univ(UVar::Integer),
|
||||||
(&BoolLiteral(_), _) => Univ(UVar::Boolean),
|
(&BoolLiteral(_), _) => Univ(UVar::Boolean),
|
||||||
(&Variable(ref name), _) => self.symbol_table
|
(&Variable(ref name), _) => self.lookup(name)
|
||||||
.lookup(name)
|
|
||||||
.ok_or(format!("Couldn't find {}", name))?,
|
.ok_or(format!("Couldn't find {}", name))?,
|
||||||
|
|
||||||
(&Call { ref f, ref arguments }, _) => {
|
(&Call { ref f, ref arguments }, _) => {
|
||||||
let f_type = self.infer(&*f)?;
|
let f_type = self.infer(&*f)?;
|
||||||
let arg_type = self.infer(arguments.get(0).unwrap())?; // TODO fix later
|
let arg_type = self.infer(arguments.get(0).unwrap())?; // TODO fix later
|
||||||
|
Loading…
Reference in New Issue
Block a user