2018-05-09 02:27:57 -07:00
|
|
|
use std::rc::Rc;
|
2018-05-09 02:02:17 -07:00
|
|
|
|
2018-05-11 22:44:21 -07:00
|
|
|
use parsing::{AST, Statement, Expression, Declaration};
|
2018-05-09 17:02:10 -07:00
|
|
|
use builtin::{BinOp, PrefixOp};
|
2018-05-09 02:27:57 -07:00
|
|
|
|
2018-05-09 02:49:49 -07:00
|
|
|
#[derive(Debug)]
|
2018-05-09 02:27:57 -07:00
|
|
|
pub struct ReducedAST(pub Vec<Stmt>);
|
|
|
|
|
2018-05-11 17:24:11 -07:00
|
|
|
#[derive(Debug, Clone)]
|
2018-05-09 02:27:57 -07:00
|
|
|
pub enum Stmt {
|
|
|
|
Binding {
|
|
|
|
name: Rc<String>,
|
2018-05-11 02:24:38 -07:00
|
|
|
constant: bool,
|
2018-05-09 02:27:57 -07:00
|
|
|
expr: Expr,
|
|
|
|
},
|
|
|
|
Expr(Expr),
|
|
|
|
}
|
|
|
|
|
2018-05-11 17:24:11 -07:00
|
|
|
#[derive(Debug, Clone)]
|
2018-05-09 02:27:57 -07:00
|
|
|
pub enum Expr {
|
2018-05-12 00:59:50 -07:00
|
|
|
Unit,
|
2018-05-09 03:04:01 -07:00
|
|
|
Lit(Lit),
|
2018-05-09 02:27:57 -07:00
|
|
|
Func(Func),
|
2018-05-11 17:24:11 -07:00
|
|
|
Val(Rc<String>),
|
2018-05-09 02:27:57 -07:00
|
|
|
Call {
|
2018-05-11 23:34:26 -07:00
|
|
|
f: Box<Expr>,
|
2018-05-09 02:27:57 -07:00
|
|
|
args: Vec<Expr>,
|
|
|
|
},
|
2018-05-11 00:45:32 -07:00
|
|
|
UnimplementedSigilValue
|
2018-05-09 02:27:57 -07:00
|
|
|
}
|
|
|
|
|
2018-05-11 17:24:11 -07:00
|
|
|
#[derive(Debug, Clone)]
|
2018-05-09 02:27:57 -07:00
|
|
|
pub enum Lit {
|
2018-05-11 00:38:40 -07:00
|
|
|
Nat(u64),
|
|
|
|
Int(i64),
|
2018-05-09 17:02:10 -07:00
|
|
|
Float(f64),
|
2018-05-09 02:27:57 -07:00
|
|
|
Bool(bool),
|
|
|
|
StringLit(Rc<String>),
|
|
|
|
}
|
|
|
|
|
2018-05-11 17:24:11 -07:00
|
|
|
#[derive(Debug, Clone)]
|
2018-05-11 00:38:40 -07:00
|
|
|
pub enum Func {
|
|
|
|
BuiltIn(Rc<String>),
|
|
|
|
UserDefined {
|
2018-05-11 23:23:54 -07:00
|
|
|
name: Option<Rc<String>>,
|
2018-05-11 00:38:40 -07:00
|
|
|
params: Vec<Rc<String>>,
|
|
|
|
body: Vec<Stmt>,
|
|
|
|
}
|
2018-05-09 02:27:57 -07:00
|
|
|
}
|
|
|
|
|
2018-05-10 23:41:51 -07:00
|
|
|
impl AST {
|
2018-05-11 00:45:32 -07:00
|
|
|
pub fn reduce(&self) -> ReducedAST {
|
2018-05-10 23:41:51 -07:00
|
|
|
let mut output = vec![];
|
|
|
|
for statement in self.0.iter() {
|
2018-05-11 22:44:21 -07:00
|
|
|
output.push(statement.reduce());
|
2018-05-09 02:49:49 -07:00
|
|
|
}
|
2018-05-11 00:45:32 -07:00
|
|
|
ReducedAST(output)
|
2018-05-09 02:49:49 -07:00
|
|
|
}
|
|
|
|
}
|
2018-05-09 02:27:57 -07:00
|
|
|
|
2018-05-11 22:44:21 -07:00
|
|
|
impl Statement {
|
|
|
|
fn reduce(&self) -> Stmt {
|
|
|
|
use parsing::Statement::*;
|
|
|
|
match self {
|
|
|
|
ExpressionStatement(expr) => Stmt::Expr(expr.reduce()),
|
|
|
|
Declaration(decl) => decl.reduce(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-10 23:41:51 -07:00
|
|
|
impl Expression {
|
2018-05-11 00:45:32 -07:00
|
|
|
fn reduce(&self) -> Expr {
|
2018-05-10 23:41:51 -07:00
|
|
|
use parsing::ExpressionType::*;
|
|
|
|
let ref input = self.0;
|
2018-05-11 00:45:32 -07:00
|
|
|
match input {
|
2018-05-12 01:44:03 -07:00
|
|
|
NatLiteral(n) => Expr::Lit(Lit::Nat(*n)),
|
2018-05-11 23:26:02 -07:00
|
|
|
FloatLiteral(f) => Expr::Lit(Lit::Float(*f)),
|
|
|
|
StringLiteral(s) => Expr::Lit(Lit::StringLit(s.clone())),
|
|
|
|
BoolLiteral(b) => Expr::Lit(Lit::Bool(*b)),
|
|
|
|
BinExp(binop, lhs, rhs) => binop.reduce(lhs, rhs),
|
|
|
|
PrefixExp(op, arg) => op.reduce(arg),
|
|
|
|
Value(name) => Expr::Val(name.clone()),
|
2018-05-11 23:34:26 -07:00
|
|
|
Call { f, arguments } => Expr::Call {
|
|
|
|
f: Box::new(f.reduce()),
|
|
|
|
args: arguments.iter().map(|arg| arg.reduce()).collect(),
|
2018-05-11 02:58:14 -07:00
|
|
|
},
|
2018-05-11 00:45:32 -07:00
|
|
|
e => Expr::UnimplementedSigilValue,
|
|
|
|
}
|
2018-05-10 23:41:51 -07:00
|
|
|
}
|
2018-05-09 02:49:49 -07:00
|
|
|
}
|
2018-05-10 23:41:51 -07:00
|
|
|
|
|
|
|
impl Declaration {
|
2018-05-11 00:45:32 -07:00
|
|
|
fn reduce(&self) -> Stmt {
|
2018-05-11 02:24:38 -07:00
|
|
|
use self::Declaration::*;
|
|
|
|
match self {
|
2018-05-11 22:44:21 -07:00
|
|
|
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 {
|
2018-05-11 23:23:54 -07:00
|
|
|
name: None,
|
2018-05-11 22:44:21 -07:00
|
|
|
params: params.iter().map(|param| param.0.clone()).collect(),
|
|
|
|
body: statements.iter().map(|stmt| stmt.reduce()).collect(),
|
|
|
|
})
|
|
|
|
},
|
2018-05-11 02:24:38 -07:00
|
|
|
_ => Stmt::Expr(Expr::UnimplementedSigilValue)
|
|
|
|
}
|
2018-05-10 23:41:51 -07:00
|
|
|
}
|
2018-05-09 02:27:57 -07:00
|
|
|
}
|
2018-05-09 17:02:10 -07:00
|
|
|
|
2018-05-10 23:41:51 -07:00
|
|
|
impl BinOp {
|
2018-05-11 00:45:32 -07:00
|
|
|
fn reduce(&self, lhs: &Box<Expression>, rhs: &Box<Expression>) -> Expr {
|
2018-05-11 23:34:26 -07:00
|
|
|
let f = Box::new(Expr::Func(Func::BuiltIn(self.sigil().clone())));
|
2018-05-11 00:45:32 -07:00
|
|
|
Expr::Call { f, args: vec![lhs.reduce(), rhs.reduce()]}
|
2018-05-10 23:41:51 -07:00
|
|
|
}
|
2018-05-09 17:02:10 -07:00
|
|
|
}
|
|
|
|
|
2018-05-10 23:41:51 -07:00
|
|
|
impl PrefixOp {
|
2018-05-11 00:45:32 -07:00
|
|
|
fn reduce(&self, arg: &Box<Expression>) -> Expr {
|
2018-05-11 23:34:26 -07:00
|
|
|
let f = Box::new(Expr::Func(Func::BuiltIn(self.sigil().clone())));
|
2018-05-11 00:47:56 -07:00
|
|
|
Expr::Call { f, args: vec![arg.reduce()]}
|
2018-05-10 23:41:51 -07:00
|
|
|
}
|
2018-05-09 17:02:10 -07:00
|
|
|
}
|