|
#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>
|
|
#include <string.h>
|
|
#include <stdbool.h>
|
|
|
|
static char* read_file(const char *filename) {
|
|
FILE *file = fopen(filename, "r");
|
|
if (!file) return NULL;
|
|
fseek(file, 0, SEEK_END);
|
|
long size = ftell(file);
|
|
fseek(file, 0, SEEK_SET);
|
|
char *content = malloc(size + 1);
|
|
size_t read_bytes = fread(content, 1, size, file); (void)read_bytes;
|
|
content[size] = '\0';
|
|
fclose(file);
|
|
return content;
|
|
}
|
|
|
|
static bool run_example(const char *filename, const char *class_name) {
|
|
printf("\n========================================\n");
|
|
printf("Running: %s\n", filename);
|
|
printf("========================================\n");
|
|
|
|
char *source = read_file(filename);
|
|
if (!source) return false;
|
|
|
|
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) {
|
|
free(source);
|
|
return false;
|
|
}
|
|
|
|
RavaSemanticAnalyzer_t *analyzer = rava_semantic_analyzer_create();
|
|
if (!rava_semantic_analyze(analyzer, ast)) {
|
|
free(source);
|
|
return false;
|
|
}
|
|
|
|
RavaIRGenerator_t *ir_gen = rava_ir_generator_create(analyzer);
|
|
RavaProgram_t *program = rava_ir_generate(ir_gen, ast);
|
|
|
|
if (!program) {
|
|
free(source);
|
|
return false;
|
|
}
|
|
|
|
RavaVM_t *vm = rava_vm_create(program);
|
|
printf("\nOutput:\n");
|
|
|
|
if (!rava_vm_execute(vm, class_name, "main")) {
|
|
printf("\nRuntime error: %s\n", vm->error_message);
|
|
rava_vm_destroy(vm);
|
|
free(source);
|
|
return false;
|
|
}
|
|
|
|
printf("\n✅ Execution completed successfully!\n");
|
|
|
|
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);
|
|
free(source);
|
|
|
|
return true;
|
|
}
|
|
|
|
int main() {
|
|
run_example("examples/07_CollatzConjecture.java", "CollatzConjecture");
|
|
run_example("examples/08_PascalTriangle.java", "PascalTriangle");
|
|
return 0;
|
|
}
|