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
|
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "include/util.h"
#include "include/parser.h"
#include "include/token.h"
// initialize a parser
parser_t* parser_init(lexer_t* lexer) {
parser_t* parser = calloc(1, sizeof(struct PARSER_STRUC));
parser->lexer = lexer;
parser->token = lexer_get_next_token(lexer);
return parser;
}
// check for expected token, or throw syntax error
void parser_token_expect(parser_t* parser, int token_type) {
if (parser->token->type == token_type) {
log_inf(strcat("Got expected token", lexer_get_c_as_string(parser->lexer)));
parser->token = lexer_get_next_token(parser->lexer);
} else {
die("Unexpected token type: [%d]", token_type);
}
}
// creates the abstract syntax tree
tree_t* parser_parse(parser_t* parser) {
return parser_parse_hunks(parser);
}
tree_t* parser_parse_token(parser_t* parser) {
if (parser_is_def_tag(parser->token->value)) {
return parser_parse_def(parser);
} else {
return parser_parse_call(parser);
}
}
// parse a single chunk
tree_t* parser_parse_chunk(parser_t* parser) {
switch (parser->token->type) {
case TOKEN_KEYWORD: {
return parser_parse_keyword(parser);
break;
}
default: {
log_inf("Skipping non-keyword token");
lexer_next(parser->lexer);
break;
}
}
}
// parse a list of chunks
tree_t* parser_parse_chunks(parser_t* parser) {
tree_t* subtree = tree_init(TREE_SUBTREE);
subtree->data.subtree.val = calloc(1, sizeof(struct TREE_STRUC));
tree_t* chunk = parser_parse_chunk(parser);
subtree->data.subtree.val[0] = chunk;
while (parser->token->type == TOKEN_END) {
parser_check_expect(parser, TOKEN_END);
tree_t* chunk = parser_parse_chunk(parser);
subtree->data.subtree.size += 1;
subtree->data.subtree.val = realloc(
subtree->data.subtree.val,
subtree->data.subtree.size * sizeof(struct TREE_STRUC)
);
subtree->data.subtree.val[subtree->data.subtree.size - 1] = chunk;
}
return subtree;
}
tree_t* parser_parse_expr(parser_t* parser) {
switch (parser->token->type) {
case TOKEN_STR:
parser_parse_str(parser);
break;
default:
log_war("Skipping unknown token");
lexer_next(parser->lexer);
}
};
tree_t* parser_parse_fac(parser_t* parser) {};
tree_t* parser_parse_term(parser_t* parser) {};
tree_t* parser_parse_fn_call(parser_t* parser) {};
tree_t* parser_parse_fn_def(parser_t* parser) {};
tree_t* parser_parse_var(parser_t* parser) {
char* token_val = parser->token->value;
parser_check_expect(parser, TOKEN_KEYWORD); // var name or fn name
// check if function
if (parser->token->type == TOKEN_FN_APPLY) {
return parser_parse_fn_call(parser);
}
tree_t* var = tree_init(TREE_VAR);
var->data.var.name = token_val;
return var;
};
tree_t* parser_parse_var_def(parser_t* parser) {
int var_is_const;
parser_check_expect(parser, TOKEN_KEYWORD); // let
char* var_name = parser->token->value; // set variable name
parser_check_expect(parser, TOKEN_KEYWORD); // expect variable name & advance
if (parser->token->type == TOKEN_DEFINE_CONST) { // check either constant or mutable variable
var_is_const = 1;
parser_check_expect(parser, TOKEN_DEFINE_CONST);
} else {
var_is_const = 0;
parser_check_expect(parser, TOKEN_DEFINE_MUT);
}
tree_t* var_val = parser_parse_expr(parser); // set the value
tree_t* var_def = tree_init(TREE_VAR_DEF); // create the var, as a subtree
var_def->data.var_def.name = var_name;
var_def->data.var_def.val = var_val;
var_def->data.var_def.is_const = &var_is_const;
return var_def;
};
tree_t* parser_parse_str(parser_t* parser) {
log_inf("Entered str");
tree_t* str = tree_init(TREE_STR);
str->data.str.val = parser->token->value;
parser_check_expect(parser, TOKEN_STR);
return str;
};
tree_t* parser_parse_keyword(parser_t* parser) {
if (strcmp(parser->token->value, "let")) {
return parser_parse_var_def(parser);
} else if (strcmp(parser->token->value, "fn")) {
return parser_parse_fn_def(parser);
} else {
return parser_parse_var(parser); // assume attempting to call
// variable/function;
// TODO: differentiate b/w the
// two on call; may be a syntax
// change :P just to make the
// interpreter easier
}
}
int parser_is_def_tag(char* maybe_tag) {
if (strcmp(maybe_tag, "int") ||
strcmp(maybe_tag, "str")) {
return 1;
} else { return 0; }
}
|