Pattern-matching in reduced AST

This commit is contained in:
greg 2018-07-24 03:12:00 -07:00
parent 75bf4b5697
commit a2b1b0f953
2 changed files with 33 additions and 27 deletions

View File

@ -1,6 +1,6 @@
use std::rc::Rc;
use ast::{AST, Statement, Expression, Declaration, Discriminator, IfExpressionBody};
use ast::{AST, Statement, Expression, Declaration, Discriminator, IfExpressionBody, Pattern};
use symbol_table::{Symbol, SymbolSpec, SymbolTable};
use builtin::{BinOp, PrefixOp};
@ -45,6 +45,10 @@ pub enum Expr {
then_clause: Vec<Stmt>,
else_clause: Vec<Stmt>,
},
Match {
cond: Box<Expr>,
arms: Vec<(Pattern, Vec<Stmt>)>
},
UnimplementedSigilValue
}
@ -112,12 +116,18 @@ impl Expression {
args: arguments.iter().map(|arg| arg.reduce(symbol_table)).collect(),
},
TupleLiteral(exprs) => Expr::Tuple(exprs.iter().map(|e| e.reduce(symbol_table)).collect()),
IfExpression { discriminator, body } => {
let cond = Box::new(match **discriminator {
IfExpression { discriminator, body } => reduce_if_expression(discriminator, body, symbol_table),
_ => Expr::UnimplementedSigilValue,
}
}
}
fn reduce_if_expression(discriminator: &Discriminator, body: &IfExpressionBody, symbol_table: &SymbolTable) -> Expr {
let cond = Box::new(match *discriminator {
Discriminator::Simple(ref expr) => expr.reduce(symbol_table),
_ => panic!(),
});
match **body {
match *body {
IfExpressionBody::SimpleConditional(ref then_clause, ref else_clause) => {
let then_clause = then_clause.iter().map(|expr| expr.reduce(symbol_table)).collect();
let else_clause = match else_clause {
@ -136,10 +146,6 @@ impl Expression {
},
_ => panic!(),
}
},
_ => Expr::UnimplementedSigilValue,
}
}
}
impl Declaration {