Add function call support

main
trivernis 4 months ago
parent b5fa1c0efe
commit 899c1cebd5
Signed by: Trivernis
GPG Key ID: 7E6D18B61C8D2F4B

@ -3,8 +3,11 @@ file = { "hello world" }
statement = { decl }
decl = { "let" ~ #name = ident ~ #args = (ident)* ~ "=" ~ expr }
expr = { infix_expr | term }
expr = { infix_expr | call_expr | term }
term = _{ ident | literal | "(" ~ expr ~ ")" }
call_expr = {
#name = ident ~ #args = (term)+
}
infix_expr = {
#lhs = term ~ operator ~ #rhs = expr
}

@ -81,3 +81,36 @@ fn it_parses_multiple_operations() {
])
]);
}
#[test]
fn it_parses_calls() {
parses_to!(parser: SnekParser, input: "print a", rule: Rule::expr, tokens: [
expr(0, 7, [
call_expr(0, 7, [
ident(0, 5),
ident(6, 7),
])
])
]);
parses_to!(parser: SnekParser, input: "print (1 + 1) a", rule: Rule::expr, tokens: [
expr(0, 15, [
call_expr(0, 15, [
ident(0, 5),
expr(7, 12, [
infix_expr(7, 12, [
literal(7, 8, [
integer(7, 8)
]),
operator(9, 10),
expr(11, 12, [
literal(11, 12, [
integer(11, 12)
]),
])
])
]),
ident(14, 15)
])
])
]);
}

@ -1,4 +1,4 @@
mod infix_expressions;
mod expressions;
mod literals;
mod statements;

@ -3,7 +3,7 @@ use pest::{consumes_to, parses_to};
use crate::{Rule, SnekParser};
#[test]
fn it_parses_assignments() {
fn it_parses_plain_assignments() {
parses_to!(parser: SnekParser, input: "let a = 1", rule: Rule::statement, tokens: [
statement(0, 9, [
decl(0, 9, [
@ -17,3 +17,26 @@ fn it_parses_assignments() {
])
]);
}
#[test]
fn it_parses_assignments_with_args() {
parses_to!(parser: SnekParser, input: "let add a = a + 1", rule: Rule::statement, tokens: [
statement(0, 17, [
decl(0, 17, [
ident(4, 7),
ident(8, 9),
expr(12, 17, [
infix_expr(12, 17, [
ident(12, 13),
operator(14, 15),
expr(16, 17, [
literal(16, 17, [
integer(16, 17)
])
])
])
])
])
])
]);
}

Loading…
Cancel
Save