-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmathlib.cpp
More file actions
225 lines (217 loc) · 6.3 KB
/
mathlib.cpp
File metadata and controls
225 lines (217 loc) · 6.3 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#if 0
//This file is not used by Asar, it's just included as a smarter alternative to editing math.cpp if
//you want it for your own projects (unless, of course, you need Asar's highly specialized features for some weird reason).
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include "autoarray.h"
bool math_pri=true;
bool math_round=false;
bool math_xorexp=false;
#define error(str) throw str
static const char * str;
static int64_t getnumcore();
static int64_t getnum();
static int64_t eval(int depth);
static int64_t getnumcore()
{
if (*str=='(')
{
str++;
int64_t rval=eval(0);
if (*str!=')') error("Mismatched parentheses.");
str++;
return rval;
}
if (*str=='$')
{
if (!isxdigit(str[1])) error("Invalid hex value.");
if (tolower(str[2])=='x') return -42;//let str get an invalid value so it'll throw an invalid operator later on
return strtoul(str+1, (char**)&str, 16);
}
if (*str=='%')
{
if (str[1]!='0' && str[1]!='1') error("Invalid binary value.");
return strtoul(str+1, (char**)&str, 2);
}
if (isdigit(*str) || *str=='.')
{
return strtod(str, (char**)&str);
}
if (isalpha(*str) || *str<0 || *str>127)
{
const char * start=str;
while (isalnum(*str) || *str=='_' || *str<0 || *str>127) str++;
int len=str-start;
while (*str==' ') str++;
if (*str=='(')
{
str++;
while (*str==' ') str++;
autoarray<int64_t> params;
int numparams=0;
if (*str!=')')
{
while (true)
{
while (*str==' ') str++;
params[numparams++]=eval(0);
while (*str==' ') str++;
if (*str==',')
{
str++;
continue;
}
if (*str==')')
{
str++;
break;
}
error("Malformed function call.");
}
}
#define func(name, numpar, code) \
if (!strncasecmp(start, name, len) && name[len]==0) \
{ \
if (numparams==numpar) return (code); \
else error("Wrong number of parameters to function."); \
}
#define varfunc(name, code) \
if (!strncasecmp(start, name, len) && name[len]==0) \
{ \
code; \
}
func("sqrt", 1, sqrt(params[0]));
func("sin", 1, sin(params[0]));
func("cos", 1, cos(params[0]));
func("tan", 1, tan(params[0]));
func("asin", 1, asin(params[0]));
func("acos", 1, acos(params[0]));
func("atan", 1, atan(params[0]));
func("arcsin", 1, asin(params[0]));
func("arccos", 1, acos(params[0]));
func("arctan", 1, atan(params[0]));
func("log", 1, log(params[0]));
func("log10", 1, log10(params[0]));
func("log2", 1, log(params[0])/log(2.0));
func("ln", 1, log(params[0]));
func("lg", 1, log10(params[0]));
//varfunc("min", {
// if (!numparams) error("Wrong number of parameters to function.");
// double minval=params[0];
// for (int i=1;i<numparams;i++)
// {
// if (params[i]<minval) minval=params[i];
// }
// return minval;
// });
//varfunc("max", {
// if (!numparams) error("Wrong number of parameters to function.");
// double maxval=params[0];
// for (int i=1;i<numparams;i++)
// {
// if (params[i]>maxval) maxval=params[i];
// }
// return maxval;
// });
#undef func
#undef varfunc
error("Unknown function.");
}
else
{
#define const(name, val) if (!strnicmp(start, name, len)) return val
const("pi", 3.141592653589793238462);
const("\xCF\x80", 3.141592653589793238462);
const("\xCE\xA0", 3.141592653589793238462);//case insensitive pi, yay
const("e", 2.718281828459045235360);
#undef const
error("Unknown constant.");
}
}
error("Invalid number.");
}
static int64_t sanitize(int64_t val)
{
if (val!=val) error("NaN encountered.");
if (math_round) return (int)val;
return val;
}
static int64_t getnum()
{
while (*str==' ') str++;
#define prefix(name, func) if (!strnicmp(str, name, strlen(name))) { str+=strlen(name); int64_t val=getnum(); return sanitize(func); }
prefix("-", -val);
prefix("~", ~(int)val);
prefix("+", val);
//prefix("#", val);
#undef prefix
return sanitize(getnumcore());
}
static int64_t eval(int depth)
{
int64_t left=getnum();
int64_t right;
while (*str==' ') str++;
while (*str && *str!=')' && *str!=',')
{
while (*str==' ') str++;
#define oper(name, thisdepth, contents) \
if (!strnicmp(str, name, strlen(name))) \
{ \
if (math_pri) \
{ \
if (depth<=thisdepth) \
{ \
str+=strlen(name); \
right=eval(thisdepth+1); \
} \
else return left; \
} \
else \
{ \
str+=strlen(name); \
right=getnum(); \
} \
left=sanitize(contents); \
continue; \
}
oper("**", 4, pow(left, right));
if (math_xorexp) oper("^", 4, pow(left, right));
oper("*", 3, left*right);
oper("/", 3, right?left/right:error("Division by zero."));
oper("%", 3, right?fmod(left, right):error("Modulos by zero."));
oper("+", 2, left+right);
oper("-", 2, left-right);
oper("<<", 1, (unsigned int)left<<(unsigned int)right);
oper(">>", 1, (unsigned int)left>>(unsigned int)right);
oper("&", 0, (unsigned int)left&(unsigned int)right);
oper("|", 0, (unsigned int)left|(unsigned int)right);
oper("^", 0, (unsigned int)left^(unsigned int)right);
error("Unknown operator.");
#undef oper
}
return left;
}
int64_t math(const char * s, const char ** e)
{
try
{
str=s;
int64_t rval=eval(0);
if (*str)
{
if (*str==',') error("Invalid input.");
else error("Mismatched parentheses.");
}
*e=NULL;
return rval;
}
catch (const char * error)
{
*e=error;
return 0;
}
}
#endif