libmya 0.1.0
Library to parse Mya language.
Loading...
Searching...
No Matches
parse_statement_register.c
Go to the documentation of this file.
1#include "ast.h"
2#include "module.h"
3#include "parser.h"
4
5unsigned int
7{
8 ast_node_t* node_statement = ast_add_children(parent, NT_STATEMENT, token);
9 token_t* tkid = token + 1;
10
11 if (tkid->type != TK_IDENTIFIER) {
13 module,
14 tkid->line,
15 tkid->column,
16 tkid->lexeme.length,
17 "Expected an identifier here. Example: register r1[32] = Reg{1}"
18 );
19
20 return 1;
21 }
22
23 ast_add_children(node_statement, NT_IDENTIFIER, tkid);
24 unsigned int ntokens = 2 + parse_size_spec(module, node_statement, token + 2, "register r1[32] = Reg{1}");
25
26 token_t* tkequal = token + ntokens;
27
28 if (tkequal->type != TK_EQUAL) {
30 module,
31 tkequal->line,
32 tkequal->column,
33 tkequal->lexeme.length,
34 "Expected an equal symbol here. Example: register r1[32] = Reg{1}"
35 );
36
37 return ntokens + 1;
38 }
39
40 return ntokens + 1 + parse_bitfield_spec(module, node_statement, tkequal + 1);
41}
ast_node_t * ast_add_children(ast_node_t *parent, node_type_t type, token_t *token)
Add a new children for the given AST node.
Definition ast.c:40
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:97
unsigned int parse_statement_register(module_t *module, ast_node_t *parent, token_t *token)
Parse a register statement adding it as a children on parent AST node.
unsigned int parse_size_spec(module_t *module, ast_node_t *parent, token_t *token, const char *example)
Parse a size specification in the format [ EXPRESSION ] adding it as a children on parent AST node.
Definition parse_common.c:7
unsigned int parse_bitfield_spec(module_t *module, ast_node_t *parent, token_t *token)
Parse a bitfield specification in the format Bitfield { FIELD_LIST } or Bitfield { EXPRESSION }...
unsigned int length
The length of the string.
Definition dstring.h:13
Struct that represents a Mya module.
Definition module.h:34
Struct for a Mya token.
Definition token.h:34
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_IDENTIFIER
Definition ast.h:17
@ NT_STATEMENT
Definition ast.h:21
struct module module_t
Struct that represents a Mya module.
struct token token_t
Struct for a Mya token.
@ TK_IDENTIFIER
Definition token.h:19
@ TK_EQUAL
Definition token.h:18