Skip to content

Commit 5ceefb9

Browse files
committed
added multiple error case
1 parent a755c16 commit 5ceefb9

File tree

4 files changed

+45
-28
lines changed

4 files changed

+45
-28
lines changed

lolzak.ys

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
task main(){
2-
println("hello world");
3-
println("hello world");
4-
println("hello world");
5-
println("hello world");
6-
if (True) {
7-
println("\thello world");
8-
return 0;
1+
## Lets create a fibonacci sequence
2+
3+
task add(n) {
4+
return n+=3;
5+
};
6+
7+
task function(n) {
8+
while (n < 10) {
9+
n-=1;
10+
n = add(n);
11+
println("N: %V", n);
912
};
13+
return n;
1014
};
1115

12-
println("hello world %V", waarde);
16+
println("Function: %V", function(0));
1317

14-
main();
15-
main();
16-
main();
1718

1819

my_parser.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ Node *parser(Token *p_tokens) {
161161
p_next->type = NODE_ARGS;
162162
p_node->childs[0] = *p_next;
163163
p_node->end_t = i;
164+
164165
return p_node;
165166
} else {
166167
// Checking if next token is a + operator and the token after that is also an operator if so then its a increment operator
@@ -296,6 +297,7 @@ Node *parser(Token *p_tokens) {
296297

297298
// Parsing the body
298299
Node *p_body = parser(p_tokens);
300+
299301
p_node->num_childs += 1;
300302
p_node->childs = realloc(p_node->childs, p_node->num_childs * sizeof(Node));
301303
p_node->childs[1] = *p_body;
@@ -308,14 +310,18 @@ Node *parser(Token *p_tokens) {
308310
// Checking for return statement
309311
if (strcmp(p_tokens[i].value, "return") == 0) {
310312
p_node = malloc(sizeof(Node));
311-
313+
int line = p_tokens[i].start;
312314
p_node->type = NODE_RETURN_STMT;
313315
p_node->start_t = i;
314316
p_node->num_childs = 0;
315317

316318
i += 1;
317319
// Parsing the return value
318320
Node *p_child = parser(p_tokens);
321+
if (strcmp(p_tokens[i].value, ";") != 0) {
322+
printf("\033[1;31m\nError at line %d:\n\tExpected a semicolon after return statement\033[0m\n", line);
323+
exit(1);
324+
}
319325
p_node->num_childs += 1;
320326
p_node->childs = malloc(p_node->num_childs * sizeof(Node));
321327
p_node->childs[0] = *p_child;

tokenizer.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ Token *tokenize_code(char *p_code) {
236236
fprintf(stderr, "Memory allocation failed\n");
237237
exit(1);
238238
}
239-
token_list[token_count] = (Token) { token_type, token_value };
239+
token_list[token_count] = (Token) { token_type, token_value, line_number };
240240
token_count++;
241241

242242
i++;
@@ -286,7 +286,7 @@ Token *tokenize_code(char *p_code) {
286286
fprintf(stderr, "Memory allocation failed\n");
287287
exit(1);
288288
}
289-
token_list[token_count] = (Token) { token_type, token_value };
289+
token_list[token_count] = (Token) { token_type, token_value, line_number };
290290
token_count++;
291291

292292
i++;
@@ -333,7 +333,7 @@ Token *tokenize_code(char *p_code) {
333333
fprintf(stderr, "Memory allocation failed\n");
334334
exit(1);
335335
}
336-
token_list[token_count] = (Token) { token_type, token_value };
336+
token_list[token_count] = (Token) { token_type, token_value, line_number };
337337
token_count++;
338338

339339
continue;
@@ -377,7 +377,7 @@ Token *tokenize_code(char *p_code) {
377377
fprintf(stderr, "Memory allocation failed\n");
378378
exit(1);
379379
}
380-
token_list[token_count] = (Token) { token_type, token_value };
380+
token_list[token_count] = (Token) { token_type, token_value, line_number };
381381
token_count++;
382382

383383
continue;

yoinkle_runtime.c

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ Variable *runtime(Node *NODE, Token *p_tokens, Node *AST) {
9898
}
9999
}
100100
if (!var_found) {
101-
printf("\nError at line %d:\n\tVariable '%s' does not exist\n", p_tokens[NODE->start_t].start, identifier);
101+
printf("\033[1;31m\nError at line %d:\n\tVariable '%s' does not exist\n\033[0m", p_tokens[NODE->start_t].start, identifier);
102102
exit(1);
103103
}
104104
break;
@@ -181,9 +181,12 @@ Variable *runtime(Node *NODE, Token *p_tokens, Node *AST) {
181181
}
182182
result->value.string_value = new_string;
183183
} else {
184-
printf("Error: Unsupported operation\n");
185-
printf("Cannot perform operation of type %s and %s\n",
186-
left->type == VAR_INT ? "int" : left->type == VAR_FLOAT ? "float" : left->type == VAR_STRING ? "string" : left->type == VAR_BOOLEAN ? "boolean" : "null", right->type == VAR_INT ? "int" : right->type == VAR_FLOAT ? "float" : right->type == VAR_STRING ? "string" : right->type == VAR_BOOLEAN ? "boolean" : "null");
184+
printf(
185+
"\033[1;31m\nError at line %d: OperationError\n\tCannot perform operation of type %s and %s\n\033[0m",
186+
p_tokens[NODE->childs[0].start_t].start,
187+
left->type == VAR_INT ? "int" : left->type == VAR_FLOAT ? "float" : left->type == VAR_STRING ? "string" : left->type == VAR_BOOLEAN ? "boolean" : "null",
188+
right->type == VAR_INT ? "int" : right->type == VAR_FLOAT ? "float" : right->type == VAR_STRING ? "string" : right->type == VAR_BOOLEAN ? "boolean" : "null"
189+
);
187190
exit(1);
188191
}
189192
return result;
@@ -328,7 +331,7 @@ Variable *runtime(Node *NODE, Token *p_tokens, Node *AST) {
328331
}
329332
}
330333
if (!function_found) {
331-
printf("\nError at line %d:\n\tFunction '%s' does not exist\n", p_tokens[NODE->start_t].start, func_name);
334+
printf("\033[1;31m\nError at line %d:\n\tFunction '%s' does not exist\n\033[0m", p_tokens[NODE->start_t].start, func_name);
332335
exit(1);
333336
}
334337
free(args);
@@ -448,9 +451,13 @@ Variable *runtime(Node *NODE, Token *p_tokens, Node *AST) {
448451
result_cond->value.boolean_value = strcmp(left_cond->value.string_value, right_cond->value.string_value) != 0;
449452
}
450453
} else {
451-
printf("Error: Unsupported operation\n");
452-
printf("Cannot perform comparison of type %s and %s\n",
453-
left_cond->type == VAR_INT ? "int" : left_cond->type == VAR_FLOAT ? "float" : left_cond->type == VAR_STRING ? "string" : left_cond->type == VAR_BOOLEAN ? "boolean" : "null", right_cond->type == VAR_INT ? "int" : right_cond->type == VAR_FLOAT ? "float" : right_cond->type == VAR_STRING ? "string" : right_cond->type == VAR_BOOLEAN ? "boolean" : "null");
454+
455+
printf(
456+
"\033[1;31m\nError at line %d: CompareError\n\tCannot compare conditions of type %s and %s\n\033[0m",
457+
p_tokens[NODE->childs[0].start_t].start,
458+
left_cond->type == VAR_INT ? "int" : left_cond->type == VAR_FLOAT ? "float" : left_cond->type == VAR_STRING ? "string" : left_cond->type == VAR_BOOLEAN ? "boolean" : "null",
459+
right_cond->type == VAR_INT ? "int" : right_cond->type == VAR_FLOAT ? "float" : right_cond->type == VAR_STRING ? "string" : right_cond->type == VAR_BOOLEAN ? "boolean" : "null"
460+
);
454461
exit(1);
455462
}
456463
return result_cond;
@@ -531,9 +538,12 @@ Variable *runtime(Node *NODE, Token *p_tokens, Node *AST) {
531538
result_short->value.float_value = left_short->value.float_value / right_short->value.int_value;
532539
}
533540
} else {
534-
printf("Error: Unsupported operation\n");
535-
printf("Cannot perform operation of type %s and %s\n",
536-
left_short->type == VAR_INT ? "int" : left_short->type == VAR_FLOAT ? "float" : left_short->type == VAR_STRING ? "string" : left_short->type == VAR_BOOLEAN ? "boolean" : "null", right_short->type == VAR_INT ? "int" : right_short->type == VAR_FLOAT ? "float" : right_short->type == VAR_STRING ? "string" : right_short->type == VAR_BOOLEAN ? "boolean" : "null");
541+
printf(
542+
"\033[1;31m\nError at line %d: OperationError\n\tCannot perform operation of type %s and %s\n\033[0m",
543+
p_tokens[NODE->childs[0].start_t].start,
544+
left_short->type == VAR_INT ? "int" : left_short->type == VAR_FLOAT ? "float" : left_short->type == VAR_STRING ? "string" : left_short->type == VAR_BOOLEAN ? "boolean" : "null",
545+
right_short->type == VAR_INT ? "int" : right_short->type == VAR_FLOAT ? "float" : right_short->type == VAR_STRING ? "string" : right_short->type == VAR_BOOLEAN ? "boolean" : "null"
546+
);
537547
exit(1);
538548
}
539549
// Saving the result to the left variable

0 commit comments

Comments
 (0)