evalExpr.c
2.79 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <malloc.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <expValue.h>
#include <tepal.h>
#include <tepal_pars.h>
#include <evalExpr.h>
/*
* static function (internal only)
*/
static
inline
s_expVal *
stringPlus (s_expVal * _op1, s_expVal * _op2)
{
s_expVal * ret;
char * op1 = expValueString (_op1);
char * op2 = expValueString (_op2);
char * retVal = (char *) malloc (
sizeof (char) * (strlen (op1) + strlen (op2) + 1));
strcpy (retVal, op1);
strcat (retVal, op2);
ret = expValueStringNew (retVal);
free (retVal);
free (op2);
free (op1);
return ret;
}
static
inline
s_expVal *
stringNeg (s_expVal * _op)
{
s_expVal * ret;
int i;
char * op = expValueString (_op);
int len = strlen (op) - 1;
for (i=0; i<=(len/2); i++)
{
char tmp = op[i];
op[i] = op[len-i];
op[len-i] = tmp;
}
ret = expValueStringNew (op);
free (op);
return ret;
}
static
inline
s_expVal *
evalIntExp (int op, s_expVal * _op1, s_expVal * _op2)
{
long op1 = expValueInt (_op1);
long op2 = (_op2 != NULL) ? expValueInt (_op2) : 0;
if (op == NEG) return expValueIntNew (- op1);
switch (expValueGetType (_op2))
{
case EXP_TYP_INT:
case EXP_TYP_FLOAT:
switch (op)
{
case PLUS: return expValueIntNew (op1 + op2);
case MINUS: return expValueIntNew (op1 - op2);
case TIMES: return expValueIntNew (op1 * op2);
case OVER: return expValueIntNew (op1 / op2);
case MODULO: return expValueIntNew (op1 % op2);
}
case EXP_TYP_STRING:
if (op == PLUS)
return stringPlus (_op1, _op2);
exitError (ERR_STRING_OPERATOR, op);
}
}
static
inline
s_expVal *
evalFloatExp (int op, s_expVal * _op1, s_expVal * _op2)
{
double op1 = expValueFloat (_op1);
double op2 = (_op2 != NULL) ? expValueFloat (_op2) : 0.0;
if (op == NEG) return expValueFloatNew (- op1);
switch (expValueGetType (_op2))
{
case EXP_TYP_INT:
case EXP_TYP_FLOAT:
switch (op)
{
case MODULO: exitError (ERR_FLOAT_OPERATOR, op);
case PLUS: return expValueFloatNew (op1 + op2);
case MINUS: return expValueFloatNew (op1 - op2);
case TIMES: return expValueFloatNew (op1 * op2);
case OVER: return expValueFloatNew (op1 / op2);
}
case EXP_TYP_STRING:
if (op == PLUS)
return stringPlus (_op1, _op2);
exitError (ERR_STRING_OPERATOR, op);
}
}
/*
* public functions (interface)
*/
s_expVal *
evalExpr (int op, s_expVal * op1, s_expVal * op2)
{
switch (expValueGetType (op1))
{
case EXP_TYP_INT:
return evalIntExp (op, op1, op2);
case EXP_TYP_FLOAT:
if (op != MODULO)
return evalFloatExp (op, op1, op2);
exitError (ERR_FLOAT_OPERATOR, op);
case EXP_TYP_STRING:
if (op == PLUS)
return stringPlus (op1, op2);
if (op == NEG)
return stringNeg (op1);
exitError (ERR_STRING_OPERATOR, op);
}
}