libmya 0.1.0
Library to parse Mya language.
Loading...
Searching...
No Matches
module.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ast.h"
#include "cqueue.h"
#include "debug.h"
#include "dstring.h"
#include "error_handling.h"
#include "module.h"
#include "token.h"
#include "types/token.h"
Include dependency graph for module.c:

Go to the source code of this file.

Functions

error_code_t module_init (module_t *module, const char *filepath)
 Initialize a module struct.
 
token_tmodule_add_token (module_t *module)
 Add a new token to module.
 
error_code_t module_getc (module_t *module, int *chret)
 Get next character on module's file, removing it from the queue.
 
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.
 
void module_close (module_t *module)
 Close the given module.
 
module_tmodule_add_submodule (module_t *module)
 Add submodule for the given 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.
 
void module_print_errors (module_t *module)
 Print all errors on the module.
 
unsigned int module_count_errors (module_t *module)
 Returns the total number of errors on the given module and submodules.
 

Function Documentation

◆ module_add_error()

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.

Parameters
moduleThe module where to add the error.
lineThe module's line where the error is.
columnThe line's column where the error starts.
lengthThe length of the part where has an error in the line. It's starts on column and ends on column + length.
messageThe error message.

Definition at line 119 of file module.c.

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}
#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
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
char filepath[MODULE_MAX_FILEPATH_SIZE+1]
Module's filepath.
Definition module.h:49
unsigned int _errors_length
Internal field with the size of errors allocated memory.
Definition module.h:45
unsigned int errors_count
Number of errors on errors list.
Definition module.h:44
struct module_error module_error_t
Struct that represents an error inside a Mya module.

◆ module_add_submodule()

module_t * module_add_submodule ( module_t * module)

Add submodule for the given module.

Parameters
moduleThe module where to add the submodule.
Returns
The pointer for the new submodule. The submodule isn't initialized yet. Is responsability of the caller to initialize the struct.

Definition at line 108 of file module.c.

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}
unsigned int submodules_count
Number of submodules on submodules list.
Definition module.h:46
unsigned int _submodules_length
Internal field with the size of submodules allocated memory.
Definition module.h:47

◆ module_add_token()

token_t * module_add_token ( module_t * module)

Add a new token to module.

Parameters
moduleThe module where the token will be added.
Returns
A pointer to the added token.

Definition at line 49 of file module.c.

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}
unsigned int _tokens_length
Internal field with the size of tokens allocated memory.
Definition module.h:43
unsigned int tokens_count
Number of tokens on tokens list.
Definition module.h:42

◆ module_close()

void module_close ( module_t * module)

Close the given module.

Parameters
moduleThe module to be closed.

Definition at line 76 of file module.c.

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}
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
void dstring_close(dstring_t *string)
Closes the dynamic string.
Definition dstring.c:18
void module_close(module_t *module)
Close the given module.
Definition module.c:76
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
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
void token_close(token_t *token)
Closes the given token.
Definition token.c:19

◆ module_count_errors()

unsigned int module_count_errors ( module_t * module)

Returns the total number of errors on the given module and submodules.

Parameters
moduleThe module where to count the errors.
Returns
The total number of errors.

Definition at line 156 of file module.c.

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}
unsigned int module_count_errors(module_t *module)
Returns the total number of errors on the given module and submodules.
Definition module.c:156

◆ module_getc()

error_code_t module_getc ( module_t * module,
int * chret )

Get next character on module's file, removing it from the queue.

Parameters
moduleThe module to get the character.
chretPointer where the character will be saved.
Returns
ERR_EMPTY on file doesn't have more characters.
ERR_OK on character as get successful.

Definition at line 60 of file module.c.

61{
62 _module_fill_queue(module);
63
64 return cqueue_get(&module->_queue, chret);
65}
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

◆ module_init()

error_code_t module_init ( module_t * module,
const char * filepath )

Initialize a module struct.

Open the filepath and initializes the tokens list.

Parameters
moduleThe module to be initialized.
filepathThe filepath for the module file.
Returns
ERR_FILE_NOT_FOUND on module file is not found.
ERR_OK on success.

Definition at line 18 of file module.c.

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}
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 cqueue_init(cqueue_t *queue, unsigned int max_length)
Initializes a circular queue.
Definition cqueue.c:10
@ ERR_FILE_NOT_FOUND
Definition err.h:17
@ ERR_OK
Definition err.h:15
@ NT_ROOT
Definition ast.h:19
#define MODULE_MAX_FILEPATH_SIZE
Definition module.h:10
#define MODULE_MAX_QUEUE_LENGTH
Definition module.h:17

◆ module_lookup()

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.

Parameters
moduleThe module to get the character.
chretPointer where the character will be saved.
seekThe index of the character to get (0 means the current, 1 the next and so one). Should not be >= MODULE_MIN_QUEUE_LENGTH to avoid possible errors.
Returns
ERR_EMPTY on file doesn't have more characters.
ERR_INVALID_INDEX on the seek is invalid.
ERR_OK on character as get successful.

Definition at line 68 of file module.c.

69{
70 _module_fill_queue(module);
71
72 return cqueue_lookup(&module->_queue, chret, seek);
73}
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

◆ module_print_errors()

void module_print_errors ( module_t * module)

Print all errors on the module.

Parameters
moduleThe module to print all errors.

Definition at line 139 of file module.c.

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}
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_print_errors(module_t *module)
Print all errors on the module.
Definition module.c:139
char * data
Pointer for the raw string content (a normal C string).
Definition dstring.h:12