@@ -12,103 +12,81 @@ Test(compiler, should_compile_expressions)
1212{
1313 Chunk chunk ;
1414 init_chunk (& chunk );
15-
16- char * source = "3 + 2" ;
17-
18- bool result = compile (source , & chunk );
19-
20- uint8_t expected_bytes [] = {OP_CONSTANT , 0 , OP_CONSTANT ,
21- 1 , OP_ADD , OP_RETURN };
15+ char * source = "print 3 + 2;" ;
16+ bool result = compile (source , & chunk );
17+ uint8_t expected_bytes [] = {OP_CONSTANT , 0 , OP_CONSTANT , 1 ,
18+ OP_ADD , OP_PRINT , OP_RETURN };
2219 double expected_constants [] = {3 , 2 };
23-
2420 cr_assert_eq (result , true);
25- assert_bytecode (& chunk , expected_bytes , 6 );
21+ assert_bytecode (& chunk , expected_bytes , 7 );
2622 assert_constants (& chunk , expected_constants , 2 );
2723}
2824
2925Test (compiler , should_start_with_multiplication )
3026{
3127 Chunk chunk ;
3228 init_chunk (& chunk );
33-
34- char * source = "3 + 2 * 9" ;
35-
36- bool result = compile (source , & chunk );
37-
38- uint8_t expected_bytes [] = {OP_CONSTANT , 0 , OP_CONSTANT ,
39- 1 , OP_CONSTANT , 2 ,
40- OP_MULTIPLY , OP_ADD , OP_RETURN };
29+ char * source = "print 3 + 2 * 9;" ;
30+ bool result = compile (source , & chunk );
31+ uint8_t expected_bytes [] = {OP_CONSTANT , 0 , OP_CONSTANT , 1 ,
32+ OP_CONSTANT , 2 , OP_MULTIPLY , OP_ADD ,
33+ OP_PRINT , OP_RETURN };
4134 double expected_constants [] = {3 , 2 , 9 };
42-
4335 cr_assert_eq (result , true);
44- assert_bytecode (& chunk , expected_bytes , 9 );
36+ assert_bytecode (& chunk , expected_bytes , 10 );
4537 assert_constants (& chunk , expected_constants , 3 );
4638}
4739
4840Test (compiler , should_respect_grouping )
4941{
5042 Chunk chunk ;
5143 init_chunk (& chunk );
52-
53- char * source = "3 + 2 * (9 + 3)" ;
54-
55- bool result = compile (source , & chunk );
56-
57- uint8_t expected_bytes [] = {
58- OP_CONSTANT , 0 , OP_CONSTANT , 1 , OP_CONSTANT , 2 ,
59- OP_CONSTANT , 3 , OP_ADD , OP_MULTIPLY , OP_ADD , OP_RETURN };
60-
61- double expected_constants [] = {3 , 2 , 9 , 3 };
62-
44+ char * source = "print 3 + 2 * (9 + 3);" ;
45+ bool result = compile (source , & chunk );
46+ uint8_t expected_bytes [] = {OP_CONSTANT , 0 , OP_CONSTANT , 1 ,
47+ OP_CONSTANT , 2 , OP_CONSTANT , 3 ,
48+ OP_ADD , OP_MULTIPLY , OP_ADD , OP_PRINT ,
49+ OP_RETURN };
50+ double expected_constants [] = {3 , 2 , 9 , 3 };
6351 cr_assert_eq (result , true);
64- assert_bytecode (& chunk , expected_bytes , 12 );
52+ assert_bytecode (& chunk , expected_bytes , 13 );
6553 assert_constants (& chunk , expected_constants , 4 );
6654}
6755
6856Test (compiler , should_compile_nil )
6957{
7058 Chunk chunk ;
7159 init_chunk (& chunk );
72-
73- char * source = "nil" ;
74-
75- bool result = compile (source , & chunk );
76-
60+ char * source = "print nil;" ;
61+ bool result = compile (source , & chunk );
7762 cr_assert_eq (result , true);
63+ assert_bytecode (& chunk , (uint8_t []){OP_NIL , OP_PRINT , OP_RETURN }, 3 );
7864}
7965
8066Test (compiler , should_compile_booleans )
8167{
8268 Chunk chunk ;
8369 init_chunk (& chunk );
84-
85- char * source = "true" ;
86-
87- bool result = compile (source , & chunk );
88-
70+ char * source = "print true;" ;
71+ bool result = compile (source , & chunk );
8972 cr_assert_eq (result , true);
90- assert_bytecode (& chunk , (uint8_t []){OP_TRUE , OP_RETURN }, 2 );
73+ assert_bytecode (& chunk , (uint8_t []){OP_TRUE , OP_PRINT , OP_RETURN }, 3 );
9174}
9275
9376Test (compiler , should_compile_equality_expressions )
9477{
9578 Chunk chunk ;
9679 init_chunk (& chunk );
97-
98- char * source = "12 == 6 * 2" ;
99-
100- bool result = compile (source , & chunk );
101-
102- uint8_t expected_bytecodes [] = {OP_CONSTANT , 0 , OP_CONSTANT ,
103- 1 , OP_CONSTANT , 2 ,
104- OP_MULTIPLY , OP_EQUAL , OP_RETURN };
105- double expected_constants [] = {12 , 6 , 2 };
106-
80+ char * source = "print 12 == 6 * 2;" ;
81+ bool result = compile (source , & chunk );
82+ uint8_t expected_bytecodes [] = {
83+ OP_CONSTANT , 0 , OP_CONSTANT , 1 , OP_CONSTANT ,
84+ 2 , OP_MULTIPLY , OP_EQUAL , OP_PRINT , OP_RETURN };
85+ double expected_constants [] = {12 , 6 , 2 };
10786 cr_assert_eq (result , true);
108- assert_bytecode (& chunk , expected_bytecodes , 9 );
87+ assert_bytecode (& chunk , expected_bytecodes , 10 );
10988 assert_constants (& chunk , expected_constants , 3 );
11089}
111-
11290static void assert_bytecode (Chunk * chunk , const uint8_t * expected , int count )
11391{
11492 cr_assert_eq (chunk -> count , count );
0 commit comments