From 1f9a9cdf71c45bb8a65c3c2298dfb8a2752337a9 Mon Sep 17 00:00:00 2001 From: Natapat Samutpong Date: Mon, 11 Apr 2022 05:28:04 +0700 Subject: [PATCH] tuples codegen & some examples --- crates/codegen/src/ts.rs | 14 ++++++++++++-- crates/hir/src/lib.rs | 1 + example/tuples.hz | 33 +++++++++++++++++++++++++++++++++ runtime/tuples.ts | 4 ++++ 4 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 example/tuples.hz create mode 100644 runtime/tuples.ts diff --git a/crates/codegen/src/ts.rs b/crates/codegen/src/ts.rs index 4378415..47ad49b 100644 --- a/crates/codegen/src/ts.rs +++ b/crates/codegen/src/ts.rs @@ -75,7 +75,7 @@ impl Codegen { "len" => { format!("{}.length", self.gen_ir(&args[0], false)) }, "throw" => { format!("throw new Error({}){}", self.gen_ir(&args[0], false), semicolon!()) }, - _ => unreachable!(format!("Unknown intrinsic: {}", name)) // Shoul be handled by lowering + _ => unreachable!("{}", format!("Unknown intrinsic: {}", name)) // Shoul be handled by lowering } }, @@ -86,7 +86,7 @@ impl Codegen { .collect::>(). join(", "); format!( - "{} const _{} = ({}): {} => {};\n", + "{} const _{} = ({}): {} => {{{}}};\n", if *public { "export" } else { "" }, name, args, @@ -156,6 +156,16 @@ impl Codegen { } }, + IRKind::Tuple { values } => { + format!( + "[{}]", + values + .iter() + .map(|value| self.gen_ir(value, false)) + .collect::>() + .join(", ") + ) + }, IRKind::Vector { values } => { format!( "[{}]", diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index 254da0d..dcb1536 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -450,6 +450,7 @@ pub fn expr_to_ir(expr: &Expr) -> (Option, Option) { fn gen_type_hint(type_hint: &Typehint) -> String { match type_hint { Typehint::Single(t) => match t.as_str() { + "any" => "any".to_string(), "int" => "number".to_string(), "bool" => "boolean".to_string(), _ => t.to_string() diff --git a/example/tuples.hz b/example/tuples.hz new file mode 100644 index 0000000..354e483 --- /dev/null +++ b/example/tuples.hz @@ -0,0 +1,33 @@ +-- This is a rewrite of runtime/tuples.ts in itself +fun every_ (a : [any]) (b : [any]) (current : int) : bool = do + if @len(a) == current + then return true + else + do + let aa : any = @get(a, current) + let bb : any = @get(b, current) + if aa == bb + then return every_(a, b, current + 1) + else return false end + end + end +end + +fun every (a : [any]) (b : [any]): bool = return every_(a, b, 0) + +fun eqTuples (a : [any]) (b : [any]) : bool = + match @len(a) == @len(b) with + | true -> return every(a, b) + | else return false + end + +fun main : void = do + let foo : (int, int) = (1, 3) + let bar : (int, int) = (2, 1) + let baz : (int, int, int) = (1, 3, 1) + eqT(foo, bar) |> @write(_) + @write("\n") + eqT(foo, baz) |> @write(_) + @write("\n") + eqT(foo, (1, 3)) |> @write(_) +end \ No newline at end of file diff --git a/runtime/tuples.ts b/runtime/tuples.ts new file mode 100644 index 0000000..4b823c6 --- /dev/null +++ b/runtime/tuples.ts @@ -0,0 +1,4 @@ +export function eqTuples(a: any[], b: any[]) { + if (a.length !== b.length) return false; + return a.every((elem, i) => b[i] === elem); +} \ No newline at end of file