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 "types/token.h"
12
13static inline void
14_module_fill_queue(module_t* module);
15
17module_init(module_t* module, const char* filepath)
18{
19 module->file = fopen(filepath, "r");
20
21 if (! module->file) {
22 return ERR_FILE_NOT_FOUND;
23 }
24
25 strncpy(module->filepath, filepath, MODULE_MAX_FILEPATH_SIZE);
26 module->filepath[MODULE_MAX_FILEPATH_SIZE] = '\0';
27
28 ast_node_init(&module->ast, NULL, NT_ROOT, NULL);
29
30 module->tokens = malloc(sizeof(token_t) * MODULE_INITIAL_TOKENS_LENGTH);
31 module->tokens_count = 0;
32 module->_tokens_length = MODULE_INITIAL_TOKENS_LENGTH;
33
34 module->errors = malloc(sizeof(module_error_t) * MODULE_INITIAL_ERRORS_LENGTH);
35 module->errors_count = 0;
36 module->_errors_length = MODULE_INITIAL_ERRORS_LENGTH;
37
39
40 return ERR_OK;
41}
42
45{
47 module->_tokens_length += MODULE_TOKENS_LENGTH_INCREMENT;
48 module->tokens = realloc(module->tokens, sizeof(token_t) * module->_tokens_length);
49 }
50
51 return &module->tokens[module->tokens_count++];
52}
53
56{
57 _module_fill_queue(module);
58
59 return cqueue_get(&module->_queue, chret);
60}
61
63module_lookup(module_t* module, int* chret, unsigned int seek)
64{
65 _module_fill_queue(module);
66
67 return cqueue_lookup(&module->_queue, chret, seek);
68}
69
70void
72{
73 fclose(module->file);
74 module->file = NULL;
75
76 free(module->tokens);
77 module->tokens = NULL;
78
80
81 for (int i = 0; i < module->errors_count; i++) {
83 }
84
85 free(module->errors);
86 module->errors = NULL;
87
89}
90
91void
92module_add_error(module_t* module, unsigned int line, unsigned int column, unsigned int length, const char* message)
93{
95 module->_errors_length += MODULE_ERRORS_LENGTH_INCREMENT;
96 module->errors = realloc(module->errors, sizeof(module_error_t) * module->_errors_length);
97 }
98
99 module_error_t* error = &module->errors[module->errors_count++];
100
101 error->line = line;
102 error->column = column;
103 error->length = length;
104
105 dstring_init(&error->message, 100);
106 dstring_copy(&error->message, message);
107
108 DPRINTF1("Error at %s:%d:%d -> %s\n", module->filepath, line, column, message);
109}
110
111void
113{
114 module_error_t* error;
115
116 for (int i = 0; i < module->errors_count; i++) {
117 error = &module->errors[i];
118
119 error_module_ctx(module, error->line, error->column, error->length, error->message.data);
120 fputc('\n', stderr);
121 }
122}
123
124static inline void
125_module_fill_queue(module_t* module)
126{
127 char content[MODULE_MAX_QUEUE_LENGTH];
128 size_t size;
129
131 size = fread(content, 1, MODULE_MAX_QUEUE_LENGTH - module->_queue.length, module->file);
132
133 for (int i = 0, ch = 0; i < size && ch != EOF; i++) {
134 ch = content[i];
135
137 }
138 }
139}
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:46
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:31
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:92
token_t * module_add_token(module_t *module)
Add a new token to module.
Definition module.c:44
error_code_t module_init(module_t *module, const char *filepath)
Initialize a module struct.
Definition module.c:17
void module_print_errors(module_t *module)
Print all errors on the module.
Definition module.c:112
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:63
void module_close(module_t *module)
Close the given module.
Definition module.c:71
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:55
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
@ NT_ROOT
Definition ast.h:15
#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.