-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuiltinfunctions.d
1513 lines (1510 loc) · 44.7 KB
/
builtinfunctions.d
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
module otya.smilebasic.builtinfunctions;
import std.conv;
import std.typecons;
import std.typetuple;
import std.traits;
import std.stdio;
import std.ascii;
import std.range;
import otya.smilebasic.error;
import otya.smilebasic.type;
import otya.smilebasic.petitcomputer;
import otya.smilebasic.sprite;
import otya.smilebasic.vm;
//プチコンの引数省略は特殊なので
//LOCATE ,,0のように省略できる
struct DefaultValue(T, bool skippable = true)
{
T value;
bool isDefault;
this(T v, bool f)
{
value = v;
isDefault = f;
}
this(T v)
{
value = v;
isDefault = false;
}
this(bool f)
{
isDefault = f;
}
void setDefaultValue(T v)
{
if(isDefault)
value = v;
}
mixin Proxy!value;
}
struct StartOptional
{
const char[] name;
}
alias ValueType = otya.smilebasic.type.ValueType;
struct BuiltinFunctionArgument
{
ValueType argType;
bool optionalArg;
bool skipArg;
}
//オーバーロード用
class BuiltinFunctions
{
private BuiltinFunction[] func;
this(BuiltinFunction f)
{
func = new BuiltinFunction[1];
func[0] = f;
}
void addFunction(BuiltinFunction func)
{
this.func ~= func;
}
BuiltinFunction overloadResolution(int argc, int outargc)
{
BuiltinFunction va;
//とりあえず引数の数で解決させる,というよりコンパイル時に型を取得する方法がない
foreach(f; func)
{
if(f.startskip <= argc && f.argments.length >= argc && f.outoptional <= outargc && f.results.length >= outargc)
return f;
if(f.variadic)
va = f;
}
//一応可変長は最後
if(va) return va;
writeln("====function overloads===");
foreach(f; func)
{
writefln("name=\"%s\", argments=%s, results = %s, variadic = %s, startoptional = %d, function pointer=%s", f.name, f.argments, f.results, f.variadic, f.startskip, f.func);
}
//引数数ちがうのは実行前にエラー
throw new IllegalFunctionCall(func[0].name);
}
}
alias DefaultValue!(int, false) optionalint;
alias DefaultValue!(int, false) optionaldouble;
alias DefaultValue!(int, false) optionalstring;
/**
ここに関数を定義すればコンパイル時にBuiltinFunctionに変換してくれる便利なクラス
*/
class BuiltinFunction
{
BuiltinFunctionArgument[] argments;
BuiltinFunctionArgument[] results;
void function(PetitComputer, Value[], Value[]) func;
int startskip;
int outoptional;
bool variadic;
string name;
this(BuiltinFunctionArgument[] argments, BuiltinFunctionArgument[] results, void function(PetitComputer, Value[], Value[]) func, int startskip,
bool variadic, string name, int outoptional)
{
this.argments = argments;
this.results = results;
this.func = func;
this.startskip = startskip;
this.variadic = variadic;
this.name = name;
this.outoptional = outoptional;
}
bool hasSkipArgument()
{
return this.startskip != this.argments.length;
}
import std.math;
/*
static pure double ABS(double a)
{
return a < 0 ? -a : a;
}*/
//static double function(double) ABS = &abs!double;
//static double function(double) SGN = &sgn!double;
static double ABS(double arg1)
{
return abs(arg1);
}
static double SGN(double arg1)
{
return sgn(arg1);
}
static double SIN(double arg1)
{
return sin(arg1);
}
static double ASIN(double arg1)
{
return asin(arg1);
}
static double SINH(double arg1)
{
return sinh(arg1);
}
static double COS(double arg1)
{
return cos(arg1);
}
static double ACOS(double arg1)
{
return acos(arg1);
}
static double COSH(double arg1)
{
return cosh(arg1);
}
static double TAN(double arg1)
{
return tan(arg1);
}
static double ATAN(double arg1, DefaultValue!(double, false) arg2)
{
if(arg2.isDefault)
{
return atan(arg1);
}
return atan2(arg1, cast(double)arg2);
}
static double TANH(double arg1)
{
return tanh(arg1);
}
static double RAD(double arg1)
{
return arg1 * std.math.PI / 180;
}
static double DEG(double arg1)
{
return arg1 * 180 / std.math.PI;
}
static double PI()
{
return std.math.PI;
}
//static ABS = function double(double x) => abs(this.result == ValueType.Double ? 1 : 0);
static void LOCATE(PetitComputer p, DefaultValue!int x, DefaultValue!int y, DefaultValue!(int, false) z)
{
x.setDefaultValue(p.CSRX);
y.setDefaultValue(p.CSRY);
z.setDefaultValue(p.CSRZ);
p.CSRX = cast(int)x;
p.CSRY = cast(int)y;
p.CSRZ = cast(int)z;
}
static void COLOR(PetitComputer p, DefaultValue!int fore, DefaultValue!(int, false) back)
{
fore.setDefaultValue(p.consoleForeColor);
back.setDefaultValue(p.consoleBackColor);
p.consoleForeColor = cast(int)fore;
p.consoleBackColor = cast(int)back;
}
static void VSYNC(PetitComputer p, DefaultValue!int time)
{
time.setDefaultValue(1);
p.vsync(cast(int)time);
}
static void WAIT(PetitComputer p, DefaultValue!int time)
{
time.setDefaultValue(1);
p.vsync(cast(int)time);
}
//TODO:プチコンのCLSには引数の個数制限がない
static void CLS(PetitComputer p/*vaarg*/)
{
p.cls;
}
static void ASSERT__(PetitComputer p, int cond, wstring message)
{
if(!cond)
{
p.printConsole("Assertion failed: ", message, "\n");
}
assert(cond, message.to!string);
}
static int BUTTON(PetitComputer p, DefaultValue!(int, false) mode, DefaultValue!(int, false) mp)
{
if(!mp.isDefault)
{
writeln("NOTIMPL:BUTTON(ID, MPID)");
}
return p.button;
}
static void VISIBLE(PetitComputer p, DefaultValue!(int) console, DefaultValue!(int) graphic, DefaultValue!(int) BG, DefaultValue!(int) sprite)
{
}
static void XSCREEN(PetitComputer p, int mode, DefaultValue!(int, false) a, DefaultValue!(int, false) b)
{
a.setDefaultValue(512);
b.setDefaultValue(4);
if(mode == 2 || mode == 3)
{
a.setDefaultValue(256);
b.setDefaultValue(2);
}
p.xscreen(mode, cast(int)a, cast(int)b);
}
static void DISPLAY(PetitComputer p, int display)
{
p.display(display);
}
static void GCLS(PetitComputer p, DefaultValue!(int, false) color)
{
color.setDefaultValue(0);
p.gfill(p.useGRP, 0, 0, 511, 511, cast(int)color);
}
static void GPSET(PetitComputer p, int x, int y, DefaultValue!(int, false) color)
{
color.setDefaultValue(p.gcolor);
p.gpset(p.useGRP, x, y, cast(int)color);
}
static void GLINE(PetitComputer p, int x, int y, int x2, int y2, DefaultValue!(int, false) color)
{
color.setDefaultValue(p.gcolor);
p.gline(p.useGRP, x, y, x2, y2, cast(int)color);
}
static void GBOX(PetitComputer p, int x, int y, int x2, int y2, DefaultValue!(int, false) color)
{
color.setDefaultValue(p.gcolor);
p.gbox(p.useGRP, x, y, x2, y2, cast(int)color);
}
static void GFILL(PetitComputer p, int x, int y, int x2, int y2, DefaultValue!(int, false) color)
{
color.setDefaultValue(p.gcolor);
p.gfill(p.useGRP, x, y, x2, y2, cast(int)color);
}
static void GCOLOR(PetitComputer p, int color)
{
p.gcolor = color;
}
static void GPRIO(PetitComputer p, int z)
{
p.gprio = z;
}
static void GPAGE(PetitComputer p, int showPage, int usePage)
{
p.showGRP = showPage;
p.useGRP = usePage;
}
static void GPAINT(PetitComputer p, int x, int y, DefaultValue!(int, false) color, DefaultValue!(int, false) color2)
{
color.setDefaultValue(p.gcolor);
p.gpaint(p.useGRP, x, y, cast(int)color);
}
static void BGMPLAY(PetitComputer p, int music)
{
}
static void BEEP(PetitComputer p, DefaultValue!(int, false) beep, DefaultValue!(int, false) pitch, DefaultValue!(int, false) volume, DefaultValue!(int, false) pan)
{
}
static void STICK(PetitComputer p, DefaultValue!(int, false) mp, out int x, out int y)
{
//JOYSTICK?
x = 0;
y = 0;
}
static int RGB(int R, int G, int B, DefaultValue!(int, false) _)
{
if(!_.isDefault)
{
//やや強引なオーバーロード
return PetitComputer.RGB(cast(ubyte)R, cast(ubyte)G, cast(ubyte)B, cast(ubyte)_);
}
return PetitComputer.RGB(cast(ubyte)R, cast(ubyte)G, cast(ubyte)B);
}
static int RND(int max)
{
import std.random;
return uniform(0, max - 1 + 1);
}
static void DTREAD(DefaultValue!(wstring, false) date, out int Y, out int M, out int D/*W*/)
{
import std.datetime;
auto currentTime = Clock.currTime();
if(date.isDefault)
{
Y = currentTime.year;
M = currentTime.month;
D = currentTime.day;
}
else
{
import std.format;
auto v = date.value;
formattedRead(v, "%d/%d/%d", &Y, &M, &D);
}
}
//hairetuha?
static int LEN(wstring str)
{
return cast(int)str.length;
}
static double VAL(wstring str)
{
try
{
if(str.length > 2 && str[0..2] == "&H")
{
return str[2..$].to!int(16);
}
if(str.length > 2 && str[0..2] == "&B")
{
return str[2..$].to!int(2);
}
double val = str.to!double;
return val;
}
catch(Exception e)
{
return 0;//toriaezu
}
}
static double FLOOR(double val)
{
return val.floor;
}
static double ROUND(double val)
{
return val.round;
}
static double CEIL(double val)
{
return val.ceil;
}
static wstring MID(wstring str, int i, int len)
{
if(i + len > str.length)
{
if(i >= str.length)
{
return "";//範囲外で空文字
}
return str[i..$];//iがまだ範囲内なら最後まで
}
//挙動未定
return str[i..i + len];
}
//INSTRSUSBTLEFT
static wstring LEFT(wstring str, int len)
{
return str[0..len];
}
static wstring RIGHT(wstring str, int len)
{
return str[$ - len..$];
}
static wstring SUBST(wstring str, int i, Value alen, DefaultValue!(Value,false) areplace)
{
int len = 1;
wstring replace = "";
if(alen.isNumber)
{
len = alen.castInteger;
replace = areplace.castString;
}
else
{
replace = alen.castString;
//省略されたらi以降の全文字を置換
return str[0..i] ~ replace;
}
if(str.length <= i + len)
{
return str[0..i] ~ replace;
}
str.replaceInPlace(i, i + len, replace);
return str;
}
static int INSTR(Value vstart, Value vstr1, DefaultValue!(wstring, false) vstr2)
{
import std.string;
int start = 0;
wstring str1, str2;
if(!vstr2.isDefault)
{
start = vstart.castInteger;
str1 = vstr1.castString;
str2 = cast(wstring)vstr2;
}
else
{
str1 = vstart.castString;
str2 = vstr1.castString;
}
return cast(int)(str1[start..$].indexOf(str2, CaseSensitive.no));
}
static int ASC(wstring str)
{
return cast(int)str[0];
}
static wstring STR(int val)
{
return val.to!wstring;
}
static wstring HEX(int val, DefaultValue!(int, false) digits)
{
import std.format;
if(digits > 8)
{
throw new OutOfRange();
}
FormatSpec!char f;
f.spec = 'X';
f.flZero = !digits.isDefault;
f.width = cast(int)digits;
auto w = appender!wstring();
formatValue(w, val, f);
return cast(immutable)(w.data);
}
static void SPSET(PetitComputer p, int id, int defno, DefaultValue!(int, false) V, DefaultValue!(int, false) W, DefaultValue!(int, false) H, DefaultValue!(int, false) ATTR)
{
if(!V.isDefault && !W.isDefault)
{
int u = defno;
int v = cast(int)V;
int w = 16, h = 16, attr = 1;
if(!ATTR.isDefault)
{
w = cast(int)W;
h = cast(int)H;
attr = cast(int)ATTR;
}
else
{
if(!W.isDefault && !H.isDefault)
{
w = cast(int)W;
h = cast(int)H;
}
if(!W.isDefault && H.isDefault)
{
attr = cast(int)W;
}
}
p.sprite.spset(id, u, v, w, h, cast(SpriteAttr)attr);
return;
}
p.sprite.spset(id, defno);
}
static void SPCHR(PetitComputer p, int id, int defno, DefaultValue!(int, false) V, DefaultValue!(int, false) W, DefaultValue!(int, false) H, DefaultValue!(int, false) ATTR)
{
if(!V.isDefault && !W.isDefault)
{
int u = defno;
int v = cast(int)V;
int w = 16, h = 16, attr = 1;
if(!ATTR.isDefault)
{
w = cast(int)W;
h = cast(int)H;
attr = cast(int)ATTR;
}
else
{
if(!W.isDefault && !H.isDefault)
{
w = cast(int)W;
h = cast(int)H;
}
if(!W.isDefault && H.isDefault)
{
attr = cast(int)W;
}
}
p.sprite.spchr(id, u, v, w, h, cast(SpriteAttr)attr);
return;
}
p.sprite.spchr(id, defno);
}
static void SPHIDE(PetitComputer p, int id)
{
p.sprite.sphide(id);
}
static void SPSHOW(PetitComputer p, int id)
{
p.sprite.spshow(id);
}
static void SPOFS(PetitComputer p, int id, double x, double y, DefaultValue!(int, false) z)
{
if(z.isDefault)
{
p.sprite.spofs(id, x, y);
}
else
{
p.sprite.spofs(id, x, y, cast(int)z);
}
}
@StartOptional("z")
static void SPOFS(PetitComputer p, int id, out double x, out double y, out int z)
{
p.sprite.getspofs(id, x, y, z);
}
static void SPANIM(PetitComputer p, Value[] va_args)
{
//TODO:配列
auto args = retro(va_args);
int no = args[0].castInteger;
double[] animdata = new double[args.length - 2];
int i;
foreach(a; args[2..$])
{
animdata[i++] = a.castDouble;
}
if(args[1].isString)
p.sprite.spanim(no, args[1].castString, animdata);
if(args[1].isNumber)
p.sprite.spanim(no, cast(SpriteAnimTarget)(args[1].castInteger), animdata);
}
static void SPDEF(PetitComputer p, Value[] va_args)
{
switch(va_args.length)
{
case 0:
p.sprite.spdef();//初期化
return;
case 1://array
{
if(va_args[0].isNumberArray)
{
writeln("NOTIMPL:SPDEF ARRAY");
//return;
}
if(va_args[0].isString)
{
VM vm = p.vm;
vm.pushDataIndex();
vm.restoreData(va_args[0].castString);
auto count = vm.readData().castInteger;//読み込むスプライト数
int defno = 0;//?
for(int i = 0; i < count; i++)
{
int U = vm.readData().castInteger;
int V = vm.readData().castInteger;
int W = vm.readData().castInteger;
int H = vm.readData().castInteger;
int HX = vm.readData().castInteger;
int HY = vm.readData().castInteger;
int ATTR = vm.readData().castInteger;
p.sprite.SPDEFTable[defno] = SpriteDef(U, V, W, H, HX, HY, cast(SpriteAttr)ATTR);
defno++;
}
vm.popDataIndex();
return;
}
throw new IllegalFunctionCall("SPDEF");
}
default:
}
{
int defno = va_args[0].castInteger;
int U = va_args[1].castInteger;
int V = va_args[2].castInteger;
int W = 16, H = 16, HX = 0, HY = 0, ATTR = 1;
if(va_args.length > 3)
{
W = va_args[3].castInteger;
}
if(va_args.length > 4)
{
H = va_args[4].castInteger;
}
if(va_args.length > 5)
{
HX = va_args[5].castInteger;
}
if(va_args.length > 6)
{
HY = va_args[6].castInteger;
}
if(va_args.length > 7)
{
ATTR = va_args[7].castInteger;
}
p.sprite.SPDEFTable[defno] = SpriteDef(U, V, W, H, HX, HY, cast(SpriteAttr)ATTR);
}
}
static void SPCLR(PetitComputer p, DefaultValue!(int, false) i)
{
if(i.isDefault)
p.sprite.spclr();
else
p.sprite.spclr(cast(int)i);
}
static void SPHOME(PetitComputer p, int i, int hx, int hy)
{
p.sprite.sphome(i, hx, hy);
}
static void SPSCALE(PetitComputer p, int i, double x, double y)
{
p.sprite.spscale(i, x, y);
}
static void SPROT(PetitComputer p, int i, double rot)
{
p.sprite.sprot(i, rot);
}
static void SPCOLOR(PetitComputer p, int id, int color)
{
p.sprite.spcolor(id, cast(uint)color);
}
static void SPLINK(PetitComputer p, int child, int parent)
{
p.sprite.splink(child, parent);
}
static void SPUNLINK(PetitComputer p, int id)
{
p.sprite.spunlink(id);
}
static void BGMSTOP(PetitComputer p)
{
writeln("NOTIMPL:BGMSTOP");
}
static int BGMCHK(PetitComputer p)
{
writeln("NOTIMPL:BGMCHK");
return false;
}
static int CHKCHR(PetitComputer p, int x, int y)
{
return cast(int)(p.console[y][x].character);
}
static wstring FORMAT(PetitComputer p, Value[] va_args)
{
alias retro!(Value[]) VaArgs;
auto args = retro(va_args);
auto format = args[0].castString;
import std.array : appender;
import std.format;
import std.string;
auto w = appender!wstring();
int j = 1;
for(int i = 0; i < format.length; i++)
{
auto f = format[i];
if(f == '%')
{
int d = cast(int)indexOf(format, 'D', CaseSensitive.yes);
if(d != -1)
{
auto spec = singleSpec(format[i .. d + 1]);
spec.spec = 'd';
formatValue(w, args[j].castInteger, spec);
j++;
i = d;
continue;
}
d = cast(int)indexOf(format, 'X', CaseSensitive.yes);
if(d != -1)
{
auto spec = singleSpec(format[i .. d + 1]);
spec.spec = cast(char)format[d];
formatValue(w, args[j].castInteger, spec);
j++;
i = d;
continue;
}
d = cast(int)indexOf(format, 'S', CaseSensitive.yes);
if(d != -1)
{
auto spec = singleSpec(format[i .. d + 1]);
spec.spec = 's';
formatValue(w, args[j].castString, spec);
j++;
i = d;
continue;
}
d = cast(int)indexOf(format, 'F', CaseSensitive.yes);
if(d != -1)
{
auto spec = singleSpec(format[i .. d + 1]);
spec.spec = 'f';
formatValue(w, args[j].castDouble, spec);
j++;
i = d;
continue;
}
}
w ~= f;
}
//プチコン互換FOMA
return cast(immutable)w.data();
}
static wstring CHR(int code)
{
return (cast(wchar)code).to!wstring;
}
static double POW(double a1, double a2)
{
return a1 ^^ a2;
}
static double SQR(double a1)
{
return sqrt(a1);
}
//GalateaTalk利用面倒くさい...
static void TALK(wstring a1)
{
}
static void BGCLR(PetitComputer p, DefaultValue!(int, false) layer)
{
if(layer.isDefault)
{
foreach(bg; p.allBG)
{
bg.clear;
}
return;
}
p.getBG(cast(int)layer).clear;
}
static void BGSCREEN(PetitComputer p, int layer, int w, int h)
{
p.getBG(layer).screen(w, h);
}
static void BGOFS(PetitComputer p, int layer, int x, int y, DefaultValue!(int, false) z)
{
z.setDefaultValue(p.getBG(layer).offsetz);
p.getBG(layer).ofs(x, y, cast(int)z);
}
static void BGCLIP(PetitComputer p, int layer, DefaultValue!(int, false) x, DefaultValue!(int, false) y,
DefaultValue!(int, false) x2, DefaultValue!(int, false) y2)
{
if(x.isDefault && y.isDefault && x2.isDefault && y2.isDefault)
{
p.getBG(layer).clip();
}
if(x.isDefault || y.isDefault || x2.isDefault || y2.isDefault)
{
throw new IllegalFunctionCall("BGCLIP");
}
p.getBG(layer).clip(cast(int)x, cast(int)y, cast(int)x2, cast(int)y2);
}
static void BGPUT(PetitComputer p, int layer, int x, int y, int screendata)
{
p.getBG(layer).put(x, y, screendata);
}
static void BGHOME(PetitComputer p, int layer, int x, int y)
{
p.getBG(layer).home(x, y);
}
static void BGSCALE(PetitComputer p, int layer, double x, double y)
{
p.getBG(layer).scale(x, y);
}
static void BGROT(PetitComputer p, int layer, double rot)
{
p.getBG(layer).rot(rot);
}
static void EFCON()
{
}
static void EFCOFF()
{
}
static void EFCSET(Value[])
{
}
static void EFCWET(Value[])
{
}
static void COPY(PetitComputer p, Value[] rawargs)
{
auto args = retro(rawargs);
//文字列はリテラル渡すとType mismatch
if(args.length > 5 || args.length < 2 || !args[0].isArray)
{
throw new IllegalFunctionCall("COPY");
}
//COPY string, string->文字列COPY
//COPY array, string->DATA COPY
Value dst = args[0];
int dstoffset = 0;
int srcoffset = 0;
int len = dst.length;//省略時はコピー元の末尾まで
if(args[1].isString && !args[0].isString)
{
//DATAから
VM vm = p.vm;
vm.pushDataIndex();
vm.restoreData(args[1].castString);
for(int i = 0; i < len; i++)
{
Value data = vm.readData();
dst[dstoffset++] = data;
}
vm.popDataIndex();
return;
}
throw new IllegalFunctionCall("COPY (Not implemented error)");
}
//alias void function(PetitComputer, Value[], Value[]) BuiltinFunc;
static BuiltinFunctions[wstring] builtinFunctions;
static this()
{
foreach(name; __traits(derivedMembers, BuiltinFunction))
{
//writeln(name);
static if(/*__traits(isStaticFunction, __traits(getMember, BuiltinFunction, name)) && */name[0].isUpper)
{
foreach(i, F; __traits(getOverloads, BuiltinFunction, name))
{
//pragma(msg, AddFunc!(BuiltinFunction, name));
wstring suffix = "";
if(is(ReturnType!(__traits(getMember, BuiltinFunction, name)) == wstring))
{
suffix = "$";
}
wstring name2 = name ~ suffix;
auto func = builtinFunctions.get(name2, null);
alias BFD = BuiltinFunctionData!(BuiltinFunction, name, i);
pragma(msg, AddFunc!BFD);
auto f = new BuiltinFunction(
GetFunctionParamType!(BFD),
GetFunctionReturnType!(BFD),
mixin(AddFunc!(BFD)),
GetStartSkip!(BFD),
IsVariadic!(BFD),
name,
GetOutStartSkip!(BFD)
);
if(func)
{
builtinFunctions[name2].addFunction(f);
}
else
{
builtinFunctions[name2] = new BuiltinFunctions(f);
}
//writeln(AddFunc!(BuiltinFunction, name));
}
}
}
}
}
template GetOutStartSkip(BFD)
{
static if(__traits(getAttributes, __traits(getOverloads, BFD.C_, BFD.N)[BFD.I_]).length == 1 &&
is(typeof(__traits(getAttributes, __traits(getOverloads, BFD.C_, BFD.N)[BFD.I_])[0]) == StartOptional))
{
enum so = __traits(getAttributes, __traits(getOverloads, BFD.C_, BFD.N)[BFD.I_])[0];
int GetOutStartSkip()
{
int k;
foreach (j, i; ParameterIdentifierTuple!(__traits(getOverloads, BFD.C_, BFD.N)[BFD.I_]))
{
if(i == so.name)
{
return k;
}
else if(BFD.ParameterStorageClass[j] & ParameterStorageClass.out_)
{
k++;
}
}
return 0;
}
}
else
{
int GetOutStartSkip()
{
return 0;
}
}
}
template GetStartSkip(BFD)
{
private template SkipSkip(int I, P...)
{
static if(P.length <= I)
{
enum SkipSkip = I - is(P[0] == PetitComputer);
}
else static if(BFD.ParameterStorageClass[I] & ParameterStorageClass.out_)
{
enum SkipSkip = I - is(P[0] : PetitComputer);
}
else static if(is(P[I] == DefaultValue!(int, false)))
{
enum SkipSkip = I - is(P[0] : PetitComputer);
}
else static if(is(P[I] == DefaultValue!(double, false)))
{
enum SkipSkip = I - is(P[0] : PetitComputer);
}
else static if(is(P[I] == DefaultValue!(wstring, false)))
{
enum SkipSkip = I - is(P[0] : PetitComputer);
}
else static if(is(P[I] == DefaultValue!(Value, false)))
{
enum SkipSkip = I - is(P[0] : PetitComputer);
}
else
{
enum SkipSkip = SkipSkip!(I + 1, P);
}
}
enum GetStartSkip = SkipSkip!(0, BFD.ParameterType);
}
template GetBuiltinFunctionArgment(P...)
{
static if(is(P[0] == double))
{
const string arg = "ValueType.Double, false";
}
else static if(is(P[0] == int))
{
const string arg = "ValueType.Integer, false";
}
else static if(is(P[0] == wstring))
{
const string arg = "ValueType.String, false";
}
else static if(is(P[0] == DefaultValue!int))
{
const string arg = "ValueType.Integer, true";
}
else static if(is(P[0] == DefaultValue!(int, false)))
{
const string arg = "ValueType.Integer, true";
}
else static if(is(P[0] == OptionalOutValue!int))
{
const string arg = "ValueType.Integer, true";
}
else static if(is(P[0] == DefaultValue!double))
{
const string arg = "ValueType.Double, true";
}
else static if(is(P[0] == DefaultValue!(double, false)))
{
const string arg = "ValueType.Double, true";
}
else static if(is(P[0] == DefaultValue!(wstring)))
{
const string arg = "ValueType.String, false";
}
else static if(is(P[0] == DefaultValue!(wstring, false)))
{
const string arg = "ValueType.String, true";
}
else static if(is(P[0] == Value[]))
{
const string arg = "";
}
else static if(is(P[0] == DefaultValue!(Value)) || is(P[0] == Value))
{
const string arg = "ValueType.Void, false";