libmya 0.1.0
Library to parse Mya language.
Loading...
Searching...
No Matches
eval_expression.c
Go to the documentation of this file.
1#include <stdbool.h>
2
3#include "evaluator.h"
4#include "hashtable.h"
5#include "module.h"
6#include "types/operators.h"
7
8inline int64_t
9_op_binary_eval(int64_t a, operator_t op, int64_t b);
10
11inline int64_t
12_op_unary_eval(operator_t op, int64_t a);
13
14int64_t
16{
17 int64_t result;
18
19 if (ast->type != NT_EXPRESSION) {
21 module,
22 ast->token->line,
23 ast->token->column,
24 ast->token->lexeme.length,
25 "Unexpected token inside an expression."
26 );
27
28 return 0;
29 }
30
31 switch (ast->token->type) {
32 case TK_OPEN_PARENS:
33 return eval_expression(variables, module, ast->children);
34 case TK_NUMBER:
35 return ast->token->value;
36 case TK_IDENTIFIER:
37 if (hashtable_get(variables, ast->token->lexeme.data, &result) != ERR_OK) {
38 module_add_error(module, ast->token->line, ast->token->column, ast->token->lexeme.length, "Undefined variable.");
39 return 0;
40 }
41
42 return result;
43 case TK_OPERATOR:
44 bool is_binary = (ast->children_count == 2);
45 ast_node_t* a = &ast->children[0];
46
47 if (is_binary) {
48 ast_node_t* b = &ast->children[1];
49 return _op_binary_eval(
50 eval_expression(variables, module, a),
51 ast->token->value,
52 eval_expression(variables, module, b)
53 );
54 }
55
56 return _op_unary_eval(ast->token->value, eval_expression(variables, module, a));
57 default:
58 return 0;
59 }
60}
61
62inline int64_t
63_op_binary_eval(int64_t a, operator_t op, int64_t b)
64{
65 switch (op) {
66 case OP_DIV:
67 return a / b;
68 case OP_MULT:
69 return a * b;
70 case OP_MINUS:
71 return a - b;
72 case OP_PLUS:
73 return a + b;
74 case OP_SHIFT_LEFT:
75 return a << b;
76 case OP_SHIFT_RIGHT:
77 return a >> b;
78 case OP_AND:
79 return a & b;
80 case OP_XOR:
81 return a ^ b;
82 case OP_OR:
83 return a | b;
84 default:
85 return 0;
86 }
87}
88
89inline int64_t
91{
92 switch (op) {
93 case OP_NOT:
94 return ~a;
95 case OP_NEGATE:
96 return -a;
97 default:
98 return 0;
99 }
100}
@ ERR_OK
Definition err.h:15
int64_t _op_unary_eval(operator_t op, int64_t a)
int64_t _op_binary_eval(int64_t a, operator_t op, int64_t b)
int64_t eval_expression(hashtable_t *variables, module_t *module, ast_node_t *ast)
Evaluates a mathetical expression.
error_code_t hashtable_get(hashtable_t *hashtable, const char *key, int64_t *value)
Get the value of the specified key inside the hashtable.
Definition hashtable.c:73
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
enum operator operator_t
@ OP_NEGATE
Definition operators.h:9
@ OP_SHIFT_LEFT
Definition operators.h:13
@ OP_NOT
Definition operators.h:10
@ OP_SHIFT_RIGHT
Definition operators.h:14
@ OP_PLUS
Definition operators.h:12
@ OP_DIV
Definition operators.h:6
@ OP_XOR
Definition operators.h:15
@ OP_MULT
Definition operators.h:8
@ OP_AND
Definition operators.h:5
@ OP_MINUS
Definition operators.h:7
@ OP_OR
Definition operators.h:11
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
Struct that represents a Mya module.
Definition module.h:36
long long int value
Integer value of the token.
Definition token.h:38
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_EXPRESSION
Definition ast.h:14
struct hashtable hashtable_t
A hashtable.
struct module module_t
Struct that represents a Mya module.
@ TK_NUMBER
Definition token.h:21
@ TK_IDENTIFIER
Definition token.h:19
@ TK_OPEN_PARENS
Definition token.h:24
@ TK_OPERATOR
Definition token.h:25