#include "../lexer/lexer.h" #include "../parser/parser.h" #include "../semantic/semantic.h" #include "../ir/ir.h" #include "../ir/ir_gen.h" #include "../runtime/runtime.h" #include #include #include static int test_count = 0; static int pass_count = 0; static void run_test_int(const char* name, const char* source, int expected) { test_count++; RavaLexer_t* lexer = rava_lexer_create(source); RavaParser_t* parser = rava_parser_create(lexer); RavaASTNode_t* ast = rava_parser_parse(parser); if (!ast || parser->had_error) { printf("FAIL: %s - Parse error: %s\n", name, parser->error_message ? parser->error_message : "unknown"); rava_parser_destroy(parser); rava_lexer_destroy(lexer); return; } RavaSemanticAnalyzer_t* analyzer = rava_semantic_analyzer_create(); rava_semantic_analyze(analyzer, ast); RavaIRGenerator_t* ir_gen = rava_ir_generator_create(analyzer); RavaProgram_t* program = rava_ir_generate(ir_gen, ast); RavaVM_t* vm = rava_vm_create(program); if (!rava_vm_execute(vm, "Test", "main")) { printf("FAIL: %s - Runtime error: %s\n", name, vm->error_message ? vm->error_message : "unknown"); rava_vm_destroy(vm); rava_program_destroy(program); rava_ir_generator_destroy(ir_gen); rava_semantic_analyzer_destroy(analyzer); rava_ast_node_destroy(ast); rava_parser_destroy(parser); rava_lexer_destroy(lexer); return; } RavaValue_t result = rava_vm_get_result(vm); int32_t result_int = rava_value_as_int(result); if (result_int == expected) { printf("PASS: %s (result=%d)\n", name, result_int); pass_count++; } else { printf("FAIL: %s (expected=%d, got=%d)\n", name, expected, result_int); } rava_vm_destroy(vm); rava_program_destroy(program); rava_ir_generator_destroy(ir_gen); rava_semantic_analyzer_destroy(analyzer); rava_ast_node_destroy(ast); rava_parser_destroy(parser); rava_lexer_destroy(lexer); } int main(void) { printf("=== Super Keyword Tests ===\n\n"); run_test_int("super.method() basic", "class Parent {\n" " int getValue() {\n" " return 10;\n" " }\n" "}\n" "class Child extends Parent {\n" " int getValue() {\n" " return super.getValue() + 5;\n" " }\n" "}\n" "public class Test {\n" " public static int main() {\n" " Child c = new Child();\n" " return c.getValue();\n" " }\n" "}\n", 15); run_test_int("super.method() with args", "class Parent {\n" " int add(int a, int b) {\n" " return a + b;\n" " }\n" "}\n" "class Child extends Parent {\n" " int add(int a, int b) {\n" " return super.add(a, b) * 2;\n" " }\n" "}\n" "public class Test {\n" " public static int main() {\n" " Child c = new Child();\n" " return c.add(3, 4);\n" " }\n" "}\n", 14); run_test_int("super.method() chain", "class GrandParent {\n" " int getValue() {\n" " return 100;\n" " }\n" "}\n" "class Parent extends GrandParent {\n" " int getValue() {\n" " return super.getValue() + 10;\n" " }\n" "}\n" "class Child extends Parent {\n" " int getValue() {\n" " return super.getValue() + 1;\n" " }\n" "}\n" "public class Test {\n" " public static int main() {\n" " Child c = new Child();\n" " return c.getValue();\n" " }\n" "}\n", 111); printf("\n=== Results: %d/%d tests passed ===\n", pass_count, test_count); return (pass_count == test_count) ? 0 : 1; }