Website Summary Files Commits Branches @commit @diff @stats
path: root / JSImpl / test
diff options
context:
space:
mode:
1 files changed, 271 insertions, 1 deletions
-rw-r--r--JSImpl/test/CodeGenerationTests.cpp272
diff --git a/JSImpl/test/CodeGenerationTests.cpp b/JSImpl/test/CodeGenerationTests.cpp
index 4a8ebbe..dd13855 100644
--- a/JSImpl/test/CodeGenerationTests.cpp
+++ b/JSImpl/test/CodeGenerationTests.cpp
@@ -235,4 +235,274 @@ TEST(CodeGen, NestedObjects)
"17: halt\n";
ASSERT_TRUE(asmb == expected);
-} \ No newline at end of file
+}
+
+
+TEST(CodeGen, ObjectGetters)
+{
+ IPLString source = "var a = {c: 3}; var k = a.c.t;\n";
+ 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 r2 3.000000\n"
+ "2: string r3 c\n"
+ "3: set r1 r3 r2\n"
+ "4: mov r0 r1\n"
+ "5: string r5 c\n"
+ "6: get r0 r5 r6\n"
+ "7: string r7 t\n"
+ "8: get r6 r7 r8\n"
+ "9: mov r4 r8\n"
+ "10: pop 9\n"
+ "11: halt\n";
+
+ ASSERT_TRUE(asmb == expected);
+}
+
+TEST(CodeGen, ObjectSetter)
+{
+ IPLString source = "var a = {c: 3}; a.c = 5;\n";
+ 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 7\n"
+ "1: const r2 3.000000\n"
+ "2: string r3 c\n"
+ "3: set r1 r3 r2\n"
+ "4: mov r0 r1\n"
+ "5: const r4 5.000000\n"
+ "6: string r5 c\n"
+ "7: get r0 r5 r6\n"
+ "8: mov r6 r4\n"
+ "9: pop 7\n"
+ "10: halt\n";
+
+ ASSERT_TRUE(asmb == expected);
+}
+
+
+TEST(CodeGen, BasicDoWhile)
+{
+ IPLString source = "var a = 5; do{ a--; }while(a != 0)\n";
+ 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: mov r3 r0\n"
+ "5: sub r0 r0 r2\n"
+ "6: const r4 0.000000\n"
+ "7: neq r5 r0 r4\n"
+ "8: jmpt r5 3\n"
+ "9: pop 6\n"
+ "10: halt\n";
+
+ ASSERT_TRUE(asmb == expected);
+}
+
+
+TEST(CodeGen, BasicWhile)
+{
+ IPLString source = "var a = 5; while(a != 0){ a--; }\n";
+ 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 0.000000\n"
+ "4: neq r3 r0 r2\n"
+ "5: jmpf r3 10\n"
+ "6: const r4 1.000000\n"
+ "7: mov r5 r0\n"
+ "8: sub r0 r0 r4\n"
+ "9: jmp 3\n"
+ "10: pop 6\n"
+ "11: halt\n";
+
+ ASSERT_TRUE(asmb == expected);
+}
+
+TEST(CodeGen, ContinueDoWhile)
+{
+ IPLString source = "var a = 5;\n"
+ "do {\n"
+ " a--;\n"
+ " if(a == 4) continue;\n"
+ "}while(a != 0)\n";
+ 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 8\n"
+ "1: const r1 5.000000\n"
+ "2: mov r0 r1\n"
+ "3: const r2 1.000000\n"
+ "4: mov r3 r0\n"
+ "5: sub r0 r0 r2\n"
+ "6: const r4 4.000000\n"
+ "7: eq r5 r0 r4\n"
+ "8: jmpf r5 10\n"
+ "9: jmp 3\n"
+ "10: const r6 0.000000\n"
+ "11: neq r7 r0 r6\n"
+ "12: jmpt r7 3\n"
+ "13: pop 8\n"
+ "14: halt\n";
+
+ ASSERT_TRUE(asmb == expected);
+}
+
+TEST(CodeGen, BreakDoWhile)
+{
+ IPLString source = "var a = 5;\n"
+ "do {\n"
+ " a--;\n"
+ " if(a == 4) break;\n"
+ "}while(a != 0)\n";
+ 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 8\n"
+ "1: const r1 5.000000\n"
+ "2: mov r0 r1\n"
+ "3: const r2 1.000000\n"
+ "4: mov r3 r0\n"
+ "5: sub r0 r0 r2\n"
+ "6: const r4 4.000000\n"
+ "7: eq r5 r0 r4\n"
+ "8: jmpf r5 10\n"
+ "9: jmp 13\n"
+ "10: const r6 0.000000\n"
+ "11: neq r7 r0 r6\n"
+ "12: jmpt r7 3\n"
+ "13: pop 8\n"
+ "14: halt\n";
+
+ ASSERT_TRUE(asmb == expected);
+}
+
+
+TEST(CodeGen, BreakWhile)
+{
+ IPLString source = "var a = 5;\n"
+ "while(a != 0) {\n"
+ " a--;\n"
+ " if(a == 4) break;\n"
+ "}\n";
+ 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 8\n"
+ "1: const r1 5.000000\n"
+ "2: mov r0 r1\n"
+ "3: const r2 0.000000\n"
+ "4: neq r3 r0 r2\n"
+ "5: jmpf r3 14\n"
+ "6: const r4 1.000000\n"
+ "7: mov r5 r0\n"
+ "8: sub r0 r0 r4\n"
+ "9: const r6 4.000000\n"
+ "10: eq r7 r0 r6\n"
+ "11: jmpf r7 13\n"
+ "12: jmp 14\n"
+ "13: jmp 3\n"
+ "14: pop 8\n"
+ "15: halt\n";
+
+ ASSERT_TRUE(asmb == expected);
+}
+
+TEST(CodeGen, ContinueWhile)
+{
+ IPLString source = "var a = 5;\n"
+ "while(a != 0) {\n"
+ " a--;\n"
+ " if(a == 4) continue;\n"
+ "}\n";
+ 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 8\n"
+ "1: const r1 5.000000\n"
+ "2: mov r0 r1\n"
+ "3: const r2 0.000000\n"
+ "4: neq r3 r0 r2\n"
+ "5: jmpf r3 14\n"
+ "6: const r4 1.000000\n"
+ "7: mov r5 r0\n"
+ "8: sub r0 r0 r4\n"
+ "9: const r6 4.000000\n"
+ "10: eq r7 r0 r6\n"
+ "11: jmpf r7 13\n"
+ "12: jmp 3\n"
+ "13: jmp 3\n"
+ "14: pop 8\n"
+ "15: halt\n";
+
+ ASSERT_TRUE(asmb == expected);
+}
+
+TEST(CodeGen, ContinueFor)
+{
+ IPLString source = "for (var i = 0; i < 5; i++ ){ if(i == 1) continue; }\n";
+ 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 8\n"
+ "1: const r1 0.000000\n"
+ "2: mov r0 r1\n"
+ "3: const r2 5.000000\n"
+ "4: less r3 r0 r2\n"
+ "5: jmpf r3 14\n"
+ "6: const r4 1.000000\n"
+ "7: eq r5 r0 r4\n"
+ "8: jmpf r5 10\n"
+ "9: jmp 10\n"
+ "10: const r6 1.000000\n"
+ "11: mov r7 r0\n"
+ "12: add r0 r0 r6\n"
+ "13: jmp 3\n"
+ "14: pop 8\n"
+ "15: halt\n";
+
+ ASSERT_TRUE(asmb == expected);
+}
+
+TEST(CodeGen, BreakFor)
+{
+ IPLString source = "for (var i = 0; i < 5; i++ ){ if(i == 1) break; }\n";
+ 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 8\n"
+ "1: const r1 0.000000\n"
+ "2: mov r0 r1\n"
+ "3: const r2 5.000000\n"
+ "4: less r3 r0 r2\n"
+ "5: jmpf r3 14\n"
+ "6: const r4 1.000000\n"
+ "7: eq r5 r0 r4\n"
+ "8: jmpf r5 10\n"
+ "9: jmp 14\n"
+ "10: const r6 1.000000\n"
+ "11: mov r7 r0\n"
+ "12: add r0 r0 r6\n"
+ "13: jmp 3\n"
+ "14: pop 8\n"
+ "15: halt\n";
+
+ ASSERT_TRUE(asmb == expected);
+}