-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathexample_bytecode.bt
More file actions
59 lines (50 loc) · 1.63 KB
/
example_bytecode.bt
File metadata and controls
59 lines (50 loc) · 1.63 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
create a ; add 'a' variable to the list
create b ; add 'b' variable to the list
; this tells the interpreter to make a new variable
push a, 55 ; push the value 55 into 'a'
push b, 55 ; push the value 55 into 'b'
; can push both literals and variables
; haven't decided whether this should be by reference or copy
; although 'push' would suggest moving into the variable
; maybe we should have a 'ref' ('reference', 'copy') mnemonic
add a, a, b ; add the variables 'a', 'b', and store it in 'a'
; operators are all in the form 'op ret, a, b'
; except for unary operators they are 'op ret, var'
call c, [a, b]; call the function 'c' with the arguments 'a' and 'b'
; define 'identifier' as a function
func a [arg0, arg1, arg2]
create result
gt result, arg0, arg2
jez result, <location>
ret arg1
<location>
ret arg0; returns null
; generation of if statements
; if ( condition )
; is turned into
; create "if line number file"
; cmp "if line number file", condition, 0
; jnz condition "if line number label"
; --- this is the false branch
; jump "if line number file end"
; "if line number label":
; --- this is the true branch
; jump "if line number file end"
; --- this is the end of the if condition
;
; this means in reality the function above looks something like this
func a [arg0, arg1, arg2]
create result
gt result, arg0, arg2
jnz result, <true>
ret arg1
jmp <end>
<true>
ret arg0
jmp <end>
;
; in python it would be If(name, condition, true_block, false_block)
; a function will check through this and convert it into offsets
; (also happens at top level)
; [byte]
; [mnemonic][count][args]....