schala/schala-lang/src/ast_reducing.rs

109 lines
2.5 KiB
Rust
Raw Normal View History

2018-05-09 02:27:57 -07:00
use std::rc::Rc;
2018-05-09 02:02:17 -07:00
2018-05-09 02:49:49 -07:00
use parsing::{AST, 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-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 {
f: Func,
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 {
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
use parsing::Statement::*;
let mut output = vec![];
for statement in self.0.iter() {
match statement {
2018-05-11 00:45:32 -07:00
&ExpressionStatement(ref expr) => output.push(Stmt::Expr(expr.reduce())),
&Declaration(ref decl) => output.push(decl.reduce()),
2018-05-10 23:41:51 -07:00
}
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-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-11 00:38:40 -07:00
&IntLiteral(ref n) => Expr::Lit(Lit::Nat(*n)), //TODO I should rename IntLiteral if I want the Nat/Int distinction, which I do
2018-05-10 23:41:51 -07:00
&FloatLiteral(ref f) => Expr::Lit(Lit::Float(*f)),
&StringLiteral(ref s) => Expr::Lit(Lit::StringLit(s.clone())),
&BoolLiteral(ref b) => Expr::Lit(Lit::Bool(*b)),
2018-05-11 00:45:32 -07:00
&BinExp(ref binop, ref lhs, ref rhs) => binop.reduce(lhs, rhs),
&PrefixExp(ref op, ref arg) => op.reduce(arg),
2018-05-11 17:24:11 -07:00
&Value(ref name) => Expr::Val(name.clone()),
2018-05-11 02:58:14 -07:00
/*
&Call { ref f, ref arguments } => Expr::Call {
f: Box<Expression>,
arguments: Vec<Expression>,
},
*/
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 {
&Binding { ref name, ref constant, ref expr } => Stmt::Binding { name: name.clone(), constant: *constant, expr: expr.reduce() },
_ => 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 00:38:40 -07:00
let f = 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 00:47:56 -07:00
let f = Func::BuiltIn(self.sigil().clone());
Expr::Call { f, args: vec![arg.reduce()]}
2018-05-10 23:41:51 -07:00
}
2018-05-09 17:02:10 -07:00
}