From 16a463b1a003c356654a5cee748ec67fc8b9b96b Mon Sep 17 00:00:00 2001 From: greg Date: Thu, 10 May 2018 23:41:51 -0700 Subject: [PATCH] Method-style --- schala-lang/src/ast_reducing.rs | 63 +++++++++++++++++++-------------- schala-lang/src/lib.rs | 2 +- 2 files changed, 38 insertions(+), 27 deletions(-) diff --git a/schala-lang/src/ast_reducing.rs b/schala-lang/src/ast_reducing.rs index 5574f38..afb52c9 100644 --- a/schala-lang/src/ast_reducing.rs +++ b/schala-lang/src/ast_reducing.rs @@ -39,40 +39,51 @@ pub struct Func { body: Vec, } -pub fn perform_ast_reduction(ast: &AST) -> Result { - 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)?), +impl AST { + pub fn reduce(&self) -> Result { + use parsing::Statement::*; + let mut output = vec![]; + for statement in self.0.iter() { + match statement { + &ExpressionStatement(ref expr) => output.push(expr.reduce()?), + &Declaration(ref decl) => output.push(decl.reduce()?), + } } + Ok(ReducedAST(output)) } - Ok(ReducedAST(output)) } -fn reduce_expr(expr: &Expression) -> Result { - use parsing::ExpressionType::*; - let ref input = expr.0; - let output_expr = match input { - &IntLiteral(ref n) => Expr::Lit(Lit::Int(*n)), - &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)), - &BinExp(ref binop, ref lhs, ref rhs) => reduce_binop(binop, lhs, rhs)?, - &PrefixExp(ref op, ref arg) => reduce_prefix(op, arg)?, - e => return Err(format!("{:?} not implemented in reduction", e)) - }; - Ok(Stmt::Expr(output_expr)) -} -fn reduce_decl(expr: &Declaration) -> Result { - Ok(Stmt::Expr(Expr::Lit(Lit::Int(0)))) +impl Expression { + fn reduce(&self) -> Result { + use parsing::ExpressionType::*; + let ref input = self.0; + let output_expr = match input { + &IntLiteral(ref n) => Expr::Lit(Lit::Int(*n)), + &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)), + &BinExp(ref binop, ref lhs, ref rhs) => binop.reduce(lhs, rhs)?, + &PrefixExp(ref op, ref arg) => op.reduce(arg)?, + e => return Err(format!("{:?} not implemented in reduction", e)) + }; + Ok(Stmt::Expr(output_expr)) + } } -fn reduce_binop(binop: &BinOp, lhs: &Box, rhs: &Box) -> Result { +impl Declaration { + fn reduce(&self) -> Result { + Ok(Stmt::Expr(Expr::Lit(Lit::Int(0)))) + } +} + +impl BinOp { + fn reduce(&self, lhs: &Box, rhs: &Box) -> Result { Err(format!("NOTDONE")) + } } -fn reduce_prefix(op: &PrefixOp, arg: &Box) -> Result { +impl PrefixOp { + fn reduce(&self, arg: &Box) -> Result { Err(format!("NOTDONE")) + } } diff --git a/schala-lang/src/lib.rs b/schala-lang/src/lib.rs index 93a8b9d..2b228fc 100644 --- a/schala-lang/src/lib.rs +++ b/schala-lang/src/lib.rs @@ -95,7 +95,7 @@ fn typechecking(handle: &mut Schala, input: parsing::AST, comp: Option<&mut Unfi type TempASTReduction = (ast_reducing::ReducedAST, parsing::AST); fn ast_reducing(handle: &mut Schala, input: parsing::AST, comp: Option<&mut UnfinishedComputation>) -> Result { - let output = ast_reducing::perform_ast_reduction(&input)?; + let output = input.reduce()?; println!("REDUCED: {:?}", output); Ok((output, input)) }