|
21 | 21 | } functions; |
22 | 22 | } Interpreter; |
23 | 23 |
|
| 24 | + static MASObject *builtin_input(Interpreter *interp, MASObject **args, int arg_count); |
24 | 25 | static MASObject *evaluate(ASTNode *node, Interpreter *interp); |
25 | 26 | static MASObject *create_number(double value); |
26 | 27 | static MASObject *create_string(const char *value); |
|
165 | 166 | return obj; |
166 | 167 | } |
167 | 168 |
|
| 169 | + static MASObject *builtin_input(Interpreter *interp, MASObject **args, int arg_count) |
| 170 | +{ |
| 171 | + (void)interp; |
| 172 | + if (arg_count > 0) { |
| 173 | + // Optional prompt |
| 174 | + MASObject *prompt = args[0]; |
| 175 | + if (prompt->type == AST_STRING) { |
| 176 | + printf("%s", prompt->data.string); |
| 177 | + } |
| 178 | + fflush(stdout); |
| 179 | + } |
| 180 | + |
| 181 | + char buffer[1024]; |
| 182 | + if (!fgets(buffer, sizeof(buffer), stdin)) { |
| 183 | + // EOF or error |
| 184 | + return create_string(""); |
| 185 | + } |
| 186 | + |
| 187 | + // Remove trailing newline if present |
| 188 | + int len = strlen(buffer); |
| 189 | + if (len > 0 && buffer[len - 1] == '\n') { |
| 190 | + buffer[len - 1] = '\0'; |
| 191 | + } |
| 192 | + |
| 193 | + return create_string(buffer); |
| 194 | +} |
| 195 | + |
| 196 | +static MASObject *builtin_input_num(Interpreter *interp, MASObject **args, int arg_count) |
| 197 | +{ |
| 198 | + (void)interp; |
| 199 | + if (arg_count > 0) { |
| 200 | + MASObject *prompt = args[0]; |
| 201 | + if (prompt->type == AST_STRING) { |
| 202 | + printf("%s", prompt->data.string); |
| 203 | + } |
| 204 | + fflush(stdout); |
| 205 | + } |
| 206 | + |
| 207 | + char buffer[1024]; |
| 208 | + if (!fgets(buffer, sizeof(buffer), stdin)) { |
| 209 | + return create_number(0.0); |
| 210 | + } |
| 211 | + |
| 212 | + // Try to parse as number |
| 213 | + char *end; |
| 214 | + double val = strtod(buffer, &end); |
| 215 | + if (end == buffer || (*end != '\0' && *end != '\n')) { |
| 216 | + // Not a valid number |
| 217 | + fprintf(stderr, "Warning: input is not a number, returning 0\n"); |
| 218 | + return create_number(0.0); |
| 219 | + } |
| 220 | + |
| 221 | + return create_number(val); |
| 222 | +} |
| 223 | + |
168 | 224 | // Built-in functions |
169 | 225 | static MASObject *builtin_print(Interpreter *interp, MASObject **args, int arg_count) |
170 | 226 | { |
|
385 | 441 | free(args); |
386 | 442 | return result; |
387 | 443 | } |
| 444 | + else if (strcmp(node->data.call.name, "input") == 0) { |
| 445 | + MASObject** args = malloc(sizeof(MASObject*) * node->data.call.arg_count); |
| 446 | + for (int i = 0; i < node->data.call.arg_count; i++) { |
| 447 | + args[i] = evaluate(node->data.call.args[i], interp); |
| 448 | + } |
| 449 | + MASObject* result = builtin_input(interp, args, node->data.call.arg_count); |
| 450 | + for (int i = 0; i < node->data.call.arg_count; i++) { |
| 451 | + mas_object_decref(args[i]); |
| 452 | + } |
| 453 | + free(args); |
| 454 | + return result; |
| 455 | + } |
| 456 | + else if (strcmp(node->data.call.name, "input_num") == 0) { |
| 457 | + MASObject** args = malloc(sizeof(MASObject*) * node->data.call.arg_count); |
| 458 | + for (int i = 0; i < node->data.call.arg_count; i++) { |
| 459 | + args[i] = evaluate(node->data.call.args[i], interp); |
| 460 | + } |
| 461 | + MASObject* result = builtin_input_num(interp, args, node->data.call.arg_count); |
| 462 | + for (int i = 0; i < node->data.call.arg_count; i++) { |
| 463 | + mas_object_decref(args[i]); |
| 464 | + } |
| 465 | + free(args); |
| 466 | + return result; |
| 467 | + } |
388 | 468 |
|
389 | 469 | // Look up user-defined function |
390 | 470 | ASTNode* func = NULL; |
|
0 commit comments