88#include "llvm-c/Core.h"
99#include "hash.h"
1010
11- #define OK 1
12- #define FAIL -1
13-
1411#define ALLOC (V , T ) T* V = (T*)calloc(1, sizeof(T))
15- #define PARSE (R , F ) R = F(context); if ( R == NULL ) return NULL
16- #define PARSE_ELSE (R , F ) R = F(context); if ( R == NULL )
17-
18- #define EXPECT (x ) if ( matchLiteral(context, x) == false ) return NULL
19- #define IF_MATCH (x ) if ( matchLiteral(context, x) == true )
20-
21- #define OP_NOP 0
22- #define OP_AND 1
23- #define OP_OR 2
24- #define OP_XOR 3
25- #define OP_EQ 4
26- #define OP_NE 5
27- #define OP_GT 6
28- #define OP_LT 7
29- #define OP_GE 8
30- #define OP_LE 9
31- #define OP_SHR 10
32- #define OP_SHL 11
33- #define OP_POW 12
34- #define OP_ADD 13
35- #define OP_SUB 14
36- #define OP_MUL 15
37- #define OP_DIV 16
38- #define OP_REM 17
39- #define OP_DVT 18 //divisibility test
40- #define OP_NOT 19
41- #define OP_NEG 20
42- #define OP_DOT 21
43- #define OP_BRC 22 //braces a[1]
44- #define OP_CAL 23 //function call
12+ #define ALLOC_ARRAY (V , C , T ) T* V = (T*)calloc(C, sizeof(T))
13+ #define ALLOC_NODE (V , TOKEN , KIND )\
14+ ALLOC(V, ExpressionNode);\
15+ strcpy(V->token, TOKEN);\
16+ V->kind = kind
17+ #define CHAIN_LIST (FIELD , PREV , CURR ) \
18+ if ( FIELD == NULL ) FIELD = CURR;\
19+ if ( PREV != NULL ) PREV->next = CURR;\
20+ PREV = CURR
4521
4622typedef struct
4723{
@@ -60,147 +36,114 @@ typedef struct
6036 /* hashtable_t* module_value_bindings; */
6137 //List of bindings defined inside current function
6238 hashtable_t * function_bindings ;
39+ char token_cache [256 ];
6340
6441} Context ;
6542
66-
6743typedef struct Binding Binding ;
68- typedef struct FunctionDecl FunctionDecl ;
69- typedef struct CodeBlock CodeBlock ;
70- typedef struct Expression Expression ;
71- typedef struct EqExpression EqExpression ;
72- typedef struct CmpExpression CmpExpression ;
73- typedef struct ShiftExpression ShiftExpression ;
74- typedef struct AddExpression AddExpression ;
75- typedef struct MulExpression MulExpression ;
76- typedef struct UnaryExpression UnaryExpression ;
77- typedef struct PrimaryExpression PrimaryExpression ;
78- typedef struct BasicExpression BasicExpression ;
79- typedef struct TermExpression TermExpression ;
80-
81- typedef struct Module
82- {
83- struct ModuleElement
84- {
85- Binding * binding ;
86- struct ModuleElement * next ;
87- } * first_element , * last_element ;
88- } Module ;
8944
90- typedef struct Binding
45+ typedef enum
9146{
92- char lhs [32 ];
93- FunctionDecl * function_decl ;
94- Expression * expression ;
95- } Binding ;
96-
97- typedef struct FunctionDecl
47+ NA_TYPE ,
48+ INT ,
49+ FLOAT ,
50+ BOOL ,
51+ CHAR
52+ } ExpressionType ;
53+
54+ typedef enum
9855{
99- CodeBlock * code_block ;
100- Expression * expression ;
101- } FunctionDecl ;
102-
103- typedef struct CodeBlock
56+ NA ,
57+ OP_EQUALS ,
58+ INT_LITERAL ,
59+ FLOAT_LITERAL ,
60+ BOOL_LITERAL ,
61+ CHAR_LITERAL ,
62+ IDENTIFIER ,
63+ OPEN_PAREN ,
64+ CLOSE_PAREN ,
65+ OP_ADD ,
66+ OP_SUB ,
67+ OP_NEG , //unary -
68+ OP_POS , //unary +
69+ OP_SHR ,
70+ OP_SHL ,
71+ OP_MUL ,
72+ OP_DIV ,
73+ OP_REM ,
74+ OP_DVT ,
75+ OPEN_BRACE ,
76+ CLOSE_BRACE ,
77+ COMMA ,
78+ FN_CALL ,
79+ FN_CALL_SIMPLE , //function call without arg
80+ OP_BIND ,
81+ OP_ARROW ,
82+ OP_RETURN ,
83+ OP_COLON
84+ } TokenKind ;
85+
86+ typedef struct ExpressionNode
10487{
105- struct CodeBlockElement
106- {
107- Binding * binding ;
108- Expression * return_expression ;
88+ char token [ 32 ];
89+ TokenKind kind ;
90+ //only for function call
91+ /* int arg_count; */
10992
110- struct CodeBlockElement * next ;
111- } * first_element , * last_element ;
112-
113- } CodeBlock ;
93+ struct ExpressionNode * next ;
94+ } ExpressionNode ;
11495
11596typedef struct Expression
11697{
117- struct ExpressionElement
118- {
119- int op ;
120- EqExpression * eq_expression ;
121- struct ExpressionElement * next ;
122- } * first_element , * last_element ;
98+ ExpressionNode * first_node ;
12399} Expression ;
124100
125- typedef struct EqExpression
101+ typedef struct
126102{
127- struct EqExpressionElement
128- {
129- int op ;
130- CmpExpression * cmp_expression ;
131- struct EqExpressionElement * next ;
132- } * first_element , * last_element ;
133- } EqExpression ;
103+ Binding * first_binding ;
104+ } Module ;
134105
135- typedef struct CmpExpression
106+ typedef struct ArgDef
136107{
137- struct CmpExpressionElement
138- {
139- int op ;
140- ShiftExpression * shift_expression ;
141- struct CmpExpressionElement * next ;
142- } * first_element , * last_element ;
143- } CmpExpression ;
108+ char name [32 ];
109+ char type [32 ];
144110
145- typedef struct ShiftExpression
146- {
147- struct ShiftExpressionElement
148- {
149- int op ;
150- AddExpression * add_expression ;
151- struct ShiftExpressionElement * next ;
152- } * first_element , * last_element ;
153- } ShiftExpression ;
111+ struct ArgDef * next ;
112+ } ArgDef ;
154113
155- typedef struct AddExpression
114+ typedef struct
156115{
157- struct AddExpressionElement
158- {
159- int op ;
160- MulExpression * mul_expression ;
161- struct AddExpressionElement * next ;
162- } * first_element , * last_element ;
163- } AddExpression ;
116+ Binding * first_binding ;
117+ ArgDef * first_arg ;
118+ char output_type [32 ];
119+ } FunctionDecl ;
164120
165- typedef struct MulExpression
121+ typedef struct Binding
166122{
167- struct MulExpressionElement
168- {
169- int op ;
170- UnaryExpression * unary_expression ;
171- struct MulExpressionElement * next ;
172- } * first_element , * last_element ;
173- } MulExpression ;
123+ char lhs [32 ];
174124
175- typedef struct UnaryExpression
176- {
177- int op ;
178- PrimaryExpression * primary_expression ;
179- } UnaryExpression ;
125+ //true, if this is a binding inside a function_decl and is prefixed with `::`
126+ bool is_return ;
180127
181- typedef struct PrimaryExpression
182- {
183- BasicExpression * basic_expression ;
184- struct PrimaryExpressionElement
185- {
186- TermExpression * term_expression ;
187- struct PrimaryExpressionElement * next ;
188- } * first_element , * last_element ;
128+ //A binding can be either an expression or a function declaration
129+ FunctionDecl * function_decl ;
130+ Expression * expression ;
189131
190- } PrimaryExpression ;
132+ //declared type `x:int := 10`
133+ char decl_type [64 ];
191134
192- typedef struct BasicExpression
193- {
194- char binding_name [32 ];
195- Expression * expression ;
196- int number ;
197- } BasicExpression ;
135+ struct Binding * next ;
136+ } Binding ;
198137
199- typedef struct TermExpression
138+
139+ typedef struct FunctionArgList
200140{
201- int op ;
202- Expression * expression ;
203- char binding_name [32 ];
204- } TermExpression ;
141+ struct FunctionArg
142+ {
143+ LLVMValueRef arg ;
144+ struct FunctionArg * next ;
145+ } * first_arg ;
146+ } FunctionArgList ;
147+
205148
206149#endif
0 commit comments