Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug with EQ operation #10

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 46 additions & 57 deletions src/ir/builder.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::*;

use std::rc::Rc;
use std::cell::RefCell;
use std::rc::Rc;

#[derive(Clone, Debug)]
pub struct IrBuilder {
Expand All @@ -15,7 +15,6 @@ impl IrBuilder {
}
}


pub fn bind(&mut self, binding: Binding, rhs: ExprNode) {
let bind = Expr::Bind(binding, rhs);

Expand All @@ -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>) -> ExprNode {
Expr::List(content).node(TypeInfo::nil())
}
Expand All @@ -56,7 +49,6 @@ impl IrBuilder {
Expr::SetElement(list, index, value).node(TypeInfo::nil())
}


pub fn dict(&self, keys: Vec<ExprNode>, values: Vec<ExprNode>) -> ExprNode {
Expr::Dict(keys, values).node(TypeInfo::nil())
}
Expand All @@ -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<ExprNode>, retty: Option<TypeInfo>) -> 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())
}
Expand Down Expand Up @@ -126,43 +107,60 @@ 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);

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::<Vec<Binding>>(),
params: params
.iter()
.cloned()
.map(|x: &str| {
Binding::local(x, var.depth.unwrap_or(0) + 1, var.function_depth + 1)
})
.collect::<Vec<Binding>>(),
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>) -> 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>,
) -> 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<fn(&mut IrBuilder)>) -> ExprNode {
pub fn if_(
&mut self,
cond: ExprNode,
then_build: fn(&mut IrBuilder),
else_build: Option<fn(&mut IrBuilder)>,
) -> ExprNode {
let mut then_builder = IrBuilder::new();

then_build(&mut then_builder);
Expand All @@ -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 {
Expand All @@ -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<ExprNode> {
self.program.clone()
}
Expand Down
10 changes: 2 additions & 8 deletions src/vm/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down