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
|
#include "variable.h"
#include "parser.h"
#include <ctype.h>
#include <stdlib.h>
#include <memory.h>
#include <string.h>
#include <shelpers.h>
extern int atoi(const char *);
char *fixstring(char *s)
{
char *n = malloc(strlen(s) + 1);
int j = 0;
for (int i = 0; s[i] != '\0'; i++, j++) {
if (s[i] == '\\') {
if (s[i + 1] == 'n')
n[j] = '\n';
i++;
} else {
n[j] = s[i];
}
}
n[j] = '\0';
return n;
}
variable *make_varn(variable *v, float value)
{
if (v == 0)
v = (variable *)malloc(sizeof(variable));
v->used = 0;
v->fromc = 0;
v->valtype = NUMBER;
v->value.f = value;
return v;
}
variable *make_vars(variable *v, const char *value)
{
if (v == 0)
v = (variable *)malloc(sizeof(variable));
v->used = 0;
v->fromc = 0;
v->valtype = STRING;
v->value.p = (value != 0) ? (uint32_t)fixstring(value) : 0;
return v;
}
variable *make_varf(variable *v, uint8_t fromc, uint32_t func)
{
if (v == 0)
v = (variable *)malloc(sizeof(variable));
v->used = 0;
v->fromc = fromc;
v->valtype = FUNC;
v->value.p = func;
return v;
}
variable *make_vare(variable *v, const char *expr)
{
if (v == 0)
v = (variable *)malloc(sizeof(variable));
v->used = 0;
v->fromc = 0;
v->valtype = EXPR;
v->value.p = (uint32_t)strclone(expr);
return v;
}
int try_variable(char **name, const char *text)
{
if (name == 0)
return 0;
int neg = 1;
int i = 0;
if (text[0] == '-') {
neg = -1;
i++;
}
if (!isalpha(text[i]))
return 0;
for (i++; isalnum(text[i]); i++);
int o = (neg < 0);
if (neg < 0)
i--;
*name = (char *)malloc(i + 1);
strncpy(*name, text + o, i);
(*name)[i] = '\0';
return (neg > 0) ? i : -(i + 1);
}
int try_number(variable *v, const char *text)
{
if (v == 0)
return 0;
int decimal = -1;
char valid = 0;
int i = 0;
if (text[0] == '-')
i++;
do {
if (text[i] == '.') {
if (decimal >= 0) {
valid = 0;
break;
}
decimal = i;
} else if (isdigit(text[i])) {
valid |= 1;
} else {
break;
}
} while (text[++i] != '\0');
if (valid == 0)
return 0;
char *buf = (char *)malloc(i + 1);
strncpy(buf, text, i);
buf[i] = '\0';
make_varn(v, strtof(buf, 0));
free(buf);
return i;
}
|