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("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::<Vec<_>>().
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),
}
},

View file

@ -180,9 +180,9 @@ fn expr_parser() -> impl Parser<Token, Vec<Spanned<Expr>>, Error = Simple<Token>
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<Token, Vec<Spanned<Expr>>, Error = Simple<Token>
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_)| {
(

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

View file

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