Skip to content

Commit 07ab204

Browse files
Added Validator functionality and updated parser and header accordingly
1 parent 85629d1 commit 07ab204

6 files changed

Lines changed: 353 additions & 24 deletions

File tree

source.galo

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
let x int = 22
2+
let y int = 8
3+
x = y
4+
5+
~
6+
17
fun add(left int, right int) int
28
# inside parenthesis because all operations require parenthesis to define order of operations
39
return (left + right)

src/galo_headers.h

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ typedef struct VariableDeclaration_t {
7575
Token* name;
7676
Token* type;
7777
Node value;
78+
int id;
7879
} VariableDeclaration;
7980

8081
typedef struct Parameter_t {
@@ -89,17 +90,23 @@ typedef struct FunctionDeclaration_t {
8990
Token* return_type;
9091
Token* struct_implementation;
9192
NodeList* body;
93+
int id;
9294
} FunctionDeclaration;
9395

9496
typedef struct StructDeclaration_t {
9597
Token* name;
9698
Parameter* fields;
9799
int field_count;
100+
int id;
98101
} StructDeclaration;
99102

100-
typedef struct VariableAssignment_t {
103+
typedef struct ScopedIdentifier_t {
101104
Token** scope;
102-
int scope_size;
105+
int size;
106+
} ScopedIdentifier;
107+
108+
typedef struct VariableAssignment_t {
109+
ScopedIdentifier identifier;
103110
Node value;
104111
} VariableAssignment;
105112

@@ -139,15 +146,6 @@ typedef struct Operation_t {
139146
Node* right;
140147
} Operation;
141148

142-
typedef struct ScopedIdentifier_t {
143-
Token** scope;
144-
int size;
145-
} ScopedIdentifier;
146-
147-
typedef struct Constant_t {
148-
Token* value;
149-
} Constant;
150-
151149
NodeList* create_node_list();
152150
void free_node_list(NodeList* node_list);
153151
void add_node(NodeList* node_list, Node node);
@@ -163,13 +161,22 @@ IntList* create_int_list();
163161
void free_int_list(IntList* int_list);
164162
void add_int(IntList* int_list, int value);
165163
int* get_int(IntList* int_list, int index);
164+
void remove_int(IntList* int_list, int index);
165+
char contains_int(IntList* int_list, int value);
166166

167167
typedef struct ObjectList_t {
168168
void** objects;
169169
int size;
170170
int capacity;
171171
} ObjectList;
172172

173+
typedef struct Validator_Object_t {
174+
ObjectList* functions;
175+
ObjectList* structs;
176+
ObjectList* variables;
177+
IntList* active_variables;
178+
int last_id;
179+
} Validator_Object;
173180

174181
ObjectList* create_object_list();
175182
void free_object_list(ObjectList* object_list);
@@ -188,5 +195,9 @@ const char* get_node_type_name(enum NodeType type);
188195
void debug_parser(NodeList* ast);
189196
void debug_parser_node(Node* node);
190197

191-
void validator();
198+
Validator_Object create_validator_object();
199+
void free_validator_object(Validator_Object* validator_object);
200+
void validator(NodeList* ast, Validator_Object* validator_object);
201+
void debug_validator(Validator_Object* validator_object);
202+
192203
void run();

src/lists.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,24 @@ int* get_int(IntList* int_list, int index) {
8080
}
8181
return &int_list->int_list[index];
8282
}
83+
void remove_int(IntList* int_list, int index) {
84+
if (index < 0 || index >= int_list->size) {
85+
return;
86+
}
87+
for (int i = index; i < int_list->size - 1; i++) {
88+
int_list->int_list[i] = int_list->int_list[i + 1];
89+
}
90+
int_list->size--;
91+
}
92+
93+
char contains_int(IntList* int_list, int value) {
94+
for (int i = 0; i < int_list->size; i++) {
95+
if (int_list->int_list[i] == value) {
96+
return 1;
97+
}
98+
}
99+
return 0;
100+
}
83101

84102

85103
ObjectList* create_object_list() {

src/main.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,17 @@ int main() {
2121

2222
printf("parsing...\n");
2323
parser(token_list, object_list, ast, &index);
24-
debug_parser(ast);
24+
//debug_parser(ast);
2525

26+
Validator_Object validator_object = create_validator_object();
2627
printf("validating...\n");
27-
validator();
28-
28+
validator(ast, &validator_object);
29+
debug_validator(&validator_object);
30+
2931
printf("runnning...\n");
3032
run();
3133

34+
free_validator_object(&validator_object);
3235
free_node_list(ast);
3336
free_object_list(object_list);
3437
free_token_list(token_list);

src/parser.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ void parse_var_decl(TokenList* tokens, ObjectList* object_list, int* index, Vari
6363
exit(1);
6464
}
6565
var_decl->type = var_type;
66+
var_decl->id = -1;
6667

6768
(*index)++;
6869

@@ -103,8 +104,10 @@ void parse_var_assign(TokenList* tokens, ObjectList* object_list, int* index, Va
103104

104105
Token** var_scope_address = add_object(object_list, var_scope, sizeof(Token*) * size);
105106

106-
var_assign->scope_size = size;
107-
var_assign->scope = var_scope_address;
107+
ScopedIdentifier scoped_identifier;
108+
scoped_identifier.scope = var_scope_address;
109+
scoped_identifier.size = size;
110+
var_assign->identifier = scoped_identifier;
108111

109112
if (var_scope != NULL) {
110113
free(var_scope);
@@ -198,6 +201,7 @@ void parse_function(TokenList* tokens, ObjectList* object_list, int* index, Func
198201

199202
func_decl->name = func_name;
200203
func_decl->struct_implementation = struct_implementation;
204+
func_decl->id = -1;
201205

202206
if (get_token(tokens, *index)->type != TOKEN_PARENTHESIS_OPEN) {
203207
printf("Error: Invalid function declaration in line %d\n", get_token(tokens, *index)->line);
@@ -274,6 +278,7 @@ void parse_struct(TokenList* tokens, ObjectList* object_list, int* index, Struct
274278

275279
struct_decl->fields = field_address;
276280
struct_decl->field_count = field_count;
281+
struct_decl->id = -1;
277282
}
278283

279284
void parse_function_call(TokenList* tokens, ObjectList* object_list, int* index, FunctionCall* func_call) {
@@ -355,7 +360,7 @@ void parse_if(TokenList* tokens, ObjectList* object_list, int* index, IfStatemen
355360
if_stmt->condition = parse_expression(tokens, object_list, index);
356361

357362
if (get_token(tokens, *index)->type != TOKEN_END_OF_LINE) {
358-
printf("Error: Invalid if statement, expected end of line but found `%s` in line %d\n", get_token(tokens, *index)->line);
363+
printf("Error: Invalid if statement, expected end of line but found `%s` in line %d\n", get_token(tokens, *index)->value, get_token(tokens, *index)->line);
359364
exit(1);
360365
}
361366

@@ -398,7 +403,7 @@ void parse_if(TokenList* tokens, ObjectList* object_list, int* index, IfStatemen
398403

399404
elif.condition = parse_expression(tokens, object_list, index);
400405
if (get_token(tokens, *index)->type != TOKEN_END_OF_LINE) {
401-
printf("Error: Invalid elif statement, expected end of line but found `%s` in line %d\n", get_token(tokens, *index)->line);
406+
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);
402407
exit(1);
403408
}
404409
(*index)++;
@@ -438,7 +443,7 @@ void parse_while(TokenList* tokens, ObjectList* object_list, int* index, WhileLo
438443
while_loop->condition = parse_expression(tokens, object_list, index);
439444

440445
if (get_token(tokens, *index)->type != TOKEN_END_OF_LINE) {
441-
printf("Error: Invalid while statement, expected end of line but found `%s` in line %d\n", get_token(tokens, *index)->line);
446+
printf("Error: Invalid while statement, expected end of line but found `%s` in line %d\n", get_token(tokens, *index)->value, get_token(tokens, *index)->line);
442447
exit(1);
443448
}
444449

@@ -670,8 +675,8 @@ void debug_parser_node(Node* node) {
670675
}
671676
else if (node->type == NODE_VARIABLE_ASSIGNMENT) {
672677
VariableAssignment* var_assign = (VariableAssignment*)node->data;
673-
for (int i = 0; i < var_assign->scope_size; i++) {
674-
printf("%s ", var_assign->scope[i]->value);
678+
for (int i = 0; i < var_assign->identifier.size; i++) {
679+
printf("%s ", var_assign->identifier.scope[i]->value);
675680
}
676681
printf("= ");
677682
debug_parser_node(&var_assign->value);

0 commit comments

Comments
 (0)