Pattern-matching in reduced AST
This commit is contained in:
parent
75bf4b5697
commit
a2b1b0f953
@ -1,6 +1,6 @@
|
|||||||
use std::rc::Rc;
|
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 symbol_table::{Symbol, SymbolSpec, SymbolTable};
|
||||||
use builtin::{BinOp, PrefixOp};
|
use builtin::{BinOp, PrefixOp};
|
||||||
|
|
||||||
@ -45,6 +45,10 @@ pub enum Expr {
|
|||||||
then_clause: Vec<Stmt>,
|
then_clause: Vec<Stmt>,
|
||||||
else_clause: Vec<Stmt>,
|
else_clause: Vec<Stmt>,
|
||||||
},
|
},
|
||||||
|
Match {
|
||||||
|
cond: Box<Expr>,
|
||||||
|
arms: Vec<(Pattern, Vec<Stmt>)>
|
||||||
|
},
|
||||||
UnimplementedSigilValue
|
UnimplementedSigilValue
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -112,12 +116,18 @@ impl Expression {
|
|||||||
args: arguments.iter().map(|arg| arg.reduce(symbol_table)).collect(),
|
args: arguments.iter().map(|arg| arg.reduce(symbol_table)).collect(),
|
||||||
},
|
},
|
||||||
TupleLiteral(exprs) => Expr::Tuple(exprs.iter().map(|e| e.reduce(symbol_table)).collect()),
|
TupleLiteral(exprs) => Expr::Tuple(exprs.iter().map(|e| e.reduce(symbol_table)).collect()),
|
||||||
IfExpression { discriminator, body } => {
|
IfExpression { discriminator, body } => reduce_if_expression(discriminator, body, symbol_table),
|
||||||
let cond = Box::new(match **discriminator {
|
_ => 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),
|
Discriminator::Simple(ref expr) => expr.reduce(symbol_table),
|
||||||
_ => panic!(),
|
_ => panic!(),
|
||||||
});
|
});
|
||||||
match **body {
|
match *body {
|
||||||
IfExpressionBody::SimpleConditional(ref then_clause, ref else_clause) => {
|
IfExpressionBody::SimpleConditional(ref then_clause, ref else_clause) => {
|
||||||
let then_clause = then_clause.iter().map(|expr| expr.reduce(symbol_table)).collect();
|
let then_clause = then_clause.iter().map(|expr| expr.reduce(symbol_table)).collect();
|
||||||
let else_clause = match else_clause {
|
let else_clause = match else_clause {
|
||||||
@ -136,10 +146,6 @@ impl Expression {
|
|||||||
},
|
},
|
||||||
_ => panic!(),
|
_ => panic!(),
|
||||||
}
|
}
|
||||||
},
|
|
||||||
_ => Expr::UnimplementedSigilValue,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Declaration {
|
impl Declaration {
|
||||||
|
Loading…
Reference in New Issue
Block a user