-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompiler_test.odin
More file actions
158 lines (117 loc) · 2.09 KB
/
Copy pathcompiler_test.odin
File metadata and controls
158 lines (117 loc) · 2.09 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
package compiler
import "core:testing"
import "core:strings"
import "core:bytes"
import "core:os"
import "core:log"
import "base:runtime"
import vmem "core:mem/virtual"
@(test)
for_loop :: proc(t: ^testing.T) {
code := `
{
var x = 0;
for(x < 5){
print(x);
x = x + 1;
}
print(20);
}
`
output := run(code)
expected := `0
1
2
3
4
20
`;
testing.expect_value(t, output, expected)
free_all(context.temp_allocator)
}
@(test)
if_else :: proc(t: ^testing.T) {
code := `
{
var x = 0;
if(x < 5){
print(x);
x = x + 1;
}else{
print(40);
}
print(x);
}
`
output := run(code)
expected := `0
1
`;
testing.expect_value(t, output, expected)
free_all(context.temp_allocator)
}
@(test)
if_else_branch :: proc(t: ^testing.T) {
code := `
{
var x = 6;
if(x < 5){
print(x);
x = x + 1;
}else{
print(40);
}
print(x);
}
`
output := run(code)
expected := `40
6
`;
testing.expect_value(t, output, expected)
free_all(context.temp_allocator)
}
@(test)
else_if :: proc(t: ^testing.T) {
code := `
{
var x = 6;
if(x == 3){
print(41);
}else if (x == 6){
print(42);
}else{
print(43);
}
print(x);
}
`
output := run(code)
expected := `42
6
`;
testing.expect_value(t, output, expected)
free_all(context.temp_allocator)
}
run :: proc(code: string) -> string {
arena :vmem.Arena
err := vmem.arena_init_growing(&arena)
assert(err == nil)
allocator := vmem.arena_allocator(&arena)
context.allocator = allocator
defer free_all()
ast := parse(code, "example.fl")
block_builder := make_block_builder()
compile_node(block_builder, ast)
block := block_build(block_builder)
vm := create_vm_from_block(block)
builder, _ := strings.builder_make(context.temp_allocator)
context.logger.procedure = log_intercept;
context.logger.data = &builder
execute(vm)
return strings.to_string(builder)
}
log_intercept :: proc(data: rawptr, level: runtime.Logger_Level, text: string, options: runtime.Logger_Options, location := #caller_location){
builder := transmute(^strings.Builder)data
strings.write_string(builder, text)
}