-
Notifications
You must be signed in to change notification settings - Fork 0
/
TBASINCL.lua
1528 lines (1264 loc) · 41.7 KB
/
TBASINCL.lua
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
-- TBASIC includes
if not _G.bit and not _G.bit32 then
error("This lua implementation does not have bit/bit32 library, aborting.")
end
if not _G.unpack and not table.unpack then
error("This lua implementation does not have unpack() function, aborting.")
end
if _G.bit32 then _G.bit = bit32 end -- Lua 5.2 and LuaJIT compatibility (which has 'bit32' but no 'bit')
if _G.unpack and not _G.table.unpack then _G.table.unpack = unpack end -- LuaJIT compatibility
-- simple binary search stole and improved from Kotlin Language
-- @param cmpval: function that returns numerical value of the value used for searching.
-- implementation: function(s) return whateverhashornumber(s) end
-- e.g. function(s) return string.hash(s) end -- for string values
-- you must implement it by yourself!
do -- Avoid heap allocs for performance
local default_cmp_fn = function(s) return string.hash(tostring(s)) end
function table.binsearch(t, value, cmpval)
local low = 1
local high = #t
local cmp = cmpval or default_cmp_fn
local value = cmp(value)
while low <= high do
local mid = bit.rshift((low + high), 1)
local midVal = t[mid]
if value > cmp(midVal) then
low = mid + 1
elseif value < cmp(midVal) then
high = mid - 1
else
return mid -- key found
end
end
return nil -- key not found
end
end
_G._TBASIC = {}
_G._TBASIC._VERNUM = 0x0004 -- 0.4
_G._TBASIC._VERSION = tonumber(string.format("%d.%d", bit.rshift(_TBASIC._VERNUM, 8), bit.band(_TBASIC._VERNUM, 0xFF)))
_G._TBASIC._HEADER = string.format(" **** TERRAN BASIC V%d.%d **** ", bit.rshift(_TBASIC._VERNUM, 8), bit.band(_TBASIC._VERNUM, 0xFF))
_G._TBASIC.PROMPT = function() print("\nREADY.") end
_G._TBASIC._INVOKEERR = function(msg, msg1)
if msg1 then
print("?L".._G._TBASIC._INTPRTR.PROGCNTR..": "..msg.." "..msg1)
else
print("?L".._G._TBASIC._INTPRTR.PROGCNTR..": "..msg, "ERROR")
end
if _TBASIC.SHOWLUAERROR then error("Error thrown") end
--os.exit(1) -- terminate
_G._TBASIC.__appexit = true -- duh, computercraft
end
_G._TBASIC._ERROR = {
SYNTAX = function() _TBASIC._INVOKEERR("SYNTAX") end,
SYNTAXAT = function(word) _TBASIC._INVOKEERR("SYNTAX ERROR AT", "'"..word.."'") end,
TYPE = function() _TBASIC._INVOKEERR("TYPE MISMATCH") end,
ILLEGALNAME = function(name, reason)
if reason then
_TBASIC._INVOKEERR("ILLEGAL NAME: ".."'"..name.."'", "REASON:"..reason)
else
_TBASIC._INVOKEERR("ILLEGAL NAME:", "'"..name.."'")
end
end,
ILLEGALARG = function(expected, got)
if (not expected) and (not got) then
_TBASIC._INVOKEERR("ILLEGAL QUANTITY")
elseif not got then
_TBASIC._INVOKEERR(expected:upper().." EXPECTED")
else
_TBASIC._INVOKEERR(expected:upper().." EXPECTED,", "GOT "..got:upper())
end
end,
NOSUCHLINE = function(line) _TBASIC._INVOKEERR("NO SUCH LINE:", line) end,
NULFN = function(var) _TBASIC._INVOKEERR("UNDEFINED FUNCTION:", "'"..var.."'") end,
NULVAR = function(var) _TBASIC._INVOKEERR("UNDEFINED VARIABLE:", "'"..var.."'") end,
DIV0 = function() _TBASIC._INVOKEERR("DIVISION BY ZERO") end,
NAN = function() _TBASIC._INVOKEERR("NOT A NUMBER") end,
STACKOVFL = function() _TBASIC._INVOKEERR("TOO MANY RECURSION") end,
LINETOOBIG = function() _TBASIC._INVOKEERR("TOO BIG LINE NUMBER") end,
NOLINENUM = function() _TBASIC._INVOKEERR("NO LINE NUMBER") end,
ABORT = function(reason)
if reason then
_TBASIC._INVOKEERR("PROGRAM", "ABORTED: "..reason)
else
_TBASIC._INVOKEERR("PROGRAM", "ABORTED")
end
end,
ARGMISSING = function(fname, remark)
if remark then
_TBASIC._INVOKEERR("MISSING ARGUMENT(S) FOR", "'"..fname.."' ("..remark..")")
else
_TBASIC._INVOKEERR("MISSING ARGUMENT(S) FOR", "'"..fname.."'")
end
end,
NOMATCHING = function(fname, match) _TBASIC._INVOKEERR("'"..fname.."' HAS NO MACTHING", "'"..match.."'") end,
TOOLONGEXEC = function() _TBASIC._INVOKEERR("TOO LONG WITHOUT YIELDING") end,
RETURNWOSUB = function() _TBASIC._INVOKEERR("RETURN WITHOUT GOSUB") end,
NEXTWOFOR = function() _TBASIC._INVOKEERR("NEXT WITHOUT FOR") end,
ASGONIF = function() _TBASIC._INVOKEERR("ASSIGNMENT ON IF CLAUSE") end,
SHELLCMD = function() _TBASIC._INVOKEERR("THIS IS A SHELL COMMAND") end,
IOERR = function() _TBASIC._INVOKEERR("READ/WRITE") end,
NOSYMFORNEXT = function() _TBASIC._INVOKEERR("NO VAR FOR NEXT CLAUSE") end,
DEV_FUCKIT = function() _TBASIC._INVOKEERR("FEELING DIRTY") end,
DEV_UNIMPL = function(fname) _TBASIC._INVOKEERR("UNIMPLEMENTED SYNTAX:", "'"..fname.."'") end
}
_G._TBASIC._FNCTION = { -- aka OPCODES because of some internal-use-only functions
-- variable control
"CLR", -- deletes all user-defined variables and functions
"DIM", -- allocates an array
"DEF", -- defines new function. Synopsis "DEF FN FOOBAR(arg)"
"FN", -- denotes function
-- flow control
"GO", "GOTO", -- considered harmful
"GOSUB", "RETURN",
"FOR", "NEXT", "IN",
"DO", -- reserved only
"IF", "THEN",
"LABEL", -- line number alias
--"ELSE", "ELSEIF", -- reserved only, will not be implemented
"END", -- terminate program cleanly
"ABORT", -- break as if an error occured
"ABORTM", -- ABORT with message
-- stdio
"PRINT",
"INPUT",
"GET", -- read single key
"HTAB", "TAB", -- set cursor's X position
"VTAB", -- set cursor's Y position
"SCROLL",
"CLS", -- clear screen
"TEXTCOL", -- foreground colour
"BACKCOL", -- background colour
-- mathematics
"ABS", "SIN", "COS", "TAN", "FLOOR", "CEIL", "ROUND", "LOG",
"INT", -- integer part of a number (3.78 -> 3, -3.03 -> -3)
"RND", -- random number 0.0 <= x < 1.0
"SGN", -- sign of a number (-1, 0, 1)
"SQRT", -- square root
"CBRT", -- cubic root
"MAX", "MIN",
"INV", -- returns (1.0 / arg)
"RAD", -- converts deg into rad
-- string manipulation
"LEN",
"LEFT", -- just like in Excel
"MID", -- -- just like in Excel (substring)
"RIGHT", -- just like in Excel
-- type conversion
"ASC", -- converts a charactor into its code point
"CHR", -- converts an integer into corresponding character
"STR", -- number to string
"VAL", -- string to number
-- misc
"REM", -- mark this line as comment
"NEW", -- clean up any programs on the buffer (this is a Shell function)
-- pc speaker
"BEEP", -- beeps. Synopsis: "BEEP", "BEEP [pattern]" (not for CC)
"TEMIT", -- emits a tone. Synopsis: "TEMIT [frequency] [seconds]" (not for CC)
-- commands
"RUN", -- run a program or a line. Synopsis: "RUN", "RUN [line]" (this is a Shell function)
"LIST", -- list currently entered program. Synopsis: "LIST", "LIST [line]", "LIST [from "-" to]" (this is a Shell function)
"NEW", -- clear program lines buffer (this is a Shell function)
"RENUM", -- re-number BASIC statements (this is a Shell function)
"DELETE", -- delete line (this is a Shell function)
-- external IO
"LOAD", -- file load. Synopsis: "LOAD [filename]"
"SAVE", -- file save. Synopsis: "SAVE [filename]"
-- internal use only!!
"ASSIGNARRAY",
"READARRAY",
}
_G._TBASIC._OPERATR = {
-- operators
">>>", "<<", ">>", "|", "&", "XOR", "!", -- bitwise operations
";", -- string concatenation
"==", ">", "<", "<=", "=<", ">=", "=>", -- TURN OFF your font ligature for this part if you're seeing two identical symbols!
"!=", "<>", "><", -- not equal
"=", ":=", -- assign
"AND", "OR", "NOT",
"^", -- math.pow, 0^0 should return 1.
"*", "/", "+", "-", -- arithmetic operations
"%", -- math.fmod
"TO", "STEP", -- integer sequence operator
"MINUS", -- unary minus (internal use only!!)
"+=", "-=", "*=", "/=", "%=" -- C-style assign
}
_G._TBASIC.OPILLEGAL = { -- illegal functions and operators (internal-use-only opcodes)
"ASSIGNARRAY",
"READARRAY",
"MINUS",
}
_G._TBASIC._INTPRTR = {}
_G._TBASIC._INTPRTR.TRACE = false -- print program counter while execution
_G._TBASIC.SHOWLUAERROR = true
local function stackpush(t, v)
t[#t + 1] = v
end
local function stackpop(t)
local v = t[#t]
t[#t] = nil
return v
end
local function stackpeek(t)
local v = t[#t]
return v
end
function string.hash(str) -- FNV-1 32-bit
local hash = 2166136261
for i = 1, #str do
hash = hash * 16777619
hash = bit.bxor(hash, str:byte(i))
end
return hash
end
_G._TBASIC._INTPRTR.RESET = function()
_TBASIC.__appexit = false
_TBASIC._INTPRTR.PROGCNTR = 0
_TBASIC._INTPRTR.MAXLINES = 999999
_TBASIC._INTPRTR.VARTABLE = {} -- table of variables. [NAME] = data
_TBASIC._INTPRTR.FNCTABLE = {} -- table of functions. [NAME] = array of strings? (TBA)
_TBASIC._INTPRTR.CALLSTCK = {} -- return points (line number)
_TBASIC._INTPRTR.LINELABL = {} -- LABEL statement table
_TBASIC._INTPRTR.STACKMAX = 2000
_TBASIC._INTPRTR.CNSTANTS = {
M_PI = 3.141592653589793, -- this is a standard implementation
M_2PI = 6.283185307179586, -- this is a standard implementation
M_E = 2.718281828459045, -- this is a standard implementation
M_ROOT2 = 1.414213562373095, -- this is a standard implementation
TRUE = true,
FALSE = false,
NIL = nil,
_VERSION = _TBASIC._VERSION
}
end
-- FUNCTION IMPLEMENTS --------------------------------------------------------
local function __readvar(varname)
-- varname could be either real name, or a data
-- if varname is a string that can be represented as number, returns tonumber(varname) ("4324" -> 4324)
-- if varname is a TBASIC string, return resolved string ("~FOOBAR" -> "FOOBAR")
-- if varname is a TBASIC variable, return resolved variable ("$FOO" -> any value stored in variable 'FOO')
--print("readvar_varname", varname)
if type(varname) == "table" or type(varname) == "nil" or type(varname) == "boolean" then
return varname
end
if tonumber(varname) then
return tonumber(varname)
end
if varname:byte(1) == 126 then
return varname:sub(2, #varname)
end
if varname:byte(1) == 36 then
local data = varname:sub(2, #varname)
if tonumber(data) then
return tonumber(data)
else
-- try for constants
local retval = _TBASIC._INTPRTR.CNSTANTS[data:upper()]
if retval ~= nil then return retval
-- try for variable table
else return _TBASIC._INTPRTR.VARTABLE[data:upper()] end
end
elseif varname:byte(1) == 37 then
local array = _TBASIC._INTPRTR.VARTABLE[varname:sub(2, #varname):upper()]
if not array or type(array) ~= "table" then
return false
elseif array.identifier == "tbasicarray" then
return array
else
error(varname.." is not an TBASIC array")
end
else
return varname -- already resolved
end
end
local function __makenewtbasicarray(dimensional)
local t = {}
t.dimension = dimensional
t.data = {} -- this data WILL BE one-based whilst TBASIC is zero-based. BEWARE!
t.identifier = "tbasicarray"
return t
end
-- ARRNAME(3,2,4), arguments denote max possible index, starting from zero
function gfnarrayget(arrname, ...)
local t = __readvar(arrname)
local function getdimensionalsum(iteration)
local i = 0
for dim = iteration, (#t.dimension) - 1 do
i = i + t.dimension[dim]
end
return i
end
local indices = {...}
local actualIndex = 0
for d = 1, #indices do
if (d < #indices) then
actualIndex = actualIndex + getdimensionalsum(d) * indices[d]
else
actualIndex = actualIndex + indices[d]
end
end
return t.data[actualIndex + 1] -- actualIndex is zero-based, but t.data is one-based
end
function gfnarrayset(arrname, value, ...)
local t = __readvar(arrname)
local function getdimensionalsum(iteration)
local i = 0
for dim = iteration, (#t.dimension) - 1 do
i = i + t.dimension[dim]
end
return i
end
local indices = {...}
local actualIndex = 0
for d = 1, #indices do
if (d < #indices) then
actualIndex = actualIndex + getdimensionalsum(d) * indices[d]
else
actualIndex = actualIndex + indices[d]
end
end
t.data[actualIndex + 1] = value -- actualIndex is zero-based, but t.data is one-based
end
local function __assert(aarg, expected)
local arg = __readvar(aarg)
if type(arg) ~= expected then
_TBASIC._ERROR.ILLEGALARG(expected, type(arg))
return
end
end
local function __assertlhand(llval, expected)
local lval = __readvar(llval)
if type(lval) ~= expected then
_TBASIC._ERROR.ILLEGALARG("LHAND: "..expected, type(lval))
return
end
end
local function __assertrhand(rrval, expected)
local rval = __readvar(rrval)
if type(rval) ~= expected then
_TBASIC._ERROR.ILLEGALARG("RHAND: "..expected, type(rval))
return
end
end
local function __checknumber(aarg)
local arg = __readvar(aarg)
if arg == nil then
_TBASIC._ERROR.ILLEGALARG("number", type(arg))
return
else
if type(arg) == "table" then
repeat
tval = arg[1]
arg = tval
until type(tval) ~= "table"
end
n = tonumber(arg)
if n == nil then
_TBASIC._ERROR.ILLEGALARG("number", type(arg))
return
else
return n
end
end
end
local function __checkstring(aarg)
local arg = __readvar(aarg)
if type(arg) == "function" then
_TBASIC._ERROR.ILLEGALARG("STRING/NUMBER/BOOL", type(arg))
return
end
if type(arg) == "table" then
repeat
tval = arg[1]
arg = tval
until type(tval) ~= "table"
end
local strarg = tostring(arg)
return strarg:byte(1) == 126 and strarg:sub(2, #strarg) or strarg
end
local function __resolvevararg(...)
local ret = {}
for _, varname in ipairs({...}) do
table.insert(ret, __readvar(varname))
end
return ret
end
_G._TBASIC.__assert = __assert
_G._TBASIC.__assertlhand = __assertlhand
_G._TBASIC.__assertrhand = __assertrhand
_G._TBASIC.__checknumber = __checknumber
_G._TBASIC.__checkstring = __checkstring
_G._TBASIC.__readvar = __readvar
_G._TBASIC.__resolvevararg = __resolvevararg
--[[
Function implementations
Cautions:
* Every function that returns STRING must prepend "~"
]]
local function _fnprint(...)
function printarg(arg)
if type(arg) == "function" then
_TBASIC._ERROR.SYNTAX()
return
end
if type(arg) == "boolean" then
if arg then io.write(" TRUE")
else io.write(" FALSE") end
elseif _TBASIC.isstring(arg) then
io.write(__checkstring(arg))
elseif _TBASIC.isnumber(arg) then -- if argument can be turned into a number (e.g. 14321, "541")
io.write(" "..arg)
elseif type(arg) == "table" then
printarg(arg[1]) -- recursion
else
io.write(tostring(arg))
end
end
local args = __resolvevararg(...)
if #args < 1 then
io.write ""
else
for i, arg in ipairs(args) do
if i > 1 then io.write "\t" end
printarg(arg)
end
end
io.write "\n"
end
local function _fngoto(lnum)
local linenum = nil
if _TBASIC.isnumber(lnum) then
linenum = __checknumber(lnum)
else
linenum = _TBASIC._INTPRTR.LINELABL[__checkstring(lnum)]
end
if linenum == nil or linenum < 1 then
_TBASIC._ERROR.NOSUCHLINE(linenum)
return
end
_TBASIC._INTPRTR.PROGCNTR = linenum - 1
end
local function _fnnewvar(varname, value)
_TBASIC._INTPRTR.VARTABLE[varname:upper()] = __readvar(value)
end
local function _fngosub(lnum)
local linenum = nil
if _TBASIC.isnumber(lnum) then
linenum = __checknumber(lnum)
else
linenum = _TBASIC._INTPRTR.LINELABL[__checkstring(lnum)]
end
stackpush(_TBASIC._INTPRTR.CALLSTCK, _TBASIC._INTPRTR.PROGCNTR) -- save current line number
_fngoto(linenum)
end
local function _fnreturn()
if #_TBASIC._INTPRTR.CALLSTCK == 0 then -- nowhere to return
_TBASIC._ERROR.RETURNWOSUB()
return
end
local return_line = stackpop(_TBASIC._INTPRTR.CALLSTCK) + 1 -- the line has GOSUB, so advance one
_fngoto(return_line)
end
local function _fnabort()
_TBASIC._ERROR.ABORT()
end
local function _fnabortmsg(reason)
_TBASIC._ERROR.ABORT(__checkstring(__readvar(reason)))
end
local function _fnif(bbool)
local bool = __readvar(bbool)
__assert(bool, "boolean")
if bool == nil then
_TBASIC._ERROR.ILLEGALARG()
return
end
if not bool then
_TBASIC._INTPRTR.PROGCNTR = _TBASIC._INTPRTR.PROGCNTR + 1
end
end
local function _fnnop()
return
end
local function _fnfor(seq)
stackpush(_TBASIC._INTPRTR.CALLSTCK, _TBASIC._INTPRTR.PROGCNTR)
end
local function _fnnext(...)
if #_TBASIC._INTPRTR.CALLSTCK == 0 then -- nowhere to return
_TBASIC._ERROR.NEXTWOFOR()
return
end
local variables = {...} -- array of strings(varname) e.g. "$X, $Y, $Z"
-- error if no symbol is specified (a common "mistake")
if #variables == 0 then
_TBASIC._ERROR.NOSYMFORNEXT()
end
local branch = false
-- dequeue intsequences
for i, v in ipairs(variables) do
local t = nil
if _TBASIC.isvariable(v) then
t = _TBASIC._INTPRTR.VARTABLE[v:sub(2, #v)]
if type(t) ~= "table" then
_TBASIC._ERROR.ILLEGALARG("ARRAY", type(t))
return
end
table.remove(t, 1)
-- unassign variable
if #t == 0 then
_TBASIC._INTPRTR.VARTABLE[v] = nil
branch = true
end
else
_TBASIC._ERROR.ILLEGALARG("ARRAY", type(t))
return
end
end
-- branch? or go back?
if not branch then
_fngoto(stackpeek(_TBASIC._INTPRTR.CALLSTCK) + 1) -- the line has FOR statement
else
stackpop(_TBASIC._INTPRTR.CALLSTCK) -- dump the stack
end
end
local function _fnabs(n)
return math.abs(__checknumber(n))
end
local function _fnsin(n)
return math.sin(__checknumber(n))
end
local function _fncos(n)
return math.cos(__checknumber(n))
end
local function _fntan(n)
return math.tan(__checknumber(n))
end
local function _fntorad(n)
return math.rad(__checknumber(n))
end
local function _fnascii(char)
return __checkstring(char):byte(1)
end
local function _fncbrt(n)
return __checknumber(n)^3
end
local function _fnceil(n)
return math.ceil(__checknumber(n))
end
local function _fnchar(code)
return "~"..string.char(__checknumber(code)) -- about "~".. ? read the cautions above!
end
local function _fnfloor(n)
return math.floor(__checknumber(n))
end
local function _fngetkeycode(...)
-- TODO get a single character from the keyboard and saves the code of the character to the given variable(s)
end
local function _fnint(n)
num = __checknumber(n)
return num >= 0 and math.floor(n) or math.ceil(n)
end
local function _fnmultinv(n) -- multiplicative invert
return 1.0 / __checknumber(n)
end
local function _fnsubstrleft(str, n)
return "~"..__checkstring(str):sub(1, __checknumber(n))
end
local function _fnsubstr(str, left, right)
return "~"..__checkstring(str):sub(__checknumber(left), __checknumber(right))
end
local function _fnsubstrright(str, n)
return "~"..__checkstring(str):sub(-__checknumber(n))
end
local function _fnlen(var)
local value = __readvar(var)
return #value
end
local function _fnloge(n)
return math.log(__checknumber(n))
end
local function _fnmax(...)
local args = __resolvevararg(...)
if #args < 1 then
_TBASIC._ERROR.ARGMISSING("MAX")
return
end
local max = -math.huge
for _, i in ipairs(args) do
local n = __checknumber(i)
if max < n then max = n end
end
return max
end
local function _fnmin(...)
local args = __resolvevararg(...)
if #args < 1 then
_TBASIC._ERROR.ARGMISSING("MIN")
return
end
local min = math.huge
for _, i in ipairs(args) do
local n = __checknumber(i)
if min > n then min = n end
end
return min
end
local function _fnrand()
return math.random()
end
local function _fnround(n)
return math.floor(__checknumber(n) + 0.5)
end
local function _fnsign(n)
local num = __checknumber(n)
return num > 0 and 1.0 or num < 0 and -1.0 or 0.0
end
local function _fnsqrt(n)
return __checknumber(n)^(0.5)
end
local function _fntostring(n)
local ret = tostring(__checknumber(n))
if not ret then
_TBASIC._ERROR.ILLEGALARG()
return
else
return "~"..ret
end
end
local function _fntonumber(s)
if tonumber(s) then return s end
return tonumber(__checkstring(s))
end
local function _fntan(n)
return math.tan(__checknumber(n))
end
local function _fninput(...) -- INPUT(var1, [var2, var3 ...])
local args = {...}
local prompt = "YOUR INPUT ? "
local prompt_numbered = "YOUR INPUT (%d OF %d) ? "
function prompt_and_get_input()
-- if there's two or more input, a number will be shown
if #args >= 2 then
io.write(string.format(prompt_numbered, argcount, #args))
else
io.write(prompt)
end
io.flush() -- print out the line right away
local value = io.read()
return value
end
if #args < 1 then
_TBASIC._ERROR.ARGMISSING("INPUT")
return
else
for argcount, varname in ipairs(args) do
local inputvalue = nil
while inputvalue == nil or inputvalue == "" do
inputvalue = prompt_and_get_input()
_opassign(varname, inputvalue)
end
end
end
end
local function _fnlabel(lname)
_TBASIC._INTPRTR.LINELABL[__checkstring(lname)] = _TBASIC._INTPRTR.PROGCNTR
end
-- dim(max_index, max_index, ...)
local function _fndim(...)
local args = {...}
local varname = args[1]
local dimensional = {}
for i, v in ipairs(args) do
if i > 1 then
dimensional[i - 1] = v + 1 -- stores size, not max_index
end
end
_opassign(varname, __makenewtbasicarray(dimensional))
end
local function _fnassignarray(arrname, value, ...)
gfnarrayset(arrname, value, ...)
end
local function _fnreadarray(arrname, ...)
return gfnarrayget(arrname, ...)
end
-- OPERATOR IMPLEMENTS --------------------------------------------------------
local function booleanise(bool)
return bool and "$TRUE" or "$FALSE"
end
function _opconcat(llval, rrval)
local lval = __readvar(llval)
local rval = __readvar(rrval)
if type(lval) == "function" then _TBASIC._ERROR.ILLEGALARG("VALUE", "FUNCTION") return end
if type(rval) == "function" then _TBASIC._ERROR.ILLEGALARG("VALUE", "FUNCTION") return end
local l = (type(lval) == "string" and lval:byte(1)) == 126 and lval:sub(2, #lval) or __checkstring(lval)
local r = (type(rval) == "string" and rval:byte(1)) == 126 and rval:sub(2, #rval) or __checkstring(rval)
return "~"..l..r
end
function _opplus(lval, rval)
local l = __checknumber(lval)
local r = __checknumber(rval)
return l + r
end
function _optimes(lval, rval)
local l = __checknumber(lval)
local r = __checknumber(rval)
return l * r
end
function _opminus(lval, rval)
local l = __checknumber(lval)
local r = __checknumber(rval)
return l - r
end
function _opdiv(lval, rval)
local l = __checknumber(lval)
local r = __checknumber(rval)
if r == 0 then
_TBASIC._ERROR.DIV0()
return
else
return _optimes(l, 1.0 / r)
end
end
function _opmodulo(lval, rval)
local expected = "number"
local l = __checknumber(lval)
local r = __checknumber(rval)
return math.fmod(l, r)
end
function _oppower(lval, rval)
local expected = "number"
local l = __checknumber(lval)
local r = __checknumber(rval)
return math.pow(l, r) -- 0^0 is 1 according to the spec, and so is the Lua's.
end
function _opassign(var, value)
if _TBASIC.isnumber(var) or _TBASIC.isfunction(var) or _TBASIC.isoperator(var) or _TBASIC.isargsep(var) then
_TBASIC._ERROR.ILLEGALNAME(var)
return
end
-- remove uncaught "$"
local varname = var:byte(1) == 36 and var:sub(2, #var) or var
-- if it still has "$", the programmer just broke the law
if varname:byte(1) == 36 then
_TBASIC._ERROR.ILLEGALNAME(varname, "HAS ILLEGAL CHARACTER '$'")
return
end
_TBASIC._INTPRTR.VARTABLE[varname:upper()] = __readvar(value)
end
function _opeq(llval, rrval)
local lval = __readvar(llval)
local rval = __readvar(rrval)
if tonumber(lval) and tonumber(rval) then
return booleanise(tonumber(lval) == tonumber(rval))
else
return booleanise(__checkstring(lval) == __checkstring(rval))
end
end
function _opne(llval, rrval)
local lval = __readvar(llval)
local rval = __readvar(rrval)
if tonumber(lval) and tonumber(rval) then
return booleanise(tonumber(lval) ~= tonumber(rval))
else
return booleanise(__checkstring(lval) ~= __checkstring(rval))
end
end
function _opgt(lval, rval)
local expected = "number"
local l = __checknumber(lval)
local r = __checknumber(rval)
return booleanise(l > r)
end
function _oplt(lval, rval)
local expected = "number"
local l = __checknumber(lval)
local r = __checknumber(rval)
return booleanise(l < r)
end
function _opge(lval, rval)
local expected = "number"
local l = __checknumber(lval)
local r = __checknumber(rval)
return booleanise(l >= r)
end
function _ople(lval, rval)
local expected = "number"
local l = __checknumber(lval)
local r = __checknumber(rval)
return booleanise(l <= r)
end
function _opband(lval, rval)
local expected = "number"
local l = __checknumber(lval)
local r = __checknumber(rval)
return bit.band(l, r)
end
function _opbor(lval, rval)
local expected = "number"
local l = __checknumber(lval)
local r = __checknumber(rval)
return bit.bor(l, r)
end
function _opbxor(lval, rval)
local expected = "number"
local l = __checknumber(lval)
local r = __checknumber(rval)
return bit.bxor(l, r)
end
function _opbnot(val)
local expected = "number"
local v = __checknumber(val)
return bit.bnot(v)
end
function _oplshift(lval, rval)
local expected = "number"
local l = __checknumber(lval)
local r = __checknumber(rval)
return bit.lshift(l, r)
end
function _oprshift(lval, rval)
local expected = "number"
local l = __checknumber(lval)
local r = __checknumber(rval)
return bit.arshift(l, r)
end
function _opurshift(lval, rval)
local expected = "number"
local l = __checknumber(lval)
local r = __checknumber(rval)