From 30331c9bf6ded1b54158877498745bd0de92b476 Mon Sep 17 00:00:00 2001 From: Natapat Samutpong Date: Fri, 18 Mar 2022 06:26:54 +0700 Subject: [PATCH] prefix transpiled variable & function --- crates/codegen/src/ts.rs | 10 +++++----- crates/parser/src/lib.rs | 11 +++++++---- example/fibonacci.hz | 11 +++++++++++ example/if.hz | 12 ++++++++++-- example/pipe.hz | 2 ++ 5 files changed, 35 insertions(+), 11 deletions(-) create mode 100644 example/fibonacci.hz diff --git a/crates/codegen/src/ts.rs b/crates/codegen/src/ts.rs index bdee1d0..52817ae 100644 --- a/crates/codegen/src/ts.rs +++ b/crates/codegen/src/ts.rs @@ -23,7 +23,7 @@ impl Codegen { self.emit(&self.gen_ir(&ir.kind, true)); } - self.emit("main();"); + self.emit("f_main();"); } fn gen_ir(&self, ir: &IRKind, should_gen_semicolon: bool) -> String { @@ -43,7 +43,7 @@ impl Codegen { IRKind::Call { name, args } => { format!( - "{}({}){}", + "f_{}({}){}", name, args .iter() @@ -67,11 +67,11 @@ impl Codegen { IRKind::Fun { name, return_type_hint, args, body } => { let args = args .iter() - .map(|arg| format!("{}: {}", arg.0, arg.1)) + .map(|arg| format!("v_{}: {}", arg.0, arg.1)) .collect::>(). join(", "); format!( - "const {} = ({}): {} => {{\n{}\n}};\n", + "const f_{} = ({}): {} => {{\n{}\n}};\n", name, args, return_type_hint, @@ -116,7 +116,7 @@ impl Codegen { Value::Int(value) => format!("{}", value), Value::Boolean(value) => format!("{}", value), Value::String(value) => format!("\"{}\"", value), - Value::Ident(value) => format!("{}", value), + Value::Ident(value) => format!("v_{}", value), } }, diff --git a/crates/parser/src/lib.rs b/crates/parser/src/lib.rs index 15908e8..982e141 100644 --- a/crates/parser/src/lib.rs +++ b/crates/parser/src/lib.rs @@ -180,9 +180,9 @@ fn expr_parser() -> impl Parser>, Error = Simple let pipe = compare.clone() .then( just(Token::Pipe) - .then(compare) + .ignore_then(compare) .repeated()) - .foldl(|lhs, (_, rhs)| { + .foldl(|lhs, rhs| { ( Expr::Pipe { lhs: Box::new(lhs), @@ -267,15 +267,18 @@ fn expr_parser() -> impl Parser>, Error = Simple let if_block = just(Token::KwIf) .ignore_then(expr.clone()) .then_ignore(just(Token::KwThen)) + .then( do_block.clone() .or(expr.clone()) - ) + ).then_ignore(just(Token::SemiColon)) + .then_ignore(just(Token::KwElse)) .then( do_block.clone() .or(expr.clone()) - ) + ).then_ignore(just(Token::SemiColon)) + .then_ignore(just(Token::KwEnd)) .map(|((cond, then), else_)| { ( diff --git a/example/fibonacci.hz b/example/fibonacci.hz new file mode 100644 index 0000000..3db2e37 --- /dev/null +++ b/example/fibonacci.hz @@ -0,0 +1,11 @@ +fun fib (n: int): int = do + if n < 2 then + return n; + else + return fib(n - 1) + fib(n - 2); + end; +end; + +fun main: void = do + fib(5) |> @write(_); +end; \ No newline at end of file diff --git a/example/if.hz b/example/if.hz index 2f2f7cd..175442f 100644 --- a/example/if.hz +++ b/example/if.hz @@ -1,7 +1,15 @@ fun main: void = do if true then - @write("True") + @write("True"); else - @write("False") + @write("False"); + end; + + if true then + do + @write("True"); + end; + else + @write("False"); end; end; \ No newline at end of file diff --git a/example/pipe.hz b/example/pipe.hz index b373e74..95baadc 100644 --- a/example/pipe.hz +++ b/example/pipe.hz @@ -6,6 +6,8 @@ fun main: void = do |> bar(_, 1) -- '70 - 1 => 69 |> @write(_); -- '69 => stdout + @write("\n"); + foo(60) -- 60 + 1 => 61 |> bar(130, _) -- 130 - '61 => 69 |> @write(_); -- '69 => stdout