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

use const for function declaration, comma separated args

This commit is contained in:
Natapat Samutpong 2022-03-06 00:13:48 +07:00
parent 1968e7ff14
commit 40f48cbbf1
5 changed files with 14 additions and 23 deletions

View file

@ -1,4 +1,4 @@
let foo: int = 1; let foo: string = 1;
fun bar (baz: int) -> int = baz + 1; fun bar (baz: int) -> int = baz + 1;
fun qux (quux: int) -> int = do fun qux (quux: int) -> int = do
@ -6,4 +6,7 @@ fun qux (quux: int) -> int = do
bar(corge); bar(corge);
end; end;
fun add (lhs: int, rhs: int) -> int = lhs + rhs;
print(add(34, 35));
print(qux(5)); print(qux(5));

View file

@ -46,7 +46,7 @@ fn gen_ir(ir: &IR) -> String {
}; };
format!( format!(
"function {}({}) {{ {} }}", "const {} = ({}) => {{ {} }};",
name, name,
args, args,
body body

View file

@ -12,7 +12,7 @@ pub enum Token {
Delimiter(char), Delimiter(char),
Semicolon, Semicolon,
Assign, Colon, Assign, Colon,
Comma, Comma, Dot,
ReturnHint, ReturnHint,
// Keywords // Keywords
@ -46,6 +46,7 @@ pub fn lexer() -> impl Parser<char, Vec<(Token, Span)>, Error = Simple<char>> {
just(':').to(Token::Colon), just(':').to(Token::Colon),
just(',').to(Token::Comma), just(',').to(Token::Comma),
just("->").to(Token::ReturnHint), just("->").to(Token::ReturnHint),
just(".").to(Token::Dot),
)); ));
let operator = choice(( let operator = choice((
@ -152,9 +153,8 @@ fn expr_parser() -> impl Parser<Token, Expr, Error = Simple<Token>> + Clone {
let expr = recursive(|expr| { let expr = recursive(|expr| {
let args = expr.clone() let args = expr.clone()
.repeated() .separated_by(just(Token::Comma))
.or_not() .allow_trailing();
.map(|item| item.unwrap_or_else(Vec::new));
let atom = literal let atom = literal
.or(ident.map(Expr::Ident)) .or(ident.map(Expr::Ident))
@ -177,7 +177,7 @@ fn expr_parser() -> impl Parser<Token, Expr, Error = Simple<Token>> + Clone {
args, args,
} }
}); });
let unary = choice(( let unary = choice((
just(Token::Operator("-".to_string())).to("-"), just(Token::Operator("-".to_string())).to("-"),
just(Token::Operator("!".to_string())).to("!"))) just(Token::Operator("!".to_string())).to("!")))
@ -263,7 +263,8 @@ fn expr_parser() -> impl Parser<Token, Expr, Error = Simple<Token>> + Clone {
(ident (ident
.then_ignore(just(Token::Colon)) .then_ignore(just(Token::Colon))
.then(ident)) .then(ident))
.repeated() .separated_by(just(Token::Comma))
.allow_trailing()
) )
.then_ignore(just(Token::Delimiter(')'))) .then_ignore(just(Token::Delimiter(')')))
.then_ignore(just(Token::ReturnHint)) .then_ignore(just(Token::ReturnHint))

View file

@ -25,6 +25,7 @@ use middle::ir;
/// Contains code generator. /// Contains code generator.
pub mod back; pub mod back;
/// Utility functions.
pub mod util; pub mod util;
use crate::util::log; use crate::util::log;

View file

@ -1,27 +1,13 @@
use core::fmt;
use crate::front::parse::Expr; use crate::front::parse::Expr;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum TypeHint { pub enum TypeHint {
Int, Int,
Float, Double, Float,
Bool, Bool,
String, String,
} }
impl fmt::Display for TypeHint {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
TypeHint::Int => write!(f, "int"),
TypeHint::Float => write!(f, "float"),
TypeHint::Double => write!(f, "double"),
TypeHint::Bool => write!(f, "bool"),
TypeHint::String => write!(f, "char*"),
}
}
}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum Value { pub enum Value {
Int(i64), Int(i64),