This commit is contained in:
me 2025-12-16 15:23:19 +02:00
parent f5a41803e9
commit fb37bae29f
5 changed files with 92 additions and 3 deletions

View file

@ -14,6 +14,10 @@ pub fn parse<T>(tokens: &mut Tokens, parser: Parser<T>) -> Result<T, Error> {
}
}
fn parse_definitions(tokens: &mut Tokens) -> ParseResult<Vec<ast::Definition>> {
tokens.many(parse_definition)
}
fn parse_definition(tokens: &mut Tokens) -> ParseResult<ast::Definition> {
if let Some(_) = tokens.next_if(&Token::Let) {
if let Some(name) = parse_identifier_name(tokens)? {
@ -51,7 +55,20 @@ fn parse_definition(tokens: &mut Tokens) -> ParseResult<ast::Definition> {
}
fn parse_expr(tokens: &mut Tokens) -> ParseResult<ast::Expr> {
parse_simple_expr(tokens)
if let Some(expr1) = parse_simple_expr(tokens)? {
if let Some(args) = tokens.between(&Token::OpenParen, &Token::CloseParen, |ts| {
ts.many_sep_by(&Token::Comma, parse_expr)
})? {
Ok(Some(ast::Expr::FunCall {
func: Box::new(expr1),
args: args,
}))
} else {
Ok(Some(expr1))
}
} else {
Ok(None)
}
}
fn parse_simple_expr(tokens: &mut Tokens) -> ParseResult<ast::Expr> {
@ -178,12 +195,24 @@ mod tests {
insta::assert_debug_snapshot!(result);
}
#[test]
fn paren_fn() {
fn fn_def() {
let program = "fn(a1, boco, c_c) { 108 }".to_string();
let result = parse(&mut scan(program), parse_expr);
insta::assert_debug_snapshot!(result);
}
#[test]
fn fncall() {
let program = "call(arg1, arg2)".to_string();
let result = parse(&mut scan(program), parse_expr);
insta::assert_debug_snapshot!(result);
}
#[test]
fn paren_fncall() {
let program = "(fn(a1, boco, c_c) { 108 })()".to_string();
let result = parse(&mut scan(program), parse_expr);
insta::assert_debug_snapshot!(result);
}
#[test]
fn let_number() {
let program = "let x = 108;".to_string();
let result = parse(&mut scan(program), parse_definition);

View file

@ -0,0 +1,25 @@
---
source: src/parser/parser.rs
expression: result
---
Ok(
FunCall {
func: Var(
Name(
"call",
),
),
args: [
Var(
Name(
"arg1",
),
),
Var(
Name(
"arg2",
),
),
],
},
)

View file

@ -0,0 +1,35 @@
---
source: src/parser/parser.rs
expression: result
---
Ok(
FunCall {
func: Func(
Fn {
args: [
Arg {
name: Name(
"a1",
),
},
Arg {
name: Name(
"boco",
),
},
Arg {
name: Name(
"c_c",
),
},
],
body: Value(
Int(
108,
),
),
},
),
args: [],
},
)

View file

@ -120,7 +120,7 @@ impl Tokens {
}
Ok(Some(results))
} else {
Ok(None)
Ok(Some(vec![]))
}
}
pub fn many<T>(&mut self, parser: Parser<T>) -> ParseResult<Vec<T>> {