libmya 0.1.0
Library to parse Mya language.
Loading...
Searching...
No Matches
eval_register.c
Go to the documentation of this file.
1#include "evaluator.h"
2#include "mir.h"
3#include "module.h"
4#include "types/keywords.h"
5
6void
8{
9 if (ast->type != NT_STATEMENT || ast->token->value != KEY_REGISTER) {
11 module,
12 ast->token->line,
13 ast->token->column,
14 ast->token->lexeme.length,
15 "Unexpected token where is expected a register statement."
16 );
17
18 return;
19 }
20
21 ast_node_t* node_name = &ast->children[0];
22 ast_node_t* node_size = &ast->children[1];
23 ast_node_t* node_spec = &ast->children[2];
24
25 int64_t size = eval_expression(&mir->variables, module, node_size);
26 if (size < 0 || size > 4096) {
28 module,
29 node_size->token->line,
30 node_size->token->column,
31 node_size->token->lexeme.length,
32 "Register size should be between 0 and 4096."
33 );
34
35 return;
36 }
37
38 mir_register_t* reg = mir_add_register(mir, node_name->token->lexeme.data, size);
39 eval_bitfield_spec(&reg->spec, module, node_spec, &mir->variables);
40}
void eval_bitfield_spec(mir_bitfield_spec_t *spec, module_t *module, ast_node_t *ast, hashtable_t *variables)
Evaluates a bitfield specification expression.
int64_t eval_expression(hashtable_t *variables, module_t *module, ast_node_t *ast)
Evaluates a mathetical expression.
void eval_register(mir_t *mir, module_t *module, ast_node_t *ast)
Evaluates a register statement.
@ KEY_REGISTER
Definition keywords.h:9
mir_register_t * mir_add_register(mir_t *mir, const char *name, uint32_t size)
Add a new register declaration for the specified MIR.
Definition mir.c:136
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 * 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
mir_bitfield_spec_t spec
Register's bitfield specification.
Definition mir.h:85
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
struct mir_register mir_register_t
Register declaration.
struct mir mir_t
Mya in-memory intermediate representation.
struct module module_t
Struct that represents a Mya module.