evalCond.c
2.02 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
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <expValue.h>
#include <tepal_pars.h>
#include <evalCond.h>
/*
* static function (internal only)
*/
static
int
evalIntComp (int op, s_expVal * _op1, s_expVal * _op2)
{
long op1 = expValueInt (_op1);
long op2 = expValueInt (_op2);
switch (op)
{
case EQ: return (op1 == op2);
case NE: return (op1 != op2);
case LT: return (op1 < op2);
case GT: return (op1 > op2);
case LE: return (op1 <= op2);
case GE: return (op1 >= op2);
}
}
static
int
evalFloatComp (int op, s_expVal * _op1, s_expVal * _op2)
{
double op1 = expValueFloat (_op1);
double op2 = expValueFloat (_op2);
switch (op)
{
case EQ: return (op1 == op2);
case NE: return (op1 != op2);
case LT: return (op1 < op2);
case GT: return (op1 > op2);
case LE: return (op1 <= op2);
case GE: return (op1 >= op2);
}
}
static
int
evalStringComp (int op, s_expVal * _op1, s_expVal * _op2)
{
char * op1 = expValueString (_op1);
char * op2 = expValueString (_op2);
long ret;
switch (op)
{
case EQ: ret = (! strcmp (op1, op2));
case NE: ret = strcmp (op1, op2);
case LT: ret = (strcmp (op1, op2) < 0) ? 1 : 0;
case GT: ret = (strcmp (op1, op2) > 0) ? 1 : 0;
case LE: ret = (strcmp (op1, op2) <= 0) ? 1 : 0;
case GE: ret = (strcmp (op1, op2) >= 0) ? 1 : 0;
}
free (op1);
free (op2);
return ret;
}
/*
* public functions (interface)
*/
int
evalCondExpr (s_expVal * _op)
{
switch (expValueGetType (_op))
{
case EXP_TYP_INT: return expValueInt (_op);
case EXP_TYP_FLOAT: return expValueInt (_op);
case EXP_TYP_STRING:
{
char * op = expValueString (_op);
long ret = strlen (op);
free (op);
return ret;
}
}
return 0;
}
int
evalComp (int op, s_expVal * op1, s_expVal * op2)
{
switch (expValueGetType (op1))
{
case EXP_TYP_INT:
return evalIntComp (op, op1, op2);
case EXP_TYP_FLOAT:
return evalFloatComp (op, op1, op2);
case EXP_TYP_STRING:
return evalStringComp (op, op1, op2);
}
}