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
|
typedef struct {
LineNo line;
LineNo col;
} Location;
typedef struct {
Location where;
char *var;
} Declaration;
arr_declaration(Declarations, Declaration, decls_)
typedef struct {
int type;
Location where;
union {
Declarations decls;
};
} Statement;
arr_declaration(Statements, Statement, stmts_)
typedef struct {
Statements stmts;
} ParsedFile;
/* TODO: Add newline tokens back in; give tokens pointer to text */
static bool parse_decls(Declarations *ds, Tokenizer *t) {
if (t->token->kind != TOKEN_IDENT) {
tokr_err(t, "Cannot declare non-identifier.");
return false;
}
t->token++;
return true;
}
static bool parse_stmt(Statement *s, Tokenizer *t) {
if (token_is_kw(t->token + 1, KW_COLON)) {
return parse_decls(&s->decls, t);
} else {
t->token++; /* TODO: This is temporary */
}
return true;
}
static bool parse_file(ParsedFile *f, Tokenizer *t) {
stmts_create(&f->stmts);
bool ret = true;
while (t->token->kind != TOKEN_EOF) {
Statement stmt = {0};
if (!parse_stmt(&stmt, t))
ret = false;
stmts_add(&f->stmts, &stmt);
}
return ret;
}
static void stmt_fprint(FILE *out, Statement *s) {
fprintf(out, "statement!\n");
}
static void parsed_file_fprint(FILE *out, ParsedFile *f) {
arr_foreach(f->stmts, Statement, stmt) {
stmt_fprint(out, stmt);
}
}
|