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 "debug.h"
7#include "dstring.h"
8#include "error_handling.h"
9#include "module.h"
10#include "queue.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
40
41 return ERR_OK;
42}
43
46{
48 module->_tokens_length += MODULE_TOKENS_LENGTH_INCREMENT;
49 module->tokens = realloc(module->tokens, sizeof(token_t) * module->_tokens_length);
50 }
51
52 return &module->tokens[module->tokens_count++];
53}
54
57{
58 _module_fill_queue(module);
59
60 return cqueue_get(&module->_queue, chret);
61}
62
64module_lookup(module_t* module, int* chret, unsigned int seek)
65{
66 _module_fill_queue(module);
67
68 return cqueue_lookup(&module->_queue, chret, seek);
69}
70
71void
73{
74 fclose(module->file);
75 module->file = NULL;
76
77 for (int i = 0; i < module->tokens_count; i++) {
79 }
80
81 free(module->tokens);
82 module->tokens = NULL;
83
85
86 for (int i = 0; i < module->errors_count; i++) {
88 }
89
90 free(module->errors);
91 module->errors = NULL;
92
94}
95
96void
97module_add_error(module_t* module, unsigned int line, unsigned int column, unsigned int length, const char* message)
98{
100 module->_errors_length += MODULE_ERRORS_LENGTH_INCREMENT;
101 module->errors = realloc(module->errors, sizeof(module_error_t) * module->_errors_length);
102 }
103
104 module_error_t* error = &module->errors[module->errors_count++];
105
106 error->line = line;
107 error->column = column;
108 error->length = length;
109
110 dstring_init(&error->message, 100);
111 dstring_copy(&error->message, message);
112
113 DPRINTF1("Error at %s:%d:%d -> %s\n", module->filepath, line, column, message);
114}
115
116void
118{
119 module_error_t* error;
120
121 for (int i = 0; i < module->errors_count; i++) {
122 error = &module->errors[i];
123
124 error_module_ctx(module, error->line, error->column, error->length, error->message.data);
125 fputc('\n', stderr);
126 }
127}
128
129static inline void
130_module_fill_queue(module_t* module)
131{
132 char content[MODULE_MAX_QUEUE_LENGTH];
133 size_t size;
134
136 size = fread(content, 1, MODULE_MAX_QUEUE_LENGTH - module->_queue.length, module->file);
137
138 for (int i = 0, ch = 0; i < size && ch != EOF; i++) {
139 ch = content[i];
140
142 }
143 }
144}
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
#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:97
token_t * module_add_token(module_t *module)
Add a new token to module.
Definition module.c:45
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:117
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:64
void module_close(module_t *module)
Close the given module.
Definition module.c:72
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:56
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
unsigned int length
The current number of items on the queue.
Definition queue.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:27
unsigned int line
The module's line where the error is.
Definition module.h:23
unsigned int length
The length of the part where has an error in the line.
Definition module.h:25
unsigned int column
The line's column where the error starts.
Definition module.h:24
Struct that represents a Mya module.
Definition module.h:34
ast_node_t ast
AST of the module.
Definition module.h:37
module_error_t * errors
List of errors inside the module.
Definition module.h:38
cqueue_t _queue
Internal queue to read file character by character.
Definition module.h:43
char filepath[MODULE_MAX_FILEPATH_SIZE+1]
Module's filepath.
Definition module.h:44
unsigned int _tokens_length
Internal field with the size of tokens allocated memory.
Definition module.h:40
unsigned int _errors_length
Internal field with the size of errors allocated memory.
Definition module.h:42
unsigned int errors_count
Number of errors on errors list.
Definition module.h:41
token_t * tokens
List of tokens inside the module.
Definition module.h:36
FILE * file
Module's file handler.
Definition module.h:35
unsigned int tokens_count
Number of tokens on tokens list.
Definition module.h:39
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:16
struct module_error module_error_t
Struct that represents a error inside a Mya module.
#define MODULE_MAX_QUEUE_LENGTH
Definition module.h:15
struct token token_t
Struct for a Mya token.