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:
parent
c095efdd7c
commit
1cbb2a6f9a
|
@ -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),
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
)
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,49 +5,51 @@ 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 {
|
||||||
let single = filter_map(|span, token| match token {
|
recursive(|ty| {
|
||||||
Token::Identifier(s) => Ok((Typehint::Single(s), span)),
|
let single = filter_map(|span, token| match token {
|
||||||
_ => Err(Simple::expected_input_found(span, Vec::new(), Some(token))),
|
Token::Identifier(s) => Ok((Typehint::Single(s), span)),
|
||||||
});
|
_ => Err(Simple::expected_input_found(span, Vec::new(), Some(token))),
|
||||||
|
|
||||||
let tuple = single
|
|
||||||
.separated_by(just(Token::Comma))
|
|
||||||
.allow_trailing()
|
|
||||||
.delimited_by(
|
|
||||||
just(Token::OpenParen),
|
|
||||||
just(Token::CloseParen),
|
|
||||||
)
|
|
||||||
.map_with_span(|args, span| {
|
|
||||||
(Typehint::Tuple(args), span)
|
|
||||||
});
|
});
|
||||||
|
|
||||||
let vector = single
|
let tuple = single
|
||||||
.delimited_by(
|
.separated_by(just(Token::Comma))
|
||||||
just(Token::OpenBracket),
|
.allow_trailing()
|
||||||
just(Token::CloseBracket),
|
.delimited_by(
|
||||||
)
|
just(Token::OpenParen),
|
||||||
.map_with_span(|arg, span| {
|
just(Token::CloseParen),
|
||||||
(Typehint::Vector(Box::new(arg)), span)
|
)
|
||||||
});
|
.map_with_span(|args, span| {
|
||||||
|
(Typehint::Tuple(args), span)
|
||||||
|
});
|
||||||
|
|
||||||
let function = single
|
let vector = single
|
||||||
.separated_by(just(Token::Comma))
|
.delimited_by(
|
||||||
.allow_trailing()
|
just(Token::OpenBracket),
|
||||||
.delimited_by(
|
just(Token::CloseBracket),
|
||||||
just(Token::Pipe),
|
)
|
||||||
just(Token::Pipe),
|
.map_with_span(|arg, span| {
|
||||||
)
|
(Typehint::Vector(Box::new(arg)), span)
|
||||||
.then_ignore(just(Token::Arrow))
|
});
|
||||||
.then(single)
|
|
||||||
.map_with_span(|(args, ret), span| {
|
|
||||||
(Typehint::Function(args, Box::new(ret)), span)
|
|
||||||
});
|
|
||||||
|
|
||||||
single
|
let function = ty
|
||||||
.or(tuple)
|
.separated_by(just(Token::Comma))
|
||||||
.or(vector)
|
.allow_trailing()
|
||||||
.or(function)
|
.delimited_by(
|
||||||
.labelled("type hint")
|
just(Token::Pipe),
|
||||||
|
just(Token::Pipe),
|
||||||
|
)
|
||||||
|
.then_ignore(just(Token::Arrow))
|
||||||
|
.then(single)
|
||||||
|
.map_with_span(|(args, ret), span| {
|
||||||
|
(Typehint::Function(args, Box::new(ret)), span)
|
||||||
|
});
|
||||||
|
|
||||||
|
single
|
||||||
|
.or(tuple)
|
||||||
|
.or(vector)
|
||||||
|
.or(function)
|
||||||
|
.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 {
|
||||||
|
|
|
@ -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
|
Loading…
Reference in a new issue