From 005521624a9e4e3399840b23175d716e80072f9f Mon Sep 17 00:00:00 2001 From: alexis Date: Wed, 4 Jan 2023 00:40:33 +0100 Subject: [PATCH 1/3] Added nil method on IrBuilder --- src/ir/builder.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/ir/builder.rs b/src/ir/builder.rs index 4d5063d..6b17017 100644 --- a/src/ir/builder.rs +++ b/src/ir/builder.rs @@ -126,6 +126,13 @@ 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 { From 271234f8b51b6007a56580cb8a17574af1b9fb11 Mon Sep 17 00:00:00 2001 From: alexis Date: Wed, 4 Jan 2023 00:43:12 +0100 Subject: [PATCH 2/3] Fixed formatting --- src/ir/builder.rs | 100 +++++++++++++++++++--------------------------- 1 file changed, 41 insertions(+), 59 deletions(-) diff --git a/src/ir/builder.rs b/src/ir/builder.rs index 6b17017..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()) } @@ -133,9 +114,12 @@ impl IrBuilder { 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); @@ -143,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); @@ -186,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 { @@ -200,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() } From eb17d9f4722b7c4292ec6e4d8eeaca73f43e1bda Mon Sep 17 00:00:00 2001 From: alexis Date: Thu, 5 Jan 2023 22:00:02 +0100 Subject: [PATCH 3/3] Fixed bug with eq operator --- src/vm/vm.rs | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) 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 } }