diff --git a/src/ir/builder.rs b/src/ir/builder.rs index 4d5063d..94ae167 100644 --- a/src/ir/builder.rs +++ b/src/ir/builder.rs @@ -1,7 +1,7 @@ use super::*; -use std::rc::Rc; use std::cell::RefCell; +use std::rc::Rc; #[derive(Clone, Debug)] pub struct IrBuilder { @@ -15,7 +15,6 @@ impl IrBuilder { } } - pub fn bind(&mut self, binding: Binding, rhs: ExprNode) { let bind = Expr::Bind(binding, rhs); @@ -35,19 +34,13 @@ impl IrBuilder { TypeInfo::nil() }; - self.emit( - Expr::Return(value).node(info) - ) + self.emit(Expr::Return(value).node(info)) } pub fn break_(&mut self) { - self.emit( - Expr::Break.node(TypeInfo::nil()) - ) + self.emit(Expr::Break.node(TypeInfo::nil())) } - - pub fn list(&self, content: Vec) -> ExprNode { Expr::List(content).node(TypeInfo::nil()) } @@ -56,7 +49,6 @@ impl IrBuilder { Expr::SetElement(list, index, value).node(TypeInfo::nil()) } - pub fn dict(&self, keys: Vec, values: Vec) -> ExprNode { Expr::Dict(keys, values).node(TypeInfo::nil()) } @@ -66,30 +58,19 @@ impl IrBuilder { } pub fn var(&self, binding: Binding) -> ExprNode { - Expr::Var( - binding - ).node( - TypeInfo::nil() - ) + Expr::Var(binding).node(TypeInfo::nil()) } pub fn call(&self, callee: ExprNode, args: Vec, retty: Option) -> ExprNode { - let call = Call { - callee, - args - }; + let call = Call { callee, args }; - Expr::Call(call).node( - if let Some(info) = retty { - info - } else { - TypeInfo::nil() - } - ) + Expr::Call(call).node(if let Some(info) = retty { + info + } else { + TypeInfo::nil() + }) } - - pub fn binary(&self, lhs: ExprNode, op: BinaryOp, rhs: ExprNode) -> ExprNode { Expr::Binary(lhs, op, rhs).node(TypeInfo::nil()) } @@ -126,9 +107,19 @@ impl IrBuilder { Expr::Literal(lit).node(info) } + pub fn nil(&self) -> ExprNode { + let info = TypeInfo::new(Type::Nil); + let lit = Literal::Nil; + Expr::Literal(lit).node(info) + } - pub fn function(&mut self, var: Binding, params: &[&str], mut body_build: impl FnMut(&mut IrBuilder)) -> ExprNode { + pub fn function( + &mut self, + var: Binding, + params: &[&str], + mut body_build: impl FnMut(&mut IrBuilder), + ) -> ExprNode { let mut body_builder = IrBuilder::new(); body_build(&mut body_builder); @@ -136,33 +127,40 @@ impl IrBuilder { let body = body_builder.build(); let func_body = IrFunctionBody { - params: params.iter().cloned().map(|x: &str| - Binding::local(x, var.depth.unwrap_or(0) + 1, var.function_depth + 1)).collect::>(), + params: params + .iter() + .cloned() + .map(|x: &str| { + Binding::local(x, var.depth.unwrap_or(0) + 1, var.function_depth + 1) + }) + .collect::>(), method: false, - inner: body + inner: body, }; let ir_func = IrFunction { var, - body: Rc::new(RefCell::new(func_body)) + body: Rc::new(RefCell::new(func_body)), }; - Expr::Function( - ir_func - ).node( - TypeInfo::nil() - ) + Expr::Function(ir_func).node(TypeInfo::nil()) } - pub fn ternary(&mut self, cond: ExprNode, then_body: ExprNode, else_body: Option) -> ExprNode { - Expr::If( - cond, - then_body, - else_body - ).node(TypeInfo::nil()) + pub fn ternary( + &mut self, + cond: ExprNode, + then_body: ExprNode, + else_body: Option, + ) -> ExprNode { + Expr::If(cond, then_body, else_body).node(TypeInfo::nil()) } - pub fn if_(&mut self, cond: ExprNode, then_build: fn(&mut IrBuilder), else_build: Option) -> ExprNode { + pub fn if_( + &mut self, + cond: ExprNode, + then_build: fn(&mut IrBuilder), + else_build: Option, + ) -> ExprNode { let mut then_builder = IrBuilder::new(); then_build(&mut then_builder); @@ -179,11 +177,7 @@ impl IrBuilder { None }; - Expr::If( - cond, - then_body, - else_body - ).node(TypeInfo::nil()) + Expr::If(cond, then_body, else_body).node(TypeInfo::nil()) } pub fn while_(&mut self, cond: ExprNode, then_build: fn(&mut IrBuilder)) -> ExprNode { @@ -193,14 +187,9 @@ impl IrBuilder { let then_body = Expr::Block(then_builder.build()).node(TypeInfo::nil()); - Expr::While( - cond, - then_body, - ).node(TypeInfo::nil()) + Expr::While(cond, then_body).node(TypeInfo::nil()) } - - pub fn build(&self) -> Vec { self.program.clone() } diff --git a/src/vm/vm.rs b/src/vm/vm.rs index 2cb97c1..0e45982 100644 --- a/src/vm/vm.rs +++ b/src/vm/vm.rs @@ -76,14 +76,8 @@ macro_rules! binary_op { let b = $self.pop(); let a = $self.pop(); - if let (Variant::Float(a), Variant::Float(b)) = (a.decode(), b.decode()) { - let c = a $op b; - $self.push(c.into()); - - return - } - - // TODO: ERROR HERE + $self.push((a == b).into()); + return } }