From d8fbc17a33dc697402653fa0ed95735cb143a9be Mon Sep 17 00:00:00 2001 From: azur <73991665+azur1s@users.noreply.github.com> Date: Wed, 4 May 2022 00:54:54 +0700 Subject: [PATCH] fix codegen formatting --- codegen/src/lib.rs | 6 +++--- core/src/main.rs | 10 ++++------ examples/factorial.hz | 7 +++++++ examples/test.hz | 11 ----------- examples/type.hz | 15 --------------- 5 files changed, 14 insertions(+), 35 deletions(-) create mode 100644 examples/factorial.hz delete mode 100644 examples/test.hz delete mode 100644 examples/type.hz diff --git a/codegen/src/lib.rs b/codegen/src/lib.rs index 18e70e7..100b63a 100644 --- a/codegen/src/lib.rs +++ b/codegen/src/lib.rs @@ -83,7 +83,7 @@ impl Codegen { Expr::Define { name, typehint, value } => { format!( - "let {} : {} = {}{}", + "let _{} : {} = {}{}", name.0, self.gen_typehint(&typehint.0), self.gen_expr(&value.0, false), @@ -91,7 +91,7 @@ impl Codegen { }, Expr::Redefine { name, value } => { format!( - "{} = {}{}", + "_{} = {}{}", name.0, self.gen_expr(&value.0, false), semicolon!()) @@ -117,7 +117,7 @@ impl Codegen { Expr::If { cond, t, f } => { format!( - "if {} {{{}}} else {{{}}}", + "if ({}) {{{}}} else {{{}}}", self.gen_expr(&cond.0, false), self.gen_expr(&t.0, false), self.gen_expr(&f.0, false)) diff --git a/core/src/main.rs b/core/src/main.rs index 3b953c7..6f717ff 100644 --- a/core/src/main.rs +++ b/core/src/main.rs @@ -4,13 +4,11 @@ use syntax::{lex::lex, parse::parse}; use codegen::Codegen; fn main() { + let path = std::env::args().nth(1).expect("No file specified"); + let input = std::fs::read_to_string(path).expect("Failed to read file"); + let time = std::time::Instant::now(); - let input = " -fun len T (vec : [T]) : int = return ;vec.length - -@write(len([1, 2, 3])) -"; // // Lex // @@ -37,6 +35,6 @@ fun len T (vec : [T]) : int = return ;vec.length let mut codegen = Codegen::new(); codegen.gen(ast.unwrap()); - let mut file = File::create("out.rs").unwrap(); + let mut file = File::create("out.ts").unwrap(); file.write_all(codegen.emitted.join("\n").as_bytes()).unwrap(); } diff --git a/examples/factorial.hz b/examples/factorial.hz new file mode 100644 index 0000000..432ddc5 --- /dev/null +++ b/examples/factorial.hz @@ -0,0 +1,7 @@ +fun factorial (n : int) : int = + if n == 0 + | return 1 + | return n * factorial(n - 1) + +result : int = factorial(5) +@write(result) \ No newline at end of file diff --git a/examples/test.hz b/examples/test.hz deleted file mode 100644 index 833250a..0000000 --- a/examples/test.hz +++ /dev/null @@ -1,11 +0,0 @@ -fun foo (a : int) (b : int) : int = do - c : int = a + b - return c -end - -bar = foo(34, 35) -@println(bar) - -if bar == 69 - | @println("That's 69") - | @println("Ok") \ No newline at end of file diff --git a/examples/type.hz b/examples/type.hz deleted file mode 100644 index e776f49..0000000 --- a/examples/type.hz +++ /dev/null @@ -1,15 +0,0 @@ -type Left E = { tag: "Left", value: E } -type Right T = { tag: "Right", value: T } - -type Either T E = Left E - | Right T - -it : Either = { tag: "Left", value: "Uninit" } - -fun rand_it : void = if @random() > 0.5 - | set it = { tag: "Left", value: "Error" } - | set it = { tag: "Right", value: 42 } - -rand() -@println(it.tag, it.value) -