From 633b4fe7a43849983a70076c8c04b4047d7df590 Mon Sep 17 00:00:00 2001 From: greg Date: Fri, 11 May 2018 00:38:40 -0700 Subject: [PATCH] Nats, some binop reduction --- schala-lang/src/ast_reducing.rs | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/schala-lang/src/ast_reducing.rs b/schala-lang/src/ast_reducing.rs index afb52c9..b44295a 100644 --- a/schala-lang/src/ast_reducing.rs +++ b/schala-lang/src/ast_reducing.rs @@ -27,16 +27,20 @@ pub enum Expr { #[derive(Debug)] pub enum Lit { - Int(u64), + Nat(u64), + Int(i64), Float(f64), Bool(bool), StringLit(Rc), } #[derive(Debug)] -pub struct Func { - params: Vec>, - body: Vec, +pub enum Func { + BuiltIn(Rc), + UserDefined { + params: Vec>, + body: Vec, + } } impl AST { @@ -45,7 +49,7 @@ impl AST { let mut output = vec![]; for statement in self.0.iter() { match statement { - &ExpressionStatement(ref expr) => output.push(expr.reduce()?), + &ExpressionStatement(ref expr) => output.push(Stmt::Expr(expr.reduce()?)), &Declaration(ref decl) => output.push(decl.reduce()?), } } @@ -54,11 +58,11 @@ impl AST { } impl Expression { - fn reduce(&self) -> Result { + 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)), + &IntLiteral(ref n) => Expr::Lit(Lit::Nat(*n)), //TODO I should rename IntLiteral if I want the Nat/Int distinction, which I do &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)), @@ -66,7 +70,7 @@ impl Expression { &PrefixExp(ref op, ref arg) => op.reduce(arg)?, e => return Err(format!("{:?} not implemented in reduction", e)) }; - Ok(Stmt::Expr(output_expr)) + Ok(output_expr) } } @@ -78,7 +82,8 @@ impl Declaration { impl BinOp { fn reduce(&self, lhs: &Box, rhs: &Box) -> Result { - Err(format!("NOTDONE")) + let f = Func::BuiltIn(self.sigil().clone()); + Ok(Expr::Call { f, args: vec![lhs.reduce()?, rhs.reduce()?]}) } }