-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast.h
44 lines (34 loc) · 875 Bytes
/
ast.h
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
#ifndef AST_H
#define AST_H
// Grammar
// <program> := <function>
// <function> := "int" <id> "(" ")" "{" <statement> "}"
// <statement> := "return" <expr> ";"
// <exp> := <term> { ("+" | "-") <term> }
// <term> := <factor> {("*" | "/") <factor>}
// <factor> := "(" <expr> ")" | <unary_op> <term> | <constant>
// <unary_op> := "!" | "~" | "-"
typedef struct Expression Expression;
struct Expression {
enum {
UnaryOperator,
BinaryOperator,
Constant,
} tag;
union {
struct UnaryOperator { Expression* expr; char* op; } unaryop;
struct BinaryOperator { Expression* expr1; Expression* expr2; char* op; } binop;
struct Constant { int value;} cst;
} data;
};
typedef struct {
Expression* expr;
} ReturnStatement;
typedef struct {
char* name;
ReturnStatement* st;
} Function;
typedef struct {
Function* fn;
} Program;
#endif // !AST_H