schala/schala-lang/src/ast_reducing.rs

58 lines
1.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-05-09 02:49:49 -07:00
use parsing::{AST, Expression, Declaration};
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-09 02:49:49 -07:00
#[derive(Debug)]
2018-05-09 02:27:57 -07:00
pub enum Stmt {
Binding {
name: Rc<String>,
expr: Expr,
},
Expr(Expr),
}
2018-05-09 02:49:49 -07:00
#[derive(Debug)]
2018-05-09 02:27:57 -07:00
pub enum Expr {
Literal(Lit),
Func(Func),
Call {
f: Func,
args: Vec<Expr>,
},
}
2018-05-09 02:49:49 -07:00
#[derive(Debug)]
2018-05-09 02:27:57 -07:00
pub enum Lit {
Int(u64),
Bool(bool),
StringLit(Rc<String>),
}
2018-05-09 02:49:49 -07:00
#[derive(Debug)]
2018-05-09 02:27:57 -07:00
pub struct Func {
params: Vec<Rc<String>>,
body: Vec<Stmt>,
}
2018-05-09 02:49:49 -07:00
pub fn perform_ast_reduction(ast: &AST) -> Result<ReducedAST, String> {
use parsing::Statement::*;
let mut output = vec![];
for statement in ast.0.iter() {
match statement {
&ExpressionStatement(ref expr) => output.push(reduce_expr(expr)?),
&Declaration(ref decl) => output.push(reduce_decl(decl)?),
}
}
Ok(ReducedAST(output))
}
2018-05-09 02:27:57 -07:00
2018-05-09 02:49:49 -07:00
fn reduce_expr(expr: &Expression) -> Result<Stmt, String> {
Ok(Stmt::Expr(Expr::Literal(Lit::Int(0))))
}
fn reduce_decl(expr: &Declaration) -> Result<Stmt, String> {
Ok(Stmt::Expr(Expr::Literal(Lit::Int(0))))
2018-05-09 02:27:57 -07:00
}