Start AST-reducing

This commit is contained in:
greg 2018-05-09 02:49:49 -07:00
parent 774ddd665b
commit 6c718e5d4f
2 changed files with 24 additions and 4 deletions

View File

@ -1,9 +1,11 @@
use std::rc::Rc; use std::rc::Rc;
use parsing::AST; use parsing::{AST, Expression, Declaration};
#[derive(Debug)]
pub struct ReducedAST(pub Vec<Stmt>); pub struct ReducedAST(pub Vec<Stmt>);
#[derive(Debug)]
pub enum Stmt { pub enum Stmt {
Binding { Binding {
name: Rc<String>, name: Rc<String>,
@ -12,6 +14,7 @@ pub enum Stmt {
Expr(Expr), Expr(Expr),
} }
#[derive(Debug)]
pub enum Expr { pub enum Expr {
Literal(Lit), Literal(Lit),
Func(Func), Func(Func),
@ -21,18 +24,34 @@ pub enum Expr {
}, },
} }
#[derive(Debug)]
pub enum Lit { pub enum Lit {
Int(u64), Int(u64),
Bool(bool), Bool(bool),
StringLit(Rc<String>), StringLit(Rc<String>),
} }
#[derive(Debug)]
pub struct Func { pub struct Func {
params: Vec<Rc<String>>, params: Vec<Rc<String>>,
body: Vec<Stmt>, body: Vec<Stmt>,
} }
pub fn perform_ast_reduction(ast: &AST) -> Result<ReducedAST, String> {
pub fn perform_ast_reduction(input: &AST) -> Result<ReducedAST, String> { use parsing::Statement::*;
Ok(ReducedAST(vec![])) 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))
}
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))))
} }

View File

@ -95,6 +95,7 @@ fn typechecking(handle: &mut Schala, input: parsing::AST, comp: Option<&mut Unfi
type TempASTReduction = (ast_reducing::ReducedAST, parsing::AST); type TempASTReduction = (ast_reducing::ReducedAST, parsing::AST);
fn ast_reducing(handle: &mut Schala, input: parsing::AST, comp: Option<&mut UnfinishedComputation>) -> Result<TempASTReduction, String> { fn ast_reducing(handle: &mut Schala, input: parsing::AST, comp: Option<&mut UnfinishedComputation>) -> Result<TempASTReduction, String> {
let output = ast_reducing::perform_ast_reduction(&input)?; let output = ast_reducing::perform_ast_reduction(&input)?;
println!("REDUCED: {:?}", output);
Ok((output, input)) Ok((output, input))
} }