libmya 0.1.0
Library to parse Mya language.
Loading...
Searching...
No Matches
eval_bitfield_spec.c
Go to the documentation of this file.
1#include "dstring.h"
2#include "evaluator.h"
3#include "mir.h"
4#include "module.h"
5
8
9void
11{
12 if (ast->type != NT_BITFIELD_SPEC) {
14 module,
15 ast->token->line,
16 ast->token->column,
17 ast->token->lexeme.length,
18 "Unexpected token where is expected a bitfield specification expression."
19 );
20
21 return;
22 }
23
24 ast_node_t* field_spec = &ast->children[0];
25 if (field_spec->type != NT_FIELD_SPEC) {
27 module,
28 ast->token->line,
29 ast->token->column,
30 ast->token->lexeme.length,
31 "Unexpected token where is expected a bitfield specification's field list or expression."
32 );
33
34 return;
35 }
36
37 dstring_copy(&spec->name, ast->token->lexeme.data);
38 spec->type = _spec_type(ast);
39
40 switch (spec->type) {
41 case FT_SPEC:
43 eval_bitfield_spec(spec->spec, module, &field_spec->children[0], variables);
44 break;
45 case FT_LITERAL:
46 spec->value = eval_expression(variables, module, field_spec->children);
47 break;
48 case FT_IDENTIFIER:
49 dstring_copy(&spec->identifier, field_spec->children[0].token->lexeme.data);
50 break;
51 case FT_FIELDS:
52 for (int i = 0; i < field_spec->children_count; i++) {
53 mir_bitfield_spec_t* field =
55 eval_bitfield_spec(field, module, &field_spec->children[i], variables);
56 }
57 break;
58 }
59}
60
63{
64 ast_node_t* field_spec = &spec->children[0];
65
66 if (field_spec->children[0].type == NT_BITFIELD_SPEC) {
67 return FT_SPEC;
68 }
69
70 if (field_spec->children[0].token->type == TK_EQUAL) {
71 return FT_FIELDS;
72 }
73
74 if (field_spec->children_count == 1 && field_spec->children[0].token->type == TK_IDENTIFIER) {
75 return FT_IDENTIFIER;
76 }
77
78 return FT_LITERAL;
79}
void dstring_copy(dstring_t *string, const char *source)
Copies the content of source to the dstring.
Definition dstring.c:50
void eval_bitfield_spec(mir_bitfield_spec_t *spec, module_t *module, ast_node_t *ast, hashtable_t *variables)
Evaluates a bitfield specification expression.
mir_bitfield_spec_type_t _spec_type(ast_node_t *spec)
int64_t eval_expression(hashtable_t *variables, module_t *module, ast_node_t *ast)
Evaluates a mathetical expression.
mir_bitfield_spec_t * mir_bitfield_spec_set_spec(mir_bitfield_spec_t *spec, const char *name, mir_bitfield_spec_type_t type)
Set the bitfield spec's sub bitfield spec.
Definition mir.c:326
mir_bitfield_spec_t * mir_bitfield_spec_add_field(mir_bitfield_spec_t *spec, const char *name, mir_bitfield_spec_type_t type)
Add a new bitfield spec's field to the given bitfield spec.
Definition mir.c:304
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
mir_bitfield_spec_type_t type
Specify the type of this bitfield specification.
Definition mir.h:70
dstring_t identifier
Bitfield's value identifier.
Definition mir.h:71
struct mir_bitfield_spec * spec
Bitfield's list of field specifications.
Definition mir.h:73
dstring_t name
Name of the bitfield.
Definition mir.h:69
int64_t value
Bitfield's literal value.
Definition mir.h:72
Struct that represents a Mya module.
Definition module.h:36
token_type_t type
Token type.
Definition token.h:35
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_FIELD_SPEC
Definition ast.h:15
@ NT_BITFIELD_SPEC
Definition ast.h:13
struct hashtable hashtable_t
A hashtable.
@ FT_LITERAL
Bitfield spec value is a literal value.
Definition mir.h:32
@ FT_FIELDS
Bitfied spec value has a body with a list of fields.
Definition mir.h:28
@ FT_SPEC
Bitfield spec value is a bitfield spec.
Definition mir.h:34
@ FT_IDENTIFIER
Bitfield spec value is a identifier.
Definition mir.h:30
enum mir_bitfield_spec_type mir_bitfield_spec_type_t
A bitfield spec type.
struct mir_bitfield_spec mir_bitfield_spec_t
A bitfield or bitfield's field declaration.
struct module module_t
Struct that represents a Mya module.
@ TK_IDENTIFIER
Definition token.h:19
@ TK_EQUAL
Definition token.h:18