Skip to content

Commit f7d5d1d

Browse files
Validator is complete, added support for not keyword in operations and negative numbers are now allowed as well as byte data type
1 parent 67737a3 commit f7d5d1d

7 files changed

Lines changed: 168 additions & 43 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ fun Color set_red(self Color, value byte) void
8686
end
8787
8888
let my_color Color = Color init(0, 0, 0)
89-
my_color set_red(255)
89+
Color set_red(my_color, 255)
9090
```
9191

9292
**Data Types**
@@ -95,7 +95,7 @@ byte, int, float, bool, string, and user-defined structs are supported.
9595
```
9696
let b byte = 255
9797
let i int = 12345
98-
let b2 byte = (b + i) # this will raise an error because byte + int results in an int
98+
let b2 byte = (b + i) # this will not raise an error because validator doesn't check bytes, overflow over 255 will rap back to 0
9999
let f float = 3.14
100100
let s string = f # raises an error, cannot assign float to string
101101
```

source.galo

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
fun add(left int, right int) int
2-
return (left + right)
3-
end
4-
5-
let result int = add(5, 324)
6-
7-
~
8-
91
fun add(left int, right int) int
102
# inside parenthesis because all operations require parenthesis to define order of operations
113
return (left + right)
@@ -64,10 +56,4 @@ fun Color set_red(self Color, value byte) void
6456
end
6557

6658
let my_color Color = Color init(0, 0, 0)
67-
my_color set_red(255)
68-
69-
let b byte = 255
70-
let i int = 12345
71-
let b2 byte = (b + i) # this will raise an error because byte + int results in an int
72-
let f float = 3.14
73-
let s string = f # raises an error, cannot assign float to string
59+
Color set_red(my_color, 255)

src/galo_headers.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ enum TokenType {
1515
TOKEN_KEYWORD_ELSE,
1616
TOKEN_KEYWORD_RETURN,
1717
TOKEN_KEYWORD_WHILE,
18+
TOKEN_KEYWORD_NOT,
1819
TOKEN_OPERATOR_ASSIGN,
1920
TOKEN_OPERATOR_ARITHMETIC,
2021
TOKEN_OPERATOR_COMPARISON,
@@ -119,11 +120,13 @@ typedef struct FunctionCall_t {
119120
typedef struct WhileLoop_t {
120121
Node condition;
121122
NodeList* body;
123+
int line;
122124
} WhileLoop;
123125

124126
typedef struct ElifIfStatement_t {
125127
Node condition;
126128
NodeList* body;
129+
int line;
127130
} ElifIfStatement;
128131

129132
typedef struct IfStatement_t {
@@ -133,6 +136,7 @@ typedef struct IfStatement_t {
133136
int elif_count;
134137
char has_else;
135138
NodeList* else_body;
139+
int line;
136140
} IfStatement;
137141

138142
typedef struct ReturnStatement_t {
@@ -143,6 +147,7 @@ typedef struct Operation_t {
143147
Token* operator;
144148
Node* left;
145149
Node* right;
150+
char is_not_operator;
146151
} Operation;
147152

148153
NodeList* create_node_list();

src/lexer.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ void lexer(const char* source_code, TokenList* token_list) {
116116
exit(1);
117117
break;
118118
case '+': case '-': case '*': case '/': case '%':
119+
if (source_code[i] == '-' && start_of_token == -1 && (source_code[i + 1] == '0' || source_code[i + 1] == '1' || source_code[i + 1] == '2' || source_code[i + 1] == '3' || source_code[i + 1] == '4' || source_code[i + 1] == '5' || source_code[i + 1] == '6' || source_code[i + 1] == '7' || source_code[i + 1] == '8' || source_code[i + 1] == '9' || source_code[i + 1] == '.')) {
120+
start_of_token = i;
121+
lexing_code = LEXING_CODE_NUMBER;
122+
break;
123+
}
119124
token.type = TOKEN_OPERATOR_ARITHMETIC;
120125
token.value = source_code[i] == '+' ? "+" :
121126
source_code[i] == '-' ? "-" :
@@ -207,6 +212,15 @@ void lexer_add_word_or_number_token(const char* source_code, int i, int* start_o
207212
is_float++;
208213
continue;
209214
}
215+
if (token_value[j] == '-') {
216+
if (j == 0) {
217+
continue;
218+
}
219+
else {
220+
printf("Lexer Error: Invalid number format: `%s` in line %d\n", token_value, line);
221+
exit(1);
222+
}
223+
}
210224
if (token_value[j] < '0' || token_value[j] > '9') {
211225
// Handle error: invalid number format
212226
printf("Lexer Error: Invalid number format: `%s` in line %d\n", token_value, line);
@@ -215,7 +229,7 @@ void lexer_add_word_or_number_token(const char* source_code, int i, int* start_o
215229
}
216230
*lexing_code = LEXING_CODE_NONE;
217231

218-
if (is_float > 0) {
232+
if (is_float == 1) {
219233
token->type = TOKEN_CONSTANT_FLOAT;
220234
return;
221235
} else if (is_float == 0) {
@@ -260,10 +274,14 @@ void lexer_add_word_or_number_token(const char* source_code, int i, int* start_o
260274
token->type = TOKEN_NATIVE_TYPE;
261275
} else if (strcmp(token_value, "byte") == 0) {
262276
token->type = TOKEN_NATIVE_TYPE;
277+
} else if (strcmp(token_value, "void") == 0) {
278+
token->type = TOKEN_NATIVE_TYPE;
263279
} else if (strcmp(token_value, "and") == 0) {
264280
token->type = TOKEN_OPERATOR_LOGICAL;
265281
} else if (strcmp(token_value, "or") == 0) {
266282
token->type = TOKEN_OPERATOR_LOGICAL;
283+
} else if (strcmp(token_value, "not") == 0) {
284+
token->type = TOKEN_KEYWORD_NOT;
267285
} else {
268286
token->type = TOKEN_IDENTIFIER;
269287
}
@@ -287,6 +305,7 @@ const char* get_token_type_name(enum TokenType type) {
287305
case TOKEN_KEYWORD_ELSE: return "TOKEN_KEYWORD_ELSE";
288306
case TOKEN_KEYWORD_RETURN: return "TOKEN_KEYWORD_RETURN";
289307
case TOKEN_KEYWORD_WHILE: return "TOKEN_KEYWORD_WHILE";
308+
case TOKEN_KEYWORD_NOT: return "TOKEN_KEYWORD_NOT";
290309
case TOKEN_OPERATOR_ASSIGN: return "TOKEN_OPERATOR_ASSIGN";
291310
case TOKEN_OPERATOR_ARITHMETIC: return "TOKEN_OPERATOR_ARITHMETIC";
292311
case TOKEN_OPERATOR_COMPARISON: return "TOKEN_OPERATOR_COMPARISON";

src/lists.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "galo_headers.h"
55
#include <stdlib.h>
66
#include <string.h>
7+
#include <stdio.h>
78

89
TokenList* create_token_list() {
910
TokenList* token_list = (TokenList*)malloc(sizeof(TokenList));
@@ -76,13 +77,15 @@ void add_int(IntList* int_list, int value) {
7677
}
7778
int* get_int(IntList* int_list, int index) {
7879
if (index < 0 || index >= int_list->size) {
79-
return NULL;
80+
printf("Int index out of bounds: %d\n", index);
81+
exit(1);
8082
}
8183
return &int_list->int_list[index];
8284
}
8385
void remove_int_index(IntList* int_list, int index) {
8486
if (index < 0 || index >= int_list->size) {
85-
return;
87+
printf("Int index out of bounds: %d\n", index);
88+
exit(1);
8689
}
8790
for (int i = index; i < int_list->size - 1; i++) {
8891
int_list->int_list[i] = int_list->int_list[i + 1];
@@ -96,6 +99,8 @@ void remove_int_value(IntList* int_list, int value) {
9699
return;
97100
}
98101
}
102+
printf("Int value not found: %d\n", value);
103+
exit(1);
99104
}
100105

101106
char contains_int(IntList* int_list, int value) {

src/parser.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@ void parse_if(TokenList* tokens, ObjectList* object_list, int* index, IfStatemen
357357
}
358358
(*index)++;
359359

360+
if_stmt->line = get_token(tokens, *index)->line;
360361
if_stmt->condition = parse_expression(tokens, object_list, index);
361362

362363
if (get_token(tokens, *index)->type != TOKEN_END_OF_LINE) {
@@ -401,6 +402,7 @@ void parse_if(TokenList* tokens, ObjectList* object_list, int* index, IfStatemen
401402

402403
ElifIfStatement elif;
403404

405+
elif.line = get_token(tokens, *index)->line;
404406
elif.condition = parse_expression(tokens, object_list, index);
405407
if (get_token(tokens, *index)->type != TOKEN_END_OF_LINE) {
406408
printf("Error: Invalid elif statement, expected end of line but found `%s` in line %d\n", get_token(tokens, *index)->value, get_token(tokens, *index)->line);
@@ -440,6 +442,7 @@ void parse_while(TokenList* tokens, ObjectList* object_list, int* index, WhileLo
440442
}
441443
(*index)++;
442444

445+
while_loop->line = get_token(tokens, *index)->line;
443446
while_loop->condition = parse_expression(tokens, object_list, index);
444447

445448
if (get_token(tokens, *index)->type != TOKEN_END_OF_LINE) {
@@ -591,6 +594,28 @@ Node parse_expression(TokenList* tokens, ObjectList* object_list, int* index) {
591594
}
592595
} else if (token->type == TOKEN_PARENTHESIS_OPEN) {
593596
(*index)++;
597+
if (get_token(tokens, *index)->type == TOKEN_KEYWORD_NOT) {
598+
Token* op = get_token(tokens, *index);
599+
(*index)++;
600+
601+
Node inner = parse_expression(tokens, object_list, index);
602+
if (get_token(tokens, *index)->type != TOKEN_PARENTHESIS_CLOSE) {
603+
printf("Error: Invalid token expression `%s` expected `)` for not operation in line %d\n", token->value, token->line);
604+
exit(1);
605+
}
606+
(*index)++;
607+
608+
Operation operation;
609+
operation.is_not_operator = 1;
610+
operation.left = add_object(object_list, &inner, sizeof(Node));
611+
operation.right = NULL;
612+
operation.operator = op;
613+
614+
node.type = NODE_OPERATION;
615+
node.data = add_object(object_list, &operation, sizeof(Operation));
616+
return node;
617+
}
618+
594619
Node left = parse_expression(tokens, object_list, index);
595620
if (get_token(tokens, *index)->type != TOKEN_PARENTHESIS_CLOSE) {
596621
Token* op = get_token(tokens, *index);
@@ -609,6 +634,7 @@ Node parse_expression(TokenList* tokens, ObjectList* object_list, int* index) {
609634
(*index)++;
610635

611636
Operation operation;
637+
operation.is_not_operator = 0;
612638
operation.left = add_object(object_list, &left, sizeof(Node));
613639
operation.right = add_object(object_list, &right, sizeof(Node));
614640
operation.operator = op;
@@ -731,6 +757,12 @@ void debug_parser_node(Node* node) {
731757
printf(")");
732758
} else if (node->type == NODE_OPERATION) {
733759
Operation* operation = (Operation*)node->data;
760+
if (operation->is_not_operator) {
761+
printf("( not ");
762+
debug_parser_node(operation->left);
763+
printf(") ");
764+
return;
765+
}
734766
printf("( ");
735767
debug_parser_node(operation->left);
736768
printf("%s ", operation->operator->value);

0 commit comments

Comments
 (0)