-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule.jai
More file actions
408 lines (323 loc) · 13.1 KB
/
module.jai
File metadata and controls
408 lines (323 loc) · 13.1 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
// typechecking callbcak allows additional return value for replacement node
Typecheck_Callback :: type_of(typecheck_node);
Execute\ _Callback :: type_of(execute_node);
Evaluate\_Callback :: type_of(evaluate_node);
Serialize_Callback :: type_of(sprint_node);
Script :: struct {
ast_root: *Node_Block;
flags: Flags;
variables: [..] External_Variable;
procedures: [..] External_Procedure;
directives: [..] Directive;
named_blocks: [..] *Node_Block;
type_table: [..] Name_And_Type;
operator_table: [..] Operator;
// added for the sake of convenience and use in repl
global_declarations: [..] *Node_Declaration;
pool: Flat_Pool;
error: Error;
stack_base: *void;
stack_size: int;
stack_ptr: *void;
#if USING_DYNCALL {
dyncall_vm: *DCCallVM;
}
lexer: Lexer;
current_scope: *Node; // perhaps this should be passed on stack, only used during parsing
// callbacks for handling custom node types
typecheck_callback: Typecheck_Callback;
execute\ _callback: Execute\ _Callback;
evaluate\_callback: Evaluate\_Callback;
serialize_callback: Serialize_Callback;
// to make it easy to override parsing procedures individually, while still being able to make use of many of the convenient higher-order procs like parse_comma_separated_expressions.
my_parse_expression := parse_expression;
my_parse_binary := parse_binary;
my_parse_leaf := parse_leaf;
// NOTE: get_virtual_member gets passed the script, so implicitly they're also getting the get_virtual_member_user_data pointer
get_virtual_member: (*Script, Any, string, *Type_Info) -> bool, Any;
get_virtual_member_user_data: *void;
Flags :: enum_flags {
INITIALIZED;
PARSED;
TYPECHECKED;
}
}
deinit_script :: (using script: *Script) {
array_reset(*variables);
array_reset(*procedures);
array_reset(*named_blocks);
array_reset(*directives);
array_reset(*type_table);
array_reset(*global_declarations);
fini(*pool);
#if USING_DYNCALL {
if script.dyncall_vm {
dcFree(script.dyncall_vm);
script.dyncall_vm = null;
}
}
}
init_script :: (script: *Script, stack_size := 1024) {
script.* = .{};
init(*script.pool);
script.named_blocks.allocator = context.allocator;
// TODO: ensure that stack base is aligned to 8 bytes
script.stack_size = stack_size;
script.stack_base = get(*script.pool, script.stack_size);
script.stack_ptr = script.stack_base;
// allocate dyncall vm that will be reused
#if USING_DYNCALL {
script.dyncall_vm = dcNewCallVM(2048);
dcMode(script.dyncall_vm, DC_CALL_C_DEFAULT);
dcReset(script.dyncall_vm);
}
script.ast_root = alloc_node(script, Node_Block);
script.current_scope = script.ast_root;
array_add(*script.operator_table, ..default_operator_table);
script.flags |= .INITIALIZED;
}
has_error :: (script: *Script) -> bool { return script.error.type != .NO_ERROR; }
clear_error :: (script: *Script) { script.error = .{}; }
// copy_script_static_variables :: (dst: *Script, src: *Script) {
// free_script(dst); // just in case data is already there...
// dst.* = .{};
// init(*dst.pool);
// // TODO: how to copy all of syntax tree...
// // this is annoying because we will have to clone the whole tree recursively and
// }
Name_And_Type :: struct {
name: string;
type: *Type_Info;
}
// ensures no name conflicts
register_type :: (script: *Script, name: string, type: Type) { register_type(script, name, type.(*Type_Info)); }
register_type :: (script: *Script, name: string, type: *Type_Info) {
for *script.type_table {
if it.name == name {
log("Warning: attempt register a type with same name as previously registered type.");
return;
}
}
array_add(*script.type_table, .{ name, type });
}
get_named_block :: (script: *Script, name: string) -> *Node_Block {
for script.named_blocks
if it.name == name
return it;
return null;
}
// STACK STUFF
PTR_SIZE :: size_of(*void);
stack_pop :: inline (script: *Script, $$is_lvalue := false) -> *void {
script.stack_ptr -= PTR_SIZE;
assert(script.stack_ptr >= script.stack_base, "script stack pointer was below stack base!");
if is_lvalue {
return script.stack_ptr.(**void).*;
} else {
return script.stack_ptr;
}
}
stack_peek :: inline (script: *Script, $$is_lvalue := false) -> *void {
stack_ptr := script.stack_ptr - PTR_SIZE;
assert(stack_ptr >= script.stack_base, "script stack pointer was below stack base!");
if is_lvalue {
return stack_ptr.(**void).*;
} else {
return stack_ptr;
}
}
stack_push_any :: (script: *Script, any: Any, is_lvalue := false) -> bool {
if script.stack_ptr >= script.stack_base + script.stack_size {
set_execution_error(script, "Ran out of space on stack!", node = null);
return false;
}
defer script.stack_ptr += PTR_SIZE;
if is_aggr(any.type) || is_lvalue {
script.stack_ptr.(**void).* = any.value_pointer; // store the pointer itself instead of the value
} else {
assert(any.type.runtime_size <= PTR_SIZE);
memcpy(script.stack_ptr, any.value_pointer, PTR_SIZE); // copy the value being pointed at for register-sized values (INT, FLOAT, ENUM)
}
return true;
}
stack_push_zeroes :: (script: *Script, size: int) -> bool {
if script.stack_ptr + size >= script.stack_base + script.stack_size {
set_execution_error(script, "Ran out of space on stack!", node = null);
return false;
}
memset(script.stack_ptr, 0, size);
script.stack_ptr += size;
return true;
}
stack_pop_any :: inline (script: *Script, ti: *Type_Info, is_lvalue := false) -> Any {
script.stack_ptr -= PTR_SIZE;
assert(script.stack_ptr >= script.stack_base, "script stack pointer was below stack base!");
if is_aggr(ti) || is_lvalue {
return Any.{ ti, script.stack_ptr.(**void).* }; // additional dereference to get pointer to value
} else {
assert(ti.runtime_size <= PTR_SIZE);
return Any.{ ti, script.stack_ptr };
}
}
stack_peek_any :: inline (script: *Script, ti: *Type_Info, is_lvalue := false) -> Any {
ptr := script.stack_ptr - PTR_SIZE;
assert(script.stack_ptr >= script.stack_base, "script stack pointer was below stack base!");
if is_aggr(ti) || is_lvalue {
return Any.{ ti, ptr.(**void).* }; // additional dereference to get pointer to value
} else {
assert(ti.runtime_size <= PTR_SIZE);
return Any.{ ti, ptr };
}
}
is_on_stack :: (using script: *Script, ptr: *void) -> bool {
return ptr >= script.stack_base && ptr < script.stack_base + script.stack_size;
}
Walk_Nodes_Callback :: #type (script: *Script, node: *Node, data: *void) -> (bool, *Node);
walk_nodes :: (script: *Script, _node: **Node, callback: Walk_Nodes_Callback, data: *void) -> bool {
node := _node.*;
if node == null {
dprint("Warning: node was null in walk_nodes.\n");
return true; // TODO: make false later probably...
}
ok, replacement := callback(script, node, data);
if !ok return false;
if replacement replace_node(_node, replacement);
// recurse for certain node types
if node.node_type == {
case Node_Operation;
operation := node.(*Node_Operation);
return walk_nodes(script, xx *operation.left, callback, data)
&& walk_nodes(script, xx *operation.right, callback, data);
case Node_Procedure_Call;
procedure_call := node.(*Node_Procedure_Call);
for 0..procedure_call.arguments.count-1 {
node_ptr_ptr := *procedure_call.arguments[it];
if !walk_nodes(script, node_ptr_ptr, callback, data) return false;
}
return true;
case Node_Dot;
dot := node.(*Node_Dot);
return walk_nodes(script, xx *dot.left, callback, data)
&& walk_nodes(script, xx *dot.right, callback, data);
case Node_Cast;
node_cast := node.(*Node_Cast);
if !walk_nodes(script, xx *node_cast.value, callback, data) return false;
if !(node_cast.flags & .IMPLICIT) {
if !walk_nodes(script, xx *node_cast.type_expression, callback, data) return false;
}
case Node_Subscript;
subscript := node.(*Node_Subscript);
return walk_nodes(script, xx *subscript.base_expression, callback, data)
&& walk_nodes(script, xx *subscript.indexing_expression, callback, data);
case Node_Declaration;
declaration := node.(*Node_Declaration);
return walk_nodes(script, xx *declaration.type_expression, callback, data)
&& walk_nodes(script, xx *declaration.init_expression, callback, data);
case Node_Block;
block := node.(*Node_Block);
for 0..block.statements.count-1 {
node_ptr_ptr := *block.statements[it];
if !walk_nodes(script, node_ptr_ptr, callback, data) return false;
}
return true;
case Node_If_Statement;
if_statement := node.(*Node_If_Statement);
return walk_nodes(script, xx *if_statement.condition, callback, data)
&& walk_nodes(script, xx *if_statement.statement, callback, data);
case Node_While_Loop;
while_loop := node.(*Node_While_Loop);
return walk_nodes(script, xx *while_loop.condition, callback, data)
&& walk_nodes(script, xx *while_loop.statement, callback, data);
case Node_For_Loop;
for_loop := node.(*Node_For_Loop);
if for_loop.control_type == {
case .RANGE;
if !walk_nodes(script, xx *for_loop.range.lower, callback, data) return false;
if !walk_nodes(script, xx *for_loop.range.upper, callback, data) return false;
case .ARRAY;
if !walk_nodes(script, xx *for_loop.array_expression, callback, data) return false;
}
if !walk_nodes(script, xx *for_loop.statement, callback, data) return false;
return true;
}
return true;
}
// CONVENIENCE STUFF
// user can append to this as they wish, or just change it here
common_types: [..] struct { name: string; type: *Type_Info; } = .[
.{ "u8", type_info(u8) },
.{ "u16", type_info(u16) },
.{ "u32", type_info(u32) },
.{ "u64", type_info(u64) },
.{ "s8", type_info(s8) },
.{ "s16", type_info(s16) },
.{ "s32", type_info(s32) },
.{ "s64", type_info(s64) },
.{ "int", type_info(int) },
.{ "float", type_info(float) },
.{ "float32", type_info(float32) },
.{ "float64", type_info(float64) },
];
register_common_types :: (script: *Script) {
for common_types register_type(script, it.name, it.type);
}
#module_parameters ()(DEBUG := false, USING_DYNCALL := true);
#load "node.jai";
#load "lexer.jai";
#load "parse.jai";
#load "typecheck.jai";
#load "execute.jai";
#load "evaluate.jai";
#load "serialize.jai";
#load "variable.jai";
#load "procedure.jai";
#load "directive.jai";
#load "operator.jai";
#load "result.jai";
#scope_module
#import "Basic";
#import "Math";
#import "String";
using,except(next_power_of_two) Hash_Table :: #import "Hash_Table";
#import "Flat_Pool";
#import "Reflection";
#import "Compiler"; // for 'is_subclass_of'
#import "Utils";
Convert :: #import "Convert";
#if USING_DYNCALL {
#import "dyncall";
}
#if DEBUG { #add_context dprint_indent: int; }
dprint :: (format_string: string, args: .. Any) {
#if DEBUG {
for 0..context.dprint_indent print(" ");
print(format_string, ..args);
}
}
dprint_push_indent :: () #expand {
#if DEBUG {
context.dprint_indent += 1;
`defer context.dprint_indent -= 1;
}
}
// ========== Flat Pool ==========
// Moved these helper procs out of Utils module, will just have to duplicate it into other modules when needed.
// is_this_yours for flat pool allocator
is_this_yours :: (pool: *Flat_Pool, memory: *void) -> bool {
return flat_pool_allocator_proc(.IS_THIS_YOURS, 0, 0, memory, pool).(bool);
}
copy_to_pool_if_needed :: (pool: *Flat_Pool, any: Any) -> Any {
if is_this_yours(pool, any.value_pointer) return any;
copy := New_Any(any.type, initialized = false,, get_pool_allocator(pool));
memcpy(copy.value_pointer, any.value_pointer, any.type.runtime_size);
return copy;
}
copy_to_pool_if_needed :: (pool: *Flat_Pool, value: *$T) -> *T {
if is_this_yours(pool, value) return value;
copy := get(pool, size_of(T)).(*T);
copy.* = value.*;
return copy;
}
get_pool_allocator :: (pool: *Flat_Pool) -> Allocator {
return .{ flat_pool_allocator_proc, pool };
}