-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsic_node.py
More file actions
435 lines (352 loc) · 11.8 KB
/
sic_node.py
File metadata and controls
435 lines (352 loc) · 11.8 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
from __future__ import annotations
import enum
import sic_token as tk
import typing
class NoneNode:
def __init__(self):
pass
def to_dict(self):
return {}
class CPrimaryType(enum.Enum):
VOID = enum.auto() # 'void'
CHAR = enum.auto() # 'char' or 'signed char'
# UCHAR = enum.auto() # 'unsigned char'
SHORT = enum.auto() # 'short' or 'short int' or 'signed short' or 'signed short int'
# USHORT = enum.auto() # 'unsigned short'
INT = enum.auto() # 'int' or 'signed' or 'signed int'
# UINT = enum.auto() # 'unsigned' or 'unsigned int'
LONG = enum.auto() # 'long' or 'long int' or 'signed long' or 'signed long int'
# ULONG = enum.auto() # 'unsigned long'
FLOAT = enum.auto() # 'float'
DOUBLE = enum.auto() # 'double'
def to_dict(self):
return {
"node": "CPrimaryType",
"value": self.name
}
class CTypeSpecifier(enum.IntFlag):
VOID = 1 << 0
CHAR = 1 << 2
SHORT = 1 << 4
INT = 1 << 6
LONG = 1 << 8
FLOAT = 1 << 10
DOUBLE = 1 << 12
SIGNED = 1 << 14
UNSIGNED = 1 << 16
c_type_specifier_counter_to_c_primary_type: dict[CTypeSpecifier, CPrimaryType] = {
CTypeSpecifier.VOID: CPrimaryType.VOID,
CTypeSpecifier.CHAR: CPrimaryType.CHAR,
CTypeSpecifier.CHAR + CTypeSpecifier.SIGNED: CPrimaryType.CHAR,
# CTypeSpecifier.CHAR + CTypeSpecifier.UNSIGNED: CPrimaryType.UCHAR,
CTypeSpecifier.SHORT: CPrimaryType.SHORT,
CTypeSpecifier.SHORT + CTypeSpecifier.INT: CPrimaryType.SHORT,
CTypeSpecifier.SIGNED + CTypeSpecifier.SHORT: CPrimaryType.SHORT,
CTypeSpecifier.SHORT + CTypeSpecifier.INT + CTypeSpecifier.SIGNED: CPrimaryType.SHORT,
# CTypeSpecifier.UNSIGNED + CTypeSpecifier.SHORT: CPrimaryType.USHORT,
CTypeSpecifier.INT: CPrimaryType.INT,
CTypeSpecifier.SIGNED: CPrimaryType.INT,
CTypeSpecifier.SIGNED + CTypeSpecifier.INT: CPrimaryType.INT,
# CTypeSpecifier.UNSIGNED: CPrimaryType.UINT,
# CTypeSpecifier.UNSIGNED + CTypeSpecifier.INT: CPrimaryType.UINT,
CTypeSpecifier.LONG: CPrimaryType.LONG,
CTypeSpecifier.LONG + CTypeSpecifier.INT: CPrimaryType.LONG,
CTypeSpecifier.SIGNED + CTypeSpecifier.LONG: CPrimaryType.LONG,
CTypeSpecifier.SIGNED + CTypeSpecifier.LONG + CTypeSpecifier.INT: CPrimaryType.LONG,
# CTypeSpecifier.UNSIGNED + CTypeSpecifier.LONG: CPrimaryType.ULONG,
CTypeSpecifier.FLOAT: CPrimaryType.FLOAT,
CTypeSpecifier.DOUBLE: CPrimaryType.DOUBLE,
}
class Identifier:
def __init__(self, token: tk.Token):
self.token = token
def to_dict(self):
return {
"node": "Identifier",
"name": self.token.string
}
class CharLiteral:
def __init__(self, token: tk.Token):
self.token = token
def to_dict(self):
return {
"node": "CharLiteral",
"value": self.token.string
}
class ConstantLiteral:
def __init__(self, token: tk.Token):
self.token = token
def to_dict(self):
return {
"node": "ConstantLiteral",
"value": self.token.string
}
class CUnaryOp:
def __init__(self, kind: CUnaryOpKind, expression: Node):
self.kind = kind
self.expression = expression
def to_dict(self):
return {
"node": "CUnaryOp",
"kind": self.kind.to_dict(),
"expression": self.expression.to_dict()
}
class CUnaryOpKind(enum.Enum):
""""""
# https://www.scaler.com/topics/pre-increment-and-post-increment-in-c/
# increase/increase then return
PreIncrease = enum.auto()
PreDecrease = enum.auto()
# return then increase/increase
PostIncrease = enum.auto()
PostDecrease = enum.auto()
Plus = enum.auto() # '+'
Minus = enum.auto() # '-'
BitwiseNOT = enum.auto() # '~'
LogicalNOT = enum.auto() # '!'
Sizeof = enum.auto() # 'sizeof'
def to_dict(self):
return {
"node": "CUnaryOpKind",
"value": self.name
}
class CCast:
def __init__(self, type_name: TypeName, expression: Node):
self.type_name = type_name
self.expression = expression
def to_dict(self):
return {
"node": "CCast",
"type_name": self.type_name.to_dict(),
"expression": self.expression.to_dict()
}
class CBinaryOpKind(enum.Enum):
Addition = enum.auto()
Subtraction = enum.auto()
Multiplication = enum.auto()
Division = enum.auto()
Modulus = enum.auto()
Assignment = enum.auto()
EqualTo = enum.auto()
NotEqualTo = enum.auto()
GreaterThan = enum.auto()
LessThan = enum.auto()
GreaterThanOrEqualTo = enum.auto()
LessThanOrEqualTo = enum.auto()
BitwiseAND = enum.auto()
BitwiseOR = enum.auto()
BitwiseXOR = enum.auto()
LeftShift = enum.auto()
RightShift = enum.auto()
LogicalAND = enum.auto()
LogicalOR = enum.auto()
MultiplicationAssignment = enum.auto()
DivisionAssignment = enum.auto()
ModulusAssignment = enum.auto()
AdditionAssignment = enum.auto()
SubtractionAssignment = enum.auto()
LeftShiftAssignment = enum.auto()
RightShiftAssignment = enum.auto()
BitwiseAndAssignment = enum.auto()
BitwiseXorAssignment = enum.auto()
BitwiseOrAssignment = enum.auto()
def to_dict(self):
return {
"node": "CBinaryOpKind",
"value": self.name
}
class CBinaryOp:
def __init__(self, kind: CBinaryOpKind, left: Node, right: Node):
self.kind: CBinaryOpKind = kind
self.left: Node = left
self.right: Node = right
def to_dict(self):
return {
"node": "CBinaryOp",
"kind": self.kind.to_dict(),
"left": self.left.to_dict(),
"right": self.right.to_dict()
}
class CTernaryOp:
def __init__(self, condition: Node, true_value: Node, false_value: Node):
self.condition: Node = condition
self.true_value: Node = true_value
self.false_value: Node = false_value
def to_dict(self):
return {
"node": "CTernaryOp",
"condition": self.condition.to_dict(),
"true_value": self.true_value.to_dict(),
"false_value": self.false_value.to_dict()
}
class Declaration:
def __init__(self, type_name: TypeName, declarator: Declarator):
self.type_name = type_name
self.declarator = declarator
@property
def identifier(self) -> Identifier:
return self.declarator[0]
@property
def initializer(self) -> Node:
return self.declarator[1]
def to_dict(self):
return {
"node": "Declaration",
"type_name": self.type_name.to_dict(),
"declarators": [self.declarator[0].to_dict(), self.declarator[1].to_dict()]
}
class Continue:
def __init__(self, token: tk.Token):
self.token = token
def to_dict(self):
return {
"node": "Continue",
"token": self.token.string,
}
class Break:
def __init__(self, token: tk.Token):
self.token = token
def to_dict(self):
return {
"node": "Break",
"token": self.token.string,
}
class Return:
def __init__(self, expression: Node, token: tk.Token):
self.expression = expression
self.token = token
def to_dict(self):
return {
"node": "Return",
"expression": self.expression.to_dict(),
"token": self.token.string,
}
class While:
def __init__(self, condition: Node, body: Node):
self.condition = condition
self.body = body
def to_dict(self):
return {
"node": "While",
"condition": self.condition.to_dict(),
"body": self.body.to_dict()
}
class For:
def __init__(self, init: Node, condition: Node, update: Node, body: Node):
self.init = init
self.condition = condition
self.update = update
self.body = body
def to_dict(self):
return {
"node": "For",
"init": self.init.to_dict(),
"condition": self.condition.to_dict(),
"update": self.update.to_dict(),
"body": self.body.to_dict()
}
class CompoundStatement:
def __init__(self, declarations: list[Declaration], statements: list[Node]):
self.declarations = declarations
self.statements = statements
def to_dict(self):
return {
"node": "CompoundStatement",
"declarations": [declaration.to_dict() for declaration in self.declarations],
"statements": [statement.to_dict() for statement in self.statements]
}
class If:
def __init__(self, condition: Node, body: Node, else_body: Node):
self.condition = condition
self.body = body
self.else_body = else_body
def to_dict(self):
return {
"node": "If",
"condition": self.condition.to_dict(),
"body": self.body.to_dict(),
"else_body": self.else_body.to_dict()
}
class FunctionDeclaration:
def __init__(self, type_name: TypeName, identifier: Identifier, parameters_declaration: list[Declaration]):
self.type_name = type_name
self.identifier = identifier
self.parameters_declaration = parameters_declaration
def to_dict(self):
return {
"node": "FunctionDeclaration",
"type_name": self.type_name.to_dict(),
"identifier": self.identifier.to_dict(),
"parameters_declaration": [declaration.to_dict() for declaration in self.parameters_declaration]
}
class FunctionDefinition:
def __init__(self, type_name: TypeName, identifier: Identifier, parameters_declaration: list[Declaration], body: Node):
self.type_name = type_name
self.identifier = identifier
self.parameters_declaration = parameters_declaration
self.body = body
def to_dict(self):
return {
"node": "FunctionDeclaration",
"type_name": self.type_name.to_dict(),
"identifier": self.identifier.to_dict(),
"parameters_declaration": [declaration.to_dict() for declaration in self.parameters_declaration],
"body": self.body.to_dict()
}
class FunctionCall:
def __init__(self, identifier: Identifier, arguments: list[Node]):
self.identifier = identifier
self.arguments = arguments
def to_dict(self):
return {
"node": "FunctionCall",
"identifier": self.identifier.to_dict(),
"arguments": [argument.to_dict() for argument in self.arguments]
}
TypeName = CPrimaryType
Node = typing.Union[NoneNode,
Identifier,
CharLiteral,
ConstantLiteral,
CUnaryOp, CCast,
CBinaryOp,
CTernaryOp,
TypeName,
Continue,
Break,
Return,
While,
For,
CompoundStatement,
If,
FunctionDeclaration,
FunctionDefinition,
FunctionCall
]
ExpressionTypes = typing.Union[
NoneNode,
CBinaryOp,
CharLiteral,
ConstantLiteral,
Identifier,
CUnaryOp,
CCast,
CTernaryOp,
FunctionCall,
]
StatementTypes = typing.Union[
NoneNode,
CompoundStatement,
ExpressionTypes,
Continue,
Break,
Return,
While,
For,
If,
]
Declarator = typing.Union[typing.Tuple[Identifier, Node],
typing.Tuple[NoneNode, NoneNode] # for function parameter
]
ExternalDeclaration = typing.Union[FunctionDefinition, FunctionDeclaration, Declaration]
TranslationUnit = typing.List[ExternalDeclaration]