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

emit intrinsic

This commit is contained in:
Natapat Samutpong 2022-03-18 06:57:52 +07:00
parent 30331c9bf6
commit 2d45cf240b
3 changed files with 17 additions and 5 deletions

View file

@ -59,7 +59,8 @@ impl Codegen {
match name.as_str() { match name.as_str() {
"write" => { format!("write({}){}\n", self.gen_ir(&args[0], false), semicolon!()) }, "write" => { format!("write({}){}\n", self.gen_ir(&args[0], false), semicolon!()) },
"write_file" => { format!("writeFile({}, {}){}\n", self.gen_ir(&args[0], false), self.gen_ir(&args[1], false), semicolon!()) }, "write_file" => { format!("writeFile({}, {}){}\n", self.gen_ir(&args[0], false), self.gen_ir(&args[1], false), semicolon!()) },
"read" => { todo!() }, "read" => { todo!() },
"emit" => { format!("{}", self.gen_ir(&args[0], false).trim_start_matches('"').trim_end_matches('"')) },
_ => unreachable!(format!("Unknown intrinsic: {}", name)) // Shoul be handled by lowering _ => unreachable!(format!("Unknown intrinsic: {}", name)) // Shoul be handled by lowering
} }
}, },

View file

@ -1,10 +1,11 @@
use std::ops::Range; use std::ops::Range;
use parser::Expr; use parser::Expr;
const INTRINSICS: [&str; 3] = [ const INTRINSICS: [&str; 4] = [
"write", "write",
"read", "read",
"write_file", "write_file",
"emit",
]; ];
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -204,13 +205,20 @@ pub fn expr_to_ir(expr: &Expr) -> (Option<IRKind>, Option<LoweringError>) {
} }
_ => return (None, Some(LoweringError { span: name.1.clone(), message: "Expected identifier".to_string(), note: None })) _ => return (None, Some(LoweringError { span: name.1.clone(), message: "Expected identifier".to_string(), note: None }))
}; };
let mut largs = Vec::new(); let mut largs = Vec::new();
for arg in &args.0 { for arg in &args.0 {
let arg = expr_to_ir(&arg.0); let larg = expr_to_ir(&arg.0);
if_err_return!(arg.1); if_err_return!(larg.1);
largs.push(arg.0.unwrap()); // Check if the args is string
if let IRKind::Value{ .. } = larg.0.clone().unwrap() {
largs.push(larg.0.clone().unwrap());
} else {
return (None, Some(LoweringError { span: arg.1.clone(), message: "Expected string".to_string(), note: None }))
}
} }
let ir_kind = IRKind::Intrinsic { name, args: largs }; let ir_kind = IRKind::Intrinsic { name, args: largs };
return (Some(ir_kind), None); return (Some(ir_kind), None);
}, },

3
example/emit.hz Normal file
View file

@ -0,0 +1,3 @@
fun main: void = do
@emit("console.log('Hello, World!')");
end;