-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_generator.c
More file actions
73 lines (60 loc) · 1.93 KB
/
main_generator.c
File metadata and controls
73 lines (60 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Syntax Analyzer/Syntax_Analyzer.h"
#include "CodeGenerator/C_Generator.h"
#include "CodeGenerator/Python_Generator.h"
#ifdef _WIN32
#define STRICMP _stricmp
#else
#include <strings.h>
#define STRICMP strcasecmp
#endif
/* Find OPTION -> LANG node in AST */
static const char* get_target_language(ASTNode *root) {
if (!root) return NULL;
for (size_t i = 0; i < root->child_count; i++) {
ASTNode *child = root->children[i];
if (child && child->kind == AST_OPTION) {
if (child->child_count > 0 && child->children[0]->kind == AST_LANG) {
return child->children[0]->lexeme;
}
}
}
return NULL;
}
int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "Usage: %s <source.algo>\n", argv[0]);
return EXIT_FAILURE;
}
const char *source_file = argv[1];
const char *output_file = NULL;
/* Parse_File fait: lexer + syntax + (chez toi) semantic_check */
ASTNode *root = Parse_File(source_file);
if (!root) {
fprintf(stderr, "Error: analysis failed.\n");
return EXIT_FAILURE;
}
const char *lang = get_target_language(root);
if (!lang) {
fprintf(stderr, "Error: no target language specified (LANGAGE: ...).\n");
AST_Free(root);
return EXIT_FAILURE;
}
if (STRICMP(lang, "c") == 0) {
output_file = "output/output.c";
Generate_C_Code(root, output_file);
printf("C code generated in %s\n", output_file);
} else if (STRICMP(lang, "python") == 0) {
output_file = "output/output.py";
Generate_Python_Code(root, output_file);
printf("Python code generated in %s\n", output_file);
} else {
fprintf(stderr, "Error: unsupported language: %s\n", lang);
AST_Free(root);
return EXIT_FAILURE;
}
AST_Free(root);
return EXIT_SUCCESS;
}