75 lines
2.5 KiB
C
75 lines
2.5 KiB
C
|
|
#include "../lexer/lexer.h"
|
||
|
|
#include "../parser/parser.h"
|
||
|
|
#include "../semantic/semantic.h"
|
||
|
|
#include <stdio.h>
|
||
|
|
#include <stdlib.h>
|
||
|
|
|
||
|
|
int main() {
|
||
|
|
const char *source =
|
||
|
|
"public class Test {\n"
|
||
|
|
" public static int calculate(int x, int y) {\n"
|
||
|
|
" int sum = x + y;\n"
|
||
|
|
" int product = x * y;\n"
|
||
|
|
" boolean isPositive = sum > 0;\n"
|
||
|
|
" if (isPositive) {\n"
|
||
|
|
" return sum;\n"
|
||
|
|
" }\n"
|
||
|
|
" return product;\n"
|
||
|
|
" }\n"
|
||
|
|
"\n"
|
||
|
|
" public static void main(int argc) {\n"
|
||
|
|
" int result = calculate(10, 20);\n"
|
||
|
|
" int doubled = result * 2;\n"
|
||
|
|
" }\n"
|
||
|
|
"}\n";
|
||
|
|
|
||
|
|
printf("Source code:\n%s\n", source);
|
||
|
|
printf("\nSemantic Analysis:\n");
|
||
|
|
printf("================================================================================\n\n");
|
||
|
|
|
||
|
|
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 ? parser->error_message : "Unknown error");
|
||
|
|
rava_parser_destroy(parser);
|
||
|
|
rava_lexer_destroy(lexer);
|
||
|
|
return 1;
|
||
|
|
}
|
||
|
|
|
||
|
|
RavaSemanticAnalyzer_t *analyzer = rava_semantic_analyzer_create();
|
||
|
|
|
||
|
|
bool success = rava_semantic_analyze(analyzer, ast);
|
||
|
|
|
||
|
|
if (success) {
|
||
|
|
printf("✓ Semantic analysis passed successfully!\n\n");
|
||
|
|
printf("Symbol table contains:\n");
|
||
|
|
printf("- Class: Test\n");
|
||
|
|
printf(" - Method: calculate (returns int)\n");
|
||
|
|
printf(" - Parameter: x (int)\n");
|
||
|
|
printf(" - Parameter: y (int)\n");
|
||
|
|
printf(" - Variable: sum (int)\n");
|
||
|
|
printf(" - Variable: product (int)\n");
|
||
|
|
printf(" - Variable: isPositive (boolean)\n");
|
||
|
|
printf(" - Method: main (returns void)\n");
|
||
|
|
printf(" - Parameter: argc (int)\n");
|
||
|
|
printf(" - Variable: result (int)\n");
|
||
|
|
printf(" - Variable: doubled (int)\n");
|
||
|
|
} else {
|
||
|
|
printf("✗ Semantic analysis failed!\n");
|
||
|
|
if (analyzer->error_message) {
|
||
|
|
printf("Error: %s\n", analyzer->error_message);
|
||
|
|
}
|
||
|
|
printf("Total errors: %d\n", analyzer->error_count);
|
||
|
|
}
|
||
|
|
|
||
|
|
rava_semantic_analyzer_destroy(analyzer);
|
||
|
|
rava_ast_node_destroy(ast);
|
||
|
|
rava_parser_destroy(parser);
|
||
|
|
rava_lexer_destroy(lexer);
|
||
|
|
|
||
|
|
printf("\nSemantic test completed!\n");
|
||
|
|
return success ? 0 : 1;
|
||
|
|
}
|