77int run_depth = 0 ;
88
99Variable * var_stack ;
10+ Variable * function_var_stack ;
11+ int in_function = 0 ;
1012
1113int 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