/
math.cpp
487 lines (456 loc) · 12.3 KB
/
math.cpp
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
//Don't try using this in your own project, it's got a lot of Asar-specific tweaks. Use mathlib.cpp instead.
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include "autoarray.h"
#include "scapegoat.hpp"
#include "libstr.h"
#include "libsmw.h"
#include "asar.h"
bool math_pri=true;
bool math_round=false;
extern bool emulatexkas;
//int bp(const char * str)
//{
// throw str;
// return 0;
//}
//#define error(str) bp(str)
#define error(str) throw str
static const char * str;
static long double getnumcore();
static long double getnum();
static long double eval(int depth);
bool confirmname(const char * name);
//label (bool foundLabel) (bool forwardLabel)
//userfunction
bool foundlabel;
bool forwardlabel;
extern lightweight_map<string, string> defines;
struct funcdat {
autoptr<char*> name;
int numargs;
autoptr<char*> argbuf;//this one isn't used, it's just to free it up
autoptr<char**> arguments;
autoptr<char*> content;
};
autoarray<funcdat> userfunc;
int numuserfunc=0;
const char * createuserfunc(const char * name, const char * arguments, const char * content)
{
if (!confirmqpar(content)) return "Mismatched parentheses";
for (int i=0;i<numuserfunc;i++)
{
if (!strcmp(name, userfunc[i].name))
{
return "Duplicate function name";
}
}
funcdat& thisone=userfunc[numuserfunc];
thisone.name=strdup(name);
thisone.argbuf=strdup(arguments);
thisone.arguments=qsplit(thisone.argbuf, ",", &(thisone.numargs));
thisone.content=strdup(content);
for (int i=0;thisone.arguments[i];i++)
{
if (!confirmname(thisone.arguments[i]))
{
userfunc.remove(numuserfunc);
return "Invalid argument name";
}
}
numuserfunc++;
return NULL;
}
char ** funcargnames;
long double * funcargvals;
int numuserfuncargs;
int snestopc_pick(int addr);
static long double validaddr(long double in, long double len)
{
int addr=snestopc_pick(in);
if (addr<0 || addr+len-1>romlen_r) return 0;
else return 1;
}
static long double read1(long double in)
{
int addr=snestopc_pick(in);
if (addr<0) error("read1(): Address doesn't map to ROM.");
else if (addr+1>romlen_r) error("Address out of bounds.");
else return
romdata_r[addr ] ;
return 0;
}
static long double read2(long double in)
{
int addr=snestopc_pick(in);
if (addr<0) error("read2(): Address doesn't map to ROM.");
else if (addr+2>romlen_r) error("Address out of bounds.");
else return
romdata_r[addr ] |
(romdata_r[addr+1]<< 8);
return 0;
}
static long double read3(long double in)
{
int addr=snestopc_pick(in);
if (addr<0) error("read3(): Address doesn't map to ROM.");
else if (addr+3>romlen_r) error("Address out of bounds.");
else return
romdata_r[addr ] |
(romdata_r[addr+1]<< 8)|
(romdata_r[addr+2]<<16);
return 0;
}
static long double read4(long double in)
{
int addr=snestopc_pick(in);
if (addr<0) error("read4(): Address doesn't map to ROM.");
else if (addr+4>romlen_r) error("Address out of bounds.");
else return
romdata_r[addr ] |
(romdata_r[addr+1]<< 8)|
(romdata_r[addr+2]<<16)|
(romdata_r[addr+3]<<24);
return 0;
}
static long double read1s(long double in, long double def)
{
int addr=snestopc_pick(in);
if (addr<0) return def;
else if (addr+0>romlen_r) return def;
else return
romdata_r[addr ] ;
return 0;
}
static long double read2s(long double in, long double def)
{
int addr=snestopc_pick(in);
if (addr<0) return def;
else if (addr+1>romlen_r) return def;
else return
romdata_r[addr ] |
(romdata_r[addr+1]<< 8);
return 0;
}
static long double read3s(long double in, long double def)
{
int addr=snestopc_pick(in);
if (addr<0) return def;
else if (addr+2>romlen_r) return def;
else return
romdata_r[addr ] |
(romdata_r[addr+1]<< 8)|
(romdata_r[addr+2]<<16);
return 0;
}
static long double read4s(long double in, long double def)
{
int addr=snestopc_pick(in);
if (addr<0) return def;
else if (addr+3>romlen_r) return def;
else return
romdata_r[addr ] |
(romdata_r[addr+1]<< 8)|
(romdata_r[addr+2]<<16)|
(romdata_r[addr+3]<<24);
return 0;
}
extern unsigned int table[256];
static long double getnumcore()
{
/*if (*str=='!' && false) // defines.
{
string find;
if (defines.find(str+1, find)) {
str = find;
return getnumcore(); // reiterate on define contents
}
}*/
if (*str=='(')
{
str++;
long double 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 (*str=='\'')
{
if (!str[1] || str[2]!='\'') error("Invalid character.");
unsigned int rval=table[(unsigned char)str[1]];
str+=3;
return rval;
}
if (isdigit(*str))
{
return strtod(str, (char**)&str);
}
if (isalpha(*str) || *str=='_' || *str=='.' || *str=='?')
{
const char * start=str;
while (isalnum(*str) || *str=='_') str++;
int len=str-start;
while (*str==' ') str++;
if (*str=='(')
{
str++;
while (*str==' ') str++;
autoarray<long double> 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.");
}
}
long double rval;
for (int i=0;i<numuserfunc;i++)
{
if ((int)strlen(userfunc[i].name)==len && !strncmp(start, userfunc[i].name, len))
{
if (userfunc[i].numargs!=numparams) error("Wrong number of parameters to function.");
char ** oldfuncargnames=funcargnames;
long double * oldfuncargvals=funcargvals;
const char * oldstr=str;
int oldnumuserfuncargs=numuserfuncargs;
funcargnames=userfunc[i].arguments;
funcargvals=params;
str=userfunc[i].content;
numuserfuncargs=numparams;
rval=eval(0);
funcargnames=oldfuncargnames;
funcargvals=oldfuncargvals;
str=oldstr;
numuserfuncargs=oldnumuserfuncargs;
return rval;
}
}
if (*str=='_') str++;
#define func(name, numpar, code) \
if (!strncasecmp(start, name, len)) \
{ \
if (numparams==numpar) return (code); \
else error("Wrong number of parameters to function."); \
}
#define varfunc(name, code) \
if (!strncasecmp(start, name, len)) \
{ \
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("read1", 1, read1(params[0]));
func("read2", 1, read2(params[0]));
func("read3", 1, read3(params[0]));
func("read4", 1, read4(params[0]));
func("read1", 2, read1s(params[0], params[1]));
func("read2", 2, read2s(params[0], params[1]));
func("read3", 2, read3s(params[0], params[1]));
func("read4", 2, read4s(params[0], params[1]));
func("canread1", 1, validaddr(params[0], 1));
func("canread2", 1, validaddr(params[0], 2));
func("canread3", 1, validaddr(params[0], 3));
func("canread4", 1, validaddr(params[0], 4));
func("canread", 2, validaddr(params[0], params[1]));
//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
{
for (int i=0;i<numuserfuncargs;i++)
{
if (!strncmp(start, funcargnames[i], len)) return funcargvals[i];
}
foundlabel=true;
int i=labelval(&start);
str=start;
//if (start!=str) error("Internal error. Send this patch to Alcaro.");//not gonna add sublabel/macrolabel support here
if (i==-1) forwardlabel=true;
return (int)i&0xFFFFFF;
//#define const(name, val) if (!strncasecmp(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 long double sanitize(long double val)
{
if (val!=val) error("NaN encountered.");
if (math_round) return (int)val;
return val;
}
static long double getnum()
{
while (*str==' ') str++;
#define prefix(name, func) if (!strncasecmp(str, name, strlen(name))) { str+=strlen(name); long double val=getnum(); return sanitize(func); }
prefix("-", -val);
prefix("~", ~(int)val);
prefix("+", val);
if (emulatexkas) prefix("#", val);
#undef prefix
return sanitize(getnumcore());
}
extern autoarray<int> poslabels;
extern autoarray<int> neglabels;
static long double eval(int depth)
{
if (str[0]=='+' || str[0]=='-')
{
int i;
char top=str[0];
for (i=0;str[i] && str[i]!=')';i++)
{
if (str[i]!=top) goto notposneglabel;
}
str+=i;
foundlabel=true;
if (top=='+') forwardlabel=true;
if (top=='+') return labelval(S":pos_"+dec(i)+"_"+dec(poslabels[i]))&0xFFFFFF;
else return labelval(S":neg_"+dec(i)+"_"+dec(neglabels[i]))&0xFFFFFF;
}
notposneglabel:
recurseblock rec;
long double left=getnum();
long double right;
while (*str==' ') str++;
while (*str && *str!=')' && *str!=',')
{
while (*str==' ') str++;
if (math_round) left=(int)left;
#define oper(name, thisdepth, contents) \
if (!strncmp(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));
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;
}
//static autoptr<char*> freeme;
long double math(const char * s, const char ** e)
{
//free(freeme);
//freeme=NULL;
foundlabel=false;
forwardlabel=false;
try
{
str=s;
long double 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;
}
//catch (string& error)
//{
// freeme=strdup(error);
// *e=error;
// return 0;
//}
}
void initmathcore()
{
//not needed
}
void deinitmathcore()
{
userfunc.reset();
}