libmya 0.1.0
Library to parse Mya language.
Loading...
Searching...
No Matches
eval_bitfield.c File Reference
#include <ctype.h>
#include "evaluator.h"
#include "mir.h"
#include "module.h"
#include "types/keywords.h"
Include dependency graph for eval_bitfield.c:

Go to the source code of this file.

Functions

void eval_bitfield (mir_t *mir, module_t *module, ast_node_t *ast)
 Evaluates a bitfield statement.
 

Function Documentation

◆ eval_bitfield()

void eval_bitfield ( mir_t * mir,
module_t * module,
ast_node_t * ast )

Evaluates a bitfield statement.

Parameters
mirThe MIR struct where to add the intermediate represetation.
moduleMya module where the statement is in.
astThe AST node where start the statement.

Definition at line 9 of file eval_bitfield.c.

10{
11 if (ast->type != NT_STATEMENT || ast->token->value != KEY_BITFIELD) {
13 module,
14 ast->token->line,
15 ast->token->column,
16 ast->token->lexeme.length,
17 "Unexpected token where is expected a bitfield statement."
18 );
19
20 return;
21 }
22
23 ast_node_t* node_name = &ast->children[0];
24 ast_node_t* node_size = &ast->children[1];
25
26 int64_t size = eval_expression(&mir->variables, module, node_size);
27 if (size < 0 || size > 512) {
29 module,
30 node_size->token->line,
31 node_size->token->column,
32 node_size->token->lexeme.length,
33 "Bitfield size should be between 0 and 512."
34 );
35
36 return;
37 }
38
39 const char* name = node_name->token->lexeme.data;
40 if (! isupper(name[0])) {
42 module,
43 node_name->token->line,
44 node_name->token->column,
45 node_name->token->lexeme.length,
46 "Bitfield name should start with upper case."
47 );
48
49 return;
50 }
51
52 mir_bitfield_t* bitfield = mir_add_bitfield(mir, name, size);
53 if (ast->children_count != 3) {
54 return;
55 }
56
57 ast_node_t* node_body = &ast->children[2];
58 if (node_body->type != NT_BITFIELD_BODY) {
60 module,
61 node_body->token->line,
62 node_body->token->column,
63 node_body->token->lexeme.length,
64 "Bitfield body declaration expected here."
65 );
66
67 return;
68 }
69
70 int64_t total_size = 0;
71
72 for (int i = 0; i < node_body->children_count; i++) {
73 ast_node_t* field = &node_body->children[i];
74 const char* field_name = field->token->lexeme.data;
75 int64_t field_size = eval_expression(&mir->variables, module, field->children);
76
77 mir_bitfield_add_field(bitfield, field_name, field_size);
78
79 total_size += field_size;
80 }
81
82 if (size != total_size) {
84 module,
85 node_size->token->line,
86 node_size->token->column,
87 node_size->token->lexeme.length,
88 "Bitfield size should be equal to the sum of all field sizes."
89 );
90 }
91}
int64_t eval_expression(hashtable_t *variables, module_t *module, ast_node_t *ast)
Evaluates a mathetical expression.
@ KEY_BITFIELD
Definition keywords.h:5
mir_bitfield_field_t * mir_bitfield_add_field(mir_bitfield_t *bitfield, const char *name, uint32_t size)
Add a new field to a bitfield declaration.
Definition mir.c:94
mir_bitfield_t * mir_add_bitfield(mir_t *mir, const char *name, uint32_t size)
Add a new bitfield declaration.
Definition mir.c:58
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
unsigned int children_count
Definition ast.h:32
token_t * token
Definition ast.h:30
struct ast_node * children
Definition ast.h:31
node_type_t type
Definition ast.h:29
unsigned int length
The length of the string.
Definition dstring.h:13
char * data
Pointer for the raw string content (a normal C string).
Definition dstring.h:12
Mya in-memory intermediate representation.
Definition mir.h:117
hashtable_t variables
Hashtable of variables.
Definition mir.h:121
Struct that represents a Mya module.
Definition module.h:36
long long int value
Integer value of the token.
Definition token.h:38
dstring_t lexeme
Lexeme of the token.
Definition token.h:41
unsigned int line
Token line inside the module.
Definition token.h:36
unsigned int column
Column of the token position on the line.
Definition token.h:37
struct ast_node ast_node_t
@ NT_STATEMENT
Definition ast.h:21
@ NT_BITFIELD_BODY
Definition ast.h:12
struct mir_bitfield mir_bitfield_t
A bitfield declaration.