From e19e4685459b3d0fc4e3f5917967c744232985e2 Mon Sep 17 00:00:00 2001 From: Natapat Samutpong Date: Thu, 24 Mar 2022 11:18:53 +0700 Subject: [PATCH] woopsie --- crates/codegen/src/ts.rs | 4 +++- crates/hir/src/lib.rs | 3 ++- example/err/throw.hz | 3 +++ 3 files changed, 8 insertions(+), 2 deletions(-) create mode 100644 example/err/throw.hz diff --git a/crates/codegen/src/ts.rs b/crates/codegen/src/ts.rs index 1e111b7..544612e 100644 --- a/crates/codegen/src/ts.rs +++ b/crates/codegen/src/ts.rs @@ -62,11 +62,13 @@ impl Codegen { "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!()) }, "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('"')) }, "get" => { format!("{}[{}]", self.gen_ir(&args[0], false), self.gen_ir(&args[1], false)) }, "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 } }, diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index 4fb8814..0d6fe2d 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -1,7 +1,7 @@ use std::ops::Range; use parser::Expr; -const INTRINSICS: [&str; 7] = [ +const INTRINSICS: [&str; 8] = [ "write", "read", "write_file", @@ -9,6 +9,7 @@ const INTRINSICS: [&str; 7] = [ "emit", "get", "len", + "throw", ]; #[derive(Debug, Clone)] diff --git a/example/err/throw.hz b/example/err/throw.hz new file mode 100644 index 0000000..c82d8c9 --- /dev/null +++ b/example/err/throw.hz @@ -0,0 +1,3 @@ +fun main: void = do + @throw("woopsie"); +end;