forked from Wilfred/babyc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
syntax.h
174 lines (119 loc) · 3.6 KB
/
syntax.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "list.h"
#ifndef BABYC_SYNTAX_HEADER
#define BABYC_SYNTAX_HEADER
typedef enum {
IMMEDIATE,
VARIABLE,
UNARY_OPERATOR,
BINARY_OPERATOR,
// We already use 'RETURN' and 'IF' as token names.
BLOCK,
IF_STATEMENT,
RETURN_STATEMENT,
DEFINE_VAR,
FUNCTION,
FUNCTION_CALL,
FUNCTION_ARGUMENTS,
ASSIGNMENT,
WHILE_SYNTAX,
TOP_LEVEL
} SyntaxType;
typedef enum { BITWISE_NEGATION, LOGICAL_NEGATION } UnaryExpressionType;
typedef enum {
ADDITION,
SUBTRACTION,
MULTIPLICATION,
LESS_THAN,
LESS_THAN_OR_EQUAL,
} BinaryExpressionType;
struct Syntax;
typedef struct Syntax Syntax;
typedef struct Immediate { int value; } Immediate;
typedef struct Variable {
// TODO: once we have other types, we will need to store type here.
char *var_name;
} Variable;
typedef struct UnaryExpression {
UnaryExpressionType unary_type;
Syntax *expression;
} UnaryExpression;
typedef struct BinaryExpression {
BinaryExpressionType binary_type;
Syntax *left;
Syntax *right;
} BinaryExpression;
typedef struct FunctionArguments { List *arguments; } FunctionArguments;
typedef struct FunctionCall {
char *function_name;
Syntax *function_arguments;
} FunctionCall;
typedef struct Assignment {
char *var_name;
Syntax *expression;
} Assignment;
typedef struct IfStatement {
Syntax *condition;
Syntax *then;
} IfStatement;
typedef struct DefineVarStatement {
char *var_name;
Syntax *init_value;
} DefineVarStatement;
typedef struct WhileStatement {
Syntax *condition;
Syntax *body;
} WhileStatement;
typedef struct ReturnStatement { Syntax *expression; } ReturnStatement;
typedef struct Block { List *statements; } Block;
typedef struct Function {
char *name;
List *parameters;
Syntax *root_block;
} Function;
typedef struct Parameter {
// TODO: once we have other types, we will need to store type here.
char *name;
} Parameter;
typedef struct TopLevel { List *declarations; } TopLevel;
struct Syntax {
SyntaxType type;
union {
Immediate *immediate;
Variable *variable;
UnaryExpression *unary_expression;
BinaryExpression *binary_expression;
Assignment *assignment;
ReturnStatement *return_statement;
FunctionArguments *function_arguments;
FunctionCall *function_call;
IfStatement *if_statement;
DefineVarStatement *define_var_statement;
WhileStatement *while_statement;
Block *block;
Function *function;
TopLevel *top_level;
};
};
Syntax *immediate_new(int value);
Syntax *variable_new(char *var_name);
Syntax *bitwise_negation_new(Syntax *expression);
Syntax *logical_negation_new(Syntax *expression);
Syntax *addition_new(Syntax *left, Syntax *right);
Syntax *subtraction_new(Syntax *left, Syntax *right);
Syntax *multiplication_new(Syntax *left, Syntax *right);
Syntax *less_than_new(Syntax *left, Syntax *right);
Syntax *less_or_equal_new(Syntax *left, Syntax *right);
Syntax *function_call_new(char *function_name, Syntax *func_args);
Syntax *function_arguments_new();
Syntax *assignment_new(char *var_name, Syntax *expression);
Syntax *return_statement_new(Syntax *expression);
Syntax *block_new(List *statements);
Syntax *if_new(Syntax *condition, Syntax *then);
Syntax *define_var_new(char *var_name, Syntax *init_value);
Syntax *while_new(Syntax *condition, Syntax *body);
Syntax *function_new(char *name, Syntax *root_block);
Syntax *top_level_new();
void syntax_free(Syntax *syntax);
char *syntax_type_name(Syntax *syntax);
void print_syntax(Syntax *syntax);
#endif