-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcompile.c
More file actions
493 lines (429 loc) · 12.5 KB
/
compile.c
File metadata and controls
493 lines (429 loc) · 12.5 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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <ctype.h>
const char* OS = "linux";
const char* ARCH = "amd64";
const char* VERSION = "a0.0.4_debug";
void print_help(char** argv){
printf("XASM Compile %s Community Version (%s_%s)\n",OS,VERSION,ARCH);
printf("Usage: %s [<options>] [<arguments> ...] [input file]\n",argv[0]);
printf("\nOptions:\n");
printf(" -h, --help\t\tshow this message, then exit\n");
printf(" -v, --version\t\tshow compile version number, then exit\n");
printf(" -o, --output FILE\tset output file\n");
}
typedef struct //存储从命令行参数中得到的各项数据与一些获取到的flag
{
char* input_fpath;
char* output_fpath;
int exit;
}Option;
int str_eq(char* s1,char* s2){
for(int index = 0;;index++){
if(s1[index] != s2[index]){
return 0;
}
if(s1[index] == '\0'){
return 1;
}
}
}
int str_eqi(char* s1,char* s2){
for(int index = 0;;index++){
if(tolower(s1[index]) != tolower(s2[index])){
return 0;
}
if(s1[index] == '\0'){
return 1;
}
}
}
int logger(const char* __fmt,...){
va_list args;
va_start(args,__fmt);
int i = vfprintf(stdout,__fmt,args);
fflush(stdout);
va_end(args);
return i;
}
Option loop_parse_options(int argc,char** argv){
Option option;
int has_output_file = 0;
int has_input_file = 0;
option.input_fpath = "main.asm";
option.output_fpath = "main";
option.exit = 0;
for(int index = 1;index < argc;index++){
char* cur_arg = argv[index];
if(str_eq(cur_arg,"-h") || str_eq(cur_arg,"--help")){
print_help(argv);
option.exit = 1;
break;
}else if(str_eq(cur_arg,"-v") || str_eq(cur_arg,"--version")){
printf("%s\n",VERSION);
option.exit = 1;
break;
}else if(str_eq(cur_arg,"-o") || str_eq(cur_arg,"--output")){
if(index+1 < argc && argv[index+1][0] != '-'){
has_output_file = 1;
option.output_fpath = argv[index+1];
index++;
continue;
}else{
printf("Missing output file after '%s'\n",cur_arg);
option.exit = 1;
break;
}
}else if(cur_arg[0] == '-'){
printf("Unknown option: '%s'\n",cur_arg);
option.exit = 1;
break;
}else{
has_input_file = 1;
option.input_fpath = cur_arg;
continue;
}
}
return option;
}
Option parse_options(int argc, char** argv){
Option option;
if(argc == 1){
print_help(argv);
option.exit = 1;
}
return loop_parse_options(argc,argv);
}
typedef enum {
NORMAL,
STRING,
COMMENT,
}CommandKind;
typedef struct {
CommandKind kind;
int length;
char hex;
char valid;
char* str;
}Command;
Command parse_command(Command command){
command.valid = 1;
int hex;
if(str_eqi(command.str,"add")){
hex = 0x01;
}else if(str_eqi(command.str,"sub")){
hex = 0x02;
}else if(str_eqi(command.str,"xadd")){
hex = 0x03;
}else if(str_eqi(command.str,"xsub")){
hex = 0x04;
}else if(str_eqi(command.str,"and")){
hex = 0x11;
}else if(str_eqi(command.str,"or")){
hex = 0x12;
}else if(str_eqi(command.str,"not")){
hex = 0x13;
}else if(str_eqi(command.str,"mov")){
hex = 0x21;
}else if(str_eqi(command.str,"copy")){
hex = 0x22;
}else if(str_eqi(command.str,"goto")){
hex = 0x23;
}else if(str_eqi(command.str,"geta")){
hex = 0x24;
}else if(str_eqi(command.str,">")){
hex = 0x25;
}else if(str_eqi(command.str,"<")){
hex = 0x26;
}else if(str_eqi(command.str,"=")){
hex = 0x27;
}else if(str_eqi(command.str,"lm")){
hex = 0x28;
}else if(str_eqi(command.str,"rm")){
hex = 0x29;
}else if(str_eqi(command.str,"exit")){
hex = 0x2a;
}else if(str_eqi(command.str,"putc")){
hex = 0x31;
}else if(str_eqi(command.str,"putn")){
hex = 0x32;
}else if(str_eqi(command.str,"puth")){
hex = 0x33;
}else if(str_eqi(command.str,"getc")){
hex = 0x34;
}else if(str_eqi(command.str,"getn")){
hex = 0x35;
}else if(str_eqi(command.str,"geth")){
hex = 0x36;
}else if(str_eqi(command.str,"&")){
hex = 0x41;
}else if(str_eqi(command.str,"|")){
hex = 0x42;
}else if(str_eqi(command.str,"^")){
hex = 0x43;
}else if(str_eqi(command.str,"~")){
hex = 0x44;
}else if(str_eqi(command.str,"<<")){
hex = 0x45;
}else if(str_eqi(command.str,">>")){
hex = 0x46;
}else if(str_eqi(command.str,"sto")){
hex = 0xff;
}else if(!sscanf(command.str,"%x",&hex)){
command.valid = 0;
return command;
}
command.hex = hex;
return command;
}
typedef enum {
STRING_SQM,
STRING_DQM,
}StringKind;
char readbuf[0xffffffff];
Command read_command(FILE* file_ptr){
Command command;
CommandKind kind = NORMAL;
int readindex = 0;
int continue_flag = 1;
StringKind string_kind = STRING_SQM;
for(;continue_flag;readindex++)
{
// readbuf = (char*)realloc(readbuf,readindex+1);
readbuf[readindex] = fgetc(file_ptr);
if(readbuf[readindex] == EOF){
break;
}
switch (kind)
{
case STRING:
switch (string_kind)
{
case STRING_SQM:
switch (readbuf[readindex])
{
case '\'':
continue_flag = 0;
break;
case '\\':
readindex++;
readbuf[readindex] = fgetc(file_ptr);
switch (readbuf[readindex])
{
case EOF:
continue_flag = 0;
break;
case 'n':
readindex--;
readbuf[readindex] = '\n';
break;
case 't':
readindex--;
readbuf[readindex] = '\t';
break;
case 'r':
readindex--;
readbuf[readindex] = '\r';
break;
case 'x':
readindex--;
fscanf(file_ptr, "%02hhx", &readbuf[readindex]);
break;
default:
readindex--;
readbuf[readindex] = readbuf[readindex+1];
break;
}
break;
default:
continue;
break;
}
break;
case STRING_DQM:
switch (readbuf[readindex])
{
case '"':
continue_flag = 0;
break;
case '\\':
readindex++;
readbuf[readindex] = fgetc(file_ptr);
switch (readbuf[readindex])
{
case EOF:
continue_flag = 0;
break;
case 'n':
readindex--;
readbuf[readindex] = '\n';
break;
case 't':
readindex--;
readbuf[readindex] = '\t';
break;
case 'r':
readindex--;
readbuf[readindex] = '\r';
break;
case 'x':
readindex--;
fscanf(file_ptr, "%02hhx", &readbuf[readindex]);
break;
default:
readindex--;
readbuf[readindex] = readbuf[readindex+1];
break;
}
break;
default:
continue;
break;
}
break;
}
break;
case COMMENT:
switch (readbuf[readindex])
{
case '\n':
continue_flag = 0;
break;
default:
continue;
break;
}
break;
case NORMAL:
switch (readbuf[readindex])
{
case ' ':
continue_flag = 0;
break;
case '\t':
continue_flag = 0;
break;
case '\n':
continue_flag = 0;
break;
case '\'':
string_kind = STRING_SQM;
kind = STRING;
readindex--;
break;
case '"':
string_kind = STRING_DQM;
kind = STRING;
readindex--;
break;
case '/':
readindex++;
readbuf[readindex] = fgetc(file_ptr);
switch (readbuf[readindex])
{
case EOF:
continue_flag = 0;
break;
case '/':
kind = COMMENT;
break;
default:
break;
}
break;
case '-':
readindex++;
readbuf[readindex] = fgetc(file_ptr);
switch (readbuf[readindex])
{
case EOF:
continue_flag = 0;
break;
case '-':
kind = COMMENT;
break;
default:
break;
}
break;
case '#':
kind = COMMENT;
break;
default:
continue;
break;
}
break;
default:
break;
}
if(!continue_flag){
break;
}
}
if(readbuf[readindex] == EOF && readindex == 0){
readbuf[readindex] = '\0';
command.length = EOF;
command.kind = kind;
command.str = readbuf;
return command;
}
command.length = readindex;
command.kind = kind;
readbuf[readindex] = '\0';
command.str = readbuf;
return command;
}
void compile_file(char* input_fpath,char* output_fpath){
FILE* input_fptr = fopen(input_fpath,"r");
FILE* output_fptr = fopen(output_fpath,"w");
if(input_fptr == NULL){
printf("Could not open source file '%s'\n",input_fpath);
return;
}
if(output_fptr == NULL)
{
printf("Could not create output file '%s'\n",output_fpath);
return;
}
Command command;
while((command = read_command(input_fptr)).length != EOF){
if(command.length == 0){
continue;
}
logger("\nreader position: %zu\n",ftell(input_fptr));
if(command.kind == COMMENT){
logger("comment: %s\n",command.str);
continue;
}
if(command.kind == STRING){
logger("writebuffer: %s\n",command.str);
fputs(command.str,output_fptr);
fflush(output_fptr);
continue;
}
logger("readbuffer: %s\n",command.str);
if((command = parse_command(command)).valid){
logger("writebuffer: %x\n",command.hex);
fputc(command.hex,output_fptr);
fflush(output_fptr);
}else{
logger("invalid command: %s\n",command.str);
}
}
fclose(input_fptr);
fclose(output_fptr);
}
int main(int argc, char **argv){
Option options = parse_options(argc,argv);
if(options.exit == 1){
return 0;
}
char* input_fpath = options.input_fpath;
char* output_fpath = options.output_fpath;
logger("input filepath: %s\n",input_fpath);
logger("output filepath: %s\n",output_fpath);
compile_file(input_fpath,output_fpath);
return 0;
}