Skip to content

Commit eb25ad4

Browse files
committed
feature: Add example program.
bugfix: Fix array assignment + argument register sizes.
1 parent 8db0d50 commit eb25ad4

5 files changed

Lines changed: 72 additions & 6 deletions

File tree

examples/prime.bl

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Demo program that calculates prime numbers
2+
#
3+
# Compile:
4+
# blangc prime.bl prime --ld_flags "-dynamic-linker /lib64/ld-linux-x86-64.so.2 -lc"
5+
#
6+
7+
external def printf(fmt: <u8>, ...): u32
8+
external stdout : <u8>
9+
external def fflush(out: <u8>) : u32
10+
11+
how_many:u32 = 100
12+
primes: u32[100] # we can't say [how_many] because
13+
# consts dont exist yet
14+
count: u32 = 0
15+
16+
def is_prime(n: u32): u8 {
17+
v:u32 = n
18+
if n < 2:
19+
return 0
20+
21+
i: u32 = 0
22+
23+
# blocks can be indented or embraced
24+
for 0..count as i:u32:
25+
if primes[i] * primes[i] > n:
26+
break
27+
if v % primes[i] == 0:
28+
return 0
29+
30+
return 1
31+
}
32+
33+
def main(): u8 {
34+
n: u32 = 2
35+
36+
while count <= how_many {
37+
if is_prime(n) {
38+
primes[count] = n
39+
count = count + 1
40+
}
41+
# statements can be terminated with ;, or if not ambiguous
42+
# then left unterminated
43+
n = n + 1;
44+
}
45+
46+
printf("First %d Prime Numbers\n", how_many);
47+
for 0..count as i:u32:
48+
printf("- %d is prime\n", primes[i])
49+
fflush(stdout)
50+
51+
return 0
52+
}

src/blang/compiler/compiler.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def import_node_to_filepath(node, imported_from: Path):
146146
def compile_import(node, context: Context) -> str:
147147
asm = []
148148
filename = import_node_to_filepath(node, context.filename)
149-
print(filename)
149+
150150
# open the file, read it, parse it,
151151
module = parse_file(filename)
152152

@@ -448,7 +448,6 @@ def compile_block(node, context: Context):
448448
asm = []
449449
context.new_frame()
450450
for child in node.children:
451-
print(child.type)
452451
code, registers = compile(child, context)
453452
asm.extend(code)
454453
for reg in registers:
@@ -480,7 +479,7 @@ def ensure_is_a_register(code, maybe_reg, context):
480479
reg = context.take_a_register()
481480
reg.type = maybe_reg.type
482481
reg.indirection_count = maybe_reg.indirection_count
483-
code.append(f"mov {reg.full_reg}, {maybe_reg}")
482+
code.append(f"mov {reg}, {maybe_reg}")
484483
return reg
485484

486485

@@ -842,6 +841,7 @@ def compile_assignment(node, context: Context):
842841
sizespec = SizeSpecifiers[identifier.size]
843842

844843
asm = [
844+
*asm,
845845
*code,
846846
f"mov {sizespec} [{target_address}], {reg} ; {identifier}[...]=... ", # assign the value
847847
]
@@ -1436,5 +1436,5 @@ def compiler(file, debug=False):
14361436

14371437
if not module:
14381438
return None
1439-
print_tree(module)
1439+
14401440
return compile(module, Context(filename=file))[0]

test/test_array_set.bl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ def main(): u32 {
77
i:u32 = 0
88
local_array[0] = 20
99
glob_array[0] = 7
10+
11+
i=3
12+
local_array[i]=99
13+
if local_array[3]!=99:
14+
return 1
1015

1116
return local_array[0]+glob_array[0]
1217
}

test/test_divide.bl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ def print_number(number: u64) :u8{
6161
return 0;
6262
}
6363

64+
def pass_u32(in:u32):u32:
65+
other:u32 = in
66+
return other
6467

6568
def main(): u8 {
6669
a:u64 = 19302
@@ -95,5 +98,11 @@ def main(): u8 {
9598
if i32|-10| / i32|5| != -2 { return 2}
9699

97100
if i32|10| / i32|-5| != -2 { return 3}
98-
return 0
101+
102+
103+
if 5 % 3 != 2:
104+
return 4
105+
106+
if pass_u32(99) != 99:
107+
return 5
99108
}

test/test_printf.bl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# expected_exit_code = 0
22
# ***ld_flag*** -lc
3-
# ***ld_flag*** -dynamic-linker /lib64/ld-linux-x86-64.so.2
3+
# ***ld_flag*** -dynamic-linker /lib64/ld-linux-x86-64.so.2
44
# ***expected output***
55
# ****
66
# The value is 272727

0 commit comments

Comments
 (0)