-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlua.cu
More file actions
1446 lines (1194 loc) · 36.2 KB
/
lua.cu
File metadata and controls
1446 lines (1194 loc) · 36.2 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
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
// TODO: Use 64 bit floats, when available in auro
import auro.system {
void println (string);
void error (string);
void exit (int);
int argc ();
string argv (int);
}
import auro.buffer {
type buffer;
buffer `new` (int size) as newbuf;
void set (buffer, int index, int value) as bufset;
}
import auro.string {
string itos (int);
int length (string) as strlen;
int codeof (char);
char, int charat(string, int);
string add (string, char) as addch;
char newchar (int);
string slice (string, int, int);
string `new` (buffer) as newstr;
buffer tobuffer (string);
}
import auro.math {
float floor (float);
float trunc (float);
float mod (float, float) as fmod;
float pow (float base, float exponent) as fpow;
}
any anyInt (int x) { return x as any; }
any anyStr (string x) { return x as any; }
any anyTable (Table x) { return x as any; }
any anyFn (Function x) { return x as any; }
any anyBool (bool x) { return x as any; }
bool testInt (any a) { return a is int; }
bool testStr (any a) { return a is string; }
bool testBool (any a) { return a is bool; }
bool testTable (any a) { return a is Table; }
bool testFn (any a) { return a is Function; }
bool testNil (any a) { return a is nil_t; }
int getInt (any a) { return a as int; }
string getStr (any a) { return a as string; }
bool getBool (any a) { return a as bool; }
Table getTable (any a) { return a as Table; }
Function getFn (any a) { return a as Function; }
import auro.utils.arraylist (any) {
type `` as Array {
any get (int);
void push (any);
void set (int, any);
int len ();
}
Array `new` () as newArray;
}
import auro.function (Stack as in0, Stack as out0) {
type `` as Function {
Stack apply (Stack);
}
module `new` as newfn;
module closure;
}
export Function;
export closure;
struct unit_t {bool dummy;}
type nil_t (unit_t);
struct Stack {
int pos;
Array arr;
any first (Stack this) {
if (this.more())
return this.arr[this.pos];
else return nil();
}
any next (Stack this) {
if (this.more()) {
any a = this.arr[this.pos];
this.pos = this.pos + 1;
return a;
} else return nil();
}
void push (Stack this, any a) {
this.arr.push(a);
}
bool more (Stack this) {
return this.pos < this.arr.len();
}
void append (Stack this, Stack that) {
int i = that.pos;
while (i < that.arr.len()) {
this.push(that.arr[i]);
i = i+1;
}
}
int length (Stack this) {
int l = this.arr.len() - this.pos;
if (l < 0) return 0;
return l;
}
Stack copy (Stack this) { return new Stack(this.pos, this.arr); }
}
Stack newStack () {
return new Stack(0, newArray());
}
bool isSpace (char ch) {
int code = codeof(ch); // \t \n ' '
return (code == 9) || (code == 10) || (code == 32);
}
any parseNum (string s) {
int len = strlen(s);
int i = 0;
char ch;
int value = 0;
int anydigit = false;
// Skip whitespace, fail or start hex
while (i < len) {
ch, i = charat(s, i);
if ((codeof(ch) == 48) && (i < len)) { // '0'
ch, i = charat(s, i);
if ((codeof(ch) == 88) || (codeof(ch) == 120)) // Xx
goto hexadecimal;
}
if (!isSpace(ch)) goto digit;
}
hexadecimal:
while (i < len) {
ch, i = charat(s, i);
int c = codeof(ch);
//if (codeof(ch) == 46) goto decimals;
if ((c <= 57) && (c >= 48)) { // 0-9
value = (value*16) + (c-48);
} else if ((c <= 70) && (c >= 65)) { // A-F
value = (value*16) + (c-65) + 10;
} else if ((c >= 97) && (c <= 102)) { // a-f
value = (value*16) + (c-97) + 10;
} else goto endint;
}
// Decimal digits
while (i < len) {
ch, i = charat(s, i);
digit:
int c = codeof(ch);
if ((c == 46) || (c == 69) || (c == 101)) // '.' 'E' 'e'
goto floatPart;
if ((c > 57) || (c < 48)) // not a digit
goto endint; // break
value = (value*10) + (c-48);
anydigit = true;
}
endint:
// Skip whitespace or fail
while (i < len) {
ch, i = charat(s, i);
if (!isSpace(ch)) return nil();
}
return value as any;
floatPart:
float fval;
float ten = itof(10);
if (codeof(ch) == 46) { // '.'
int digits = 0;
// Decimal fractional digits
while (i < len) {
ch, i = charat(s, i);
if ((codeof(ch) > 57) || (codeof(ch) < 48))
goto endflt; // break
value = (value*10) + (codeof(ch)-48);
digits = digits + 1;
anydigit = true;
}
endflt:
fval = itof(value);
while (digits > 0) {
fval = fval / ten;
digits = digits - 1;
}
} else fval = itof(value);
if ((codeof(ch) == 69) || (codeof(ch) == 101)) { // Ee
bool positive = true;
int expval = 0;
ch, i = charat(s, i); // skip 'e'
if (codeof(ch) == 45) { // '-'
ch, i = charat(s, i); // advance
positive = false;
} else if (codeof(ch) == 43) { // '+'
ch, i = charat(s, i); // also advance
}
// At least 1 digit is required
if ((codeof(ch) > 57) || (codeof(ch) < 48)) return nil();
goto expdigit;
while (i < len) {
ch, i = charat(s, i);
expdigit:
if ((codeof(ch) > 57) || (codeof(ch) < 48))
goto endexp; // break
expval = (expval*10) + (codeof(ch)-48);
}
endexp:
if (positive) {
while (expval > 0) {
fval = fval * ten;
expval = expval - 1;
}
} else {
while (expval > 0) {
fval = fval / ten;
expval = expval - 1;
}
}
}
if (!anydigit) return nil();
while (i < len) {
ch, i = charat(s, i);
if (!isSpace(ch)) return nil();
}
return fval as any;
}
any getNum (any a) {
if ((a is int) || (a is float)) return a;
if (a is string) return parseNum(a as string);
return nil();
}
float, float, bool getFloats (any a, any b) {
float fa, fb;
if (a is float) fa = a as float;
else if (a is int) fa = itof(a as int);
else return itof(0), itof(0), false;
if (b is float) fb = b as float;
else if (b is int) fb = itof(b as int);
else return itof(0), itof(0), false;
return fa, fb, true;
}
private any? meta_binop (any a, any b, string key) {
Table? meta = get_metatable(a);
if (meta.isnull()) {
meta = get_metatable(b);
if (meta.isnull()) goto err;
}
any index = meta.get().get(key as any);
if (testFn(index)) {
Function f = getFn(index);
Stack args = newStack();
args.push(a);
args.push(b);
Stack result = f.apply(args);
return result.first() as any?;
}
err:
return new any?();
}
private any meta_arith (any a, any b, string key) {
any? r = meta_binop(a, b, key);
if (r.isnull())
error("Lua: attempt to perform arithmetic on a " + typestr(a) + " value");
return r.get();
}
any add (any a, any b) {
if ((a is int) && (b is int))
return ((a as int) + (b as int)) as any;
float fa, fb; bool t;
fa, fb, t = getFloats(a, b);
if (t) return (fa + fb) as any;
return meta_arith(a, b, "__add");
}
any sub (any a, any b) {
if ((a is int) && (b is int))
return ((a as int) - (b as int)) as any;
float fa, fb; bool t;
fa, fb, t = getFloats(a, b);
if (t) return (fa - fb) as any;
return meta_arith(a, b, "__sub");
}
any mul (any a, any b) {
if ((a is int) && (b is int))
return ((a as int) * (b as int)) as any;
float fa, fb; bool t;
fa, fb, t = getFloats(a, b);
if (t) return (fa * fb) as any;
return meta_arith(a, b, "__mul");
}
any div (any a, any b) {
float fa, fb; bool t;
fa, fb, t = getFloats(a, b);
if (t) return (fa / fb) as any;
return meta_arith(a, b, "__div");
}
any idiv (any a, any b) {
if ((a is int) && (b is int)) {
int ia = a as int, ib = b as int;
int r = ia / ib;
// Int division truncates, but lua division floors
// negative results are rounded the wrong way
if ((r > 0) || ((ia * ib) > 0) || ((r * ib) == ia))
return r as any;
return (r - 1) as any;
}
float fa, fb; bool t;
fa, fb, t = getFloats(a, b);
if (t) return floor(fa / fb) as any;
return meta_arith(a, b, "__idiv");
}
import auro.int {
int mod (int, int) as imod;
}
any mod (any a, any b) {
if ((a is int) && (b is int)) {
int ia = a as int, ib = b as int;
int r = imod(ia, ib);
if ((r * ib) >= 0) return r as any;
return (r + ib) as any;
}
float fa, fb; bool t;
fa, fb, t = getFloats(a, b);
if (!t) return meta_arith(a, b, "__mod");
float r = fmod(fa, fb);
if ((r * fb) >= itof(0)) return r as any;
return (r + fb) as any;
}
any pow (any a, any b) {
float fa, fb; bool t;
fa, fb, t = getFloats(a, b);
if (t) return fpow(fa, fb) as any;
return meta_arith(a, b, "__pow");
}
any concat (any a, any b) {
return anyStr(tostr(a) + tostr(b));
}
// TODO: Real nil, not just 0
any nil () { return (new unit_t(true) as nil_t) as any; }
any `true` () { return true as any; }
any `false` () { return false as any; }
export anyStr as string;
export anyInt as int;
export anyFn as function;
string typestr (any a) {
if (testTable(a)) return "table";
else if (testStr(a)) return "string";
else if ((a is float) || testInt(a)) return "number";
else if (testNil(a)) return "nil";
else if (testBool(a)) return "bool";
else if (testFn(a)) return "function";
else return "userdata";
}
string tostr (any a) {
if (testStr(a)) return getStr(a);
else if (a is float) return ftos(a as float);
else if (testInt(a)) return itos(getInt(a));
else if (testBool(a)) {
if (getBool(a))
return "true";
else
return "false";
}
Table? meta = get_metatable(a);
if (!meta.isnull()) {
any index = meta.get().get("__tostring" as any);
if (testFn(index)) {
Function f = getFn(index);
Stack args = newStack();
args.push(a);
Stack result = f.apply(args);
any r = result.first();
if (r is string) return r as string;
error("'__tostring' must return a string");
}
}
if (a is Table) return "table:" + itos((a as Table).id);
if (a is UserData) return "userdata:" + itos(((a as UserData) as UserDataInner).id);
return typestr(a);
}
bool tobool (any a) {
if (testBool(a)) return getBool(a);
else if (testNil(a)) return false;
else return true;
}
bool equals (any a, any b) {
if (testNil(a) && testNil(b)) return true;
if (testInt(a) && testInt(b)) return getInt(a) == getInt(b);
if (testStr(a) && testStr(b)) return getStr(a) == getStr(b);
float fa, fb; bool t;
fa, fb, t = getFloats(a, b);
if (t) return fa == fb;
if (testBool(a) && testBool(b)) {
bool _a = getBool(a), _b = getBool(b);
return (_a && _b) || (!_a && !_b);
}
if (testTable(a) && testTable(b)) {
Table ta = getTable(a);
Table tb = getTable(b);
if (ta.id == tb.id) return true;
any? r = meta_binop(a, b, "__eq");
if (r.isnull()) return false;
return tobool(r.get());
}
if ((a is UserData) && (b is UserData)) {
UserDataInner ta = (a as UserData) as UserDataInner;
UserDataInner tb = (b as UserData) as UserDataInner;
if (ta.id == tb.id) return true;
any? r = meta_binop(a, b, "__eq");
if (r.isnull()) return false;
return tobool(r.get());
}
return false;
}
private any meta_cmp (any a, any b, string key) {
any? r = meta_binop(a, b, key);
if (r.isnull())
error("Lua: attempt to compare " + typestr(a) + " with a " + typestr(b));
return r.get();
}
private int str_cmp (string a, string b) {
int al = strlen(a), bl = strlen(b);
int len = al; if (bl < al) len = bl;
int i = 0;
while (i < len) {
int ca = codeof(charat(a, i));
int cb = codeof(charat(b, i));
if (ca < cb) return 0-1;
if (ca > cb) return 1;
i = i+1;
}
if (al < bl) return 0-1;
if (al > bl) return 1;
return 0;
}
private bool _lt (any a, any b) {
if ((a is int) && (b is int))
return (a as int) < (b as int);
float fa, fb; bool t;
fa, fb, t = getFloats(a, b);
if (t) return fa < fb;
if ((a is string) && (b is string))
return str_cmp(a as string, b as string) < 0;
return tobool(meta_cmp(a, b, "__lt"));
}
private bool _le (any a, any b) {
if ((a is int) && (b is int))
return (a as int) <= (b as int);
float fa, fb; bool t;
fa, fb, t = getFloats(a, b);
if (t) return fa <= fb;
if ((a is string) && (b is string))
return str_cmp(a as string, b as string) <= 0;
return tobool(meta_cmp(a, b, "__le"));
}
any eq (any a, any b) { return equals(a, b) as any; }
any ne (any a, any b) { return anyBool(!equals(a, b)); }
any lt (any a, any b) { return _lt(a, b) as any; }
any le (any a, any b) { return _le(a, b) as any; }
any gt (any a, any b) { return (!_le(a, b)) as any; }
any ge (any a, any b) { return (!_lt(a, b)) as any; }
any not (any a) { return anyBool(!tobool(a)); }
any unm (any a) {
if (a is int) return (0 - (a as int)) as any;
if (a is float) return (itof(0) - (a as float)) as any;
Table? meta = get_metatable(a);
if (!meta.isnull()) {
any index = meta.get().get("__unm" as any);
if (testFn(index)) {
Function f = getFn(index);
Stack args = newStack();
args.push(a);
Stack result = f.apply(args);
return result.first();
}
}
error("Lua: attempt to perform arithmetic on a " + typestr(a) + " value");
}
Stack call (any _f, Stack args) {
if (testFn(_f)) {
Function f = getFn(_f);
Stack r = f.apply(args);
return r;
} else {
error("Lua: attempt to call a non-function value");
}
}
//======= Objects =======//
struct UserDataInner {
int id;
any data;
Table? meta;
}
type UserData (UserDataInner);
UserData newUserData (any data, Table? meta) {
int id = IdState.id;
IdState.id = id + 1;
return (new UserDataInner(id, data, meta)) as UserData;
}
//======= Objects =======//
bool checkKey (any a) { return testStr(a) || testInt(a); }
struct Pair { any key; any val; }
import auro.utils.arraylist (Pair) {
type `` as PairArr {
Pair get (int);
void set (int, Pair);
int len ();
void push (Pair);
}
PairArr `new` () as emptyPairArr;
}
struct MapPair { string k; any v; }
import auro.utils.stringmap (any) {
type `` as Map {
any? get (string);
void set (string, any);
void delete (string);
}
Map `new` () as newMap;
type iterator {
MapPair? next ();
}
iterator `new\x1diterator` (Map) as newIter;
}
struct Table {
int id;
Map map;
Array arr;
PairArr pairs;
MetaTable? meta;
iterator? iter;
string? lastKey;
// Notes: All keys are valid, only that raw userdata (any type not defined
// in this module) have no equality, so they can be set but not retrieved.
// Every key assigned is never removed, only it's value replaced with nil.
// Int keys greater than the length + 1 are assigned as generic keys, so
// when the array part catches up, that key will be stored twice.
// One iterator is mantained, so that for-in loops can run in hopefully
// constant time, but performance will degrade if next is used arbitrarily.
// TODO: When auro gets hashmaps, use a single hashmap for everything non-int
any get (Table this, any key) {
if (key is int) {
int k = key as int;
if ((k > 0) && (k <= this.arr.len()))
return this.arr[k-1];
} else if (key is string) {
any? v = this.map[key as string];
if (v.isnull()) return nil();
else return v.get();
}
// Fallback
int i = 0;
while (i < this.pairs.len()) {
Pair pair = this.pairs[i];
if (equals(key, pair.key)) return pair.val;
i = i+1;
}
return nil();
}
void set (Table this, any key, any value) {
if (key is int) {
int k = key as int;
if (k == (this.arr.len() + 1)) {
this.arr.push(value);
} else if ((k > 0) && (k <= this.arr.len())) {
this.arr[k-1] = value;
} else goto fallback;
} else if (key is string) {
this.map[key as string] = value;
} else {
fallback:
int i = 0;
while (i < this.pairs.len()) {
Pair pair = this.pairs[i];
if (equals(key, pair.key)) {
pair.val = value;
return;
}
i = i+1;
}
Pair pair = new Pair(key, value);
this.pairs.push(pair);
}
}
// This is a complicated function...
any nextKey (Table this, any key) {
// If key is nil, just start iterating all keys
if (testNil(key)) {
// First integer key
if (this.arr.len() > 0) return 1 as any;
first_string_key:
iterator iter = newIter(this.map);
MapPair? pair = iter.next();
// at least one pair, otherwise fallback
if (!pair.isnull()) {
string k = pair.get().k;
this.iter = iter as iterator?;
this.lastKey = k as string?;
return k as any;
}
first_other_key:
if (this.pairs.len() > 0)
return this.pairs[0].key;
else return nil();
}
if (key is int) {
int k = key as int;
int len = this.arr.len();
if ((k > 0) && (k < len)) {
return (k+1) as any;
} else if ((k == len) && (len > 0)) {
// was last integer key
goto first_string_key;
} else {
// outside of array, probably stored as other or not here
// but definitely not as a string
goto fallback;
}
}
if (key is string) {
string k = key as string;
// Doesn't match current iterator. Find pair that matches
if (this.lastKey.isnull() || !(this.lastKey.get() == k)) {
iterator iter = newIter(this.map);
MapPair? pair = iter.next();
while (!pair.isnull()) {
// Found. Save the iterator and proceed
if (pair.get().k == k) {
this.iter = iter as iterator?;
this.lastKey = k as string?;
goto do_string;
}
}
// Not found, table doesn't have that key
return nil();
}
do_string:
MapPair? pair = this.iter.get().next();
if (pair.isnull()) {
// Was last key, return first callback key or finish
goto first_other_key;
} else {
string k = pair.get().k;
this.lastKey = k as string?;
return k as any;
}
}
fallback:
if (testNil(key) && (this.pairs.len() > 0))
return this.pairs[0].key;
int i = 0;
while (i < this.pairs.len()) {
Pair pair = this.pairs[i];
if (equals(key, pair.key)) {
if ((i+1) < this.pairs.len())
return this.pairs[i+1].key;
return nil();
}
i = i+1;
}
return nil();
}
}
void table_append (any _t, any _n, Stack stack) {
Table t = getTable(_t);
int n = getInt(_n);
while (stack.more()) {
t.set(anyInt(n), stack.next());
n = n+1;
}
}
type MetaTable (Table);
private Table emptyTable () {
int id = IdState.id;
IdState.id = id + 1;
return new Table(
id, newMap(), newArray(), emptyPairArr(),
new MetaTable?(), new iterator?(), new string?()
);
}
any newTable () { return emptyTable() as any; }
Table? get_metatable (any a) {
if (testTable(a)) {
MetaTable? meta = getTable(a).meta;
if (meta.isnull()) return new Table?();
return (meta.get() as Table) as Table?;
}
if (a is UserData) return ((a as UserData) as UserDataInner).meta;
if (testStr(a)) return State.string_meta as Table?;
return new Table?();
}
any get (any a, any k) {
if (testTable(a)) {
any val = getTable(a).get(k);
if (!testNil(val)) return val;
}
Table? meta = get_metatable(a);
if (!meta.isnull()) {
any index = meta.get().get(anyStr("__index"));
if (testTable(index)) return get(index, k);
if (testFn(index)) {
Function f = getFn(index);
Stack args = newStack();
args.push(a);
args.push(k);
Stack result = f.apply(args);
return result.first();
}
}
if (testTable(a)) return nil();
error("Lua: tried to index a non-table value (" + tostr(a) + ")");
}
void set (any t, any k, any v) {
if (t is Table) (t as Table).set(k, v);
else error("Lua: tried to index a non-table value (" + tostr(t) + ")");
}
any length (any a) {
if (testStr(a)) return anyInt(strlen(getStr(a)));
Table? mt = get_metatable(a);
if (!mt.isnull()) {
any len_fn = mt.get().get(anyStr("__len"));
if (!testNil(len_fn))
return call(len_fn, stackof(a)).first();
}
if (a is Table) {
Table t = a as Table;
// Tentative limit (remember the array is 0-index while lua is 1-index)
int i = t.arr.len() - 1;
// TODO: Still needs logarithmic time, using binary search
if ((i >= 0) && (t.arr[i] is nil_t)) {
// False limit, must be lower
while (i >= 0) {
if (testNil(t.arr[i])) i = i-1;
else return (i+1) as any;
}
return 0 as any;
} else if (t.get((i+2) as any) is nil_t) {
return (i+1) as any;
} else {
// False limit, must be higher
i = i+3;
while (true) {
if (t.get(anyInt(i)) is nil_t)
return anyInt(i-1);
i = i+1;
}
}
}
error("Lua: attempt to get length of a " + typestr(a) + " value");
}
//======= State =======//
struct IdStateT { int id; }
struct StateT {
bool ready;
Table _G;
Table string;
Table string_meta;
Table file_meta;
Table loaded;
}
StateT State = new StateT(false, emptyTable(), emptyTable(), emptyTable(), emptyTable(), emptyTable());
IdStateT IdState = new IdStateT(0);
//======= Core Library =======//
// Helpers
private string simple_string (any a, string n, string fname) {
if (testStr(a)) return getStr(a);
if (testInt(a)) return itos(getInt(a));
error("Lua: bad argument #" + n + " to '" + fname + "' (string expected, got " + typestr(a) + ")");
}
private int simple_number (any a, string n, string fname) {
any a = getNum(a);
if (a is int) return a as int;
error("Lua: bad argument #" + n + " to '" + fname + "' (number expected, got " + typestr(a) + ")");
}
private int simple_number_or (any a, int d, string n, string fname) {
if (testNil(a)) return d;
return simple_number(a, n, fname);
}
private Stack stackof (any a) {
Stack stack = newStack();
stack.push(a);
return stack;
}
Stack _print (Stack args) {
bool first = true;
string str = "";
while (args.more()) {
any a = args.next();
if (first) first = false;
else str = str + "\t";
str = str + tostr(a);
}
println(str);
return newStack();
} import module newfn (_print) { Function `` () as __print; }
Stack _assert (Stack args) {
any val = args.next();
if (tobool(val)) {
Stack ret = newStack();
ret.push(val);
return ret;
} else {
any amsg = args.next();
string msg = tostr(amsg);
if (testNil(amsg)) msg = "assertion failed!";
error(msg);
}
} import module newfn (_assert) { Function `` () as __assert; }
Stack _error (Stack args) { error(tostr(args.next())); }
import module newfn (_error) { Function `` () as __error; }
Stack _tostring (Stack args) { return stackof(anyStr(tostr(args.next()))); }
import module newfn (_tostring) { Function `` () as __tostring; }
Stack _tonumber (Stack args) {
any a = args.next();
if ((a is int) || (a is float)) return stackof(a);
if (a is string) return stackof(parseNum(a as string));
return newStack();
}
import module newfn (_tonumber) { Function `` () as __tonumber; }
Stack _type (Stack args) { return stackof(anyStr(typestr(args.next()))); }
import module newfn (_type) { Function `` () as __type; }
Stack _getmeta (Stack args) {
int v = args.next();
if (testNil(v)) error("Lua: bad argument #1 to 'getmetatable' (value expected)");
Table? meta = get_metatable(v);
if (!meta.isnull()) {
return stackof(anyTable(meta.get()));
}
return stackof(nil());
}
import module newfn (_getmeta) { Function `` () as __getmeta; }
Stack _setmeta (Stack args) {
any a = args.next(), b = args.next();
if (!testTable(a)) error("Lua: bad argument #1 to 'getmetatable' (table expected, got "+typestr(a)+")");
if (!testTable(b)) error("Lua: bad argument #2 to 'getmetatable' (table expected, got "+typestr(b)+")");
Table t = getTable(a);
Table meta = getTable(b);
t.meta = (meta as MetaTable) as MetaTable?;
return stackof(a);
}
import module newfn (_setmeta) { Function `` () as __setmeta; }
Stack _next (Stack args) {
any a = args.next();
if (!testTable(a)) error("Lua: bad argument #1 to 'next' (table expected, got "+typestr(a)+")");