-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathast.h
More file actions
179 lines (152 loc) · 3.3 KB
/
ast.h
File metadata and controls
179 lines (152 loc) · 3.3 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
#pragma once
#include "tok.h"
#include <llvm-c/Core.h>
#include <llvm-c/DataTypes.h>
#include <llvm-c/Types.h>
#include <llvm-c/ExecutionEngine.h>
#include <llvm-c/BitReader.h>
#include <llvm-c/BitWriter.h>
#include <llvm-c/Object.h>
#include <llvm-c/OrcBindings.h>
/** All Ast Node Types */
typedef enum {
BinaryAST,
LiteralAST,
AssignmentAST,
CastAST,
UnaryAST,
ReturnAST,
BlockAST,
IfAST,
ElseAST,
ElifAST,
ForAST,
WhileAST,
BreakAST,
ContinueAST,
PrintAST,
InputAST,
RandAST,
} AstType;
typedef enum {
StringValue = 10,
UIntValue = 11,
IntValue = 12,
DoubleValue = 13,
PointerValue = 14
} AstValue;
/** Generic Container Type */
typedef struct Ast Ast;
/** Container for direct literal */
typedef struct {
TokEntity literal;
} LiteralAst;
typedef struct {
TokEntity op;
Ast *expr;
} UnaryAst;
/** Recursive Value Resolving */
typedef struct BinaryAst BinaryAst;
typedef struct BinaryAst {
Ast *lhs;
TokEntity op;
Ast *rhs;
} BinaryAst;
/** Variable Assignment */
typedef struct {
TokEntity name;
Ast *expr;
} AssignmentAst;
/** Generic Printing */
typedef struct {
Ast *expr;
} PrintAst;
typedef struct {
Ast *expr;
} InputAst;
typedef struct {
Ast **stmts;
} BlockAst;
typedef struct {
BlockAst *body;
} ElseAst;
typedef struct {
Ast *condition;
BlockAst *body;
ElseAst *else_;
Ast *elif;
} IfAst;
typedef struct {
Ast *condition;
BlockAst *body;
} WhileAst;
typedef struct {
Ast *min;
Ast *max;
} RandAst;
typedef struct Ast {
AstType type;
union {
LiteralAst literalast;
UnaryAst unaryast;
BinaryAst binaryast;
AssignmentAst assignmentast;
PrintAst printast;
InputAst inputast;
BlockAst blockast;
ElseAst elseast;
IfAst ifast;
WhileAst whileast;
RandAst randast;
};
} Ast;
extern const char * requesting_var;
extern const char * requesting_fn;
LLVMValueRef *variables;
void rnd_str(char *s);
static LLVMValueRef add_input(LLVMModuleRef mod)
{
LLVMTypeRef input_args[] = { LLVMPointerType(LLVMInt8Type(), 0) };
LLVMTypeRef input_ty = LLVMFunctionType (
LLVMPointerType(LLVMInt8Type(), 0),
input_args,
1,
false
);
LLVMValueRef input = __add__("input", LLVMAddFunction(mod, "input", input_ty));
LLVMValueRef msg = LLVMGetParam(input, 0);
LLVMSetValueName(msg, "msg");
LLVMBuilderRef builder = LLVMCreateBuilder();
LLVMBasicBlockRef entry = LLVMAppendBasicBlock(input, "entry");
LLVMPositionBuilderAtEnd(builder, entry);
LLVMValueRef printf_fn = __get__("printf");
LLVMValueRef printf_args[] = { LLVMBuildGlobalStringPtr(builder, "%s", "%s"), msg };
LLVMBuildCall (
builder,
printf_fn,
printf_args,
2,
""
);
LLVMValueRef malloc_fn = __get__("malloc");
LLVMValueRef malloc_args[] = { LLVMConstInt(LLVMInt64Type(), 1, true) };
LLVMValueRef buf = LLVMBuildCall (
builder,
malloc_fn,
malloc_args,
1,
"x"
);
LLVMValueRef gets_fn = __get__("gets");
LLVMValueRef gets_args[] = { buf };
LLVMValueRef t = LLVMBuildCall(
builder,
gets_fn,
gets_args,
1,
""
);
LLVMBuildRet(builder, buf); // Note: do not LLVMBuildLoad
return input;
}
LLVMValueRef resolve_ast(LLVMExecutionEngineRef engine, LLVMBuilderRef builder, Ast *type);