-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtamguint.cxx
executable file
·462 lines (362 loc) · 19.9 KB
/
tamguint.cxx
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
/*
* Tamgu (탐구)
*
* Copyright 2019-present NAVER Corp.
* under BSD 3-clause
*/
/* --- CONTENTS ---
Project : Tamgu (탐구)
Version : See tamgu.cxx for the version number
filename : tamguint.cxx
Date : 2017/09/01
Purpose :
Programmer : Claude ROUX (claude.roux@naverlabs.com)
Reviewer :
*/
#include "tamgu.h"
#include "tamguint.h"
#include "tamgustring.h"
#include "tamguustring.h"
#include "tamguivector.h"
#include "instructions.h"
#include "tamgulong.h"
//We need to declare once again our local definitions.
Exporting basebin_hash<intMethod> Tamguint::methods;
static ThreadLock classlock;
//MethodInitialization will add the right references to "name", which is always a new method associated to the object we are creating
void Tamguint::AddMethod(TamguGlobal* global, string name, intMethod func, unsigned long arity, string infos) {
short idname = global->Getid(name);
methods[idname] = func;
if (global->infomethods.find(a_int) != global->infomethods.end() &&
global->infomethods[a_int].find(name) != global->infomethods[a_int].end())
return;
global->infomethods[a_int][name] = infos;
global->RecordArity(a_int, idname, arity);
global->RecordArity(a_intthrough, idname, arity);
global->RecordArity(a_iloop, idname, arity);
}
void Tamguint::Setidtype(TamguGlobal* global) {
Locking lock(classlock);
if (Tamguint::methods.isEmpty())
Tamguint::InitialisationModule(global,"");
}
bool Tamguint::InitialisationModule(TamguGlobal* global, string version) {
methods.clear();
Tamguint::AddMethod(global, "isemoji", &Tamguint::MethodIsemoji, P_NONE, "isemoji(): Test if a string only contains emoji characters");
Tamguint::AddMethod(global, "emoji", &Tamguint::MethodEmoji, P_NONE, "emoji(): Return the textual description of an emoji");
global->returntypes[global->Getid("emoji")] = a_string;
Tamguint::AddMethod(global, "chr", &Tamguint::Methodchr, P_NONE, "chr(): return the character matching the unicode code");
Tamguint::AddMethod(global, "succ", &Tamguint::MethodSucc, P_NONE, "succ(): return the successor of an integer");
Tamguint::AddMethod(global, "pred", &Tamguint::MethodPred, P_NONE, "pred(): Return the predecessor of a byte.");
Tamguint::AddMethod(global, "isprime", &Tamguint::MethodPrime, P_NONE, "prime(): return true is the number is a prime");
Tamguint::AddMethod(global, "factors", &Tamguint::MethodPrimefactors, P_NONE, "factors(): return the list of prime factors");
global->returntypes[global->Getid("factors")] = a_ivector;
Tamguint::AddMethod(global, "bit", &Tamguint::MethodBit, P_ONE, "bit(int i): check if the ith bit is 0 or 1");
Tamguint::AddMethod(global, "invert", &Tamguint::MethodInvert, P_NONE, "invert(): value inversion as a fraction");
Tamguint::AddMethod(global, "format", &Tamguint::MethodFormat, P_ONE, "format(string pattern): Return a string matching the C pattern.");
Tamguint::AddMethod(global, "abs", &Tamguint::Methodabs, P_NONE, "abs(): call fabs on the value");
Tamguint::AddMethod(global, "acos", &Tamguint::Methodacos, P_NONE, "acos(): call acos on the value");
Tamguint::AddMethod(global, "acosh", &Tamguint::Methodacosh, P_NONE, "acosh(): call acosh on the value");
Tamguint::AddMethod(global, "asin", &Tamguint::Methodasin, P_NONE, "asin(): call asin on the value");
Tamguint::AddMethod(global, "asinh", &Tamguint::Methodasinh, P_NONE, "asinh(): call asinh on the value");
Tamguint::AddMethod(global, "atan", &Tamguint::Methodatan, P_NONE, "atan(): call atan on the value");
Tamguint::AddMethod(global, "atanh", &Tamguint::Methodatanh, P_NONE, "atanh(): call atanh on the value");
Tamguint::AddMethod(global, "cbrt", &Tamguint::Methodcbrt, P_NONE, "cbrt(): call cbrt on the value");
Tamguint::AddMethod(global, "cos", &Tamguint::Methodcos, P_NONE, "cos(): call cos on the value");
Tamguint::AddMethod(global, "cosh", &Tamguint::Methodcosh, P_NONE, "cosh(): call cosh on the value");
Tamguint::AddMethod(global, "erf", &Tamguint::Methoderf, P_NONE, "erf(): call erf on the value");
Tamguint::AddMethod(global, "erfc", &Tamguint::Methoderfc, P_NONE, "erfc(): call erfc on the value");
Tamguint::AddMethod(global, "exp", &Tamguint::Methodexp, P_NONE, "exp(): call exp on the value");
Tamguint::AddMethod(global, "exp2", &Tamguint::Methodexp2, P_NONE, "exp2(): call exp2 on the value");
Tamguint::AddMethod(global, "expm1", &Tamguint::Methodexpm1, P_NONE, "expm1(): call expm1 on the value");
Tamguint::AddMethod(global, "floor", &Tamguint::Methodfloor, P_NONE, "floor(): call floor on the value");
Tamguint::AddMethod(global, "lgamma", &Tamguint::Methodlgamma, P_NONE, "lgamma(): call lgamma on the value");
Tamguint::AddMethod(global, "ln", &Tamguint::Methodln, P_NONE, "ln(): call log on the value");
Tamguint::AddMethod(global, "log", &Tamguint::Methodlog, P_NONE, "log(): call log10 on the value");
Tamguint::AddMethod(global, "log1p", &Tamguint::Methodlog1p, P_NONE, "log1p(): call log1p on the value");
Tamguint::AddMethod(global, "log2", &Tamguint::Methodlog2, P_NONE, "log2(): call log2 on the value");
Tamguint::AddMethod(global, "logb", &Tamguint::Methodlogb, P_NONE, "logb(): call logb on the value");
Tamguint::AddMethod(global, "nearbyint", &Tamguint::Methodnearbyint, P_NONE, "nearbyint(): call nearbyint on the value");
Tamguint::AddMethod(global, "rint", &Tamguint::Methodrint, P_NONE, "rint(): call rint on the value");
Tamguint::AddMethod(global, "round", &Tamguint::Methodround, P_NONE, "round(): call round on the value");
Tamguint::AddMethod(global, "sin", &Tamguint::Methodsin, P_NONE, "sin(): call sin on the value");
Tamguint::AddMethod(global, "sinh", &Tamguint::Methodsinh, P_NONE, "sinh(): call sinh on the value");
Tamguint::AddMethod(global, "sqrt", &Tamguint::Methodsqrt, P_NONE, "sqrt(): call sqrt on the value");
Tamguint::AddMethod(global, "tan", &Tamguint::Methodtan, P_NONE, "tan(): call tan on the value");
Tamguint::AddMethod(global, "tanh", &Tamguint::Methodtanh, P_NONE, "tanh(): call tanh on the value");
Tamguint::AddMethod(global, "tgamma", &Tamguint::Methodtgamma, P_NONE, "tgamma(): call tgamma on the value");
Tamguint::AddMethod(global, "trunc", &Tamguint::Methodtrunc, P_NONE, "trunc(): call trunc on the value");
Tamguint::AddMethod(global, "even", &Tamguint::Methodeven, P_NONE, "even(): return true is the value is even");
Tamguint::AddMethod(global, "odd", &Tamguint::Methododd, P_NONE, "odd(): return true is the value is odd");
if (version != "") {
global->newInstance[a_int] = new Tamguint(0, global);
global->newInstance[a_intthrough] = global->newInstance[a_int];
global->RecordCompatibilities(a_int);
global->RecordCompatibilities(a_intthrough);
global->RecordCompatibilities(a_iloop);
}
Tamguatomicint::InitialisationModule(global, version);
return true;
}
Exporting basebin_hash<atomicintMethod> Tamguatomicint::methods;
Exporting short Tamguatomicint::idtype = 0;
void Tamguatomicint::AddMethod(TamguGlobal* global, string name, atomicintMethod func, unsigned long arity, string infos) {
short idname = global->Getid(name);
methods[idname] = func;
if (global->infomethods.find(idtype) != global->infomethods.end() &&
global->infomethods[idtype].find(name) != global->infomethods[idtype].end())
return;
global->infomethods[idtype][name] = infos;
global->RecordArity(idtype, idname, arity);
}
bool Tamguatomicint::InitialisationModule(TamguGlobal* global, string version) {
methods.clear();
Tamguatomicint::idtype = global->Getid("a_int");
Tamguatomicint::AddMethod(global, "isemoji", &Tamguatomicint::MethodIsemoji, P_NONE, "isemoji(): Test if a string only contains emoji characters");
Tamguatomicint::AddMethod(global, "emoji", &Tamguatomicint::MethodEmoji, P_NONE, "emoji(): Return the textual description of an emoji");
global->returntypes[global->Getid("emoji")] = a_string;
Tamguatomicint::AddMethod(global, "chr", &Tamguatomicint::Methodchr, P_NONE, "chr(): return the character matching the unicode code");
Tamguatomicint::AddMethod(global, "succ", &Tamguatomicint::MethodSucc, P_NONE, "succ(): return the successor of an integer");
Tamguatomicint::AddMethod(global, "pred", &Tamguatomicint::MethodPred, P_NONE, "pred(): Return the predecessor of a byte.");
Tamguatomicint::AddMethod(global, "isprime", &Tamguatomicint::MethodPrime, P_NONE, "prime(): return true is the number is a prime");
Tamguatomicint::AddMethod(global, "factors", &Tamguatomicint::MethodPrimefactors, P_NONE, "factors(): return the list of prime factors");
global->returntypes[global->Getid("factors")] = a_ivector;
Tamguatomicint::AddMethod(global, "bit", &Tamguatomicint::MethodBit, P_ONE, "bit(int i): check if the ith bit is 0 or 1");
Tamguatomicint::AddMethod(global, "invert", &Tamguatomicint::MethodInvert, P_NONE, "invert(): value inversion as a fraction");
Tamguatomicint::AddMethod(global, "format", &Tamguatomicint::MethodFormat, P_ONE, "format(string pattern): Return a string matching the C pattern.");
Tamguatomicint::AddMethod(global, "abs", &Tamguatomicint::Methodabs, P_NONE, "abs(): call fabs on the value");
Tamguatomicint::AddMethod(global, "acos", &Tamguatomicint::Methodacos, P_NONE, "acos(): call acos on the value");
Tamguatomicint::AddMethod(global, "acosh", &Tamguatomicint::Methodacosh, P_NONE, "acosh(): call acosh on the value");
Tamguatomicint::AddMethod(global, "asin", &Tamguatomicint::Methodasin, P_NONE, "asin(): call asin on the value");
Tamguatomicint::AddMethod(global, "asinh", &Tamguatomicint::Methodasinh, P_NONE, "asinh(): call asinh on the value");
Tamguatomicint::AddMethod(global, "atan", &Tamguatomicint::Methodatan, P_NONE, "atan(): call atan on the value");
Tamguatomicint::AddMethod(global, "atanh", &Tamguatomicint::Methodatanh, P_NONE, "atanh(): call atanh on the value");
Tamguatomicint::AddMethod(global, "cbrt", &Tamguatomicint::Methodcbrt, P_NONE, "cbrt(): call cbrt on the value");
Tamguatomicint::AddMethod(global, "cos", &Tamguatomicint::Methodcos, P_NONE, "cos(): call cos on the value");
Tamguatomicint::AddMethod(global, "cosh", &Tamguatomicint::Methodcosh, P_NONE, "cosh(): call cosh on the value");
Tamguatomicint::AddMethod(global, "erf", &Tamguatomicint::Methoderf, P_NONE, "erf(): call erf on the value");
Tamguatomicint::AddMethod(global, "erfc", &Tamguatomicint::Methoderfc, P_NONE, "erfc(): call erfc on the value");
Tamguatomicint::AddMethod(global, "exp", &Tamguatomicint::Methodexp, P_NONE, "exp(): call exp on the value");
Tamguatomicint::AddMethod(global, "exp2", &Tamguatomicint::Methodexp2, P_NONE, "exp2(): call exp2 on the value");
Tamguatomicint::AddMethod(global, "expm1", &Tamguatomicint::Methodexpm1, P_NONE, "expm1(): call expm1 on the value");
Tamguatomicint::AddMethod(global, "floor", &Tamguatomicint::Methodfloor, P_NONE, "floor(): call floor on the value");
Tamguatomicint::AddMethod(global, "lgamma", &Tamguatomicint::Methodlgamma, P_NONE, "lgamma(): call lgamma on the value");
Tamguatomicint::AddMethod(global, "ln", &Tamguatomicint::Methodln, P_NONE, "ln(): call log on the value");
Tamguatomicint::AddMethod(global, "log", &Tamguatomicint::Methodlog, P_NONE, "log(): call log10 on the value");
Tamguatomicint::AddMethod(global, "log1p", &Tamguatomicint::Methodlog1p, P_NONE, "log1p(): call log1p on the value");
Tamguatomicint::AddMethod(global, "log2", &Tamguatomicint::Methodlog2, P_NONE, "log2(): call log2 on the value");
Tamguatomicint::AddMethod(global, "logb", &Tamguatomicint::Methodlogb, P_NONE, "logb(): call logb on the value");
Tamguatomicint::AddMethod(global, "nearbyint", &Tamguatomicint::Methodnearbyint, P_NONE, "nearbyint(): call nearbyint on the value");
Tamguatomicint::AddMethod(global, "rint", &Tamguatomicint::Methodrint, P_NONE, "rint(): call rint on the value");
Tamguatomicint::AddMethod(global, "round", &Tamguatomicint::Methodround, P_NONE, "round(): call round on the value");
Tamguatomicint::AddMethod(global, "sin", &Tamguatomicint::Methodsin, P_NONE, "sin(): call sin on the value");
Tamguatomicint::AddMethod(global, "sinh", &Tamguatomicint::Methodsinh, P_NONE, "sinh(): call sinh on the value");
Tamguatomicint::AddMethod(global, "sqrt", &Tamguatomicint::Methodsqrt, P_NONE, "sqrt(): call sqrt on the value");
Tamguatomicint::AddMethod(global, "tan", &Tamguatomicint::Methodtan, P_NONE, "tan(): call tan on the value");
Tamguatomicint::AddMethod(global, "tanh", &Tamguatomicint::Methodtanh, P_NONE, "tanh(): call tanh on the value");
Tamguatomicint::AddMethod(global, "tgamma", &Tamguatomicint::Methodtgamma, P_NONE, "tgamma(): call tgamma on the value");
Tamguatomicint::AddMethod(global, "trunc", &Tamguatomicint::Methodtrunc, P_NONE, "trunc(): call trunc on the value");
Tamguatomicint::AddMethod(global, "even", &Tamguatomicint::Methodeven, P_NONE, "even(): return true is the value is even");
Tamguatomicint::AddMethod(global, "odd", &Tamguatomicint::Methododd, P_NONE, "odd(): return true is the value is odd");
if (version != "") {
global->newInstance[Tamguatomicint::idtype] = new Tamguatomicint(0, global);
global->RecordCompatibilities(Tamguatomicint::idtype);
}
return true;
}
Tamgu* Tamguint::MethodIsemoji(Tamgu* contextualpattern, short idthread, TamguCall* callfunc) {
return booleantamgu[c_is_emoji((TAMGUCHAR)value)];
}
Tamgu* Tamguint::MethodEmoji(Tamgu* contextualpattern, short idthread, TamguCall* callfunc) {
string s = Emoji((TAMGUCHAR)value);
return globalTamgu->Providewithstring(s);
}
Tamgu* Tamguint::Methodchr(Tamgu* contextualpattern, short idthread, TamguCall* callfunc) {
Tamguustring* r = globalTamgu->Provideustring(L"");
r->value = (wchar_t)value;
return r;
}
Tamgu* Tamguint::MethodFormat(Tamgu* contextualpattern, short idthread, TamguCall* callfunc) {
char buffer[101];
Tamgu* kformat = callfunc->Evaluate(0, contextualpattern, idthread);
string sformat = kformat->String();
#ifdef WIN32
_set_invalid_parameter_handler(wrongSprintf);
errorsprintf = false;
sprintf_s(buffer, 100, STR(sformat), Integer());
if (errorsprintf == true)
return globalTamgu->Returnerror(e_incorrect_format_specifier, idthread);
#else
int spres;
spres = sprintf_s(buffer, 100, STR(sformat), Integer());
if (spres<0)
return globalTamgu->Returnerror("TAMGUI(124):Incorrect format specifier or size too long", idthread);
#endif
return globalTamgu->Providestring(buffer);
}
Tamgu* Tamguint::MethodBit(Tamgu* contextualpattern, short idthread, TamguCall* callfunc) {
long ith = callfunc->Evaluate(0, contextualpattern, idthread)->Integer();
long v = value;
if (ith < 0 || ith >= sizeof(long)* 8)
return aFALSE;
v &= 1 << ith;
if (!v)
return aFALSE;
return aTRUE;
}
//-----------------------------------------
Tamgu* Tamguatomicint::MethodIsemoji(Tamgu* contextualpattern, short idthread, TamguCall* callfunc) {
return booleantamgu[c_is_emoji((TAMGUCHAR)value)];
}
Tamgu* Tamguatomicint::MethodEmoji(Tamgu* contextualpattern, short idthread, TamguCall* callfunc) {
string s = Emoji((TAMGUCHAR)value);
return globalTamgu->Providewithstring(s);
}
Tamgu* Tamguatomicint::Methodchr(Tamgu* contextualpattern, short idthread, TamguCall* callfunc) {
Tamguustring* r = globalTamgu->Provideustring(L"");
r->value = (wchar_t)value;
return r;
}
Tamgu* Tamguatomicint::MethodFormat(Tamgu* contextualpattern, short idthread, TamguCall* callfunc) {
char buffer[101];
Tamgu* kformat = callfunc->Evaluate(0, contextualpattern, idthread);
string sformat = kformat->String();
#ifdef WIN32
_set_invalid_parameter_handler(wrongSprintf);
errorsprintf = false;
sprintf_s(buffer, 100, STR(sformat), Integer());
if (errorsprintf == true)
return globalTamgu->Returnerror(e_incorrect_format_specifier, idthread);
#else
int spres;
spres = sprintf_s(buffer, 100, STR(sformat), Integer());
if (spres<0)
return globalTamgu->Returnerror("TAMGUI(124):Incorrect format specifier or size too long", idthread);
#endif
return globalTamgu->Providestring(buffer);
}
Tamgu* Tamguatomicint::MethodBit(Tamgu* contextualpattern, short idthread, TamguCall* callfunc) {
long ith = callfunc->Evaluate(0, contextualpattern, idthread)->Integer();
long v = value;
if (ith < 0 || ith >= sizeof(long)* 8)
return aFALSE;
v &= 1 << ith;
if (!v)
return aFALSE;
return aTRUE;
}
void TamguLoopInteger::Callfunction() {
TamguCallFunction2 kfunc(function);
Tamgu* ki = globalTamgu->ProvideConstint(position);
kfunc.arguments.push_back(this);
kfunc.Push(ki);
Tamgu* ke = kfunc.Eval(aNULL, aNULL, globalTamgu->GetThreadid());
ke->Release();
ki->Resetreference();
}
Tamgu* TamguLoopInteger::Put(Tamgu* context, Tamgu* ke, short idthread) {
if (ke->isVectorContainer()) {
position = 0;
interval.clear();
for (int i = 0; i < ke->Size(); i++)
interval.push_back(ke->getinteger(i));
value = interval[0];
return aTRUE;
}
if (ke->Type() == a_iloop) {
TamguLoopInteger* kl = (TamguLoopInteger*)ke;
interval = kl->interval;
value = kl->value;
position = kl->position;
return aTRUE;
}
if (interval.size() == 0) {
position = 0;
value = 0;
return aTRUE;
}
position = ke->Integer();
if (position >= interval.size())
position = position % interval.size();
else
if (position<0)
position = (interval.size() + position) % interval.size();
value = interval[position];
return aTRUE;
}
Tamgu* TamguLoopInteger::Vector(short idthread) {
Tamguivector* kvect = globalTamgu->Provideivector();
kvect->values = interval;
return kvect;
}
Exporting Tamgu* Tamguint::plus(Tamgu* a, bool itself) {
if (!itself && a->isFloat()) {
double v = value;
return globalTamgu->ProvideConstfloat(v + a->Float());
}
BLONG v = value;
v += a->Long();
if (IsLong(v))
return globalTamgu->Providelong(v);
if (itself) {
value = v;
return this;
}
return globalTamgu->ProvideConstint(v);
}
Exporting Tamgu* Tamguint::multiply(Tamgu* a, bool itself) {
if (!itself && a->isFloat()) {
double v = value;
return globalTamgu->ProvideConstfloat(v*a->Float());
}
BLONG v = value;
v *= a->Long();
if (IsLong(v))
return globalTamgu->Providelong(v);
if (itself) {
value = v;
return this;
}
return globalTamgu->ProvideConstint(v);
}
Exporting Tamgu* Tamguint::shiftleft(Tamgu* a, bool itself) {
BLONG v = value;
v <<= a->Integer();
if (IsLong(v))
return globalTamgu->Providelong(v);
if (itself) {
value = v;
return this;
}
return globalTamgu->ProvideConstint(v);
}
Exporting Tamgu* Tamguatomicint::plus(Tamgu* a, bool itself) {
if (!itself && a->isFloat()) {
double v = value;
return globalTamgu->ProvideConstfloat(v + a->Float());
}
BLONG v = value;
v += a->Long();
if (IsLong(v))
return globalTamgu->Providelong(v);
if (itself) {
value = v;
return this;
}
return globalTamgu->ProvideConstint(v);
}
Exporting Tamgu* Tamguatomicint::multiply(Tamgu* a, bool itself) {
if (!itself && a->isFloat()) {
double v = value;
return globalTamgu->ProvideConstfloat(v*a->Float());
}
BLONG v = value;
v *= a->Long();
if (IsLong(v))
return globalTamgu->Providelong(v);
if (itself) {
value = v;
return this;
}
return new Tamguatomicint(v);
}