1
1
Fork 0
mirror of https://github.com/azur1s/bobbylisp.git synced 2024-10-16 02:37:40 -05:00

binary and unary, fix some missing semicolon

This commit is contained in:
Natapat Samutpong 2022-03-12 10:25:52 +07:00
parent ebaee0ea0c
commit 2035dd115d
6 changed files with 60 additions and 20 deletions

View file

@ -4,7 +4,7 @@ Programming language that compiles to C++!
```sml
fun main: int = do
@write("Hello, World!\n");
return 0;
return 69;
end;
```

View file

@ -47,7 +47,7 @@ impl Codegen {
}
IRKind::Fun { name, return_type_hint, args, body } => {
let args = args.iter().map(|arg| format!("{} {}", arg.1, arg.0)).collect::<Vec<_>>().join(", ");
format!("{} {}({}) {{\n{}}}\n", return_type_hint, name, args, self.gen_ir(body))
format!("{} {}({}) {{\n{};\n}}\n", return_type_hint, name, args, self.gen_ir(body))
},
IRKind::Return { value } => {
format!("return {};\n", self.gen_ir(value))
@ -62,6 +62,12 @@ impl Codegen {
IRKind::If { cond, body, else_body } => {
format!("if ({}) {{\n{}}} else {{\n{}}}\n", self.gen_ir(cond), self.gen_ir(body), self.gen_ir(else_body))
},
IRKind::Unary { op, right } => {
format!("{}{}", op, self.gen_ir(right))
},
IRKind::Binary { left, op, right } => {
format!("{} {} {}", self.gen_ir(left), op, self.gen_ir(right))
},
IRKind::Value { value } => {
match value {
@ -71,6 +77,8 @@ impl Codegen {
Value::Ident(value) => format!("{}", value),
}
},
#[allow(unreachable_patterns)]
_ => { dbg!(ir); todo!() },
}
}

View file

@ -14,8 +14,9 @@ pub enum IRKind {
Intrinsic { name: String, args: Vec<Self> },
Do { body: Vec<Self> },
If { cond: Box<Self>, body: Box<Self>, else_body: Box<Self> },
Value { value: Value },
Unary { op: String, right: Box<Self> },
Binary { op: String, left: Box<Self>, right: Box<Self> },
Value { value: Value },
Return { value: Box<Self> },
}
@ -60,23 +61,26 @@ macro_rules! if_err_return {
pub fn expr_to_ir(expr: &Expr) -> (Option<IRKind>, Option<LoweringError>) {
match expr {
Expr::Let { name, type_hint, value } => {
let value = expr_to_ir(&value.0);
if_err_return!(value.1);
Expr::Unary { op, rhs } => {
let rhs_ir = expr_to_ir(&rhs.0);
if_err_return!(rhs_ir.1);
let value = value.0.unwrap();
let ir_kind = IRKind::Define { name: name.clone(), type_hint: type_hint.clone(), value: Box::new(value) };
return (Some(ir_kind), None);
return (Some(IRKind::Unary { op: op.to_string(), right: Box::new(rhs_ir.0.unwrap()) }), None);
}
Expr::Binary { lhs, op, rhs } => {
let lhs_ir = expr_to_ir(&lhs.0);
if_err_return!(lhs_ir.1);
let rhs_ir = expr_to_ir(&rhs.0);
if_err_return!(rhs_ir.1);
return (Some(IRKind::Binary { op: op.to_string(), left: Box::new(lhs_ir.0.unwrap()), right: Box::new(rhs_ir.0.unwrap()) }), None)
},
Expr::Call { name, args } => {
let name = match &name.0 {
Expr::Identifier(s) => {
if INTRINSICS.contains(&s.as_str()) { s.clone() }
else {
return (None, Some(LoweringError { span: name.1.clone(), message: format!("Unknown intrinsic: {}", s) }));
}
}
Expr::Identifier(s) => s.clone(),
// Should never happen because the parser should have caught this
_ => return (None, Some(LoweringError { span: name.1.clone(), message: "Expected identifier".to_string() }))
};
@ -92,9 +96,23 @@ pub fn expr_to_ir(expr: &Expr) -> (Option<IRKind>, Option<LoweringError>) {
return (Some(ir_kind), None);
},
Expr::Let { name, type_hint, value } => {
let value = expr_to_ir(&value.0);
if_err_return!(value.1);
let value = value.0.unwrap();
let ir_kind = IRKind::Define { name: name.clone(), type_hint: type_hint.clone(), value: Box::new(value) };
return (Some(ir_kind), None);
},
Expr::Intrinsic { name, args } => {
let name = match &name.0 {
Expr::Identifier(s) => s.clone(),
Expr::Identifier(s) => {
if INTRINSICS.contains(&s.as_str()) { s.clone() }
else {
return (None, Some(LoweringError { span: name.1.clone(), message: format!("Unknown intrinsic: {}", s) }));
}
}
_ => return (None, Some(LoweringError { span: name.1.clone(), message: "Expected identifier".to_string() }))
};
let mut largs = Vec::new();

14
example/69.hz Normal file
View file

@ -0,0 +1,14 @@
fun add2 (lhs: int) (rhs: int): int = do
return lhs + rhs;
end;
fun main: int = do
let result: int = add2(34, 35);
@write(result);
@write("\n");
if result == 69 then
@write("big cool")
else
@write("not cool")
end;
end;

View file

@ -1,4 +0,0 @@
fun main: int = do
@write("Hello, World!\n");
return 0;
end;

4
example/hello_world.hz Normal file
View file

@ -0,0 +1,4 @@
fun main: int = do
@writesss("Hello, World!\n");
return 69;
end;