-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathba.c
107 lines (85 loc) · 2.48 KB
/
ba.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "include/lexer.h"
#include "include/parser.h"
#include "include/binder.h"
#include "include/compiler.h"
void usage() {
printf("usage: ba <cmd> <filename>\n");
printf("cmd option:\n\ttokenize: tokenize the given file.\n\t");
printf("parse: Get the AST and check the grammar syntax.\n\t");
printf("bind: Get the stackframe and check the semantique syntax.\n\t");
printf("disassemble: Disassemble the entry program.\n\t");
printf("dump: Get bytecode onto the console by the given entry program.\n\t");
printf("compile: Compile the entry program.\n");
exit(1);
}
Tokens *__tokenize(char *filename) {
uint8_t *program = read_entry(filename);
Tokens *tokens = new_tokens();
tokenize(&tokens, program);
return tokens;
}
AST *__parse(char *filename) {
Tokens *tokens = __tokenize(filename);
AST *ast = parse(&tokens);
return ast;
}
Stackframe *__bind(char *filename) {
AST *ast = __parse(filename);
Stackframe *sf = bind(ast);
return sf;
}
Bytecode *__compile(char *filename) {
Tokens *tokens = __tokenize(filename);
AST *ast = parse(&tokens);
Stackframe *sf = bind(ast);
Bytecode *bytecode = compile(ast, sf);
return bytecode;
}
char *outfile(char *filename) {
size_t i = strlen(filename) - 1;
if (!(filename[i] == 'a' && filename[i - 1] == 'b' && filename[i - 2] == '.')) {
printf("Entry program must have *.ba format.\n");
exit(1);
}
filename[i] = 'b';
filename[i - 1] = 's';
return filename;
}
int main(int argc, char **argv) {
if (argc != 3) {
usage();
}
char *cmd = argv[1];
char *filename = argv[2];
if (strcmp(cmd, "compile") == 0) {
Bytecode *bytecode = __compile(filename);
char *bytefile = outfile(filename);
dump(bytecode, bytefile);
}else if (strcmp(cmd, "dump") == 0) {
Bytecode *bytecode = __compile(filename);
dumps(bytecode);
printf("\n");
} else if (strcmp(cmd, "disassemble") == 0) {
Bytecode *bytecode = __compile(filename);
disassemble(bytecode);
printf("\n");
} else if (strcmp(cmd, "bind") == 0) {
Stackframe *sf = __bind(filename);
display_frame(sf);
printf("\n");
} else if (strcmp(cmd, "parse") == 0) {
AST *ast = __parse(filename);
display_tree(ast);
printf("\n");
} else if (strcmp(cmd, "tokenize") == 0) {
Tokens *tokens = __tokenize(filename);
display_tokens(tokens);
printf("\n");
} else {
usage();
}
return 0;
}