Symbol table accepts variables

This commit is contained in:
greg 2017-10-08 13:51:56 -07:00
parent 62edc7c996
commit 3f9ae5fac3
2 changed files with 40 additions and 11 deletions

View File

@ -392,7 +392,7 @@ macro_rules! expect {
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub struct AST(pub Vec<Statement>); pub struct AST(pub Vec<Statement>);
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq, Clone)]
pub enum Statement { pub enum Statement {
ExpressionStatement(Expression), ExpressionStatement(Expression),
Declaration(Declaration), Declaration(Declaration),
@ -403,7 +403,7 @@ type TypeName = Rc<String>;
type TraitName = Rc<String>; type TraitName = Rc<String>;
type FormalParamList = Vec<(ParamName, Option<TypeName>)>; type FormalParamList = Vec<(ParamName, Option<TypeName>)>;
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq, Clone)]
pub enum Declaration { pub enum Declaration {
FuncDecl { FuncDecl {
name: Rc<String>, name: Rc<String>,
@ -423,20 +423,20 @@ pub enum Declaration {
}, },
} }
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq, Clone)]
pub struct TypeBody(pub Vec<Variant>); pub struct TypeBody(pub Vec<Variant>);
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq, Clone)]
pub enum Variant { pub enum Variant {
Singleton(Rc<String>), Singleton(Rc<String>),
//ArgumentConstructor, //ArgumentConstructor,
//Record //Record
} }
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq, Clone)]
pub struct Expression(pub ExpressionType, pub Option<TypeAnno>); pub struct Expression(pub ExpressionType, pub Option<TypeAnno>);
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq, Clone)]
pub enum TypeAnno { pub enum TypeAnno {
Tuple(Vec<TypeAnno>), Tuple(Vec<TypeAnno>),
Singleton { Singleton {
@ -445,7 +445,7 @@ pub enum TypeAnno {
} }
} }
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq, Clone)]
pub enum ExpressionType { pub enum ExpressionType {
IntLiteral(u64), IntLiteral(u64),
FloatLiteral(f64), FloatLiteral(f64),
@ -467,16 +467,16 @@ pub enum ExpressionType {
MatchExpression(Box<Expression>, Vec<MatchArm>) MatchExpression(Box<Expression>, Vec<MatchArm>)
} }
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq, Clone)]
pub struct MatchArm { pub struct MatchArm {
pat: Pattern, pat: Pattern,
expr: Expression, expr: Expression,
} }
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq, Clone)]
pub struct Pattern(Rc<String>); pub struct Pattern(Rc<String>);
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq, Clone)]
pub struct Operation(pub Rc<String>); pub struct Operation(pub Rc<String>);
impl Operation { impl Operation {

View File

@ -1,9 +1,14 @@
use std::collections::HashMap; use std::collections::HashMap;
use std::rc::Rc;
use schala_lang::parsing::{AST, Statement, Declaration, Expression, ExpressionType, Operation, TypeAnno}; use schala_lang::parsing::{AST, Statement, Declaration, Expression, ExpressionType, Operation, TypeAnno};
#[derive(Debug, PartialEq, Eq, Hash)] #[derive(Debug, PartialEq, Eq, Hash)]
struct PathSpecifier(String); struct PathSpecifier {
name: Rc<String>,
kind: &'static str,
constant: bool,
}
struct SymbolTable { struct SymbolTable {
map: HashMap<PathSpecifier, Expression>, map: HashMap<PathSpecifier, Expression>,
@ -15,6 +20,30 @@ impl SymbolTable {
} }
fn add_symbols(&mut self, ast: &AST) { fn add_symbols(&mut self, ast: &AST) {
use self::Declaration::*;
for statement in ast.0.iter() {
match statement {
&Statement::ExpressionStatement(_) => (),
&Statement::Declaration(ref d) => {
match d {
&FuncDecl { .. } => (),
&TypeDecl { .. } => (),
&TypeAlias { .. } => (),
&Binding {ref name, ref constant, ref expr} => {
let spec = PathSpecifier {
name: name.clone(),
kind: "binding",
constant: *constant
};
let binding_contents = (*expr).clone();
self.map.insert(spec, binding_contents);
},
&Impl { .. } => (),
}
}
}
}
} }
} }