-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathast_to_builder.jou
640 lines (557 loc) · 28.9 KB
/
ast_to_builder.jou
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
import "stdlib/str.jou"
import "stdlib/mem.jou"
import "../ast.jou"
import "../errors_and_warnings.jou"
import "../evaluate.jou"
import "../types.jou"
import "../types_in_ast.jou"
import "./either_builder.jou"
class LocalVar:
name: byte[100]
# All local variables are represented as pointers to stack space, even
# if they are never reassigned. LLVM will optimize the mess.
ptr: EitherBuilderValue
class Loop:
on_break: EitherBuilderBlock
on_continue: EitherBuilderBlock
class AstToBuilder:
builder: EitherBuilder*
locals: LocalVar*
nlocals: int
loops: Loop*
nloops: int
returns_a_value: bool
location: Location
# Returns old location. Use it to restore the location when you're done.
def set_location(self, location: Location) -> Location:
old = self->location
self->location = location
# If no reasonable location is available (e.g. implicit return at end of function),
# continue using the previous location for now.
if location.path != NULL and location.lineno != 0:
self->builder->set_location(location)
return old
def begin_function(self, sig: Signature*, location: Location, locals: LocalVariable*, nlocals: int, public: bool) -> None:
old = self->set_location(location)
# First n local variables are the arguments
assert sig->nargs >= 0
assert sig->nargs <= nlocals
self->builder->begin_function(sig, public)
self->returns_a_value = (sig->returntype != NULL)
if nlocals > self->nlocals:
self->locals = realloc(self->locals, sizeof(self->locals[0]) * nlocals)
assert self->locals != NULL
self->nlocals = nlocals
for i = 0; i < nlocals; i++:
var_name = locals[i].name
var_type = locals[i].type
var_ptr = self->builder->stack_alloc(var_type, var_name)
self->locals[i] = LocalVar{name = var_name, ptr = var_ptr}
if i < sig->nargs:
# First n local variables are the function arguments
self->builder->set_ptr(var_ptr, self->builder->get_argument(i, var_type))
if (WINDOWS or MACOS or NETBSD) and sig->is_main_function():
# Insert a call to the special startup function into main()
jou_startup_sig = Signature{name = "_jou_startup"}
self->builder->call(&jou_startup_sig, NULL, 0)
self->set_location(old)
def local_var_exists(self, name: byte*) -> bool:
for i = 0; i < self->nlocals; i++:
if strcmp(self->locals[i].name, name) == 0:
return True
return False
def local_var_ptr(self, name: byte*) -> EitherBuilderValue:
for i = 0; i < self->nlocals; i++:
if strcmp(self->locals[i].name, name) == 0:
return self->locals[i].ptr
assert False
def build_function_call(self, call: AstCall*) -> EitherBuilderValue:
assert call->method_call_self == NULL
assert call->nargs <= 100
args: EitherBuilderValue[100]
for i = 0; i < call->nargs; i++:
args[i] = self->build_expression(&call->args[i])
result = self->builder->call(call->called_signature, args, call->nargs)
if call->called_signature->is_noreturn:
# Code after the function call will not run. Place it to a new block.
self->builder->unreachable()
self->builder->set_current_block(self->builder->add_block())
return result
def build_method_call(self, call: AstCall*) -> EitherBuilderValue:
assert call->method_call_self != NULL
# leave room for self
assert call->nargs <= 99
args: EitherBuilderValue[100]
k = 0
want_pointer = call->called_signature->argtypes[0]->is_pointer_type()
got_pointer = call->uses_arrow_operator
if want_pointer and not got_pointer:
args[k++] = self->build_address_of_expression(call->method_call_self)
elif got_pointer and not want_pointer:
args[k++] = self->builder->dereference(self->build_expression(call->method_call_self))
else:
args[k++] = self->build_expression(call->method_call_self)
for i = 0; i < call->nargs; i++:
args[k++] = self->build_expression(&call->args[i])
return self->builder->call(call->called_signature, args, k)
def build_binop(self, op: AstExpressionKind, lhs: EitherBuilderValue, rhs: EitherBuilderValue) -> EitherBuilderValue:
match op:
case AstExpressionKind.Eq:
return self->builder->eq(lhs, rhs)
case AstExpressionKind.Ne:
return self->builder->not_(self->builder->eq(lhs, rhs))
case AstExpressionKind.Lt:
return self->builder->lt(lhs, rhs)
case AstExpressionKind.Gt:
return self->builder->lt(rhs, lhs)
case AstExpressionKind.Le:
return self->builder->not_(self->builder->lt(rhs, lhs))
case AstExpressionKind.Ge:
return self->builder->not_(self->builder->lt(lhs, rhs))
case AstExpressionKind.Add:
return self->builder->add(lhs, rhs)
case AstExpressionKind.Sub:
return self->builder->sub(lhs, rhs)
case AstExpressionKind.Mul:
return self->builder->mul(lhs, rhs)
case AstExpressionKind.Div:
return self->builder->div(lhs, rhs)
case AstExpressionKind.Mod:
return self->builder->mod(lhs, rhs)
case _:
assert False
def build_inplace_binop(self, op: AstExpressionKind, lhs: AstExpression*, rhs: AstExpression*) -> None:
lhs_ptr = self->build_address_of_expression(lhs)
rhs_value = self->build_expression(rhs)
old_value = self->builder->dereference(lhs_ptr)
new_value = self->build_binop(op, old_value, rhs_value)
self->builder->set_ptr(lhs_ptr, new_value)
def build_instantiation(self, class_type: Type*, inst: AstInstantiation*) -> EitherBuilderValue:
assert class_type != NULL
assert class_type->kind == TypeKind.Class
inst_ptr = self->builder->stack_alloc(class_type, NULL)
self->builder->memset_to_zero(inst_ptr)
for i = 0; i < inst->nfields; i++:
field_ptr = self->builder->class_field_pointer(inst_ptr, inst->field_names[i])
field_value = self->build_expression(&inst->field_values[i])
self->builder->set_ptr(field_ptr, field_value)
return self->builder->dereference(inst_ptr)
def build_and(self, lhsexpr: AstExpression*, rhsexpr: AstExpression*) -> EitherBuilderValue:
# Must be careful with side effects.
#
# # lhs returning False means we don't evaluate rhs
# if lhs:
# result = rhs
# else:
# result = False
lhstrue = self->builder->add_block()
lhsfalse = self->builder->add_block()
done = self->builder->add_block()
resultptr = self->builder->stack_alloc(boolType, NULL)
# if lhs:
self->builder->branch(self->build_expression(lhsexpr), lhstrue, lhsfalse)
self->builder->set_current_block(lhstrue)
# result = rhs
self->builder->set_ptr(resultptr, self->build_expression(rhsexpr))
# end if
self->builder->jump(done)
# else:
self->builder->set_current_block(lhsfalse)
# result = False
self->builder->set_ptr(resultptr, self->builder->boolean(False))
# end else
self->builder->jump(done)
self->builder->set_current_block(done)
return self->builder->dereference(resultptr)
def build_or(self, lhsexpr: AstExpression*, rhsexpr: AstExpression*) -> EitherBuilderValue:
# Must be careful with side effects.
#
# # lhs returning True means we don't evaluate rhs
# if lhs:
# result = True
# else:
# result = rhs
lhstrue = self->builder->add_block()
lhsfalse = self->builder->add_block()
done = self->builder->add_block()
resultptr = self->builder->stack_alloc(boolType, NULL)
# if lhs:
self->builder->branch(self->build_expression(lhsexpr), lhstrue, lhsfalse)
self->builder->set_current_block(lhstrue)
# result = True
self->builder->set_ptr(resultptr, self->builder->boolean(True))
# end if
self->builder->jump(done)
# else:
self->builder->set_current_block(lhsfalse)
# result = rhs
self->builder->set_ptr(resultptr, self->build_expression(rhsexpr))
# end else
self->builder->jump(done)
self->builder->set_current_block(done)
return self->builder->dereference(resultptr)
def build_increment_or_decrement(self, inner: AstExpression*, pre: bool, diff: int) -> EitherBuilderValue:
assert diff == 1 or diff == -1 # 1=increment, -1=decrement
ptr = self->build_address_of_expression(inner)
old_value = self->builder->dereference(ptr)
t = inner->types.implicit_cast_type
if t->is_number_type():
new_value = self->builder->add(old_value, self->builder->cast(self->builder->integer(intType, diff), t))
else:
new_value = self->builder->indexed_pointer(old_value, self->builder->integer(longType, diff))
self->builder->set_ptr(ptr, new_value)
if pre:
return new_value
else:
return old_value
def build_array(self, t: Type*, items: AstExpression*, nitems: int) -> EitherBuilderValue:
assert t->kind == TypeKind.Array
assert t->array.len == nitems
arr_ptr = self->builder->stack_alloc(t, NULL)
first_item_ptr = self->builder->cast(arr_ptr, t->array.item_type->pointer_type())
for i = 0; i < nitems; i++:
i_built = self->builder->integer(longType, i)
item_ptr = self->builder->indexed_pointer(first_item_ptr, i_built)
item_value = self->build_expression(&items[i])
self->builder->set_ptr(item_ptr, item_value)
return self->builder->dereference(arr_ptr)
def build_expression_without_implicit_cast(self, expr: AstExpression*) -> EitherBuilderValue:
match expr->kind:
case AstExpressionKind.String:
if expr->types.orig_type == byteType->pointer_type():
return self->builder->string(expr->string)
else:
assert expr->types.orig_type->kind == TypeKind.Array
assert expr->types.orig_type->array.item_type == byteType
return self->builder->string_array(expr->string, expr->types.orig_type->array.len)
case AstExpressionKind.Byte:
return self->builder->integer(byteType, expr->byte_value)
case AstExpressionKind.Short:
return self->builder->integer(shortType, expr->short_value)
case AstExpressionKind.Int:
return self->builder->integer(intType, expr->int_value)
case AstExpressionKind.Long:
return self->builder->integer(longType, expr->long_value)
case AstExpressionKind.Float:
return self->builder->float_or_double(floatType, expr->float_or_double_text)
case AstExpressionKind.Double:
return self->builder->float_or_double(doubleType, expr->float_or_double_text)
case AstExpressionKind.Bool:
return self->builder->boolean(expr->bool_value)
case AstExpressionKind.Null:
return self->builder->zero_of_type(voidPtrType)
case AstExpressionKind.Array:
return self->build_array(expr->types.orig_type, expr->array.items, expr->array.length)
case AstExpressionKind.Call:
if expr->call.method_call_self == NULL:
return self->build_function_call(&expr->call)
else:
return self->build_method_call(&expr->call)
case AstExpressionKind.Instantiate:
return self->build_instantiation(expr->types.orig_type, &expr->instantiation)
case AstExpressionKind.Self:
return self->builder->dereference(self->local_var_ptr("self"))
case AstExpressionKind.GetVariable:
special = get_special_constant(expr->varname)
if special == 0:
return self->builder->boolean(False)
elif special == 1:
return self->builder->boolean(True)
elif self->local_var_exists(expr->varname):
return self->builder->dereference(self->local_var_ptr(expr->varname))
else:
return self->builder->dereference(self->builder->global_var_ptr(expr->varname, expr->types.orig_type))
case AstExpressionKind.Indexing:
# ptr[foo] can always be evaluated as &ptr[foo], because ptr is already a pointer.
# We can't do this for all expressions, because e.g. &some_function() is error.
return self->builder->dereference(self->build_address_of_expression(expr))
case AstExpressionKind.GetEnumMember:
return self->builder->enum_member(expr->types.orig_type, expr->enum_member.member_name)
case AstExpressionKind.GetClassField:
if expr->class_field.uses_arrow_operator:
ptr = self->build_expression(expr->class_field.instance)
else:
# Evaluate foo.bar as (&temp)->bar, where temp is a temporary copy of foo.
# We need to copy, because it's not always possible to evaluate &foo.
# For example, consider evaluating some_function().some_field.
instance = self->build_expression(expr->class_field.instance)
ptr = self->builder->stack_alloc(expr->class_field.instance->types.implicit_cast_type, NULL)
self->builder->set_ptr(ptr, instance)
fieldptr = self->builder->class_field_pointer(ptr, expr->class_field.field_name)
return self->builder->dereference(fieldptr)
case AstExpressionKind.As:
return self->builder->cast(self->build_expression(&expr->as_->value), expr->types.orig_type)
case AstExpressionKind.SizeOf:
return self->builder->size_of(expr->operands[0].types.implicit_cast_type)
case AstExpressionKind.AddressOf:
return self->build_address_of_expression(&expr->operands[0])
case AstExpressionKind.Dereference:
return self->builder->dereference(self->build_expression(&expr->operands[0]))
case AstExpressionKind.Negate: # -x
# compute -x as 0-x
return self->builder->sub(
self->builder->zero_of_type(expr->operands[0].types.implicit_cast_type),
self->build_expression(&expr->operands[0]),
)
case AstExpressionKind.PreIncr:
return self->build_increment_or_decrement(&expr->operands[0], True, 1)
case AstExpressionKind.PreDecr:
return self->build_increment_or_decrement(&expr->operands[0], True, -1)
case AstExpressionKind.PostIncr:
return self->build_increment_or_decrement(&expr->operands[0], False, 1)
case AstExpressionKind.PostDecr:
return self->build_increment_or_decrement(&expr->operands[0], False, -1)
case (
AstExpressionKind.Add
| AstExpressionKind.Sub
| AstExpressionKind.Mul
| AstExpressionKind.Div
| AstExpressionKind.Mod
| AstExpressionKind.Eq
| AstExpressionKind.Ne
| AstExpressionKind.Gt
| AstExpressionKind.Ge
| AstExpressionKind.Lt
| AstExpressionKind.Le
):
# Note: If you port this code to another programming language, make sure
# to evaluate the operands in the correct order. C does not guarantee
# evaluation order of function arguments, but Jou does.
return self->build_binop(expr->kind, self->build_expression(&expr->operands[0]), self->build_expression(&expr->operands[1]))
case AstExpressionKind.And:
return self->build_and(&expr->operands[0], &expr->operands[1])
case AstExpressionKind.Or:
return self->build_or(&expr->operands[0], &expr->operands[1])
case AstExpressionKind.Not:
return self->builder->not_(self->build_expression(&expr->operands[0]))
assert False
def build_expression(self, expr: AstExpression*) -> EitherBuilderValue:
old_location = self->set_location(expr->location)
if expr->types.implicit_array_to_pointer_cast:
result = self->builder->cast(self->build_address_of_expression(expr), expr->types.implicit_cast_type)
else:
raw = self->build_expression_without_implicit_cast(expr)
if expr->types.orig_type == NULL and expr->types.implicit_cast_type == NULL:
# Function/method call that returns no value
assert expr->kind == AstExpressionKind.Call
result = EitherBuilderValue{}
else:
assert expr->types.orig_type != NULL
assert expr->types.implicit_cast_type != NULL
result = self->builder->cast(raw, expr->types.implicit_cast_type)
self->set_location(old_location)
return result
def build_address_of_expression(self, expr: AstExpression*) -> EitherBuilderValue:
old_location = self->set_location(expr->location)
match expr->kind:
case AstExpressionKind.GetClassField:
if expr->class_field.uses_arrow_operator:
# &ptr->field = ptr + memory offset
ptr = self->build_expression(expr->class_field.instance)
else:
# &obj.field = &obj + memory offset
ptr = self->build_address_of_expression(expr->class_field.instance)
result = self->builder->class_field_pointer(ptr, expr->class_field.field_name)
case AstExpressionKind.Self:
result = self->local_var_ptr("self")
case AstExpressionKind.GetVariable:
if self->local_var_exists(expr->varname):
result = self->local_var_ptr(expr->varname)
else:
result = self->builder->global_var_ptr(expr->varname, expr->types.orig_type)
case AstExpressionKind.Indexing:
# &ptr[index] = ptr + memory offset
ptr = self->build_expression(&expr->operands[0])
index = self->build_expression(&expr->operands[1])
result = self->builder->indexed_pointer(ptr, index)
case AstExpressionKind.Dereference:
# &*ptr = ptr
result = self->build_expression(&expr->operands[0])
case _:
assert False
self->set_location(old_location)
return result
def build_if_statement(self, ifst: AstIfStatement*) -> None:
done = self->builder->add_block()
for i = 0; i < ifst->n_if_and_elifs; i++:
cond = self->build_expression(&ifst->if_and_elifs[i].condition)
then = self->builder->add_block()
otherwise = self->builder->add_block()
self->builder->branch(cond, then, otherwise)
self->builder->set_current_block(then)
self->build_body(&ifst->if_and_elifs[i].body)
self->builder->jump(done)
self->builder->set_current_block(otherwise)
self->build_body(&ifst->else_body)
self->builder->jump(done)
self->builder->set_current_block(done)
def build_loop(self, cond: AstExpression*, incr: AstStatement*, body: AstBody*) -> None:
condblock = self->builder->add_block() # evaluate condition and go to bodyblock or doneblock
bodyblock = self->builder->add_block() # run loop body and go to incrblock
incrblock = self->builder->add_block() # run incr and go to condblock
doneblock = self->builder->add_block() # rest of the code goes here
# Start loop from condition
self->builder->jump(condblock)
# Evaluate condition and then jump to loop body or skip to after loop.
self->builder->set_current_block(condblock)
if cond == NULL:
# assume True
self->builder->jump(bodyblock)
else:
self->builder->branch(self->build_expression(cond), bodyblock, doneblock)
# Within loop body, 'break' skips to after loop, 'continue' goes to incr.
self->loops = realloc(self->loops, (self->nloops + 1) * sizeof(self->loops[0]))
assert self->loops != NULL
self->loops[self->nloops++] = Loop{on_break = doneblock, on_continue = incrblock}
# Run loop body. When done, go to incr.
self->builder->set_current_block(bodyblock)
self->build_body(body)
self->builder->jump(incrblock)
# 'break' and 'continue' are not allowed after the loop body.
assert self->nloops > 0
self->nloops--
# Run incr and jump back to condition.
self->builder->set_current_block(incrblock)
if incr != NULL:
self->build_statement(incr)
self->builder->jump(condblock)
# Code after the loop goes to "loop done" part.
self->builder->set_current_block(doneblock)
def build_match_statement(self, match_stmt: AstMatchStatement*) -> None:
match_obj = self->build_expression(&match_stmt->match_obj)
done = self->builder->add_block()
for i = 0; i < match_stmt->ncases; i++:
then = self->builder->add_block()
otherwise = EitherBuilderBlock{} # will be replaced by loop below
for k = 0; k < match_stmt->cases[i].n_case_objs; k++:
case_obj = self->build_expression(&match_stmt->cases[i].case_objs[k])
if match_stmt->func_name[0] == '\0':
cond = self->builder->eq(match_obj, case_obj)
else:
args = [match_obj, case_obj]
func_ret = self->builder->call(&match_stmt->func_signature, args, 2)
zero = self->builder->integer(match_stmt->func_signature.returntype, 0)
cond = self->builder->eq(func_ret, zero)
otherwise = self->builder->add_block()
self->builder->branch(cond, then, otherwise)
self->builder->set_current_block(otherwise)
self->builder->set_current_block(then)
self->build_body(&match_stmt->cases[i].body)
self->builder->jump(done)
self->builder->set_current_block(otherwise)
if match_stmt->case_underscore != NULL:
self->build_body(match_stmt->case_underscore)
if (
match_stmt->case_underscore == NULL
and match_stmt->match_obj.types.implicit_cast_type->kind == TypeKind.Enum
):
# The one corner case where match statement invokes UB:
# - User is matching over an enum
# - All enum members are handled (otherwise error in typecheck)
# - The value stored in the enum is not a valid value of the enum
# - There is no "case _" to catch the invalid value
#
# See also: doc/match.md
self->builder->unreachable()
else:
self->builder->jump(done)
self->builder->set_current_block(done)
def build_assert(self, assert_location: Location, assertion: AstAssertion*) -> None:
condvar = self->build_expression(&assertion->condition)
ok_block = self->builder->add_block()
error_block = self->builder->add_block()
self->builder->branch(condvar, ok_block, error_block)
self->builder->set_current_block(error_block)
argnames: byte[100][3] = ["assertion", "path", "lineno"]
argtypes: Type*[3] = [byteType->pointer_type(), byteType->pointer_type(), intType]
sig = Signature{
name = "_jou_assert_fail",
nargs = 3,
argtypes = argtypes,
argnames = argnames,
takes_varargs = False,
is_noreturn = True,
returntype_location = assert_location,
}
args = [
self->builder->string(assertion->condition_str),
self->builder->string(assert_location.path),
self->builder->integer(intType, assert_location.lineno),
]
self->builder->call(&sig, args, 3)
self->builder->unreachable()
self->builder->set_current_block(ok_block)
def build_statement(self, stmt: AstStatement*) -> None:
old_location = self->set_location(stmt->location)
match stmt->kind:
case AstStatementKind.If:
self->build_if_statement(&stmt->if_statement)
case AstStatementKind.Assert:
self->build_assert(stmt->location, &stmt->assertion)
case AstStatementKind.Pass:
pass
case AstStatementKind.WhileLoop:
self->build_loop(&stmt->while_loop.condition, NULL, &stmt->while_loop.body)
case AstStatementKind.ForLoop:
if stmt->for_loop.init != NULL:
self->build_statement(stmt->for_loop.init)
self->build_loop(
stmt->for_loop.cond, stmt->for_loop.incr, &stmt->for_loop.body)
case AstStatementKind.Match:
self->build_match_statement(&stmt->match_statement)
case AstStatementKind.Break:
assert self->nloops > 0
self->builder->jump(self->loops[self->nloops - 1].on_break)
self->builder->set_current_block(self->builder->add_block()) # for code after 'break', if any
case AstStatementKind.Continue:
assert self->nloops > 0
self->builder->jump(self->loops[self->nloops - 1].on_continue)
self->builder->set_current_block(self->builder->add_block()) # for code after 'continue', if any
case AstStatementKind.Assign:
lhs_ptr = self->build_address_of_expression(&stmt->assignment.target)
rhs = self->build_expression(&stmt->assignment.value)
self->builder->set_ptr(lhs_ptr, rhs)
case AstStatementKind.InPlaceAdd:
self->build_inplace_binop(AstExpressionKind.Add, &stmt->assignment.target, &stmt->assignment.value)
case AstStatementKind.InPlaceSub:
self->build_inplace_binop(AstExpressionKind.Sub, &stmt->assignment.target, &stmt->assignment.value)
case AstStatementKind.InPlaceMul:
self->build_inplace_binop(AstExpressionKind.Mul, &stmt->assignment.target, &stmt->assignment.value)
case AstStatementKind.InPlaceDiv:
self->build_inplace_binop(AstExpressionKind.Div, &stmt->assignment.target, &stmt->assignment.value)
case AstStatementKind.InPlaceMod:
self->build_inplace_binop(AstExpressionKind.Mod, &stmt->assignment.target, &stmt->assignment.value)
case AstStatementKind.Return:
if stmt->return_value != NULL:
r = self->build_expression(stmt->return_value)
self->builder->ret(&r)
else:
self->builder->ret(NULL)
self->builder->set_current_block(self->builder->add_block()) # for code after 'return', if any
case AstStatementKind.DeclareLocalVar:
if stmt->var_declaration.value != NULL:
var_ptr = self->local_var_ptr(stmt->var_declaration.name)
value = self->build_expression(stmt->var_declaration.value)
self->builder->set_ptr(var_ptr, value)
case AstStatementKind.ExpressionStatement:
self->build_expression(&stmt->expression)
case _:
# other statements shouldn't occur inside functions/methods
assert False
self->set_location(old_location)
def build_body(self, body: AstBody*) -> None:
for i = 0; i < body->nstatements; i++:
self->build_statement(&body->statements[i])
@public
def feed_ast_to_builder(func_ast: AstFunctionOrMethod*, func_location: Location, builder: EitherBuilder*) -> None:
public = (
func_ast->public
or func_ast->types.signature.is_main_function()
or func_ast->types.signature.get_self_class() != NULL
)
ast2ir = AstToBuilder{builder = builder}
ast2ir.begin_function(&func_ast->types.signature, func_location, func_ast->types.locals, func_ast->types.nlocals, public)
ast2ir.build_body(&func_ast->body)
builder->end_function()
free(ast2ir.locals)
free(ast2ir.loops)