aboutsummaryrefslogtreecommitdiffstats
path: root/src/parser.c
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2022-03-30 16:06:01 +0200
committerBad Diode <bd@badd10de.dev>2022-03-30 16:06:01 +0200
commit5fc604279a9fb156dd3a8ade7bdf5c0936e9f9a7 (patch)
treef08d452ccfb51c7043a90a769f959cb970870d23 /src/parser.c
parent138b466b897f94ea6a29a7b62c39caa717efafec (diff)
downloadbdl-5fc604279a9fb156dd3a8ade7bdf5c0936e9f9a7.tar.gz
bdl-5fc604279a9fb156dd3a8ade7bdf5c0936e9f9a7.zip
Add parsing for builtin arithmetic ops
Diffstat (limited to 'src/parser.c')
-rw-r--r--src/parser.c60
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
110Node * 110Node *
111parse_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
132Node *
111parse_paren(Parser *parser) { 133parse_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}