1
1
Fork 0
mirror of https://github.com/azur1s/bobbylisp.git synced 2024-10-16 02:37:40 -05:00

prefix transpiled variable & function

This commit is contained in:
Natapat Samutpong 2022-03-18 06:26:54 +07:00
parent c96ffcc4cd
commit 30331c9bf6
5 changed files with 35 additions and 11 deletions

View file

@ -23,7 +23,7 @@ impl Codegen {
self.emit(&self.gen_ir(&ir.kind, true)); 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 { fn gen_ir(&self, ir: &IRKind, should_gen_semicolon: bool) -> String {
@ -43,7 +43,7 @@ impl Codegen {
IRKind::Call { name, args } => { IRKind::Call { name, args } => {
format!( format!(
"{}({}){}", "f_{}({}){}",
name, name,
args args
.iter() .iter()
@ -67,11 +67,11 @@ impl Codegen {
IRKind::Fun { name, return_type_hint, args, body } => { IRKind::Fun { name, return_type_hint, args, body } => {
let args = args let args = args
.iter() .iter()
.map(|arg| format!("{}: {}", arg.0, arg.1)) .map(|arg| format!("v_{}: {}", arg.0, arg.1))
.collect::<Vec<_>>(). .collect::<Vec<_>>().
join(", "); join(", ");
format!( format!(
"const {} = ({}): {} => {{\n{}\n}};\n", "const f_{} = ({}): {} => {{\n{}\n}};\n",
name, name,
args, args,
return_type_hint, return_type_hint,
@ -116,7 +116,7 @@ impl Codegen {
Value::Int(value) => format!("{}", value), Value::Int(value) => format!("{}", value),
Value::Boolean(value) => format!("{}", value), Value::Boolean(value) => format!("{}", value),
Value::String(value) => format!("\"{}\"", value), Value::String(value) => format!("\"{}\"", value),
Value::Ident(value) => format!("{}", value), Value::Ident(value) => format!("v_{}", value),
} }
}, },

View file

@ -180,9 +180,9 @@ fn expr_parser() -> impl Parser<Token, Vec<Spanned<Expr>>, Error = Simple<Token>
let pipe = compare.clone() let pipe = compare.clone()
.then( .then(
just(Token::Pipe) just(Token::Pipe)
.then(compare) .ignore_then(compare)
.repeated()) .repeated())
.foldl(|lhs, (_, rhs)| { .foldl(|lhs, rhs| {
( (
Expr::Pipe { Expr::Pipe {
lhs: Box::new(lhs), lhs: Box::new(lhs),
@ -267,15 +267,18 @@ fn expr_parser() -> impl Parser<Token, Vec<Spanned<Expr>>, Error = Simple<Token>
let if_block = just(Token::KwIf) let if_block = just(Token::KwIf)
.ignore_then(expr.clone()) .ignore_then(expr.clone())
.then_ignore(just(Token::KwThen)) .then_ignore(just(Token::KwThen))
.then( .then(
do_block.clone() do_block.clone()
.or(expr.clone()) .or(expr.clone())
) ).then_ignore(just(Token::SemiColon))
.then_ignore(just(Token::KwElse)) .then_ignore(just(Token::KwElse))
.then( .then(
do_block.clone() do_block.clone()
.or(expr.clone()) .or(expr.clone())
) ).then_ignore(just(Token::SemiColon))
.then_ignore(just(Token::KwEnd)) .then_ignore(just(Token::KwEnd))
.map(|((cond, then), else_)| { .map(|((cond, then), else_)| {
( (

11
example/fibonacci.hz Normal file
View file

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

View file

@ -1,7 +1,15 @@
fun main: void = do fun main: void = do
if true then if true then
@write("True") @write("True");
else else
@write("False") @write("False");
end;
if true then
do
@write("True");
end;
else
@write("False");
end; end;
end; end;

View file

@ -6,6 +6,8 @@ fun main: void = do
|> bar(_, 1) -- '70 - 1 => 69 |> bar(_, 1) -- '70 - 1 => 69
|> @write(_); -- '69 => stdout |> @write(_); -- '69 => stdout
@write("\n");
foo(60) -- 60 + 1 => 61 foo(60) -- 60 + 1 => 61
|> bar(130, _) -- 130 - '61 => 69 |> bar(130, _) -- 130 - '61 => 69
|> @write(_); -- '69 => stdout |> @write(_); -- '69 => stdout