More work on pattern-matching

This commit is contained in:
greg 2018-08-05 18:01:42 -07:00
parent 5b5689accf
commit da59fae0d3
3 changed files with 27 additions and 11 deletions

View File

@ -157,7 +157,7 @@ pub enum Pattern {
#[derive(Debug, PartialEq, Clone)] #[derive(Debug, PartialEq, Clone)]
pub enum PatternLiteral { pub enum PatternLiteral {
NumPattern(ExpressionType), NumPattern(ExpressionType), //TODO fix
StringPattern(Rc<String>), StringPattern(Rc<String>),
BoolPattern(bool), BoolPattern(bool),
VarPattern(Rc<String>) VarPattern(Rc<String>)

View File

@ -57,8 +57,8 @@ pub enum Expr {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Alternative { pub struct Alternative {
tag: usize, tag: Option<usize>,
bound_vars: (), bound_vars: Vec<Rc<String>>,
item: Vec<Stmt>, item: Vec<Stmt>,
} }
@ -158,20 +158,34 @@ fn reduce_if_expression(discriminator: &Discriminator, body: &IfExpressionBody,
None => vec![], None => vec![],
Some(stmts) => stmts.iter().map(|expr| expr.reduce(symbol_table)).collect(), Some(stmts) => stmts.iter().map(|expr| expr.reduce(symbol_table)).collect(),
}; };
Expr::Match { //TODO this still doesn't work right
cond, /*
alternatives: vec![ let alternatives = match pat {
Pattern::TupleStruct(name, subpatterns) => {
let symbol = symbol_table.values.get(name).unwrap();
unimplemented!()
},
_ => panic!()
};
*/
let alternatives = vec![
Alternative { Alternative {
tag: 0, tag: Some(0),
bound_vars: (), bound_vars: vec![],
item: then_clause, item: then_clause,
}, },
Alternative { Alternative {
tag: 1, tag: None,
bound_vars: (), bound_vars: vec![],
item: else_clause, item: else_clause,
}, },
], ];
Expr::Match {
cond,
alternatives,
} }
}, },
IfExpressionBody::GuardList(ref _guard_arms) => panic!(), IfExpressionBody::GuardList(ref _guard_arms) => panic!(),

View File

@ -11,6 +11,8 @@ pub struct SymbolTable {
pub values: HashMap<Rc<String>, Symbol> //TODO this will eventually have real type information pub values: HashMap<Rc<String>, Symbol> //TODO this will eventually have real type information
} }
//TODO add various types of lookups here, maybe multiple hash tables internally? also make values
//non-public
impl SymbolTable { impl SymbolTable {
pub fn new() -> SymbolTable { pub fn new() -> SymbolTable {
SymbolTable { values: HashMap::new() } SymbolTable { values: HashMap::new() }