diff --git a/crates/codegen/src/ts.rs b/crates/codegen/src/ts.rs index d335b29..0115f5f 100644 --- a/crates/codegen/src/ts.rs +++ b/crates/codegen/src/ts.rs @@ -64,7 +64,9 @@ impl Codegen { "read" => { format!("read({}){}\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('"')) }, + "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 } }, diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index 5db1319..d65aa94 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -1,13 +1,14 @@ use std::ops::Range; use parser::Expr; -const INTRINSICS: [&str; 6] = [ +const INTRINSICS: [&str; 7] = [ "write", "read", "write_file", "read_file", "emit", "get", + "len", ]; #[derive(Debug, Clone)] diff --git a/example/iter.hz b/example/iter.hz new file mode 100644 index 0000000..8ddba9b --- /dev/null +++ b/example/iter.hz @@ -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;