Skip to content

Commit 1a8b98e

Browse files
committed
added read function
1 parent 03e0702 commit 1a8b98e

File tree

5 files changed

+123
-20
lines changed

5 files changed

+123
-20
lines changed

my_parser.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,8 +396,10 @@ Node *parser(Token *p_tokens) {
396396
childs_pos += 1;
397397
}
398398
}
399-
if (p_node->childs[1].type == NODE_COMPARE) {
400-
p_node->type = NODE_CONDITION;
399+
if (p_node->num_childs > 1) {
400+
if (p_node->childs[1].type == NODE_COMPARE) {
401+
p_node->type = NODE_CONDITION;
402+
}
401403
}
402404
return p_node;
403405
}

simple.ys

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#
44
This is a multi-line comment
55
#
6-
76
my_value1 = 3;
87
my_value2 = 2.5;
98
my_value1 = True;
@@ -20,18 +19,20 @@ println("%V\t", a);
2019
println("Hello World %V_%V Bye world %V", a, b, new_value);
2120
print("hello world \b\b", my_value1);
2221

23-
my_bool = 32;
22+
my_bool = 33;
2423
if (my_bool > 1) {
2524
if (my_bool > 32) {
26-
print("1 World %V", my_bool);
25+
exit(1);
2726
} else {
2827
print("2 World %V", my_bool);
2928
};
3029
} else {
3130
print("3 World %V", my_bool);
3231
};
3332

34-
## waarde = 10;
33+
waarde = read("Jouw waarde: ");
34+
antwoord = (waarde + 3);
35+
println("%V", read("Jouw waarde: \b\b\b\b"));
3536

3637
## task my_task(waarde) {
3738
## print("Hello World %V", waarde);

standard_library.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,4 +339,58 @@ void yoinkle_std_println(Variable *args, int num_args) {
339339
format_string++; // Move to the next character in the string
340340
}
341341
putchar('\n');
342+
}
343+
344+
void yoinkle_std_exit(Node *AST, Token *p_tokens, Variable *var_stack, int exit_code) {
345+
free(AST);
346+
free(p_tokens);
347+
free(var_stack);
348+
349+
AST = NULL;
350+
p_tokens = NULL;
351+
var_stack = NULL;
352+
353+
printf("\nExiting with code %d\n", exit_code);
354+
exit(exit_code);
355+
}
356+
357+
char *yoinkle_std_read(char *prompt) {
358+
if (prompt != NULL) {
359+
char *format_string = prompt;
360+
while (*format_string)
361+
{
362+
if (*format_string == '\\') {
363+
format_string++; // Move to the next character after the backslash
364+
switch (*format_string) {
365+
case 't':
366+
putchar('\t'); // Print a tab character
367+
break;
368+
case 'n':
369+
putchar('\n'); // Print a new line character
370+
break;
371+
case 'r':
372+
putchar('\r'); // Carriage return
373+
break;
374+
case 'b':
375+
putchar('\b'); // Backspace (may not show effect)
376+
break;
377+
case '\\':
378+
putchar('\\'); // Backslash
379+
break;
380+
default:
381+
putchar('\\'); // Print the backslash if it's not followed by t or n
382+
putchar(*format_string); // Print the next character
383+
}
384+
} else {
385+
putchar(*format_string); // Print the regular character
386+
}
387+
format_string++; // Move to the next character in the string
388+
}
389+
}
390+
char *input = malloc(1000);
391+
fgets(input, 1000, stdin);
392+
393+
// Remove the newline character from the input
394+
input[strcspn(input, "\n")] = 0;
395+
return input;
342396
}

standard_library.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@
55

66
void yoinkle_std_print(Variable *args, int num_args);
77
void yoinkle_std_println(Variable *args, int num_args);
8+
void yoinkle_std_exit(Node *AST, Token *p_tokens, Variable *var_stack, int exit_code);
9+
char *yoinkle_std_read(char *prompt);
810

911
#endif

yoinkle_runtime.c

Lines changed: 58 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,19 @@
77
int run_depth = 0;
88

99
Variable *var_stack;
10+
Variable *function_var_stack;
11+
int in_function = 0;
1012

1113
int var_num = 0;
12-
Variable *runtime(Node *NODE, Token *p_tokens) {
14+
Variable *runtime(Node *NODE, Token *p_tokens, Node *AST) {
1315
switch (NODE->type) {
1416
Variable *new_var;
1517
case NODE_VAR_DECL:
1618
;
1719
char *var_name = p_tokens[NODE->childs[0].start_t].value;
1820

1921
// Getting the second child of the node
20-
new_var = runtime(&NODE->childs[1], p_tokens);
22+
new_var = runtime(&NODE->childs[1], p_tokens, AST);
2123

2224
// Checking if the variable already exists
2325
int variable_exists = 0;
@@ -98,8 +100,8 @@ Variable *runtime(Node *NODE, Token *p_tokens) {
98100
break;
99101
case NODE_BIN_EXPR:
100102
;
101-
Variable *left = runtime(&NODE->childs[0], p_tokens);
102-
Variable *right = runtime(&NODE->childs[2], p_tokens);
103+
Variable *left = runtime(&NODE->childs[0], p_tokens, AST);
104+
Variable *right = runtime(&NODE->childs[2], p_tokens, AST);
103105
Variable *result = malloc(sizeof(Variable));
104106
result->name = NULL;
105107
if ((left->type == VAR_INT && right->type == VAR_INT)) {
@@ -194,7 +196,7 @@ Variable *runtime(Node *NODE, Token *p_tokens) {
194196
exit(1);
195197
}
196198
for (int i = 0; i < num_args; i++) {
197-
args[i] = *runtime(&NODE->childs[0].childs[i], p_tokens);
199+
args[i] = *runtime(&NODE->childs[0].childs[i], p_tokens, AST);
198200
}
199201
}
200202

@@ -206,26 +208,68 @@ Variable *runtime(Node *NODE, Token *p_tokens) {
206208
yoinkle_std_print(args, num_args);
207209
} else if (strcmp(func_name, "println") == 0) {
208210
yoinkle_std_println(args, num_args);
211+
} else if (strcmp(func_name, "exit") == 0) {
212+
int exit_code = 0;
213+
if (num_args > 0) {
214+
exit_code = args[0].value.int_value;
215+
}
216+
yoinkle_std_exit(AST, p_tokens, var_stack, exit_code);
217+
} else if (strcmp(func_name, "read") == 0) {
218+
char *prompt = NULL;
219+
if (num_args > 0) {
220+
prompt = args[0].value.string_value;
221+
}
222+
char *input = yoinkle_std_read(prompt);
223+
224+
new_var = malloc(sizeof(Variable));
225+
new_var->name = NULL;
226+
227+
// Checking if the input is an integer
228+
char *end;
229+
errno = 0;
230+
long long int_value = strtoll(input, &end, 10);
231+
if (errno == 0 && *end == '\0') {
232+
new_var->type = VAR_INT;
233+
new_var->value.int_value = int_value;
234+
return new_var;
235+
}
236+
// Checking if the input is a float
237+
float float_value = strtof(input, &end);
238+
if (errno == 0 && *end == '\0') {
239+
new_var->type = VAR_FLOAT;
240+
new_var->value.float_value = float_value;
241+
return new_var;
242+
}
243+
// Checking if the input is a boolean
244+
if (strcmp(input, "True") == 0 || strcmp(input, "False") == 0) {
245+
new_var->type = VAR_BOOLEAN;
246+
new_var->value.boolean_value = strcmp(input, "True") == 0 ? 1 : 0;
247+
return new_var;
248+
}
249+
// If the input is not a one of the above, then it is a string
250+
new_var->type = VAR_STRING;
251+
new_var->value.string_value = input;
252+
return new_var;
253+
} else {
254+
printf("Error: Function '%s' not found\n", func_name);
255+
exit(1);
209256
}
210257
break;
211258
case NODE_IF_STMT:
212259
;
213-
Variable *condition = runtime(&NODE->childs[0], p_tokens);
260+
Variable *condition = runtime(&NODE->childs[0], p_tokens, AST);
214261

215-
printf("Condition: %s\n", condition->value.boolean_value ? "True" : "False");
216-
217262
if (condition->value.boolean_value) {
218-
printf("Node: %d\n", NODE->childs[1].type);
219-
runtime(&NODE->childs[1].childs[0], p_tokens);
263+
runtime(&NODE->childs[1].childs[0], p_tokens, AST);
220264
} else if (NODE->num_childs == 3 && NODE->childs[2].type == NODE_ELSE_STMT) {
221-
runtime(&NODE->childs[2].childs[0], p_tokens);
265+
runtime(&NODE->childs[2].childs[0], p_tokens, AST);
222266
}
223267
break;
224268

225269
case NODE_CONDITION:
226270
;
227-
Variable *left_cond = runtime(&NODE->childs[0], p_tokens);
228-
Variable *right_cond = runtime(&NODE->childs[2], p_tokens);
271+
Variable *left_cond = runtime(&NODE->childs[0], p_tokens, AST);
272+
Variable *right_cond = runtime(&NODE->childs[2], p_tokens, AST);
229273
Variable *result_cond = malloc(sizeof(Variable));
230274
result_cond->name = NULL;
231275
if (left_cond->type == VAR_INT && right_cond->type == VAR_INT) {
@@ -291,7 +335,7 @@ void run_runtime(Node *AST, Token *p_tokens) {
291335
for (int i = 0; i < ast_length; i++) {
292336
Node *current_node = &AST->childs[i];
293337
// printf("Running node %d\n", i);
294-
runtime(current_node, p_tokens);
338+
runtime(current_node, p_tokens, AST);
295339
// printf(" Variable stack length: %d\n", var_num);
296340
// for (int j = 0; j < var_num; j++) {
297341
// if (var_stack[j].type == VAR_INT) {

0 commit comments

Comments
 (0)