Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
wesuRage committed Jan 18, 2025
2 parents 13c7e5e + c00be2b commit f74e4a8
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 10 deletions.
10 changes: 2 additions & 8 deletions examples/a.glx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,8 @@ extern string itos(int);

def main() -> int:

int number := 4;

writeln("Number is less than 5!");

for (int i := -10; i < 10 + 1; ++i):
if (i > 5):
writeln(itos(i););
end;
while (1):
writeln("OMG!! OUR FIRST LOOP!");
end;

return 0;
Expand Down
2 changes: 1 addition & 1 deletion include/frontend/ast/definitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ typedef struct
typedef struct
{
AstNode *condition;
AstNode **body;
AstNode** body;
size_t body_count;
} WhileNode;

Expand Down
13 changes: 13 additions & 0 deletions include/frontend/parser/printer/nodes/statements/print_while.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef PRINT_WHILE_H
#define PRINT_WHILE_H

#include <stdio.h>
#include <stdlib.h>
#include "frontend/ast/definitions.h"
#include "frontend/parser/printer/print_indent.h"
#include "frontend/parser/printer/print_ast.h"
#include "frontend/parser/printer/visited.h"

void print_while(const AstNode *node, int depth, VisitedNodes *visited);

#endif // PRINT_WHILE_H
1 change: 1 addition & 0 deletions include/frontend/parser/printer/print_ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "frontend/parser/printer/nodes/statements/print_variable.h"
#include "frontend/parser/printer/nodes/statements/print_function.h"
#include "frontend/parser/printer/nodes/statements/print_for.h"
#include "frontend/parser/printer/nodes/statements/print_while.h"
#include "frontend/parser/printer/nodes/statements/print_if.h"
#include "frontend/parser/printer/nodes/statements/print_extern.h"
#include "frontend/parser/printer/nodes/statements/print_enum.h"
Expand Down
1 change: 1 addition & 0 deletions src/frontend/getTokenTypeName.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ const char* getTokenTypeName(TokenType type) {
case TOKEN_SHIFT_LEFT: return "SHIFT LEFT";
case TOKEN_SHIFT_RIGHT: return "SHIFT RIGHT";
case TOKEN_STRING: return "STRING";
case TOKEN_WHILE: return "WHILE";
case TOKEN_SWITCH: return "SWITCH";
case TOKEN_TRUE: return "TRUE";
case TOKEN_TYPE_BOOL: return "TYPE BOOL";
Expand Down
27 changes: 27 additions & 0 deletions src/frontend/parser/printer/nodes/statements/print_while.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "frontend/parser/printer/nodes/statements/print_while.h"

void print_while(const AstNode *node, int depth, VisitedNodes *visited) {
if (!node || node->kind != NODE_WHILE) return;

WhileNode *while_data = (WhileNode *)node->data;

print_indent(depth + 1);
if (while_data->condition) {
print_ast_node(while_data->condition, depth + 2, visited);
} else {
print_indent(depth + 2);
printf("Condition: <NULL>\n");
}

if (while_data->body_count > 0) {
print_indent(depth + 1);
printf("Body: \n");

for (size_t i = 0; i < while_data->body_count; i++) {
print_ast_node(while_data->body[i], depth + 2, visited);
}
} else {
print_indent(depth + 1);
printf("Body: <NULL>\n");
}
}
6 changes: 6 additions & 0 deletions src/frontend/parser/printer/print_ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const char* returnASTNodeName(NodeType node_type) {
case NODE_PARAMETER: return "Function Parameter";
case NODE_FOR: return "For Statement";
case NODE_IF: return "If Statement";
case NODE_WHILE: return "While Statement";
case NODE_DECORATOR: return "Decorator";
case NODE_MEMBER: return "Member Access";
case NODE_MEMBER_PROPERTY: return "Member Property";
Expand Down Expand Up @@ -110,6 +111,11 @@ void print_ast_node(const AstNode *node, int depth, VisitedNodes *visited) {
break;
}

case NODE_WHILE: {
print_while(node, depth, visited);
break;
}

case NODE_STRING: {
print_string(node, depth);
} break;
Expand Down
2 changes: 2 additions & 0 deletions src/frontend/parser/statements/parse_stmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "frontend/parser/statements/parse_function_declaration_stmt.h"
#include "frontend/parser/statements/parse_decorator_stmt.h"
#include "frontend/parser/statements/parse_for_stmt.h"
#include "frontend/parser/statements/parse_while_stmt.h"
#include "frontend/parser/statements/parse_if_stmt.h"
#include "frontend/parser/statements/parse_extern_stmt.h"
#include "frontend/parser/statements/parse_enum_stmt.h"
Expand All @@ -22,6 +23,7 @@ AstNode *parse_stmt(Parser *parser) {
case TOKEN_PACKAGE: return parse_package_stmt(parser);
case TOKEN_IMPORT: return parse_import_stmt(parser);
case TOKEN_FOR: return parse_for_stmt(parser);
case TOKEN_WHILE: return parse_while_stmt(parser);
case TOKEN_IF: return parse_if_stmt(parser);
case TOKEN_DEF: return parse_function_declaration_stmt(parser);
case TOKEN_AT: return parse_decorator_stmt(parser);
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/parser/statements/parse_while_stmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include "frontend/parser/core.h"
#include "utils.h"


AstNode *parse_while_stmt(Parser *parser) {
int line = current_token(parser).line;
int column_start = current_token(parser).column_start;
Expand All @@ -26,6 +25,7 @@ AstNode *parse_while_stmt(Parser *parser) {
WhileNode *while_data = MALLOC_S(sizeof(WhileNode));
while_data->body = NULL;
while_data->body_count = 0;
while_data->condition = condition;

while (not_eof(parser) && current_token(parser).type != TOKEN_END) {
while_data->body = REALLOC_S(
Expand Down

0 comments on commit f74e4a8

Please sign in to comment.