-
Notifications
You must be signed in to change notification settings - Fork 0
/
codegen2_test.cc
172 lines (133 loc) · 3.89 KB
/
codegen2_test.cc
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
#include <gtest/gtest.h>
#include "carl/parser.h"
#include "carl/jit2/carljit.h"
#include "carl/jit2/codegen2.h"
#include "carl/ast/ast_printer.h"
#include "carl/jit2/runtime_types.h"
using namespace carl;
namespace {
TEST(codegen2, adt_create_basic) {
CarlJIT jit;
Parser p;
AstPrinter printer(std::cout);
Codegen2 cg;
cg.init("main");
std::string src =
"data Tree = Leaf | Node (int, Tree, Tree);"
"let tree = Node ( Leaf(), 1, Node ( Leaf(), 2, Leaf() ));"
"__debug(tree);";
ParseResult decls = p.parse_r(src);
Codegen2Module module = cg.generate(*decls);
jit.load_module(module);
auto __main = jit.lookup_ea("__carl_main")->toPtr<uint64_t()>();
__main();
// TODO: add actual checking of created instances.
ASSERT_TRUE(false);
}
TEST(codegen2, fn_mania) {
CarlJIT jit;
Parser p;
AstPrinter printer(std::cout);
Codegen2 cg;
cg.init("main");
std::string src = ""
"let two = 2;"
"fn foo(a: int): (:int) {"
" let one = 1;"
" fn bar() : int {"
" return a + one + two;"
" }"
" return bar;"
"}"
"let bar_1 = foo(1);"
"let bar_2 = foo(2);"
"return bar_1() + bar_2();";
auto decls = p.parse_r(src, false);
auto module = cg.generate(*decls);
jit.load_module(module);
auto __main = jit.lookup_ea("__carl_main")->toPtr<uint64_t()>();
uint64_t result = __main();
ASSERT_EQ(result, 4 + 5);
}
TEST(codegen2, string_fndecl_with_capture) {
CarlJIT jit;
Parser p;
AstPrinter printer(std::cout);
Codegen2 cg;
cg.init("main");
std::string src = ""
"let one = 1;"
"let two = 2;"
"fn foo(a: int, b : int): int {"
" return a + one + two + b;"
"}"
"one = one + 1;"
"return foo(39, 0);";
auto decls = p.parse_r(src, false);
auto module = cg.generate(*decls);
jit.load_module(module);
auto __main = jit.lookup_ea("__carl_main")->toPtr<uint64_t()>();
uint64_t result = __main();
ASSERT_EQ(result, 42);
}
TEST(codegen2, string_letdecl) {
CarlJIT jit;
Parser p;
AstPrinter printer(std::cout);
Codegen2 cg;
cg.init("main");
std::string src = ""
"let a = 40;"
"let b = 2;"
"return a + b;";
auto decls = p.parse_r(src, false);
auto module = cg.generate(*decls);
jit.load_module(module);
auto __main = jit.lookup_ea("__carl_main")->toPtr<uint64_t()>();
uint64_t result = __main();
ASSERT_EQ(result, 42);
}
TEST(codegen2, string_concat) {
CarlJIT jit;
Parser p;
AstPrinter printer(std::cout);
Codegen2 cg;
cg.init("main");
std::string src = "return \"hello \" + \"world!\";";
auto decls = p.parse_r(src, false);
auto module = cg.generate(*decls);
jit.load_module(module);
auto __main = jit.lookup_ea("__carl_main")->toPtr<crt_string*()>();
crt_string* result = __main();
ASSERT_STREQ(result->data, "hello world!");
ASSERT_EQ(result->len, 13);
}
TEST(codegen2, string_create) {
CarlJIT jit;
Parser p;
AstPrinter printer(std::cout);
Codegen2 cg;
cg.init("main");
std::string src = "return \"hello world!\";";
auto decls = p.parse_r(src, false);
auto module = cg.generate(*decls);
jit.load_module(module);
auto __main = jit.lookup_ea("__carl_main")->toPtr<crt_string*()>();
crt_string* result = __main();
ASSERT_STREQ(result->data, "hello world!");
ASSERT_EQ(result->len, 13);
}
TEST(codegen2, basic_expression_add) {
CarlJIT jit;
Parser p;
AstPrinter printer(std::cout);
Codegen2 cg;
cg.init("main");
std::string src = "return 1 + 2;";
auto decls = p.parse_r(src, false);
auto module = cg.generate(*decls);
jit.load_module(module);
auto __main = jit.lookup_ea("__carl_main")->toPtr<uint64_t()>();
ASSERT_EQ(__main(), 3);
}
}