Method-style
This commit is contained in:
parent
c3be644133
commit
16a463b1a0
@ -39,40 +39,51 @@ pub struct Func {
|
|||||||
body: Vec<Stmt>,
|
body: Vec<Stmt>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn perform_ast_reduction(ast: &AST) -> Result<ReducedAST, String> {
|
impl AST {
|
||||||
|
pub fn reduce(&self) -> Result<ReducedAST, String> {
|
||||||
use parsing::Statement::*;
|
use parsing::Statement::*;
|
||||||
let mut output = vec![];
|
let mut output = vec![];
|
||||||
for statement in ast.0.iter() {
|
for statement in self.0.iter() {
|
||||||
match statement {
|
match statement {
|
||||||
&ExpressionStatement(ref expr) => output.push(reduce_expr(expr)?),
|
&ExpressionStatement(ref expr) => output.push(expr.reduce()?),
|
||||||
&Declaration(ref decl) => output.push(reduce_decl(decl)?),
|
&Declaration(ref decl) => output.push(decl.reduce()?),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(ReducedAST(output))
|
Ok(ReducedAST(output))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn reduce_expr(expr: &Expression) -> Result<Stmt, String> {
|
impl Expression {
|
||||||
|
fn reduce(&self) -> Result<Stmt, String> {
|
||||||
use parsing::ExpressionType::*;
|
use parsing::ExpressionType::*;
|
||||||
let ref input = expr.0;
|
let ref input = self.0;
|
||||||
let output_expr = match input {
|
let output_expr = match input {
|
||||||
&IntLiteral(ref n) => Expr::Lit(Lit::Int(*n)),
|
&IntLiteral(ref n) => Expr::Lit(Lit::Int(*n)),
|
||||||
&FloatLiteral(ref f) => Expr::Lit(Lit::Float(*f)),
|
&FloatLiteral(ref f) => Expr::Lit(Lit::Float(*f)),
|
||||||
&StringLiteral(ref s) => Expr::Lit(Lit::StringLit(s.clone())),
|
&StringLiteral(ref s) => Expr::Lit(Lit::StringLit(s.clone())),
|
||||||
&BoolLiteral(ref b) => Expr::Lit(Lit::Bool(*b)),
|
&BoolLiteral(ref b) => Expr::Lit(Lit::Bool(*b)),
|
||||||
&BinExp(ref binop, ref lhs, ref rhs) => reduce_binop(binop, lhs, rhs)?,
|
&BinExp(ref binop, ref lhs, ref rhs) => binop.reduce(lhs, rhs)?,
|
||||||
&PrefixExp(ref op, ref arg) => reduce_prefix(op, arg)?,
|
&PrefixExp(ref op, ref arg) => op.reduce(arg)?,
|
||||||
e => return Err(format!("{:?} not implemented in reduction", e))
|
e => return Err(format!("{:?} not implemented in reduction", e))
|
||||||
};
|
};
|
||||||
Ok(Stmt::Expr(output_expr))
|
Ok(Stmt::Expr(output_expr))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
fn reduce_decl(expr: &Declaration) -> Result<Stmt, String> {
|
|
||||||
|
impl Declaration {
|
||||||
|
fn reduce(&self) -> Result<Stmt, String> {
|
||||||
Ok(Stmt::Expr(Expr::Lit(Lit::Int(0))))
|
Ok(Stmt::Expr(Expr::Lit(Lit::Int(0))))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn reduce_binop(binop: &BinOp, lhs: &Box<Expression>, rhs: &Box<Expression>) -> Result<Expr, String> {
|
impl BinOp {
|
||||||
|
fn reduce(&self, lhs: &Box<Expression>, rhs: &Box<Expression>) -> Result<Expr, String> {
|
||||||
Err(format!("NOTDONE"))
|
Err(format!("NOTDONE"))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn reduce_prefix(op: &PrefixOp, arg: &Box<Expression>) -> Result<Expr, String> {
|
impl PrefixOp {
|
||||||
|
fn reduce(&self, arg: &Box<Expression>) -> Result<Expr, String> {
|
||||||
Err(format!("NOTDONE"))
|
Err(format!("NOTDONE"))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -95,7 +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 = input.reduce()?;
|
||||||
println!("REDUCED: {:?}", output);
|
println!("REDUCED: {:?}", output);
|
||||||
Ok((output, input))
|
Ok((output, input))
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user