diff options
Diffstat (limited to 'src/parser.c')
-rw-r--r-- | src/parser.c | 60 |
1 files changed, 57 insertions, 3 deletions
diff --git a/src/parser.c b/src/parser.c index a82a97f..2a5e3e3 100644 --- a/src/parser.c +++ b/src/parser.c | |||
@@ -108,7 +108,44 @@ parse_string(Parser *parser) { | |||
108 | } | 108 | } |
109 | 109 | ||
110 | Node * | 110 | Node * |
111 | parse_builtin(Parser *parser) { | ||
112 | Token op = next_token(parser); | ||
113 | Node *node = alloc_node(NODE_BUILTIN); | ||
114 | node->builtin.type = op.type; | ||
115 | array_init(node->builtin.args, 0); | ||
116 | while (has_next(parser)) { | ||
117 | Token next = peek_token(parser); | ||
118 | if (next.type == TOKEN_RPAREN) { | ||
119 | next_token(parser); | ||
120 | return node; | ||
121 | } | ||
122 | Node *arg = parse_next(parser); | ||
123 | if (arg == NULL) { | ||
124 | break; | ||
125 | } | ||
126 | array_push(node->builtin.args, arg); | ||
127 | } | ||
128 | push_error(ERR_TYPE_PARSER, ERR_UNMATCHED_PAREN, op.line, op.col); | ||
129 | return NULL; | ||
130 | } | ||
131 | |||
132 | Node * | ||
111 | parse_paren(Parser *parser) { | 133 | parse_paren(Parser *parser) { |
134 | next_token(parser); // Skip paren. | ||
135 | Token tok = peek_token(parser); | ||
136 | // TODO: is keyword? | ||
137 | switch (tok.type) { | ||
138 | case TOKEN_ADD: | ||
139 | case TOKEN_SUB: | ||
140 | case TOKEN_MUL: | ||
141 | case TOKEN_DIV: | ||
142 | case TOKEN_MOD: { | ||
143 | return parse_builtin(parser); | ||
144 | } break; | ||
145 | default: break; | ||
146 | } | ||
147 | // print_token(tok); | ||
148 | // TODO: is in symbol table and its value is a function? | ||
112 | return NULL; // TODO: Not implemented | 149 | return NULL; // TODO: Not implemented |
113 | } | 150 | } |
114 | 151 | ||
@@ -143,14 +180,28 @@ print_node(Node *node) { | |||
143 | printf("-"); | 180 | printf("-"); |
144 | } | 181 | } |
145 | if (node->number.fractional != 0) { | 182 | if (node->number.fractional != 0) { |
146 | printf("%zu.%zu\n", node->number.integral, node->number.fractional); | 183 | printf("%zu.%zu", node->number.integral, node->number.fractional); |
147 | } else { | 184 | } else { |
148 | printf("%zu\n", node->number.integral); | 185 | printf("%zu", node->number.integral); |
149 | } | 186 | } |
150 | } break; | 187 | } break; |
151 | case NODE_STRING: { | 188 | case NODE_STRING: { |
152 | sv_write(&node->string); | 189 | sv_write(&node->string); |
153 | printf("\n"); | 190 | } break; |
191 | case NODE_BUILTIN: { | ||
192 | printf("("); | ||
193 | printf("{#%s}", token_str[node->builtin.type]); | ||
194 | size_t n_args = array_size(node->builtin.args); | ||
195 | if (n_args != 0) { | ||
196 | printf(" "); | ||
197 | } | ||
198 | for (size_t i = 0; i < n_args; ++i) { | ||
199 | print_node(node->builtin.args[i]); | ||
200 | if (i < n_args - 1) { | ||
201 | printf(" "); | ||
202 | } | ||
203 | } | ||
204 | printf(")"); | ||
154 | } break; | 205 | } break; |
155 | default: { printf("{#unk}"); } break; | 206 | default: { printf("{#unk}"); } break; |
156 | } | 207 | } |
@@ -164,9 +215,11 @@ parse(Token *tokens) { | |||
164 | }; | 215 | }; |
165 | 216 | ||
166 | // DEBUG: TOKENS | 217 | // DEBUG: TOKENS |
218 | printf("-- tokens --\n"); | ||
167 | for (size_t i = 0; i < array_size(tokens); i++) { | 219 | for (size_t i = 0; i < array_size(tokens); i++) { |
168 | print_token(tokens[i]); | 220 | print_token(tokens[i]); |
169 | } | 221 | } |
222 | printf("------------\n"); | ||
170 | 223 | ||
171 | while (has_next(&parser)) { | 224 | while (has_next(&parser)) { |
172 | Node *node = parse_next(&parser); | 225 | Node *node = parse_next(&parser); |
@@ -174,5 +227,6 @@ parse(Token *tokens) { | |||
174 | return; | 227 | return; |
175 | } | 228 | } |
176 | print_node(node); | 229 | print_node(node); |
230 | printf("\n"); | ||
177 | } | 231 | } |
178 | } | 232 | } |