Reduce defined function

This commit is contained in:
greg 2018-05-11 22:44:21 -07:00
parent e6f0710e41
commit 848306ad1a

View File

@ -1,6 +1,6 @@
use std::rc::Rc; use std::rc::Rc;
use parsing::{AST, Expression, Declaration}; use parsing::{AST, Statement, Expression, Declaration};
use builtin::{BinOp, PrefixOp}; use builtin::{BinOp, PrefixOp};
#[derive(Debug)] #[derive(Debug)]
@ -48,18 +48,24 @@ pub enum Func {
impl AST { impl AST {
pub fn reduce(&self) -> ReducedAST { pub fn reduce(&self) -> ReducedAST {
use parsing::Statement::*;
let mut output = vec![]; let mut output = vec![];
for statement in self.0.iter() { for statement in self.0.iter() {
match statement { output.push(statement.reduce());
&ExpressionStatement(ref expr) => output.push(Stmt::Expr(expr.reduce())),
&Declaration(ref decl) => output.push(decl.reduce()),
}
} }
ReducedAST(output) ReducedAST(output)
} }
} }
impl Statement {
fn reduce(&self) -> Stmt {
use parsing::Statement::*;
match self {
ExpressionStatement(expr) => Stmt::Expr(expr.reduce()),
Declaration(decl) => decl.reduce(),
}
}
}
impl Expression { impl Expression {
fn reduce(&self) -> Expr { fn reduce(&self) -> Expr {
use parsing::ExpressionType::*; use parsing::ExpressionType::*;
@ -87,7 +93,15 @@ impl Declaration {
fn reduce(&self) -> Stmt { fn reduce(&self) -> Stmt {
use self::Declaration::*; use self::Declaration::*;
match self { match self {
&Binding { ref name, ref constant, ref expr } => Stmt::Binding { name: name.clone(), constant: *constant, expr: expr.reduce() }, Binding {name, constant, expr } => Stmt::Binding { name: name.clone(), constant: *constant, expr: expr.reduce() },
FuncDecl(::parsing::Signature { name, params, type_anno }, statements) => Stmt::Binding {
name: name.clone(),
constant: true,
expr: Expr::Func(Func::UserDefined {
params: params.iter().map(|param| param.0.clone()).collect(),
body: statements.iter().map(|stmt| stmt.reduce()).collect(),
})
},
_ => Stmt::Expr(Expr::UnimplementedSigilValue) _ => Stmt::Expr(Expr::UnimplementedSigilValue)
} }
} }