Skip to content

Commit a760544

Browse files
committed
Implemented user input handling functions like input() and input_num() for user string and numeric input
1 parent 5f10f57 commit a760544

File tree

3 files changed

+88
-1
lines changed

3 files changed

+88
-1
lines changed

mas/interpreter.c

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
} functions;
2222
} Interpreter;
2323

24+
static MASObject *builtin_input(Interpreter *interp, MASObject **args, int arg_count);
2425
static MASObject *evaluate(ASTNode *node, Interpreter *interp);
2526
static MASObject *create_number(double value);
2627
static MASObject *create_string(const char *value);
@@ -165,6 +166,61 @@
165166
return obj;
166167
}
167168

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+
168224
// Built-in functions
169225
static MASObject *builtin_print(Interpreter *interp, MASObject **args, int arg_count)
170226
{
@@ -385,6 +441,30 @@
385441
free(args);
386442
return result;
387443
}
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+
}
388468

389469
// Look up user-defined function
390470
ASTNode* func = NULL;

mas/mas.exe

1.82 KB
Binary file not shown.

mas/test.mas

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,11 @@ def add(a,b):
3232
give a+b
3333
end
3434

35-
print add(2,3)
35+
print add(2,3)
36+
37+
# Input from user
38+
name = input()
39+
print(name)
40+
print "Hello!", name, "and Welcome to MAS."
41+
number = input_num()
42+
print(number)

0 commit comments

Comments
 (0)