-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluator.h
82 lines (73 loc) · 2.17 KB
/
evaluator.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#ifndef H_EVALUATOR
#define H_EVALUATOR
#include "variable_type.h"
#include "map.h"
#define EVALUATOR_DEBUG 0
#define EVALUATOR_PRINT_EXPRESSION 0
#define ARITHEMTIC_OPERATION_TEMPLATE(name,op)\
Variable operation_##name (Variable a, Variable b) {\
switch(a.type) {\
case Integer:\
switch(b.type) {\
case Integer: return (Variable)integer(a.i op b.i);\
case Float: return (Variable){.type=Float, .f=(a.i op b.f)};\
}\
break;\
case Float:\
switch(b.type) {\
case Integer: return (Variable){.type=Float, .f=(a.f op b.i)};\
case Float: return (Variable){.type=Float, .f=(a.f op b.f)};\
}\
}\
eval_error(a, b, #op);\
}\
#define RELATION_OPERATION_TEMPLATE(name,op)\
Variable operation_##name (Variable a, Variable b) {\
switch(a.type) {\
case Integer:\
switch(b.type) {\
case Integer: return (Variable)integer(a.i op b.i);\
case Float: return (Variable)integer(a.i op b.f);\
}\
break;\
case Float:\
switch(b.type) {\
case Integer: return (Variable)integer(a.f op b.i);\
case Float: return (Variable)integer(a.f op b.f);\
}\
}\
eval_error(a, b, #op);\
}\
#define UNARY_OPERATION_TEMPLATE(name,op)\
Variable operation_##name (Variable a) {\
switch(a.type) {\
case Integer: return (Variable)integer(op a.i);\
case Float: return (Variable){.type=Float, .f=op a.f};\
}\
eval_error(a, a, #op);\
}\
enum {
Invalid = 0,
Identifier,
Vague,
Number,
BinaryOperator,
BinaryOperator_p1,
UnaryOperator,
ParenthesesLeft,
ParenthesesRight,
StringSymbol,
FloatingPoint,
Operator,
None
};
enum Operand {
IntOperand = 0,
FloatOperand,
ParenthesisOperand,
VariableOperand,
UnknownOperand
};
Variable evaluate(MapObject *locals, const char* expr);
void eval_testing();
#endif