-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcompiler.c
68 lines (53 loc) · 1.14 KB
/
compiler.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
#define BUFFER_SIZE 32
#define TRUE 1
#define FALSE 0
// https://www.lysator.liu.se/c/ANSI-C-grammar-y.html
int lex_file;
int buffer;
int token(int* buf) {
int t;
t = lex(lex_file, buffer, BUFFER_SIZE);
*buf = stralloc(buffer);
return t;
}
int compile_labeled_statement() {
int mark;
mark = compile_mark();
if (compile_token(TOKEN_IDENTIFIER) && compile_token(':') && compile_statement()) {
return TRUE;
}
compile_rewind(mark);
if (compile_token(TOKEN_CASE) && compile_constant_expression() && compile_token(':') && compile_statement()) {
return TRUE;
}
compile_rewind(mark);
if (compile_token(TOKEN_DEFAULT) && compile_token(':') && compile_statement()) {
return TRUE;
}
compile_rewind(mark);
return FALSE;
}
int compile_statement() {
if (compile_labeled_statement()) {
return TRUE;
}
if (compile_compound_statement()) {
return TRUE;
}
if (compile_expression_statement()) {
return TRUE;
}
if (compile_selection_statement()) {
return TRUE;
}
if (compile_iteration_statement()) {
return TRUE;
}
if (compile_jump_statement()) {
return TRUE;
}
return FALSE;
}
int main() {
buffer = malloc(BUFFER_SIZE);
}