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

function as argument success

This commit is contained in:
Natapat Samutpong 2022-04-09 20:52:00 +07:00
parent c095efdd7c
commit 1cbb2a6f9a
4 changed files with 74 additions and 54 deletions

View file

@ -29,7 +29,7 @@ impl Codegen {
self.emit(&self.gen_ir(&ir.kind, true)); self.emit(&self.gen_ir(&ir.kind, true));
} }
self.emit("f_main();"); self.emit("_main();");
} }
fn gen_ir(&self, ir: &IRKind, should_gen_semicolon: bool) -> String { fn gen_ir(&self, ir: &IRKind, should_gen_semicolon: bool) -> String {
@ -39,7 +39,7 @@ impl Codegen {
match ir { match ir {
IRKind::Define { public, name, type_hint, value, mutable, .. } => { IRKind::Define { public, name, type_hint, value, mutable, .. } => {
format!( format!(
"{} {} v_{}: {} = {}{}\n", "{} {} _{}: {} = {}{}\n",
if *public { "export" } else { "" }, if *public { "export" } else { "" },
if *mutable { "let" } else { "const" }, if *mutable { "let" } else { "const" },
name, name,
@ -51,7 +51,7 @@ impl Codegen {
IRKind::Call { name, args, .. } => { IRKind::Call { name, args, .. } => {
format!( format!(
"f_{}({}){}", "_{}({}){}",
name, name,
args args
.iter() .iter()
@ -82,11 +82,11 @@ impl Codegen {
IRKind::Fun { public, name, return_type_hint, args, body, .. } => { IRKind::Fun { public, name, return_type_hint, args, body, .. } => {
let args = args let args = args
.iter() .iter()
.map(|arg| format!("v_{}: {}", arg.0, arg.1)) .map(|arg| format!("_{}: {}", arg.0, arg.1))
.collect::<Vec<_>>(). .collect::<Vec<_>>().
join(", "); join(", ");
format!( format!(
"{} const f_{} = ({}): {} => {};\n", "{} const _{} = ({}): {} => {};\n",
if *public { "export" } else { "" }, if *public { "export" } else { "" },
name, name,
args, args,
@ -152,7 +152,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!("v_{}", value), Value::Ident(value) => format!("_{}", value),
} }
}, },

View file

@ -455,13 +455,23 @@ fn gen_type_hint(type_hint: &Typehint) -> String {
_ => t.to_string() _ => t.to_string()
}, },
Typehint::Tuple(ts) => { Typehint::Tuple(ts) => {
let mut types = Vec::new(); let types = ts.iter().map(|arg| gen_type_hint(&arg.0)).collect::<Vec<_>>();
for t in ts {
types.push(gen_type_hint(&t.0));
}
format!("[{}]", types.join(", ")) format!("[{}]", types.join(", "))
}, },
Typehint::Vector(t) => format!("{}[]", gen_type_hint(&t.0)), Typehint::Vector(t) => format!("{}[]", gen_type_hint(&t.0)),
Typehint::Function(_args, _ret) => {dbg!(type_hint); todo!()}, // TODO: Function type Typehint::Function(args, ret) => {
let args_ty = args.iter().map(|arg| gen_type_hint(&arg.0)).collect::<Vec<_>>();
let return_ty = gen_type_hint(&ret.0);
format!(
"({}) => {}",
args_ty
.iter()
.enumerate()
.map(|(i, arg)| format!("__{}: {}", i, arg))
.collect::<Vec<_>>()
.join(", "),
return_ty
)
},
} }
} }

View file

@ -5,6 +5,7 @@ pub mod types;
use types::{Expr, Spanned, Typehint}; use types::{Expr, Spanned, Typehint};
fn typehint_parser() -> impl Parser<Token, Spanned<Typehint>, Error = Simple<Token>> + Clone { fn typehint_parser() -> impl Parser<Token, Spanned<Typehint>, Error = Simple<Token>> + Clone {
recursive(|ty| {
let single = filter_map(|span, token| match token { let single = filter_map(|span, token| match token {
Token::Identifier(s) => Ok((Typehint::Single(s), span)), Token::Identifier(s) => Ok((Typehint::Single(s), span)),
_ => Err(Simple::expected_input_found(span, Vec::new(), Some(token))), _ => Err(Simple::expected_input_found(span, Vec::new(), Some(token))),
@ -30,7 +31,7 @@ fn typehint_parser() -> impl Parser<Token, Spanned<Typehint>, Error = Simple<Tok
(Typehint::Vector(Box::new(arg)), span) (Typehint::Vector(Box::new(arg)), span)
}); });
let function = single let function = ty
.separated_by(just(Token::Comma)) .separated_by(just(Token::Comma))
.allow_trailing() .allow_trailing()
.delimited_by( .delimited_by(
@ -48,6 +49,7 @@ fn typehint_parser() -> impl Parser<Token, Spanned<Typehint>, Error = Simple<Tok
.or(vector) .or(vector)
.or(function) .or(function)
.labelled("type hint") .labelled("type hint")
})
} }
fn expr_parser() -> impl Parser<Token, Vec<Spanned<Expr>>, Error = Simple<Token>> + Clone { fn expr_parser() -> impl Parser<Token, Vec<Spanned<Expr>>, Error = Simple<Token>> + Clone {

View file

@ -1,6 +1,14 @@
fun test (foo: int) (bar: int) : int = foo * bar fun mul (foo: int) (bar: int) : int = foo * bar
fun f (fn: |int, int| -> int) (x: int) : int = fn(x) fun map
(fn: |int, int| -> int)
(to: int)
: int
= fn(to, 10)
fun main: void = do fun main: void = do
let x : int = 20 let foo : int = 69
let bar : int = 10
map(mul, foo)
|> @write(_)
end end