@@ -84,6 +84,7 @@ bool ps_ast_test_delete_block_program(ps_ast_block *block_program)
8484 ps_ast_debug_line ("Free the PROGRAM block node" );
8585 block_program = (ps_ast_block * )ps_ast_free_block (block_program );
8686 ASSERT_RETURN_FALSE (block_program == NULL );
87+ return true;
8788}
8889
8990ps_interpreter * ps_ast_test_create_interpreter (ps_ast_block * block_program )
@@ -114,6 +115,7 @@ bool ps_ast_test_delete_interpreter(ps_interpreter *interpreter, ps_ast_block *b
114115 ps_ast_debug_line ("Free interpreter" );
115116 interpreter = ps_interpreter_free (interpreter );
116117 ASSERT_RETURN_FALSE (interpreter == NULL );
118+ return true;
117119}
118120
119121/**
@@ -157,9 +159,10 @@ bool ps_ast_test_minimal()
157159 * @brief Test Assignment Pascal program
158160 * L/C 123456789012345678901234567890123456789012345678901234567890
159161 * 1 Program Assignment;
160- * 2 Var I: Integer;
162+ * 2 Var I, J : Integer;
161163 * 3 Begin
162- * 4 I := 42 * 2;
164+ * 4 I := 21 * 2;
165+ * 4 J := I;
163166 * 5 End.
164167 */
165168bool ps_ast_test_assignment ()
@@ -173,37 +176,50 @@ bool ps_ast_test_assignment()
173176 ps_interpreter * interpreter = ps_ast_test_create_interpreter (block_program );
174177 ASSERT_RETURN_FALSE (interpreter != NULL );
175178
176- ps_ast_debug_line ("Create a variable symbol I of type Integer and add it to the symbol tables" );
177- ps_value i_value = {.allocated = false, .type = & ps_system_integer , .data .i = 0 };
178- const ps_symbol * symbol_i = ps_symbol_alloc (PS_SYMBOL_KIND_VARIABLE , "I" , & i_value );
179- result = ps_interpreter_add_symbol (interpreter , (ps_symbol * )symbol_i );
179+ ps_ast_debug_line ("Create variable symbols I ² J of type Integer and add them to the symbol tables" );
180+ ps_value value_i = {.allocated = false, .type = & ps_system_integer , .data .i = 0 };
181+ ps_symbol * symbol_i = ps_symbol_alloc (PS_SYMBOL_KIND_VARIABLE , "I" , & value_i );
182+ result = ps_interpreter_add_symbol (interpreter , symbol_i );
183+ ASSERT_RETURN_FALSE (result );
184+ ps_value value_j = {.allocated = false, .type = & ps_system_integer , .data .i = 0 };
185+ ps_symbol * symbol_j = ps_symbol_alloc (PS_SYMBOL_KIND_VARIABLE , "J" , & value_j );
186+ result = ps_interpreter_add_symbol (interpreter , symbol_j );
180187 ASSERT_RETURN_FALSE (result );
181- ps_symbol_table_error error = ps_symbol_table_add (block_program -> symbols , ( ps_symbol * ) symbol_i );
188+ ps_symbol_table_error error = ps_symbol_table_add (block_program -> symbols , symbol_i );
182189 ASSERT_RETURN_FALSE (error == PS_SYMBOL_TABLE_ERROR_NONE );
183- block_program -> n_vars = 1 ;
190+ error = ps_symbol_table_add (block_program -> symbols , symbol_j );
191+ ASSERT_RETURN_FALSE (error == PS_SYMBOL_TABLE_ERROR_NONE );
192+ block_program -> n_vars = 2 ;
184193
185- ps_ast_debug_line ("Create a statement list with one statement" );
186- // a PROCEDURE CALL to Writeln with one argument: a string value "Hello, World!"
187- block_program -> statement_list = ps_ast_create_statement_list (3 , 5 , 1 );
194+ ps_ast_debug_line ("Create a statement list with 2 statements" );
195+ block_program -> statement_list = ps_ast_create_statement_list (3 , 5 , 2 );
188196 ASSERT_RETURN_FALSE (block_program -> statement_list != NULL );
189197
190- ps_ast_debug_line ("Create the assignment statement I := 42 * 2;" );
191- ps_ast_variable_simple * variable_simple =
192- ps_ast_create_variable_simple (3 , 5 , PS_AST_LVALUE_SIMPLE , (ps_symbol * )symbol_i );
193- ASSERT_RETURN_FALSE (variable_simple != NULL );
194- ps_value value_u_42 = {.allocated = false, .type = & ps_system_unsigned , .data .u = 42 };
195- ps_ast_value * rvalue_u_42 = ps_ast_create_rvalue_const (3 , 10 , value_u_42 );
196- ASSERT_RETURN_FALSE (rvalue_u_42 != NULL );
198+ ps_ast_debug_line ("Create the assignment statement I := 21 * 2;" );
199+ ps_ast_variable_simple * variable_i = ps_ast_create_variable_simple (3 , 5 , PS_AST_LVALUE_SIMPLE , symbol_i );
200+ ASSERT_RETURN_FALSE (variable_i != NULL );
201+ ps_value value_u_21 = {.allocated = false, .type = & ps_system_unsigned , .data .u = 21 };
202+ ps_ast_value * rvalue_u_21 = ps_ast_create_rvalue_const (3 , 10 , value_u_21 );
203+ ASSERT_RETURN_FALSE (rvalue_u_21 != NULL );
197204 ps_value value_u_2 = {.allocated = false, .type = & ps_system_unsigned , .data .u = 2 };
198205 ps_ast_value * rvalue_u_2 = ps_ast_create_rvalue_const (3 , 15 , value_u_2 );
199206 ASSERT_RETURN_FALSE (rvalue_u_2 != NULL );
200207 ps_ast_binary_operation * mul_operation =
201- ps_ast_create_binary_operation (3 , 13 , PS_OP_MUL , (ps_ast_node * )rvalue_u_42 , (ps_ast_node * )rvalue_u_2 );
208+ ps_ast_create_binary_operation (3 , 13 , PS_OP_MUL , (ps_ast_node * )rvalue_u_21 , (ps_ast_node * )rvalue_u_2 );
202209 ASSERT_RETURN_FALSE (mul_operation != NULL );
203- ps_ast_assignment * assignment =
204- ps_ast_create_assignment (3 , 5 , (ps_ast_node * )variable_simple , (ps_ast_node * )mul_operation );
205- ASSERT_RETURN_FALSE (assignment != NULL );
206- block_program -> statement_list -> statements [0 ] = (ps_ast_node * )assignment ;
210+ ps_ast_assignment * assignment_i =
211+ ps_ast_create_assignment (3 , 5 , (ps_ast_node * )variable_i , (ps_ast_node * )mul_operation );
212+ ASSERT_RETURN_FALSE (assignment_i != NULL );
213+ block_program -> statement_list -> statements [0 ] = (ps_ast_node * )assignment_i ;
214+
215+ ps_ast_debug_line ("Create the assignment statement J := I;" );
216+ ps_ast_variable_simple * variable_j = ps_ast_create_variable_simple (4 , 5 , PS_AST_LVALUE_SIMPLE , symbol_j );
217+ ASSERT_RETURN_FALSE (variable_j != NULL );
218+ ps_ast_variable_simple * rvalue_i = ps_ast_create_variable_simple (4 , 10 , PS_AST_RVALUE_SIMPLE , symbol_i );
219+ ps_ast_assignment * assignment_j =
220+ ps_ast_create_assignment (4 , 5 , (ps_ast_node * )variable_j , (ps_ast_node * )rvalue_i );
221+ ASSERT_RETURN_FALSE (assignment_i != NULL );
222+ block_program -> statement_list -> statements [1 ] = (ps_ast_node * )assignment_j ;
207223
208224 ps_ast_debug_line ("Debug print the program" );
209225 ps_ast_debug = true;
@@ -217,11 +233,19 @@ bool ps_ast_test_assignment()
217233 ps_ast_debug_line ("Interpreter message: %s" , interpreter -> message );
218234 ASSERT_RETURN_FALSE (result );
219235
220- ps_ast_debug_line ("Check that variable I has the expected value 84 " );
236+ ps_ast_debug_line ("Check that variable I has the expected value 42 " );
221237 ASSERT_RETURN_FALSE (symbol_i -> value != NULL );
222238 ASSERT_RETURN_FALSE (symbol_i -> value -> type == & ps_system_integer );
223239 ps_ast_debug_line ("Variable I value: %d" , symbol_i -> value -> data .i );
224- ASSERT_RETURN_FALSE (symbol_i -> value -> data .i == 84 );
240+ ASSERT_RETURN_FALSE (symbol_i -> value -> data .i == 42 );
241+
242+ ps_ast_debug_line ("Check that variable J has the expected value 42" );
243+ ASSERT_RETURN_FALSE (symbol_j -> value != NULL );
244+ ASSERT_RETURN_FALSE (symbol_j -> value -> type == & ps_system_integer );
245+ ps_ast_debug_line ("Variable J value: %d" , symbol_i -> value -> data .i );
246+ ASSERT_RETURN_FALSE (symbol_j -> value -> data .i == 42 );
247+
248+ ps_symbol_table_dump (stderr , NULL , block_program -> symbols );
225249
226250 ps_ast_debug_line ("Free symbol table for the program %s" , block_program -> name );
227251 ps_memory_free (PS_MEMORY_AST , block_program -> symbols );
0 commit comments