Skip to content

Commit

Permalink
feat(ast): type definitions
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 8777e73 commit 162c566
Show file tree
Hide file tree
Showing 8 changed files with 355 additions and 2 deletions.
17 changes: 16 additions & 1 deletion compiler/src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,22 @@ impl<'i> StatementList<'i> {
}
}

Rule::TypeDecl => {}
Rule::TypeDecl => {
let type_defs = pair
.into_inner()
.filter(|pair| pair.as_rule() != Rule::Semicolon);

for type_def in type_defs {
let mut inner = type_def.into_inner();
let identifier = inner.next().unwrap();
let kind = inner.next().unwrap();

parsed_stmts.push(Statement::Declaration(Decl::Kind(KindDecl {
name: Ident::parse(identifier)?,
kind: Kind::parse(kind)?,
})))
}
}

invalid_rule => {
return Err(BakugoParsingError::new(
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/bakugo.pest
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ ExpressionList = { Expression ~ (Comma ~ Expression)* }

// Type declarations
TypeDecl = { "type" ~ (TypeSpec | "(" ~ (TypeSpec ~ Semicolon)+ ~ ")") }
TypeSpec = { TypeDef }
TypeSpec = _{ TypeDef }

// TODO: correct TypeDef spec (remove type parameters)
// Type definitions
Expand Down
7 changes: 7 additions & 0 deletions compiler/tests/examples/07_type_def.bakugo
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
func main() {
type a int;
type (
b string;
c float;
);
};
7 changes: 7 additions & 0 deletions compiler/tests/examples/parser_error/02_type_def.bakugo
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
func main() {
type a int;
type (
b string int;
c float;
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
---
source: tests/parser.rs
expression: parsed
input_file: tests/examples/07_type_def.bakugo
---
SourceFile {
top_level: [
FnDecl(
FnDecl {
name: Ident {
value: "main",
span: Span {
str: "main",
start: 5,
end: 9,
},
},
params: [],
result_kind: None,
body: StatementList(
[
Declaration(
Kind(
KindDecl {
name: Ident {
value: "a",
span: Span {
str: "a",
start: 20,
end: 21,
},
},
kind: Simple {
name: "int",
span: Span {
str: "int",
start: 22,
end: 25,
},
},
},
),
),
Declaration(
Kind(
KindDecl {
name: Ident {
value: "b",
span: Span {
str: "b",
start: 37,
end: 38,
},
},
kind: Simple {
name: "string",
span: Span {
str: "string",
start: 39,
end: 45,
},
},
},
),
),
Declaration(
Kind(
KindDecl {
name: Ident {
value: "c",
span: Span {
str: "c",
start: 49,
end: 50,
},
},
kind: Simple {
name: "float",
span: Span {
str: "float",
start: 51,
end: 56,
},
},
},
),
),
],
),
span: Span {
str: "func main() {\n\ttype a int;\n\ttype (\n\t\tb string;\n\t\tc float;\n\t);\n}",
start: 0,
end: 63,
},
},
),
],
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
source: tests/parser.rs
expression: err.to_string()
input_file: tests/examples/parser_error/02_type_def.bakugo
---
--> 4:12
|
4 | b string int;
| ^---
|
= expected Semicolon
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
---
source: tests/parser.rs
expression: parsed
input_file: tests/examples/07_type_def.bakugo
---
pos:
- 0
- 65
pairs:
- pos:
- 0
- 65
rule: SourceFile
inner:
pos:
- 0
- 65
pairs:
- pos:
- 0
- 63
rule: TopLevelDecl
inner:
pos:
- 0
- 63
pairs:
- pos:
- 0
- 63
rule: FunctionDecl
inner:
pos:
- 5
- 61
pairs:
- pos:
- 5
- 9
rule: Ident
inner: main
- pos:
- 9
- 12
rule: Signature
inner:
pos:
- 9
- 11
pairs:
- pos:
- 9
- 11
rule: Parameters
inner: ()
- pos:
- 15
- 61
rule: StatementList
inner:
pos:
- 15
- 61
pairs:
- pos:
- 15
- 25
rule: Declaration
inner:
pos:
- 15
- 25
pairs:
- pos:
- 15
- 25
rule: TypeDecl
inner:
pos:
- 20
- 25
pairs:
- pos:
- 20
- 25
rule: TypeDef
inner:
pos:
- 20
- 25
pairs:
- pos:
- 20
- 21
rule: Ident
inner: a
- pos:
- 22
- 25
rule: Type
inner:
pos:
- 22
- 25
pairs:
- pos:
- 22
- 25
rule: Ident
inner: int
- pos:
- 25
- 26
rule: Semicolon
inner: ;
- pos:
- 28
- 60
rule: Declaration
inner:
pos:
- 28
- 60
pairs:
- pos:
- 28
- 60
rule: TypeDecl
inner:
pos:
- 37
- 57
pairs:
- pos:
- 37
- 45
rule: TypeDef
inner:
pos:
- 37
- 45
pairs:
- pos:
- 37
- 38
rule: Ident
inner: b
- pos:
- 39
- 45
rule: Type
inner:
pos:
- 39
- 45
pairs:
- pos:
- 39
- 45
rule: Ident
inner: string
- pos:
- 45
- 46
rule: Semicolon
inner: ;
- pos:
- 49
- 56
rule: TypeDef
inner:
pos:
- 49
- 56
pairs:
- pos:
- 49
- 50
rule: Ident
inner: c
- pos:
- 51
- 56
rule: Type
inner:
pos:
- 51
- 56
pairs:
- pos:
- 51
- 56
rule: Ident
inner: float
- pos:
- 56
- 57
rule: Semicolon
inner: ;
- pos:
- 60
- 61
rule: Semicolon
inner: ;
- pos:
- 63
- 64
rule: Semicolon
inner: ;
- pos:
- 65
- 65
rule: EOI
inner: ""

0 comments on commit 162c566

Please sign in to comment.