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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <utils/file.h>
#include <vm/runner/runner.h>
#include "compiler/source_code/source_code.h"
#include "fasm/code_generator/code_generator.h"
#include "fasm/lexer/lexer.h"
#include "fasm/linker/linker.h"
#include "fasm/runner/runner.h"
#include "utils/types.h"
typedef enum FelanMode {
FELAN_MODE_NONE,
FELAN_MODE_COMPILE_FASM,
FELAN_MODE_COMPILE_FELAN,
FELAN_MODE_RUN_FASM,
FELAN_MODE_RUN_FELAN,
} FelanMode;
int runFelan(const char *const filePath) {
SourceCode sourceCode = makeSourceCode();
Code *code;
code = read_whole_file("std/builtins.felan");
if (code == NULL) {
goto RETURN_ERROR;
}
pushToSourceCode(&sourceCode, code);
code = read_whole_file(filePath);
if (code == NULL) {
goto RETURN_ERROR;
}
pushToSourceCode(&sourceCode, code);
if (!runner(&sourceCode)) {
goto RETURN_ERROR;
}
deleteSourceCodeInners(sourceCode);
return 0;
RETURN_ERROR:
deleteSourceCodeInners(sourceCode);
return 1;
}
int compileFasm(const char *const filePath) {
SourceCode sourceCode = makeSourceCode();
Code *code = read_whole_file(filePath);
if (code == NULL) {
goto RETURN_ERROR;
}
pushToSourceCode(&sourceCode, code);
printf("----lexing:\n");
FasmLines *lines;
if ((lines = fasmLexer(&sourceCode)) == NULL) {
goto RETURN_ERROR;
}
for (size_t i = 0; i < sourceCode.size; ++i) {
fasmLinesPrint(lines[i]);
}
printf("----linking:\n");
FasmLinkedLines linkedLines = fasmLinker(lines, &sourceCode);
if (linkedLines.lines_size == ERROR_SIZE) {
goto RETURN_LINKED_ERROR;
}
fasmLinkedLinesPrint(linkedLines);
ByteCode bytecode = fasmCodeGenerator(&linkedLines);
deleteByteCodeInners(bytecode);
fasmLinkedLinesDeleteInner(linkedLines);
for (size_t i = 0; i < sourceCode.size; ++i) {
fasmLinesDeleteInner(lines[i]);
}
free(lines);
deleteSourceCodeInners(sourceCode);
return 0;
RETURN_LINKED_ERROR:
for (size_t i = 0; i < sourceCode.size; ++i) {
fasmLinesDeleteInner(lines[i]);
}
RETURN_ERROR:
deleteSourceCodeInners(sourceCode);
return 1;
}
int runFasm(const char *const filePath) {
SourceCode sourceCode = makeSourceCode();
Code *code = read_whole_file(filePath);
if (code == NULL) {
goto RETURN_ERROR;
}
pushToSourceCode(&sourceCode, code);
FasmLines *lines;
if ((lines = fasmLexer(&sourceCode)) == NULL) {
goto RETURN_ERROR;
}
FasmLinkedLines linkedLines = fasmLinker(lines, &sourceCode);
if (linkedLines.lines_size == ERROR_SIZE) {
goto RETURN_LINKED_ERROR;
}
ByteCode bytecode = fasmCodeGenerator(&linkedLines);
uint32_t result = fasmRunner(bytecode);
deleteByteCodeInners(bytecode);
fasmLinkedLinesDeleteInner(linkedLines);
for (size_t i = 0; i < sourceCode.size; ++i) {
fasmLinesDeleteInner(lines[i]);
}
free(lines);
deleteSourceCodeInners(sourceCode);
return result;
RETURN_LINKED_ERROR:
for (size_t i = 0; i < sourceCode.size; ++i) {
fasmLinesDeleteInner(lines[i]);
}
RETURN_ERROR:
deleteSourceCodeInners(sourceCode);
return 1;
}
int main(int argc, char *argv[]) {
FelanMode compileMode = FELAN_MODE_NONE;
char const *filePath = NULL;
for (int i = 1; i < argc; ++i) {
const char *const arg = argv[i];
if (strcmp(arg, "compile-fasm") == 0) {
if (compileMode == FELAN_MODE_NONE) {
compileMode = FELAN_MODE_COMPILE_FASM;
} else {
fprintf(stderr, "'%s' is not expected\n", arg);
return 1;
}
} else if (strcmp(arg, "compile-felan") == 0) {
if (compileMode == FELAN_MODE_NONE) {
compileMode = FELAN_MODE_COMPILE_FELAN;
} else {
fprintf(stderr, "'%s' is not expected\n", arg);
return 1;
}
fprintf(stderr, "'%s' is not yet supported\n", arg);
return 1;
} else if (strcmp(arg, "run-fasm") == 0) {
if (compileMode == FELAN_MODE_NONE) {
compileMode = FELAN_MODE_RUN_FASM;
} else {
fprintf(stderr, "'%s' is not expected\n", arg);
return 1;
}
} else if (strcmp(arg, "run-felan") == 0) {
if (compileMode == FELAN_MODE_NONE) {
compileMode = FELAN_MODE_RUN_FELAN;
} else {
fprintf(stderr, "'%s' is not expected\n", arg);
return 1;
}
} else if (filePath == NULL) {
filePath = arg;
} else {
fprintf(stderr, "'%s' is not expected\n", arg);
return 1;
}
}
if (filePath == NULL) {
fprintf(stderr, "Need a file path to operate\n");
}
switch (compileMode) {
case FELAN_MODE_COMPILE_FASM:
return compileFasm(filePath);
case FELAN_MODE_COMPILE_FELAN:
return 1;
case FELAN_MODE_RUN_FASM:
return runFasm(filePath);
case FELAN_MODE_NONE:
case FELAN_MODE_RUN_FELAN:
return runFelan(filePath);
}
}
|