Skip to content

Commit 8113def

Browse files
committed
refactor: move out ast nodes & operators into ast module
1 parent e133465 commit 8113def

File tree

16 files changed

+327
-445
lines changed

16 files changed

+327
-445
lines changed

src/parser/op.rs renamed to src/ast/mod.rs

Lines changed: 167 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,172 @@
1-
use crate::lexer::TokenKind;
1+
use crate::lexer::{span::Span, Token, TokenKind};
2+
use derive_more::derive::Display;
23
use thiserror::Error;
34

5+
#[derive(Debug, Clone, PartialEq)]
6+
pub struct Variable {
7+
pub ty: Ty,
8+
pub name: String,
9+
pub value: Option<Expr>,
10+
}
11+
12+
#[derive(Debug, Clone, PartialEq)]
13+
pub struct Block {
14+
pub open_brace: Span,
15+
pub stmts: Vec<Stmt>,
16+
pub close_brace: Span,
17+
}
18+
19+
#[derive(Debug, Clone, PartialEq)]
20+
pub enum Item {
21+
Global(Variable),
22+
Fn {
23+
ret_ty: Ty,
24+
name: String,
25+
params: Vec<(String, Ty)>,
26+
block: Option<Block>,
27+
},
28+
Struct {
29+
name: String,
30+
fields: Vec<(String, Ty)>,
31+
},
32+
}
33+
34+
#[derive(Debug, Clone, PartialEq)]
35+
pub enum Stmt {
36+
Local(Variable),
37+
Item(Item),
38+
Expr(Expr),
39+
Return(Option<Expr>),
40+
If {
41+
condition: Expr,
42+
consequence: Block,
43+
alternative: Option<Block>,
44+
},
45+
While {
46+
condition: Expr,
47+
block: Block,
48+
},
49+
For {
50+
initializer: Option<Box<Stmt>>,
51+
condition: Option<Expr>,
52+
increment: Option<Expr>,
53+
block: Block,
54+
},
55+
Continue,
56+
Break,
57+
}
58+
59+
#[derive(Debug, Clone, PartialEq)]
60+
pub enum Expr {
61+
Binary {
62+
op: BinOp,
63+
left: Box<Expr>,
64+
right: Box<Expr>,
65+
},
66+
Unary {
67+
op: UnOp,
68+
expr: Box<Expr>,
69+
},
70+
Cast {
71+
expr: Box<Expr>,
72+
ty: Ty,
73+
},
74+
Lit(ExprLit),
75+
Ident(String),
76+
Struct {
77+
name: String,
78+
fields: Vec<(String, Expr)>,
79+
},
80+
Array(Vec<Expr>),
81+
Field {
82+
expr: Box<Expr>,
83+
field: String,
84+
},
85+
StructMethod {
86+
expr: Box<Expr>,
87+
method: String,
88+
arguments: Vec<Expr>,
89+
},
90+
ArrayAccess {
91+
expr: Box<Expr>,
92+
index: Box<Expr>,
93+
},
94+
FunctionCall {
95+
expr: Box<Expr>,
96+
arguments: Vec<Expr>,
97+
},
98+
MacroCall {
99+
name: String,
100+
tokens: Vec<Token>,
101+
},
102+
}
103+
104+
#[derive(Debug, Clone, PartialEq)]
105+
pub enum ExprLit {
106+
Int(i64),
107+
UInt(u64),
108+
Bool(bool),
109+
String(String),
110+
Null,
111+
}
112+
113+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Hash, Display)]
114+
pub enum IntTy {
115+
#[display("i8")]
116+
I8,
117+
#[display("i16")]
118+
I16,
119+
#[display("i32")]
120+
I32,
121+
#[display("i64")]
122+
I64,
123+
#[display("isize")]
124+
Isize,
125+
}
126+
127+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Hash, Display)]
128+
pub enum UintTy {
129+
#[display("u8")]
130+
U8,
131+
#[display("u16")]
132+
U16,
133+
#[display("u32")]
134+
U32,
135+
#[display("u64")]
136+
U64,
137+
#[display("usize")]
138+
Usize,
139+
}
140+
141+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Hash, Display)]
142+
pub enum Ty {
143+
#[display("null")]
144+
Null,
145+
#[display("void")]
146+
Void,
147+
#[display("bool")]
148+
Bool,
149+
Int(IntTy),
150+
UInt(UintTy),
151+
Ident(String),
152+
#[display("*{_0}")]
153+
Ptr(Box<Ty>),
154+
#[display("{ty}[{len}]")]
155+
Array {
156+
ty: Box<Ty>,
157+
len: usize,
158+
},
159+
#[display("fn ({}) -> {_1}",
160+
_0
161+
.iter()
162+
.map(|type_| type_.to_string())
163+
.collect::<String>()
164+
)]
165+
Fn(Vec<Ty>, Box<Ty>),
166+
#[display("infer")]
167+
Infer,
168+
}
169+
4170
#[derive(Error, Debug)]
5171
pub enum OpParseError {
6172
#[error("Failed to parse binary operator from {0}")]

src/codegen/amd64_asm/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ mod register;
44

55
use super::Codegen;
66
use crate::{
7+
ast::{BinOp, BitwiseOp, CmpOp, IntTy, OpParseError, UintTy, UnOp},
78
ir::{Block, Expr, ExprKind, ExprLit, Id, Item, ItemFn, Node, Stmt, Ty, Variable},
8-
parser::{BinOp, BitwiseOp, CmpOp, IntTy, OpParseError, UintTy, UnOp},
99
Context,
1010
};
1111
use allocator::RegisterAllocator;

src/codegen/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
pub mod amd64_asm;
22

3-
use crate::{parser::OpParseError, Context};
3+
use crate::{ast::OpParseError, Context};
44
use thiserror::Error;
55

66
#[derive(Error, Debug)]

src/diagnostics.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ pub enum Diagnostic {
1919
IntegerLitralTooLong,
2020
#[display("function definition is not allowed here")]
2121
IllegalFunctionDefinition,
22+
#[display("type mismatch: `{_0}` expected, `{_1}` found")]
23+
TypeMismatch(String, String),
24+
#[display("type hint required")]
25+
TypeHintRequired,
26+
#[display("{_0} is not a pointer type")]
27+
NotAPointer(String),
28+
#[display("cannot cast `{_0}` into `{_1}`")]
29+
InvalidCast(String, String),
2230
}
2331

2432
#[derive(Debug, Eq, PartialEq, Display)]

src/ir/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
mod ordered_map;
22
mod types;
33

4-
use crate::parser::{BinOp, UnOp};
4+
use crate::ast::{BinOp, UnOp};
55
use bumpalo::Bump;
66

77
pub use ordered_map::OrderedMap;

src/ir/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{
2-
parser::{IntTy, UintTy},
2+
ast::{IntTy, UintTy},
33
ty_problem,
44
};
55

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
pub mod ast;
12
pub mod codegen;
23
pub mod compile;
34
pub mod diagnostics;

0 commit comments

Comments
 (0)