libmya 0.1.0
Library to parse Mya language.
Loading...
Searching...
No Matches
module.c
Go to the documentation of this file.
1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4
5#include "ast.h"
6#include "cqueue.h"
7#include "debug.h"
8#include "dstring.h"
9#include "error_handling.h"
10#include "module.h"
11#include "token.h"
12#include "types/token.h"
13
14static inline void
15_module_fill_queue(module_t* module);
16
18module_init(module_t* module, const char* filepath)
19{
20 module->file = fopen(filepath, "r");
21
22 if (! module->file) {
23 return ERR_FILE_NOT_FOUND;
24 }
25
26 strncpy(module->filepath, filepath, MODULE_MAX_FILEPATH_SIZE);
27 module->filepath[MODULE_MAX_FILEPATH_SIZE] = '\0';
28
29 ast_node_init(&module->ast, NULL, NT_ROOT, NULL);
30
31 module->tokens = malloc(sizeof(token_t) * MODULE_INITIAL_TOKENS_LENGTH);
32 module->tokens_count = 0;
33 module->_tokens_length = MODULE_INITIAL_TOKENS_LENGTH;
34
35 module->errors = malloc(sizeof(module_error_t) * MODULE_INITIAL_ERRORS_LENGTH);
36 module->errors_count = 0;
37 module->_errors_length = MODULE_INITIAL_ERRORS_LENGTH;
38
39 module->submodules = malloc(sizeof(module_t) * MODULE_INITIAL_SUBMODULES_LENGTH);
40 module->submodules_count = 0;
41 module->_submodules_length = MODULE_INITIAL_SUBMODULES_LENGTH;
42
44
45 return ERR_OK;
46}
47
50{
52 module->_tokens_length += MODULE_TOKENS_LENGTH_INCREMENT;
53 module->tokens = realloc(module->tokens, sizeof(token_t) * module->_tokens_length);
54 }
55
56 return &module->tokens[module->tokens_count++];
57}
58
61{
62 _module_fill_queue(module);
63
64 return cqueue_get(&module->_queue, chret);
65}
66
68module_lookup(module_t* module, int* chret, unsigned int seek)
69{
70 _module_fill_queue(module);
71
72 return cqueue_lookup(&module->_queue, chret, seek);
73}
74
75void
77{
78 fclose(module->file);
79 module->file = NULL;
80
81 for (int i = 0; i < module->tokens_count; i++) {
83 }
84
85 free(module->tokens);
86 module->tokens = NULL;
87
89
90 for (int i = 0; i < module->errors_count; i++) {
92 }
93
94 free(module->errors);
95 module->errors = NULL;
96
98
99 for (int i = 0; i < module->submodules_count; i++) {
101 }
102
103 free(module->submodules);
104 module->submodules = NULL;
105}
106
109{
111 module->_submodules_length += MODULE_SUBMODULES_LENGTH_INCREMENT;
112 module->submodules = realloc(module->submodules, sizeof(module_t) * module->_submodules_length);
113 }
114
115 return &module->submodules[module->submodules_count++];
116}
117
118void
119module_add_error(module_t* module, unsigned int line, unsigned int column, unsigned int length, const char* message)
120{
122 module->_errors_length += MODULE_ERRORS_LENGTH_INCREMENT;
123 module->errors = realloc(module->errors, sizeof(module_error_t) * module->_errors_length);
124 }
125
126 module_error_t* error = &module->errors[module->errors_count++];
127
128 error->line = line;
129 error->column = column;
130 error->length = length;
131
132 dstring_init(&error->message, 100);
133 dstring_copy(&error->message, message);
134
135 DPRINTF1("Error at %s:%d:%d -> %s\n", module->filepath, line, column, message);
136}
137
138void
140{
141 module_error_t* error;
142
143 for (int i = 0; i < module->submodules_count; i++) {
145 }
146
147 for (int i = 0; i < module->errors_count; i++) {
148 error = &module->errors[i];
149
150 error_module_ctx(module, error->line, error->column, error->length, error->message.data);
151 fputc('\n', stderr);
152 }
153}
154
155unsigned int
157{
158 unsigned int total = 0;
159
160 for (int i = 0; i < module->submodules_count; i++) {
162 }
163
164 total += module->errors_count;
165 return total;
166}
167
168static inline void
169_module_fill_queue(module_t* module)
170{
171 char content[MODULE_MAX_QUEUE_LENGTH];
172 size_t size;
173
175 size = fread(content, 1, MODULE_MAX_QUEUE_LENGTH - module->_queue.length, module->file);
176
177 for (int i = 0, ch = 0; i < size && ch != EOF; i++) {
178 ch = content[i];
179
181 }
182 }
183}
void ast_node_init(ast_node_t *node, ast_node_t *parent, node_type_t type, token_t *token)
Initializes an AST node.
Definition ast.c:17
void ast_close(ast_node_t *root)
Closes the given AST root node and all children.
Definition ast.c:29
void cqueue_close(cqueue_t *queue)
Close the given circular queue.
Definition cqueue.c:80
error_code_t cqueue_add(cqueue_t *queue, int item)
Add a value to the circular queue.
Definition cqueue.c:20
error_code_t cqueue_get(cqueue_t *queue, int *item)
Get the current value from the circular queue, removing it from the queue.
Definition cqueue.c:35
error_code_t cqueue_lookup(const cqueue_t *queue, int *item, unsigned int seek)
Get a value from the circular queue, without removing it from the queue.
Definition cqueue.c:50
void cqueue_init(cqueue_t *queue, unsigned int max_length)
Initializes a circular queue.
Definition cqueue.c:10
#define DPRINTF1(fmt,...)
Definition debug.h:16
void dstring_init(dstring_t *string, unsigned int buffer_size)
Initializes a dynamic string (dstring).
Definition dstring.c:10
void dstring_copy(dstring_t *string, const char *source)
Copies the content of source to the dstring.
Definition dstring.c:50
void dstring_close(dstring_t *string)
Closes the dynamic string.
Definition dstring.c:18
enum error_code error_code_t
Enumeration of error codes.
@ ERR_FILE_NOT_FOUND
Definition err.h:17
@ ERR_OK
Definition err.h:15
#define ERROR_BREAK(expr)
Definition err.h:32
void error_module_ctx(module_t *module, unsigned int line, unsigned int column, unsigned int length, const char *message)
Print a error message formated to show the code context inside the module.
void module_add_error(module_t *module, unsigned int line, unsigned int column, unsigned int length, const char *message)
Add error for the given module.
Definition module.c:119
token_t * module_add_token(module_t *module)
Add a new token to module.
Definition module.c:49
error_code_t module_init(module_t *module, const char *filepath)
Initialize a module struct.
Definition module.c:18
void module_print_errors(module_t *module)
Print all errors on the module.
Definition module.c:139
module_t * module_add_submodule(module_t *module)
Add submodule for the given module.
Definition module.c:108
error_code_t module_lookup(module_t *module, int *chret, unsigned int seek)
Get a character on module's file, without removing it from the queue.
Definition module.c:68
void module_close(module_t *module)
Close the given module.
Definition module.c:76
error_code_t module_getc(module_t *module, int *chret)
Get next character on module's file, removing it from the queue.
Definition module.c:60
unsigned int module_count_errors(module_t *module)
Returns the total number of errors on the given module and submodules.
Definition module.c:156
unsigned int length
The current number of items on the queue.
Definition cqueue.h:8
char * data
Pointer for the raw string content (a normal C string).
Definition dstring.h:12
dstring_t message
The error message to print.
Definition module.h:29
unsigned int line
The module's line where the error is.
Definition module.h:25
unsigned int length
The length of the part where has an error in the line.
Definition module.h:27
unsigned int column
The line's column where the error starts.
Definition module.h:26
Struct that represents a Mya module.
Definition module.h:36
ast_node_t ast
AST of the module.
Definition module.h:39
module_error_t * errors
List of errors inside the module.
Definition module.h:40
cqueue_t _queue
Internal queue to read file character by character.
Definition module.h:48
char filepath[MODULE_MAX_FILEPATH_SIZE+1]
Module's filepath.
Definition module.h:49
unsigned int _tokens_length
Internal field with the size of tokens allocated memory.
Definition module.h:43
unsigned int _errors_length
Internal field with the size of errors allocated memory.
Definition module.h:45
unsigned int submodules_count
Number of submodules on submodules list.
Definition module.h:46
unsigned int errors_count
Number of errors on errors list.
Definition module.h:44
token_t * tokens
List of tokens inside the module.
Definition module.h:38
FILE * file
Module's file handler.
Definition module.h:37
struct module * submodules
List of submodules included by include statement.
Definition module.h:41
unsigned int _submodules_length
Internal field with the size of submodules allocated memory.
Definition module.h:47
unsigned int tokens_count
Number of tokens on tokens list.
Definition module.h:42
void token_close(token_t *token)
Closes the given token.
Definition token.c:19
@ NT_ROOT
Definition ast.h:19
#define MODULE_MAX_FILEPATH_SIZE
Definition module.h:10
struct module module_t
Struct that represents a Mya module.
#define MODULE_MIN_QUEUE_LENGTH
Definition module.h:18
struct module_error module_error_t
Struct that represents an error inside a Mya module.
#define MODULE_MAX_QUEUE_LENGTH
Definition module.h:17
struct token token_t
Struct for a Mya token.