Skip to content

Commit 077fde0

Browse files
author
Heitor Danilo
committed
add: [statement]->to_string() debug functions
1 parent c272425 commit 077fde0

File tree

11 files changed

+257
-48
lines changed

11 files changed

+257
-48
lines changed

.vscode/settings.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@
1919
"methods.h": "c",
2020
"structs.h": "c",
2121
"mappers.h": "c",
22-
"actions.h": "c"
22+
"actions.h": "c",
23+
"characters.h": "c",
24+
"parser.h": "c",
25+
"ast.h": "c",
26+
"stddef.h": "c",
27+
"lib.h": "c",
28+
"def.h": "c",
29+
"chardef.h": "c",
30+
"langdef.h": "c",
31+
"token_test.h": "c"
2332
}
2433
}

src/ast/def.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#ifndef AST_DEFINITION_T
2+
#define AST_DEFINITION_T
3+
4+
#include <token/def.h>
5+
6+
struct Statement {
7+
char* (*token_literal)(struct Token *tok);
8+
void (*statement_node)(struct Statement *self);
9+
};
10+
11+
struct Expression {
12+
char* (*token_literal)(struct Token *tok);
13+
void (*expression_node)(struct Expression *self);
14+
};
15+
16+
struct Program {
17+
struct Statement **statments;
18+
int statments_len;
19+
};
20+
21+
struct Identifier {
22+
struct Expression *expression;
23+
struct Token *token;
24+
char *value;
25+
};
26+
27+
struct Def_statement {
28+
struct Statement *statement;
29+
struct Token *token;
30+
struct Identifier *identifier; // name
31+
struct Expression *expression; // value
32+
};
33+
34+
#endif /* AST_DEFINITION_T */

src/ast/lib.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef AST_LIB_T
2+
#define AST_LIB_T
3+
4+
#include <ast/def.h>
5+
#include <token/def.h>
6+
7+
char* __AST_program_to_string(struct Program *program);
8+
char* __AST_expression_statement_to_string(struct Statement *statement);
9+
char* __AST_var_statement_to_string(struct Statement *statement);
10+
char* __AST_return_statement_to_string(struct Statement *statement);
11+
12+
#endif /* AST_LIB_T */

src/ast/utils.c

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
#include <stdlib.h>
2+
#include <string.h>
3+
4+
#include <ast/lib.h>
5+
6+
/**
7+
* Converts a program to a string representation.
8+
*
9+
* @param prg The program to convert.
10+
*
11+
* @return The string representation of the program.
12+
*/
13+
char* __AST_program_to_string(struct Program* prg) {
14+
char* out = strdup("");
15+
if (out == NULL) return NULL;
16+
17+
for (int i = 0; i < prg->statements_size; i++) {
18+
char* statement_string = prg->statements[i]->to_string(prg->statements[i]);
19+
if (statement_string == NULL) {
20+
free(out);
21+
return NULL;
22+
}
23+
24+
char* temp = realloc(out, (strlen(out) + strlen(statement_string) + 1) * sizeof(char));
25+
if (temp == NULL) {
26+
free(out);
27+
free(statement_string);
28+
return NULL;
29+
}
30+
31+
out = temp;
32+
33+
strcat(out, statement_string);
34+
free(statement_string);
35+
}
36+
37+
// remove '\n' from at the end of the string
38+
out[strlen(out) - 1] = '\0';
39+
40+
return out;
41+
}
42+
43+
/**
44+
* Converts an expression statement to a string representation.
45+
*
46+
* @param stmt The expression statement to convert.
47+
*
48+
* @return The string representation of the expression statement.
49+
*/
50+
char* __AST_expression_statement_to_string(struct Statement* stmt) {
51+
if (stmt->expression != NULL) {
52+
// ...
53+
}
54+
55+
char* out = '\0';
56+
return out;
57+
}
58+
59+
/**
60+
* Converts a return statement to a string representation.
61+
*
62+
* @param stmt The return statement to convert.
63+
*
64+
* @return The string representation of the return statement.
65+
*/
66+
char* __AST_return_statement_to_string(struct Statement* stmt) {
67+
char* out = malloc(sizeof(char));
68+
out[0] = '\0';
69+
70+
// Allocate memory for "return " to be stored in temp
71+
char* temp = realloc(out,
72+
(strlen(stmt->token->literal) + strlen(" ") + strlen(";") + strlen("\n") + 1) * sizeof(char)
73+
);
74+
75+
if (temp == NULL) {
76+
free(out);
77+
return NULL;
78+
}
79+
80+
out = temp;
81+
82+
strcpy(out, stmt->token->literal); // "return"
83+
strcat(out, " "); // "return "
84+
85+
if (stmt->expression != NULL) {
86+
// ...
87+
}
88+
89+
strcat(out, ";");
90+
strcat(out, "\n");
91+
// At the end of this call, temp will look something like: "return {expr};\n"
92+
return out;
93+
}
94+
95+
/**
96+
* Converts a variable statement to a string representation.
97+
*
98+
* @param stmt The variable statement to convert.
99+
*
100+
* @return The string representation of the variable statement.
101+
*/
102+
char* __AST_var_statement_to_string(struct Statement* stmt) {
103+
char* out = malloc(sizeof(char));
104+
out[0] = '\0';
105+
106+
// Calculate the required length for the output string
107+
int out_length = strlen(stmt->token->literal) + strlen(" ");
108+
109+
if (stmt->mut == 1) {
110+
out_length += strlen("mut ");
111+
}
112+
113+
out_length += strlen(stmt->identifier->value) + strlen(" = ");
114+
115+
char* temp = realloc(out, (out_length + strlen(";") + strlen("\n") + 1) * sizeof(char));
116+
if (temp == NULL) {
117+
free(out);
118+
return NULL;
119+
}
120+
121+
out = temp;
122+
123+
strcpy(out, stmt->token->literal); // "var"
124+
strcat(out, stmt->mut == 0 ? " " : " mut "); // "var " || "var mut "
125+
strcat(out, stmt->identifier->value); // "var {ident}"
126+
strcat(out, " = "); // "var {ident} = "
127+
128+
if (stmt->expression != NULL) {
129+
// ...
130+
}
131+
132+
strcat(out, ";");
133+
strcat(out, "\n");
134+
// At the end of this call, temp will look something like: "var {ident} = {expr};\n"
135+
return out;
136+
}

src/helpers/characters.c

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/helpers/characters.h

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/parser/def.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef PARSER_DEFINITION_H
2+
#define PARSER_DEFINITION_H
3+
4+
#include <ast/def.h>
5+
#include <lexer/def.h>
6+
#include <token/def.h>
7+
8+
struct Parser {
9+
struct Lexer *lexer;
10+
11+
struct Token *curr_tok;
12+
struct Token *next_tok;
13+
14+
void (*next_token)(struct Parser *self);
15+
struct Program *(*parse_program)(struct Parser *self);
16+
};
17+
18+
#endif /* PARSER_DEFINITION_H */

src/parser/init.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <stdlib.h>
2+
3+
#include <lexer/lib.h>
4+
#include <parser/lib.h>
5+
6+
struct Parser *new_parser(struct Lexer *lexer) {
7+
struct Parser *parser = malloc(sizeof(struct Parser));
8+
9+
if (!parser) {
10+
return NULL;
11+
}
12+
13+
parser->lexer = lexer;
14+
parser->next_token = __P_next_token;
15+
16+
parser->next_token(parser);
17+
parser->next_token(parser);
18+
19+
return parser;
20+
}

src/parser/lib.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef PARSER_LIB_H
2+
#define PARSER_LIB_H
3+
4+
#include <ast/def.h>
5+
#include <lexer/def.h>
6+
#include <parser/def.h>
7+
8+
struct Parser *new_parser(struct Lexer *lexer);
9+
10+
struct Program *__parse_program(struct Parser *self);
11+
void __P_next_token(struct Parser *self);
12+
13+
#endif /* PARSER_LIB_H */

src/parser/utils.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <ast/def.h>
2+
3+
#include <lexer/lib.h>
4+
#include <parser/lib.h>
5+
6+
7+
void __P_next_token(struct Parser *self) {
8+
self->curr_tok = self->next_tok;
9+
self->curr_tok = self->lexer->next_token(self->lexer);
10+
}
11+
12+
struct Program *__parse_program(struct Parser *self) {
13+
return NULL;
14+
}

0 commit comments

Comments
 (0)