fix codegen formatting

pull/4/head
azur 2022-05-04 00:54:54 +07:00
parent ffd922759e
commit d8fbc17a33
5 changed files with 14 additions and 35 deletions

View File

@ -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))

View File

@ -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();
}

7
examples/factorial.hz Normal file
View File

@ -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)

View File

@ -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")

View File

@ -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<Int, String> = { 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)