Skip to content

Commit

Permalink
test(ast): var and const declaration
Browse files Browse the repository at this point in the history
Co-authored-by: Samyak S Sarnayak <samyak201@gmail.com>
  • Loading branch information
ishwar00 and Samyak2 committed Oct 1, 2023
1 parent 4268258 commit 8777e73
Show file tree
Hide file tree
Showing 32 changed files with 3,811 additions and 403 deletions.
16 changes: 8 additions & 8 deletions compiler/src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,23 +546,23 @@ impl<'i> StatementList<'i> {
match pair.as_rule() {
Rule::VarDecl | Rule::ConstDecl => {
let is_var = pair.as_rule() == Rule::VarDecl;
let var_specs = pair.into_inner().filter(|p| p.as_rule() != Rule::Semicolon);
let var_const_specs = pair.into_inner().filter(|p| p.as_rule() != Rule::Semicolon);

for var_spec in var_specs {
let var_spec_span = var_spec.as_span();
for var_const_spec in var_const_specs {
let spec_span = var_const_spec.as_span();

if !matches!(var_spec.as_rule(), Rule::VarSpec | Rule::ConstSpec) {
if !matches!(var_const_spec.as_rule(), Rule::VarSpec | Rule::ConstSpec) {
return Err(BakugoParsingError::new(
var_spec.as_span(),
var_const_spec.as_span(),
format!(
"expected a var or const decl. got {:?}.",
var_spec.as_rule()
var_const_spec.as_rule()
),
BakugoParsingErrorKind::InternalError,
));
}

let mut var_spec_inner = var_spec.into_inner();
let mut var_spec_inner = var_const_spec.into_inner();
let idents = var_spec_inner.next().unwrap();

let maybe_kind = var_spec_inner.next().unwrap();
Expand All @@ -587,7 +587,7 @@ impl<'i> StatementList<'i> {

if idents.len() != exprs.len() {
return Err(BakugoParsingError::new(
var_spec_span,
spec_span,
format!(
"Number of expressions ({}) does not match number of identifers ({})",
exprs.len(),
Expand Down
20 changes: 20 additions & 0 deletions compiler/tests/examples/06_var_const_decl.bakugo
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
func abc() {
var a = 10;
var z int = 10;
var (
b = 10;
c int = 20;
c, d, e int = 30, 40, 50;
i, j, k = 50, 60, 70;
);

const a = 10;
const i int = 10;
const (
b = 10;
c int = 20;
c, d, e int = 30, 40, 50;
i, j, k = 50, 60, 70;
);

};
11 changes: 0 additions & 11 deletions compiler/tests/examples/06_var_decl.bakugo

This file was deleted.

7 changes: 7 additions & 0 deletions compiler/tests/examples/ast_error/02_var_decl.bakugo
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
func abc() {
var a = 10;
var (
c int = 20;
f, g, h = 40;
);
};
7 changes: 7 additions & 0 deletions compiler/tests/examples/ast_error/03_var_decl.bakugo
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
func abc() {
var a = 10;
var (
c int = 20;
i, j, k int = 60, 70;
);
};
7 changes: 7 additions & 0 deletions compiler/tests/examples/ast_error/04_var_decl.bakugo
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
func abc() {
var a = 10;
var (
c int = 20;
i, j, k int = 10, 20, 30, 40;
);
};
8 changes: 8 additions & 0 deletions compiler/tests/examples/ast_error/05_const_decl.bakugo
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
func abc() {
const a = 10;
const a int = 10;
const (
c int = 20;
i, j, k int = 10, 20, 30, 40;
);
};
6 changes: 6 additions & 0 deletions compiler/tests/examples/ast_error/06_const_decl.bakugo
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
func abc() {
const (
c int = 20;
i, j, k = 60, 70;
);
};
7 changes: 7 additions & 0 deletions compiler/tests/examples/ast_error/07_const_decl.bakugo
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
func abc() {
const a = 10;
const (
c int = 20;
i, j, k int = 10, 20, 30, 40;
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ SourceFile {
},
params: [],
result_kind: None,
body: [],
body: StatementList(
[],
),
span: Span {
str: "func main() {}",
start: 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ SourceFile {
},
params: [],
result_kind: None,
body: [],
body: StatementList(
[],
),
span: Span {
str: "func main() {\n\t/* */\n\n\t/**/\n\n\t/* hey there */\n\n\t/*hey there*/\n\n\t/*bakugo*/\n\n\t/*\n\n\t*/\n\n\t/*\n\t*/\n\n\t/*\n\t\tbakugo\n\t*/\n}",
start: 0,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
source: tests/parser.rs
expression: BakugoParsingErrDebug(err)
input_file: tests/examples/ast_error/02_var_decl.bakugo
---
× Parsing error
╭─[02_var_decl.bakugo:4:1]
4c int = 20;
5f, g, h = 40;
· ──────┬─────
· ╰── Syntax error: Number of expressions (1) does not match number of identifers (3)
6 │ );
╰────

Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ SourceFile {
},
],
result_kind: None,
body: [],
body: StatementList(
[],
),
span: Span {
str: "func something(a, b bunt, c uint, d (int, )) {\n}",
start: 0,
Expand Down Expand Up @@ -275,7 +277,9 @@ SourceFile {
},
},
),
body: [],
body: StatementList(
[],
),
span: Span {
str: "func something(a, b bunt, c uint, d (int, string)) (int, string) {\n}",
start: 51,
Expand All @@ -295,7 +299,9 @@ SourceFile {
},
params: [],
result_kind: None,
body: [],
body: StatementList(
[],
),
span: Span {
str: "func empty() {\n\n}",
start: 122,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
source: tests/parser.rs
expression: BakugoParsingErrDebug(err)
input_file: tests/examples/ast_error/03_var_decl.bakugo
---
× Parsing error
╭─[03_var_decl.bakugo:4:1]
4c int = 20;
5i, j, k int = 60, 70;
· ──────────┬─────────
· ╰── Syntax error: Number of expressions (2) does not match number of identifers (3)
6 │ );
╰────

Loading

0 comments on commit 8777e73

Please sign in to comment.