-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAST.h
151 lines (125 loc) · 3.09 KB
/
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
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
#ifndef ylaciexpr_AST_h
#define ylaciexpr_AST_h
#define MAX_SYMBOL_LEN (128)
typedef struct AST_ AST;
typedef struct symbol_ Symbol;
typedef enum code_ CodeType;
#include "cstl/vector.h"
#include "cstl/unordered_map.h"
CSTL_VECTOR_INTERFACE(ASTVector, AST)
CSTL_VECTOR_INTERFACE(SymbolVector, Symbol)
CSTL_UNORDERED_MAP_INTERFACE(StrSymMap, const char *, Symbol)
enum code_ {
ETC_LIST,
VAL_NUM,
VAL_SYMBOL,
VAL_STRING,
CODE_PRINTLN,
CODE_FUNC,
CODE_VAR,
CODE_RETURN,
CODE_FOR,
OP_ADD,
OP_SUB,
OP_MUL,
OP_DIV,
OP_COMPARE_LT,
OP_COMPARE_GT,
OP_COMPARE_LE,
OP_COMPARE_GE,
OP_COMPARE_EQ,
OP_COMPARE_NEQ,
OP_ASSIGN,
OP_ASSIGN_ARRAY,
OP_CALL,
OP_REF_ARRAY
};
typedef enum symbol_type {
SYM_UNBOUND,
SYM_VALUE,
SYM_FUNC,
SYM_ARRAY
} SymbolType;
struct symbol_ {
SymbolType type;
char *name;
union {
int value;
struct {
int *data;
size_t size;
} array;
struct {
SymbolVector *params;
AST *body;
} func;
} un;
};
#define SYM_value un.value
#define SYM_param un.func.params
#define SYM_body un.func.body
#define SYM_array_data un.array.data
#define SYM_array_size un.array.size
struct AST_ {
CodeType code;
union {
/* For value (leaf node) */
int value;
/* For string */
const char *str;
/* For symbol */
Symbol *symbol;
/* For list */
ASTVector *list;
AST *unary;
struct {
AST *left;
AST *right;
} binary;
struct {
AST *first;
AST *second;
AST *third;
} trinary;
} un;
};
#define AST_value un.value
#define AST_string un.str
/* For Unary Expression */
#define AST_unary un.unary
/* For Binary Expression */
#define AST_left un.binary.left
#define AST_right un.binary.right
/* For Trinary Expression */
#define AST_first un.trinary.first
#define AST_second un.trinary.second
#define AST_third un.trinary.third
/* For List */
#define AST_list un.list
/* For Symbol Expression */
#define AST_symbol un.symbol
/* Normal Expression */
AST *AST_makeValue(int v);
AST *AST_makeString(const char *str);
AST *AST_makeUnary(CodeType type, AST *node);
AST *AST_makeBinary(CodeType type, AST *left, AST *right);
AST *AST_makeTrinary(CodeType type, AST *first, AST *second, AST *third);
AST *AST_makeFor(AST *init, AST *cond, AST *update, AST *block);
/* Symbol Definition */
AST *AST_makeSymbol(char *name);
Symbol *AST_lookupSymbol(char *name);
/* Declarations */
AST *AST_makeFunction(AST *name, AST *params, AST *body);
AST *AST_defineVariables(AST *name);
AST *AST_defineVariable(AST *name, AST *expr);
/* List Operation */
AST *AST_makeList(AST *node);
AST *AST_addList(AST *p, AST *e);
AST *AST_getList(AST *p, unsigned long index);
Symbol *Symbol_new(char *name);
extern StrSymMap *SymbolTable;
/* called from parser */
void AST_initializeVariable(AST *name, AST *expr);
void AST_declareArray(AST *name, AST *expr);
#endif
/* vim: set et ts=4 sts=4 sw=4: */