From 9d243e33b60371f75c2fac628b457fca96c74b8a Mon Sep 17 00:00:00 2001 From: MCorange Date: Sat, 21 Dec 2024 23:09:10 +0200 Subject: [PATCH] Setup parser testing with various tests --- tests/parser/enumerations.exp | 29 ++++- tests/parser/enumerations.mcl | 7 ++ tests/parser/expressions.exp | 53 +++++++++- tests/parser/expressions.mcl | 8 ++ tests/parser/functions.exp | 187 ++++++++++++++++++++++++++++++++- tests/parser/functions.mcl | 5 + tests/parser/if-statements.exp | 34 +++++- tests/parser/if-statements.mcl | 18 ++++ tests/parser/loops.exp | 106 ++++++++++++++++++- tests/parser/loops.mcl | 3 + tests/parser/structs.exp | 33 +++++- tests/parser/structs.mcl | 6 ++ 12 files changed, 483 insertions(+), 6 deletions(-) diff --git a/tests/parser/enumerations.exp b/tests/parser/enumerations.exp index fe2419b..1b216d3 100644 --- a/tests/parser/enumerations.exp +++ b/tests/parser/enumerations.exp @@ -1,6 +1,33 @@ Program { ast: Block( - [], + [ + Statement( + Enum { + name: Ident( + "Foo", + ), + fields: [], + }, + ), + Statement( + Enum { + name: Ident( + "Bar", + ), + fields: [ + Ident( + "A", + ), + Ident( + "B", + ), + Ident( + "C", + ), + ], + }, + ), + ], ), structs: {}, enums: {}, diff --git a/tests/parser/enumerations.mcl b/tests/parser/enumerations.mcl index e69de29..6cb2543 100644 --- a/tests/parser/enumerations.mcl +++ b/tests/parser/enumerations.mcl @@ -0,0 +1,7 @@ +enum Foo {} + +enum Bar { + A, + B, + C, +} diff --git a/tests/parser/expressions.exp b/tests/parser/expressions.exp index fe2419b..0fb834a 100644 --- a/tests/parser/expressions.exp +++ b/tests/parser/expressions.exp @@ -1,6 +1,57 @@ Program { ast: Block( - [], + [ + Expr( + BinOp { + typ: Eq, + left: UnOp { + typ: Star, + right: Path( + Path( + [ + Ident( + "a", + ), + ], + ), + ), + }, + right: BinOp { + typ: EqEq, + left: BinOp { + typ: Star, + left: Literal( + Number( + Number { + val: 1, + base: 10, + signed: false, + }, + ), + ), + right: Literal( + Number( + Number { + val: 3, + base: 10, + signed: false, + }, + ), + ), + }, + right: Literal( + Number( + Number { + val: 4, + base: 10, + signed: false, + }, + ), + ), + }, + }, + ), + ], ), structs: {}, enums: {}, diff --git a/tests/parser/expressions.mcl b/tests/parser/expressions.mcl index e69de29..42357fd 100644 --- a/tests/parser/expressions.mcl +++ b/tests/parser/expressions.mcl @@ -0,0 +1,8 @@ +*a = 1 * 3 == 4; +b.c = 3/4 == *a; +c->d = (a->b.c->d) / 2; +*d->e.f = 2 / a->b.c->d; +e = a->b.c->d / 2; +f = a.b.c.d / 2; +g = a.b[*a.c] * 5; + diff --git a/tests/parser/functions.exp b/tests/parser/functions.exp index fe2419b..adf58c4 100644 --- a/tests/parser/functions.exp +++ b/tests/parser/functions.exp @@ -1,6 +1,191 @@ Program { ast: Block( - [], + [ + Statement( + Fn { + struct_name: None, + name: Ident( + "main", + ), + params: [ + ( + Ident( + "argc", + ), + Owned( + Ident( + "i32", + ), + ), + ), + ( + Ident( + "argv", + ), + Ref { + inner: Array { + inner: Owned( + Ident( + "Str", + ), + ), + }, + mutable: false, + }, + ), + ], + ret_type: Some( + Owned( + Ident( + "i32", + ), + ), + ), + qual_const: false, + qual_extern: None, + body: Some( + Block( + [ + Expr( + Return( + Some( + Literal( + Number( + Number { + val: 0, + base: 10, + signed: false, + }, + ), + ), + ), + ), + ), + ], + ), + ), + }, + ), + Statement( + Fn { + struct_name: Some( + Ident( + "Baz", + ), + ), + name: Ident( + "main", + ), + params: [ + ( + Ident( + "self", + ), + Ref { + inner: Owned( + Ident( + "Baz", + ), + ), + mutable: true, + }, + ), + ( + Ident( + "a", + ), + Ref { + inner: Owned( + Ident( + "Foo", + ), + ), + mutable: false, + }, + ), + ( + Ident( + "b", + ), + Ref { + inner: Owned( + Ident( + "Bar", + ), + ), + mutable: true, + }, + ), + ], + ret_type: Some( + Ref { + inner: Owned( + Ident( + "Nya", + ), + ), + mutable: false, + }, + ), + qual_const: false, + qual_extern: None, + body: None, + }, + ), + Statement( + Fn { + struct_name: Some( + Ident( + "Baz", + ), + ), + name: Ident( + "main", + ), + params: [ + ( + Ident( + "a", + ), + Ref { + inner: Owned( + Ident( + "Foo", + ), + ), + mutable: false, + }, + ), + ( + Ident( + "b", + ), + Ref { + inner: Owned( + Ident( + "Bar", + ), + ), + mutable: true, + }, + ), + ], + ret_type: Some( + Ref { + inner: Owned( + Ident( + "Nya", + ), + ), + mutable: false, + }, + ), + qual_const: false, + qual_extern: None, + body: None, + }, + ), + ], ), structs: {}, enums: {}, diff --git a/tests/parser/functions.mcl b/tests/parser/functions.mcl index e69de29..05e2774 100644 --- a/tests/parser/functions.mcl +++ b/tests/parser/functions.mcl @@ -0,0 +1,5 @@ +fn main(argc: i32, argv: &[Str]) -> i32 { + return 0; +} +fn Baz.main(self: &mut Baz, a: &Foo, b: &mut Bar) -> &Nya; +fn Baz.main(a: &Foo, b: &mut Bar) -> &Nya; diff --git a/tests/parser/if-statements.exp b/tests/parser/if-statements.exp index fe2419b..135e680 100644 --- a/tests/parser/if-statements.exp +++ b/tests/parser/if-statements.exp @@ -1,6 +1,38 @@ Program { ast: Block( - [], + [ + Expr( + If( + IfExpr { + test: BinOp { + typ: Gt, + left: Path( + Path( + [ + Ident( + "i", + ), + ], + ), + ), + right: Literal( + Number( + Number { + val: 3, + base: 10, + signed: false, + }, + ), + ), + }, + body: Block( + [], + ), + else_if: None, + }, + ), + ), + ], ), structs: {}, enums: {}, diff --git a/tests/parser/if-statements.mcl b/tests/parser/if-statements.mcl index e69de29..887b06c 100644 --- a/tests/parser/if-statements.mcl +++ b/tests/parser/if-statements.mcl @@ -0,0 +1,18 @@ + +if i > 3 { + +} + +if 1 { + +} else { + +} + +if 1 { + +} else if a > 3 { + +} else { + +} diff --git a/tests/parser/loops.exp b/tests/parser/loops.exp index fe2419b..8825352 100644 --- a/tests/parser/loops.exp +++ b/tests/parser/loops.exp @@ -1,6 +1,110 @@ Program { ast: Block( - [], + [ + Expr( + ForLoop { + init: Statement( + Let { + name: Ident( + "i", + ), + typ: None, + val: Some( + Literal( + Number( + Number { + val: 0, + base: 10, + signed: false, + }, + ), + ), + ), + }, + ), + test: BinOp { + typ: Lt, + left: Path( + Path( + [ + Ident( + "i", + ), + ], + ), + ), + right: Literal( + Number( + Number { + val: 10, + base: 10, + signed: false, + }, + ), + ), + }, + on_loop: BinOp { + typ: AddEq, + left: Path( + Path( + [ + Ident( + "i", + ), + ], + ), + ), + right: Literal( + Number( + Number { + val: 1, + base: 10, + signed: false, + }, + ), + ), + }, + body: Block( + [], + ), + }, + ), + Expr( + WhileLoop { + test: BinOp { + typ: Gt, + left: Path( + Path( + [ + Ident( + "i", + ), + ], + ), + ), + right: Literal( + Number( + Number { + val: 3, + base: 10, + signed: false, + }, + ), + ), + }, + body: Block( + [], + ), + }, + ), + Expr( + InfLoop { + body: Block( + [], + ), + }, + ), + ], ), structs: {}, enums: {}, diff --git a/tests/parser/loops.mcl b/tests/parser/loops.mcl index e69de29..63dc8d0 100644 --- a/tests/parser/loops.mcl +++ b/tests/parser/loops.mcl @@ -0,0 +1,3 @@ +for let i = 0; i < 10; i += 1 {} +while i > 3 {} +loop {} diff --git a/tests/parser/structs.exp b/tests/parser/structs.exp index fe2419b..21c28f6 100644 --- a/tests/parser/structs.exp +++ b/tests/parser/structs.exp @@ -1,6 +1,37 @@ Program { ast: Block( - [], + [ + Statement( + Struct { + name: Ident( + "foo_t", + ), + fields: [], + }, + ), + Statement( + Struct { + name: Ident( + "bar_t", + ), + fields: [ + ( + Ident( + "a", + ), + Ref { + inner: Owned( + Ident( + "bar_t", + ), + ), + mutable: false, + }, + ), + ], + }, + ), + ], ), structs: {}, enums: {}, diff --git a/tests/parser/structs.mcl b/tests/parser/structs.mcl index e69de29..cbdde0f 100644 --- a/tests/parser/structs.mcl +++ b/tests/parser/structs.mcl @@ -0,0 +1,6 @@ + +struct foo_t {} + +struct bar_t { + a: &bar_t +}