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

Go to the source code of this file.

Functions

void eval_set (mir_t *mir, module_t *module, ast_node_t *ast)
 Evaluates a set statement.
 

Function Documentation

◆ eval_set()

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

Evaluates a set 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 8 of file eval_set.c.

9{
10 if (ast->type != NT_STATEMENT || ast->token->value != KEY_SET) {
12 module,
13 ast->token->line,
14 ast->token->column,
15 ast->token->lexeme.length,
16 "Unexpected token where is expected a set statement."
17 );
18
19 return;
20 }
21
22 ast_node_t* node_var = &ast->children[0];
23 ast_node_t* node_value = &ast->children[1];
24
25 int64_t value = eval_expression(&mir->variables, module, node_value);
26 hashtable_set(&mir->variables, node_var->token->lexeme.data, value);
27}
int64_t eval_expression(hashtable_t *variables, module_t *module, ast_node_t *ast)
Evaluates a mathetical expression.
void hashtable_set(hashtable_t *hashtable, const char *key, int64_t value)
Set the value of the specified key inside the hashtable.
Definition hashtable.c:51
@ KEY_SET
Definition keywords.h:10
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
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