libmya 0.1.0
Library to parse Mya language.
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
parse_statement_inst.c File Reference
#include "ast.h"
#include "macro_utils.h"
#include "module.h"
#include "parser.h"
#include "types/keywords.h"
Include dependency graph for parse_statement_inst.c:

Go to the source code of this file.

Functions

unsigned int parse_statement_inst (module_t *module, ast_node_t *parent, token_t *token)
 Parse a inst statement adding it as a children on parent AST node.
 

Function Documentation

◆ parse_statement_inst()

unsigned int parse_statement_inst ( module_t * module,
ast_node_t * parent,
token_t * token )

Parse a inst statement adding it as a children on parent AST node.

Parameters
moduleThe module where the AST is.
parentThe AST node parent for the statement.
tokenThe token where to start parsing the statement.
Returns
The number of tokens used on the statement.

Definition at line 14 of file parse_statement_inst.c.

15{
16 ast_node_t* node_statement = ast_add_children(parent, NT_STATEMENT, token);
17 token_t* tkid = token + 1;
18
19 if (tkid->type != TK_IDENTIFIER) {
21 module,
22 tkid->line,
23 tkid->column,
24 tkid->lexeme.length,
25 "Expected an identifier here. Example: inst mov[16](arg1: register[32], arg2: immediate[8]) { ... }"
26 );
27
28 return 1;
29 }
30
31 ast_add_children(node_statement, NT_IDENTIFIER, tkid);
32 unsigned int ntokens =
33 2 +
34 parse_size_spec(module, node_statement, token + 2, "inst mov[16](arg1: register[32], arg2: immediate[8]) { ... }");
35
36 token_t* tkopen_parens = token + ntokens;
37
38 if (tkopen_parens->type != TK_OPEN_PARENS) {
40 module,
41 tkopen_parens->line,
42 tkopen_parens->column,
43 tkopen_parens->lexeme.length,
44 "Expected a list of arguments for the instruction inside parentheses. Example: inst mov[16](arg1: register[32], "
45 "arg2: immediate[8]) { ... }"
46 );
47
48 return ntokens + 1;
49 }
50
51 ntokens += _parse_inst_args(module, node_statement, tkopen_parens);
52
53 token_t* tkopen_braces = token + ntokens;
54 if (tkopen_braces->type != TK_OPEN_BRACES) {
56 module,
57 tkopen_braces->line,
58 tkopen_braces->column,
59 tkopen_braces->lexeme.length,
60 "Expected an open braces at start of the body of the instruction. Example: inst mov[16](arg1: register[32], "
61 "arg2: immediate[8]) { ... }"
62 );
63
64 return ntokens;
65 }
66
67 ntokens += parse_fieldlist_spec(module, node_statement, token + ntokens);
68
69 return ntokens;
70}
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_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_fieldlist_spec(module_t *module, ast_node_t *parent, token_t *token)
Parse a field list specification in the format field = EXPRESSION, ... adding it as a children on par...
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 token token_t
Struct for a Mya token.
@ TK_IDENTIFIER
Definition token.h:19
@ TK_OPEN_BRACES
Definition token.h:22
@ TK_OPEN_PARENS
Definition token.h:24