schala/schala-lang/src/reduced_ast.rs

222 lines
6.1 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-07-24 03:12:00 -07:00
use ast::{AST, Statement, Expression, Declaration, Discriminator, IfExpressionBody, Pattern};
use symbol_table::{Symbol, SymbolSpec, SymbolTable};
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 {
PreBinding {
name: Rc<String>,
func: Func,
},
2018-05-09 02:27:57 -07:00
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),
Noop,
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 Expr {
2018-05-12 00:59:50 -07:00
Unit,
2018-05-09 03:04:01 -07:00
Lit(Lit),
2018-05-12 12:56:39 -07:00
Tuple(Vec<Expr>),
2018-05-09 02:27:57 -07:00
Func(Func),
2018-05-11 17:24:11 -07:00
Val(Rc<String>),
NewConstructor {
type_name: Rc<String>,
tag: usize,
2018-08-05 16:04:52 -07:00
arity: usize,
},
Constructor {
name: Rc<String>,
},
2018-05-09 02:27:57 -07:00
Call {
f: Box<Expr>,
2018-05-09 02:27:57 -07:00
args: Vec<Expr>,
},
2018-05-12 03:51:42 -07:00
Assign {
val: Box<Expr>,
expr: Box<Expr>,
},
2018-05-12 13:51:12 -07:00
Conditional {
cond: Box<Expr>,
then_clause: Vec<Stmt>,
else_clause: Vec<Stmt>,
},
2018-07-24 03:12:00 -07:00
Match {
cond: Box<Expr>,
alternatives: Vec<Alternative>
2018-07-24 03:12:00 -07:00
},
2018-05-11 00:45:32 -07:00
UnimplementedSigilValue
2018-05-09 02:27:57 -07:00
}
#[derive(Debug, Clone)]
pub struct Alternative {
tag: usize,
bound_vars: (),
item: Vec<Stmt>,
}
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-06-04 18:17:03 -07:00
Custom(Rc<String>, Vec<Expr>),
2018-05-09 02:27:57 -07:00
}
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 {
pub fn reduce(&self, symbol_table: &SymbolTable) -> ReducedAST {
2018-05-10 23:41:51 -07:00
let mut output = vec![];
for statement in self.0.iter() {
output.push(statement.reduce(symbol_table));
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, symbol_table: &SymbolTable) -> Stmt {
2018-06-04 19:25:40 -07:00
use ast::Statement::*;
2018-05-11 22:44:21 -07:00
match self {
ExpressionStatement(expr) => Stmt::Expr(expr.reduce(symbol_table)),
Declaration(decl) => decl.reduce(symbol_table),
2018-05-11 22:44:21 -07:00
}
}
}
2018-05-10 23:41:51 -07:00
impl Expression {
fn reduce(&self, symbol_table: &SymbolTable) -> Expr {
2018-06-04 19:25:40 -07:00
use ast::ExpressionType::*;
2018-05-10 23:41:51 -07:00
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(symbol_table, lhs, rhs),
PrefixExp(op, arg) => op.reduce(symbol_table, arg),
Value(name) => {
match symbol_table.values.get(name) {
2018-08-05 16:04:52 -07:00
Some(Symbol { spec: SymbolSpec::DataConstructor { index, type_args, type_name}, .. }) => Expr::NewConstructor {
type_name: type_name.clone(),
tag: index.clone(),
arity: type_args.len(),
},
_ => Expr::Val(name.clone()),
}
},
Call { f, arguments } => Expr::Call {
f: Box::new(f.reduce(symbol_table)),
args: arguments.iter().map(|arg| arg.reduce(symbol_table)).collect(),
2018-05-11 02:58:14 -07:00
},
TupleLiteral(exprs) => Expr::Tuple(exprs.iter().map(|e| e.reduce(symbol_table)).collect()),
2018-07-24 03:12:00 -07:00
IfExpression { discriminator, body } => reduce_if_expression(discriminator, body, symbol_table),
2018-05-12 12:56:39 -07:00
_ => Expr::UnimplementedSigilValue,
2018-05-11 00:45:32 -07:00
}
2018-05-10 23:41:51 -07:00
}
2018-05-09 02:49:49 -07:00
}
2018-05-10 23:41:51 -07:00
2018-07-24 03:12:00 -07:00
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 {
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 {
None => vec![],
Some(stmts) => stmts.iter().map(|expr| expr.reduce(symbol_table)).collect(),
};
Expr::Conditional { cond, then_clause, else_clause }
},
IfExpressionBody::SimplePatternMatch(ref pat, 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 {
None => vec![],
Some(stmts) => stmts.iter().map(|expr| expr.reduce(symbol_table)).collect(),
};
Expr::Match { //TODO this still doesn't work right
2018-07-25 03:05:40 -07:00
cond,
alternatives: vec![
Alternative {
tag: 0,
bound_vars: (),
item: then_clause,
},
Alternative {
tag: 1,
bound_vars: (),
item: else_clause,
},
2018-07-25 03:05:40 -07:00
],
}
2018-07-24 03:12:00 -07:00
},
2018-07-25 03:05:40 -07:00
IfExpressionBody::GuardList(ref _guard_arms) => panic!(),
2018-07-24 03:12:00 -07:00
}
}
2018-05-10 23:41:51 -07:00
impl Declaration {
fn reduce(&self, symbol_table: &SymbolTable) -> Stmt {
2018-05-11 02:24:38 -07:00
use self::Declaration::*;
2018-06-04 19:25:40 -07:00
use ::ast::Signature;
2018-05-11 02:24:38 -07:00
match self {
Binding {name, constant, expr } => Stmt::Binding { name: name.clone(), constant: *constant, expr: expr.reduce(symbol_table) },
FuncDecl(Signature { name, params, .. }, statements) => Stmt::PreBinding {
2018-05-11 22:44:21 -07:00
name: name.clone(),
func: Func::UserDefined {
name: Some(name.clone()),
2018-05-11 22:44:21 -07:00
params: params.iter().map(|param| param.0.clone()).collect(),
body: statements.iter().map(|stmt| stmt.reduce(symbol_table)).collect(),
}
2018-05-11 22:44:21 -07:00
},
2018-07-12 02:07:52 -07:00
TypeDecl { .. } => Stmt::Noop,
2018-07-18 16:01:59 -07:00
TypeAlias(_, _) => Stmt::Noop,
Interface { .. } => Stmt::Noop,
Impl { .. } => Stmt::Expr(Expr::UnimplementedSigilValue),
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 {
fn reduce(&self, symbol_table: &SymbolTable, lhs: &Box<Expression>, rhs: &Box<Expression>) -> Expr {
2018-05-12 03:51:42 -07:00
if **self.sigil() == "=" {
Expr::Assign {
val: Box::new(lhs.reduce(symbol_table)),
expr: Box::new(rhs.reduce(symbol_table)),
2018-05-12 03:51:42 -07:00
}
} else {
let f = Box::new(Expr::Func(Func::BuiltIn(self.sigil().clone())));
Expr::Call { f, args: vec![lhs.reduce(symbol_table), rhs.reduce(symbol_table)]}
2018-05-12 03:51:42 -07:00
}
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 {
fn reduce(&self, symbol_table: &SymbolTable, arg: &Box<Expression>) -> Expr {
let f = Box::new(Expr::Func(Func::BuiltIn(self.sigil().clone())));
Expr::Call { f, args: vec![arg.reduce(symbol_table)]}
2018-05-10 23:41:51 -07:00
}
2018-05-09 17:02:10 -07:00
}