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

len intrinsic + iterating example

This commit is contained in:
Natapat Samutpong 2022-03-22 06:31:45 +07:00
parent 00b97b9f3b
commit b52661c9d9
3 changed files with 24 additions and 1 deletions

View file

@ -64,7 +64,9 @@ impl Codegen {
"read" => { format!("read({}){}\n" , self.gen_ir(&args[0], false), semicolon!()) }, "read" => { format!("read({}){}\n" , self.gen_ir(&args[0], false), semicolon!()) },
"read_file" => { format!("readFile({}){}\n" , self.gen_ir(&args[0], false), semicolon!()) } "read_file" => { format!("readFile({}){}\n" , self.gen_ir(&args[0], false), semicolon!()) }
"emit" => { format!("{}", self.gen_ir(&args[0], false).trim_start_matches('"').trim_end_matches('"')) }, "emit" => { format!("{}", self.gen_ir(&args[0], false).trim_start_matches('"').trim_end_matches('"')) },
"get" => { format!("{}[{}]", self.gen_ir(&args[0], false), self.gen_ir(&args[1], false)) }, "get" => { format!("{}[{}]", self.gen_ir(&args[0], false), self.gen_ir(&args[1], false)) },
"len" => { format!("{}.length", self.gen_ir(&args[0], false)) },
_ => unreachable!(format!("Unknown intrinsic: {}", name)) // Shoul be handled by lowering _ => unreachable!(format!("Unknown intrinsic: {}", name)) // Shoul be handled by lowering
} }
}, },

View file

@ -1,13 +1,14 @@
use std::ops::Range; use std::ops::Range;
use parser::Expr; use parser::Expr;
const INTRINSICS: [&str; 6] = [ const INTRINSICS: [&str; 7] = [
"write", "write",
"read", "read",
"write_file", "write_file",
"read_file", "read_file",
"emit", "emit",
"get", "get",
"len",
]; ];
#[derive(Debug, Clone)] #[derive(Debug, Clone)]

20
example/iter.hz Normal file
View file

@ -0,0 +1,20 @@
fun iter_ (vec: vec_int) (current: int): void = do
if current == @len(vec) then
do end;
else
do
@get(vec, current) |> @write(_);
@write("\n");
iter_(vec, current + 1);
end;
end;
end;
fun iter (vec: vec_int): void = do
iter_(vec, 0);
end;
fun main: void = do
let foo: vec_int = [69, 420, 727, 1337, 42069, 69420];
iter(foo);
end;