Website Summary Files Commits Branches @commit @diff @stats
path: root / JSImpl / test / CodeGenerationTests.cpp
blob: a6ebc6eafdfdcff479a351cbe1fa0d4e24f1afad (plain) (blame)
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 <src/CommonTypes.h>
#include <src/Lexer.h>
#include <src/Parser.h>
#include "src/ByteCodeGenerator.h"

#include <gtest/gtest.h>

#include <sstream>

TEST(CodeGen, Empty)
{
	IPLString source = "";
	IPLVector<Token> tokens = Tokenize(source.c_str()).tokens;
	auto ast = Parse(tokens);
	auto asmb = GenerateByteCode(ast, source,
		ByteCodeGeneratorOptions(ByteCodeGeneratorOptions::OptimizationsType::None, false));
	IPLString expected = "0: push 0\n"
						 "1: pop 0\n"
						 "2: halt\n";
	ASSERT_TRUE(asmb == expected);
}

TEST(CodeGen, VarDeclations)
{
	IPLString source = "var a;";
	IPLVector<Token> tokens = Tokenize(source.c_str()).tokens;
	auto ast = Parse(tokens);
	auto asmb = GenerateByteCode(ast, source,
		ByteCodeGeneratorOptions(ByteCodeGeneratorOptions::OptimizationsType::None, false));
	IPLString expected = "0: push 1\n"
						 "1: pop 1\n"
						 "2: halt\n";
	ASSERT_TRUE(asmb == expected);
}

TEST(CodeGen, VarDeclationsWithValue)
{
	IPLString source = "var a = 5;";
	IPLVector<Token> tokens = Tokenize(source.c_str()).tokens;
	auto ast = Parse(tokens);
	auto asmb = GenerateByteCode(ast, source,
		ByteCodeGeneratorOptions(ByteCodeGeneratorOptions::OptimizationsType::None, false));
	IPLString expected = "0: push 2\n"
						 "1: const r1 5.000000\n"
						 "2: mov r0 r1\n"
						 "3: pop 2\n"
						 "4: halt\n";
	ASSERT_TRUE(asmb == expected);
}

TEST(CodeGen, VarDeclationsBinaryExpre)
{
	IPLString source = "var a = 5 + 6;";
	IPLVector<Token> tokens = Tokenize(source.c_str()).tokens;
	auto ast = Parse(tokens);
	auto asmb = GenerateByteCode(ast, source,
		ByteCodeGeneratorOptions(ByteCodeGeneratorOptions::OptimizationsType::None, false));
	IPLString expected = "0: push 4\n"
						 "1: const r1 6.000000\n"
						 "2: const r2 5.000000\n"
						 "3: add r3 r2 r1\n"
						 "4: mov r0 r3\n"
						 "5: pop 4\n"
						 "6: halt\n";

	ASSERT_TRUE(asmb == expected);
}

TEST(CodeGen, VariableAssignment)
{
	IPLString source = "var a; a = 5;";
	IPLVector<Token> tokens = Tokenize(source.c_str()).tokens;
	auto ast = Parse(tokens);
	auto asmb = GenerateByteCode(ast, source,
		ByteCodeGeneratorOptions(ByteCodeGeneratorOptions::OptimizationsType::None, false));
	IPLString expected = "0: push 2\n"
						 "1: const r1 5.000000\n"
						 "2: mov r0 r1\n"
						 "3: pop 2\n"
						 "4: halt\n";

	ASSERT_TRUE(asmb == expected);
}

TEST(CodeGen, MultiVariableAssignment)
{
	IPLString source = "var a; var b = 6; a = b;";
	IPLVector<Token> tokens = Tokenize(source.c_str()).tokens;
	auto ast = Parse(tokens);
	auto asmb = GenerateByteCode(ast, source,
		ByteCodeGeneratorOptions(ByteCodeGeneratorOptions::OptimizationsType::None, false));
	IPLString expected = "0: push 3\n"
						 "1: const r2 6.000000\n"
						 "2: mov r1 r2\n"
						 "3: mov r0 r1\n"
						 "4: pop 3\n"
						 "5: halt\n";

	ASSERT_TRUE(asmb == expected);
}

TEST(CodeGen, SimpleIf)
{
	IPLString source = "var a = 5; if (a < 1) { a = 7}";
	IPLVector<Token> tokens = Tokenize(source.c_str()).tokens;
	auto ast = Parse(tokens);
	auto asmb = GenerateByteCode(ast, source,
		ByteCodeGeneratorOptions(ByteCodeGeneratorOptions::OptimizationsType::None, false));
	IPLString expected = "0: push 5\n"
						 "1: const r1 5.000000\n"
						 "2: mov r0 r1\n"
						 "3: const r2 1.000000\n"
						 "4: less r3 r0 r2\n"
						 "5: jmpf r3 8\n"
						 "6: const r4 7.000000\n"
						 "7: mov r0 r4\n"
						 "8: pop 5\n"
						 "9: halt\n";

	ASSERT_TRUE(asmb == expected);
}

TEST(CodeGen, SimpleIfElse)
{
	IPLString source = "var a = 5; if (a < 1) { a = 3; } else { a = 7; }";
	IPLVector<Token> tokens = Tokenize(source.c_str()).tokens;
	auto ast = Parse(tokens);
	auto asmb = GenerateByteCode(ast, source,
		ByteCodeGeneratorOptions(ByteCodeGeneratorOptions::OptimizationsType::None, false));
	IPLString expected = "0: push 6\n"
						 "1: const r1 5.000000\n"
						 "2: mov r0 r1\n"
						 "3: const r2 1.000000\n"
						 "4: less r3 r0 r2\n"
						 "5: jmpf r3 9\n"
						 "6: const r4 3.000000\n"
						 "7: mov r0 r4\n"
						 "8: jmp 11\n"
						 "9: const r5 7.000000\n"
						 "10: mov r0 r5\n"
						 "11: pop 6\n"
						 "12: halt\n";

	ASSERT_TRUE(asmb == expected);
}

TEST(CodeGen, SimpleFor)
{
	IPLString source = "var a = 0; for (var i = 0; i < 5; i++ ){ a =  a + i; }";
	IPLVector<Token> tokens = Tokenize(source.c_str()).tokens;
	auto ast = Parse(tokens);
	auto asmb = GenerateByteCode(ast, source,
		ByteCodeGeneratorOptions(ByteCodeGeneratorOptions::OptimizationsType::None, false));
	IPLString expected = "0: push 9\n"
						 "1: const r1 0.000000\n"
						 "2: mov r0 r1\n"
						 "3: const r3 0.000000\n"
						 "4: mov r2 r3\n"
						 "5: const r4 5.000000\n"
						 "6: less r5 r2 r4\n"
						 "7: jmpf r5 14\n"
						 "8: add r6 r0 r2\n"
						 "9: mov r0 r6\n"
						 "10: const r7 1.000000\n"
						 "11: mov r8 r2\n"
						 "12: add r2 r2 r7\n"
						 "13: jmp 5\n"
						 "14: pop 9\n"
						 "15: halt\n";

	ASSERT_TRUE(asmb == expected);
}