173 lines
5.1 KiB
C
173 lines
5.1 KiB
C
|
|
#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 <stdio.h>
|
||
|
|
#include <stdlib.h>
|
||
|
|
|
||
|
|
static bool run_test(const char *name, const char *source, const char *class_name,
|
||
|
|
const char *method_name, int expected_result) {
|
||
|
|
printf("\n========================================\n");
|
||
|
|
printf("Test: %s\n", name);
|
||
|
|
printf("========================================\n");
|
||
|
|
printf("Source:\n%s\n", source);
|
||
|
|
|
||
|
|
RavaLexer_t *lexer = rava_lexer_create(source);
|
||
|
|
RavaParser_t *parser = rava_parser_create(lexer);
|
||
|
|
RavaASTNode_t *ast = rava_parser_parse(parser);
|
||
|
|
|
||
|
|
if (parser->had_error) {
|
||
|
|
printf("❌ Parse error: %s\n", parser->error_message);
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
RavaSemanticAnalyzer_t *analyzer = rava_semantic_analyzer_create();
|
||
|
|
if (!rava_semantic_analyze(analyzer, ast)) {
|
||
|
|
printf("❌ Semantic error: %s\n", analyzer->error_message);
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
RavaIRGenerator_t *ir_gen = rava_ir_generator_create(analyzer);
|
||
|
|
RavaProgram_t *program = rava_ir_generate(ir_gen, ast);
|
||
|
|
|
||
|
|
if (!program) {
|
||
|
|
printf("❌ IR generation failed\n");
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
printf("\nGenerated IR:\n");
|
||
|
|
rava_ir_print(program);
|
||
|
|
|
||
|
|
RavaVM_t *vm = rava_vm_create(program);
|
||
|
|
printf("\nExecuting %s.%s()...\n", class_name, method_name);
|
||
|
|
|
||
|
|
if (!rava_vm_execute(vm, class_name, method_name)) {
|
||
|
|
printf("❌ Runtime error: %s\n", vm->error_message);
|
||
|
|
rava_vm_destroy(vm);
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
RavaValue_t result = rava_vm_get_result(vm);
|
||
|
|
int32_t result_int = rava_value_as_int(result);
|
||
|
|
|
||
|
|
printf("Result: %d\n", result_int);
|
||
|
|
|
||
|
|
bool success = (result_int == expected_result);
|
||
|
|
if (success) {
|
||
|
|
printf("✅ PASS (expected %d, got %d)\n", expected_result, result_int);
|
||
|
|
} else {
|
||
|
|
printf("❌ FAIL (expected %d, got %d)\n", expected_result, 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);
|
||
|
|
|
||
|
|
return success;
|
||
|
|
}
|
||
|
|
|
||
|
|
int main() {
|
||
|
|
printf("================================================================================\n");
|
||
|
|
printf("RAVA JAVA INTERPRETER - END-TO-END RUNTIME TESTS\n");
|
||
|
|
printf("================================================================================\n");
|
||
|
|
|
||
|
|
int passed = 0;
|
||
|
|
int total = 0;
|
||
|
|
|
||
|
|
total++;
|
||
|
|
if (run_test("Simple Addition",
|
||
|
|
"public class Test {\n"
|
||
|
|
" public static int add() {\n"
|
||
|
|
" int a = 10;\n"
|
||
|
|
" int b = 20;\n"
|
||
|
|
" int result = a + b;\n"
|
||
|
|
" return result;\n"
|
||
|
|
" }\n"
|
||
|
|
"}\n",
|
||
|
|
"Test", "add", 30)) {
|
||
|
|
passed++;
|
||
|
|
}
|
||
|
|
|
||
|
|
total++;
|
||
|
|
if (run_test("Multiple Operations",
|
||
|
|
"public class Test {\n"
|
||
|
|
" public static int compute() {\n"
|
||
|
|
" int a = 5;\n"
|
||
|
|
" int b = 3;\n"
|
||
|
|
" int sum = a + b;\n"
|
||
|
|
" int product = a * b;\n"
|
||
|
|
" int result = sum + product;\n"
|
||
|
|
" return result;\n"
|
||
|
|
" }\n"
|
||
|
|
"}\n",
|
||
|
|
"Test", "compute", 23)) {
|
||
|
|
passed++;
|
||
|
|
}
|
||
|
|
|
||
|
|
total++;
|
||
|
|
if (run_test("Conditional (True Branch)",
|
||
|
|
"public class Test {\n"
|
||
|
|
" public static int conditional() {\n"
|
||
|
|
" int x = 10;\n"
|
||
|
|
" int result = 0;\n"
|
||
|
|
" if (x > 5) {\n"
|
||
|
|
" result = 100;\n"
|
||
|
|
" }\n"
|
||
|
|
" return result;\n"
|
||
|
|
" }\n"
|
||
|
|
"}\n",
|
||
|
|
"Test", "conditional", 100)) {
|
||
|
|
passed++;
|
||
|
|
}
|
||
|
|
|
||
|
|
total++;
|
||
|
|
if (run_test("Conditional (False Branch)",
|
||
|
|
"public class Test {\n"
|
||
|
|
" public static int conditional() {\n"
|
||
|
|
" int x = 3;\n"
|
||
|
|
" int result = 0;\n"
|
||
|
|
" if (x > 5) {\n"
|
||
|
|
" result = 100;\n"
|
||
|
|
" }\n"
|
||
|
|
" return result;\n"
|
||
|
|
" }\n"
|
||
|
|
"}\n",
|
||
|
|
"Test", "conditional", 0)) {
|
||
|
|
passed++;
|
||
|
|
}
|
||
|
|
|
||
|
|
total++;
|
||
|
|
if (run_test("Factorial (Recursive)",
|
||
|
|
"public class Test {\n"
|
||
|
|
" public static int factorial(int n) {\n"
|
||
|
|
" if (n <= 1) {\n"
|
||
|
|
" return 1;\n"
|
||
|
|
" }\n"
|
||
|
|
" return n * factorial(n - 1);\n"
|
||
|
|
" }\n"
|
||
|
|
"}\n",
|
||
|
|
"Test", "factorial", 1)) {
|
||
|
|
passed++;
|
||
|
|
}
|
||
|
|
|
||
|
|
printf("\n================================================================================\n");
|
||
|
|
printf("TEST SUMMARY\n");
|
||
|
|
printf("================================================================================\n");
|
||
|
|
printf("Passed: %d/%d\n", passed, total);
|
||
|
|
printf("Failed: %d/%d\n", total - passed, total);
|
||
|
|
|
||
|
|
if (passed == total) {
|
||
|
|
printf("\n🎉 ALL TESTS PASSED! The Rava interpreter is executing Java code!\n");
|
||
|
|
} else {
|
||
|
|
printf("\n⚠️ Some tests failed.\n");
|
||
|
|
}
|
||
|
|
|
||
|
|
return (passed == total) ? 0 : 1;
|
||
|
|
}
|