forked from wavefnd/Wave
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathast.rs
More file actions
276 lines (257 loc) · 5.81 KB
/
ast.rs
File metadata and controls
276 lines (257 loc) · 5.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
use std::collections::HashMap;
#[derive(Debug, Clone)]
pub enum Value {
Int(i64),
Float(f64),
Text(String),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum WaveType {
Int(u16),
Uint(u16),
Float(u16),
Bool,
Char,
Byte,
String,
Pointer(Box<WaveType>),
Array(Box<WaveType>, u32),
Void,
Struct(String),
}
#[derive(Debug, Clone)]
pub enum ASTNode {
Function(FunctionNode),
Program(ParameterNode),
Statement(StatementNode),
Variable(VariableNode),
Expression(Expression),
Struct(StructNode),
ProtoImpl(ProtoImplNode),
}
#[derive(Debug, Clone)]
pub struct FunctionNode {
pub name: String,
pub parameters: Vec<ParameterNode>,
pub return_type: Option<WaveType>,
pub body: Vec<ASTNode>,
}
#[derive(Debug, Clone)]
pub struct StructNode {
pub name: String,
pub fields: Vec<(String, WaveType)>,
pub methods: Vec<FunctionNode>,
}
#[derive(Debug, Clone)]
pub struct ProtoImplNode {
pub target: String,
pub methods: Vec<FunctionNode>,
}
#[derive(Debug, Clone)]
pub struct FunctionSignature {
pub name: String,
pub params: Vec<(String, WaveType)>,
pub return_type: WaveType,
}
#[derive(Debug, Clone)]
pub struct ParameterNode {
pub name: String,
pub param_type: WaveType,
pub initial_value: Option<Value>,
}
#[derive(Debug, Clone)]
pub enum FormatPart {
Literal(String),
Placeholder,
}
#[derive(Debug, Clone)]
pub enum Expression {
StructLiteral {
name: String,
fields: Vec<(String, Expression)>,
},
FunctionCall {
name: String,
args: Vec<Expression>,
},
MethodCall {
object: Box<Expression>,
name: String,
args: Vec<Expression>,
},
Literal(Literal),
Variable(String),
Deref(Box<Expression>),
AddressOf(Box<Expression>),
BinaryExpression {
left: Box<Expression>,
operator: Operator,
right: Box<Expression>,
},
IndexAccess {
target: Box<Expression>,
index: Box<Expression>,
},
ArrayLiteral(Vec<Expression>),
Grouped(Box<Expression>),
AssignOperation {
target: Box<Expression>,
operator: AssignOperator,
value: Box<Expression>,
},
Assignment {
target: Box<Expression>,
value: Box<Expression>,
},
AsmBlock {
instructions: Vec<String>,
inputs: Vec<(String, Expression)>,
outputs: Vec<(String, Expression)>,
},
FieldAccess {
object: Box<Expression>,
field: String,
},
Unary {
operator: Operator,
expr: Box<Expression>,
},
}
#[derive(Debug, Clone)]
pub enum Literal {
Number(i64),
Float(f64),
String(String),
Bool(bool),
Char(char),
Byte(u8),
}
#[derive(Debug, Clone)]
pub enum Operator {
Add,
Subtract,
Multiply,
Divide,
Remainder,
GreaterEqual,
LessEqual,
Greater,
Less,
Equal,
NotEqual,
LogicalAnd,
BitwiseAnd,
LogicalOr,
BitwiseOr,
Assign,
ShiftLeft, // <<
ShiftRight, // >>
BitwiseXor,
LogicalNot,
BitwiseNot,
Not,
}
#[derive(Debug, Clone)]
pub enum AssignOperator {
Assign, // =
AddAssign, // +=
SubAssign, // -=
MulAssign, // *=
DivAssign, // /=
RemAssign, // %=
}
#[derive(Debug, Clone)]
pub enum StatementNode {
Print(String),
PrintFormat {
format: String,
args: Vec<Expression>,
},
Println(String),
PrintlnFormat {
format: String,
args: Vec<Expression>,
},
Variable(String),
If {
condition: Expression,
body: Vec<ASTNode>,
else_if_blocks: Option<Box<Vec<(Expression, Vec<ASTNode>)>>>,
else_block: Option<Box<Vec<ASTNode>>>,
},
For {
initialization: Expression,
condition: Expression,
increment: Expression,
body: Vec<ASTNode>,
},
While {
condition: Expression,
body: Vec<ASTNode>,
},
Import(String),
Assign {
variable: String,
value: Expression,
},
AsmBlock {
instructions: Vec<String>,
inputs: Vec<(String, String)>,
outputs: Vec<(String, String)>,
},
Break,
Continue,
Return(Option<Expression>),
Expression(Expression),
}
#[derive(Debug, Clone, PartialEq)]
pub enum Mutability {
Var,
Let,
LetMut,
Const,
}
#[derive(Debug, Clone)]
pub struct VariableNode {
pub name: String,
pub type_name: WaveType,
pub initial_value: Option<Expression>,
pub mutability: Mutability,
}
#[derive(Clone)]
pub struct VariableInfo {
pub name: String,
pub mutable: bool,
pub ty: WaveType,
}
impl Expression {
pub fn as_identifier(&self) -> Option<&str> {
match self {
Expression::Variable(name) => Some(name.as_str()),
Expression::AddressOf(inner) => {
if let Expression::Variable(name) = &**inner {
Some(name.as_str())
} else {
None
}
}
_ => None,
}
}
pub fn get_wave_type(&self, variables: &HashMap<String, VariableInfo>) -> WaveType {
match self {
Expression::Variable(name) => variables
.get(name)
.unwrap_or_else(|| panic!("Variable '{}' not found", name))
.ty
.clone(),
Expression::Literal(Literal::Number(_)) => WaveType::Int(32), // 기본 int
Expression::Literal(Literal::Float(_)) => WaveType::Float(32),
Expression::Literal(Literal::String(_)) => WaveType::String,
Expression::MethodCall { .. } => {
panic!("nested method call type inference not supported yet")
}
_ => panic!("get_wave_type not implemented for {:?}", self),
}
}
}