-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbintfb.bas
3222 lines (2847 loc) · 87.4 KB
/
bintfb.bas
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
option explicit
'------------------------------------------------------------------------
' 03 Jul 2021 todo
' [x] store/retrieve variables like eval-ed4
' [x] const id[$] = number|string {, const id[$] = number|string}
' [ ] consolidate loop handling data structures
' [x] arrays
' [x] parse
' [x] allocate 1 dimensional
' [x] allocate 2 dimensional
' [x] assign (idstmt, stridstmt)
' [x] reference (strfactor, primary)
' [ ] Subs
' [ ] Functions
' [ ] Shared variables
' getvarindex& (getstrindex$), used in:
' forstmt: to reference the "i" variable
' inputstmt: to reference the "input" variable: input "", numeric_store(i)
' swapstmt: reference: swap(numeric_store(i1), numeric_store(i2))
' assignment: numeric_store(i) = value
' primary: primary# = numeric_store(i)
' idstmt (stridstmt) - only called by assignment
'@review: i = find_vname&(ident) -- lots of common code, can be combined
'@review 30 Jul 2022 12:39 pm: label: load: gets run as load command!
' need to distinguish the difference commands and labels
'------------------------------------------------------------------------
' QBASIC subset interpreter by Ed Davis.
' This is the FreeBasic version of a Simple QBASIC interpreter.
' Almost identical to the QB64 version, except for QB64 extensions.
' Compile with fb -gen gcc -O 3 -lang qb bintfb.bas
'------------------------------------------------------------------------------------------
' Currently supports:
'
' Double and string variables only. No dim for these.
' One or two dimensional arrays.
' All standard operators, with hopefully the correct precedence.
'
' String operators:
' +
' =, <>, <, >, <=, >=
'
'
' Numeric binary operators
' ^,
' *, /
' \
' mod,
' +, -,
' shl,<<, shr,>>
' =, <>, <, >, <=, >=,
' and,
' or,
' xor,
' eqv,
' imp,
'
' Numeric unary operators
' not, -, +
'
' QBASIC stmts:
' ?,
' chdir,
' circle,
' cls,
' color,
' do [until][while] .. loop [until][while]
' draw,
' environ,
' exit,
' files,
' for .. next
' gosub .. return
' goto,
' if - single and multi-line: if elseif else end if or endif
' input,
' line,
' locate,
' mid$,
' paint,
' palette,
' play,
' preset,
' print,
' pset,
' randomize,
' rem,
' screen,
' shell,
' sleep,
' sound,
' swap,
' view,
' while .. wend
' width,
' window,
'
' QBASIC string functions:
'
' chr$,
' command$,
' date$,
' environ$,
' hex$,
' inkey$,
' lcase$,
' left$
' lpad$,
' ltrim$,
' mid$,
' mki$,
' oct$,
' replace$,
' right$
' rpad$,
' rtrim$,
' space$
' str$,
' string$
' time$,
' trim$,
' ucase$,
'
' QBASIC numeric functions
' abs,
' acos,
' acosh,
' acot,
' acoth,
' acsc,
' acsch,
' asc,
' asec,
' asech,
' asin,
' asinh,
' atanh,
' atn, atan,
' cdbl,
' cint,
' clng,
' cos,
' cosh,
' cot,
' coth,
' csc,
' csch,
' csng,
' csrlin,
' cvd,
' cvi,
' exp,
' false,
' frac,
' fix,
' instr,
' int,
' len,
' ln,
' log
' log10,
' point,
' pos,
' rnd
' screen
' sec,
' sech,
' sgn,
' sin,
' sinh,
' sqr, sqrt,
' tan,
' tanh,
' timer,
' true,
' val,
'
' No other numeric data types besides double. Only suffix accepted is $ for strings.
' Only up to two dimensional arrays
' No subs or functions.
' No dim.
' Lots of other stuff missing.
'------------------------------------------------------------------------------------------
declare function accept&(s as string)
declare function any_expr&(p as integer)
declare function bool_expr&
declare function _fileexists(filename as string)
declare function numeric_expr#
declare function numeric_expr2#(p as integer)
declare function find_matching_else$
declare function gettoeol$
declare function instrfun&
declare function is_multi_line_if&
declare function is_stmt_end&
declare function peek_ch$
declare function pop_num#
declare function pop_str$
declare function primary#
declare function strexpression$
declare function strfactor$
declare sub clearprog
declare sub colorstmt
declare sub docmd(interactive as integer)
declare sub elsestmt
declare sub endifstmt
declare sub exitstmt
declare sub expect(s as string)
declare sub filesstmt
declare sub find_matching_sline_if
declare sub find_matching_pair(s1 as string, s2 as string)
declare sub forstmt
declare sub getsym
declare sub gosubline(target as integer)
declare sub gosubstmt
declare sub gotoline(target as integer)
declare sub gotostmt
declare sub idstmt
declare sub ifstmt
declare sub initvars
declare sub inputstmt
declare sub lineinputstmt
declare sub linestmt
declare sub liststmt
declare sub loadprog(fn as string)
declare sub locatestmt
declare sub midstmt
declare sub multi_ifstmt(cond as integer)
declare sub nextstmt
declare sub printstmt
declare sub randomizer
declare sub returnstmt
declare sub saveprog
declare sub showhelp
declare sub skiptoeol
declare sub stridstmt
declare sub validlinenum(n as integer)
declare sub wendstmt
declare sub whilestmt(first as integer)
const true = -1, false = 0
const e = 2.71828182845905
const halfpi = 1.5707963267949
const pi = 3.14159265358979
const max_store = 512
const varssize = 64
const stacksize = 512
const pgmsize = 6000
const tyunknown=0, tyident=1, tystrident=2, tynum=3, tystring=4
const left_side = 0, right_side = 1
dim shared the_ch as string ' last char read from input
dim shared sym as string ' last symbol read
dim shared symtype as integer ' type of last symbol read
dim shared the_num as double ' last number read
dim shared pgm(pgmsize) as string ' lines of text stored here
dim shared curline as integer ' number of current line in pgm
dim shared thelin as string ' text of current line
dim shared textp as integer ' positionn in thelin
dim shared errors as integer
dim shared curr_filename as string
type do_loop_t
lline as integer
loff as integer
end type
' do/while/for/if tracking
dim shared loopvars(varssize) as integer, looplines(varssize) as integer
dim shared loopmax(varssize) as double, loopstep(varssize) as double
dim shared loopoff(varssize) as integer
dim shared stackp as integer, loopp as integer
dim shared gosubstack(stacksize) as integer, gosuboffstack(stacksize) as integer
dim shared while_sp as integer, while_line(varssize) as integer, while_off(varssize) as integer
dim shared do_sp as integer, do_loop(stacksize) as do_loop_t
dim shared if_sp as integer, if_stack(stacksize) as integer
' for arrays: make sure the user specified index is between lo_bnd..hi_bnd inclusive
' then, computed index = v.index + user_index - v.lo_bnd
type names_t
vname as string
symtype as integer ' variable name
index as long ' index into string table; numeric table; or string/numeric array table
is_const as integer
lo_bnd as long
hi_bnd as long
lo_bnd2 as long ' only if 2 dimensional
hi_bnd2 as long ' only if 2 dimensional
multi as integer ' true if 2 dimensional
a_len as long ' non-zero if array
a_width as long ' used in computing index when 2 dimensional
end type
' variable names
dim shared var_names(1 to max_store) as names_t, var_names_max as integer
' string and numeric values
dim shared string_store(1 to max_store) as string, str_store_max as integer
dim shared numeric_store(1 to max_store) as double, num_store_max as integer
' string and numeric arrays
redim shared string_arr_store(0) as string: dim shared str_arr_stor_max as long
redim shared numeric_arr_store(0) as double: dim shared num_arr_stor_max as long
' used by expression parser
dim shared str_stack(varssize) as string
dim shared num_stack(varssize) as double
dim shared str_st_ndx as integer, num_st_ndx as integer
dim shared endif_count as integer
dim shared wend_count as integer
dim shared next_count as integer
dim shared loop_count as integer
dim shared tracing as integer
dim shared stepping as integer
dim shared filenum as long
const right_assoc = 0, left_assoc = 1, unaryminus_prec = 13, unaryplus_prec = 13, unarynot_prec = 6
'for performance timing
'dim shared scantime as double
'dim shared starttime as double
'dim shared nsyms as long
dim shared ctype_arr(255) as integer
const ct_unknown=0, ct_alpha=1, ct_digit=2, ct_period=3, ct_punc1=4
const ct_dquote=5, ct_squote=6, ct_amp=7, ct_lt=8, ct_gt=9, ct_dollar=10
dim shared atarray(1000) as long
'---------------------------------------------------------------------------------------------------
' Listed here since I can not remember them:
' % = integer (16 bit)
' & = long (32 bit)
' ! = single (default)
' # = double
' $ = string
'---------------------------------------------------------------------------------------------------
' Maybe add:
' min(x, x1, x2...), max(...), ave(...), sum(...)
'#define floor(x) ((x*2.0-0.5)shr 1)
'#define ceil(x) (-((-x*2.0-0.5)shr 1))
'---------------------------------------------------------------------------------------------------
call init_scanner
tracing = false
stepping = false
str_st_ndx = 0
num_st_ndx = 0
'------------------------------------------------------------------------
' main loop
'------------------------------------------------------------------------
dim cmd$
dim quit as integer
quit = false
'starttime = timer
cmd$ = command$
if command$(1) = "-t" then
cmd$ = ltrim$(rtrim$(mid$(command$, 4)))
if cmd$ <> "" then quit = true
end if
if cmd$ = "" then
call showhelp
else
if _fileexists(cmd$) then
pgm(0) = "run " + chr$(34) + cmd$ + chr$(34)
call initgetsym(0, 1)
call docmd(false)
else
pgm(0) = cmd$
call initgetsym(0, 1)
call docmd(true)
end if
end if
if quit then call showtime: if errors then system 1 else system
do
line input "> ", pgm(0)
if pgm(0) <> "" then
call initgetsym(0, 1)
call docmd(true)
end if
loop
' show timings
sub showtime
'dim total_time as double
'total_time = timer - starttime
'print "Total time: "; total_time; " Scan time: "; scantime; " Parse time: "; total_time - scantime; " Symbols: "; nsyms
'sleep
end sub
function at_line$
at_line$ = "": if curline > 0 then at_line$ = "(" + str$(curline) + ")"
end function
function rest_of_line$
rest_of_line$ = sym + " " + the_ch + " " + mid$(thelin, textp)
end function
sub dump_tables
dim i as integer
print "name", "index", "lo", "hi", "len"
for i = 1 to var_names_max
print var_names(i).vname, var_names(i).index, var_names(i).lo_bnd, var_names(i).hi_bnd, var_names(i).a_len
next
end sub
'------------------------------------------------------------------------
' command processor
'------------------------------------------------------------------------
sub docmd(interactive as integer)
'print "docmd"
errors = false
restart_loop:
stackp = 0 ' these were -1 ??? @review
loopp = 0 ' these were -1 ??? @review
while_sp = 0
do_sp = 0
if_sp = 0
do
loop_top:
if errors then exit sub
while sym = "" and curline > 0 and curline < pgmsize
call initgetsym(curline + 1, 1)
wend
if tracing then print "["; curline; "] "; sym; " "; the_ch; " "; ltrim$(mid$(thelin, textp))
if stepping then sleep
select case sym
case "": exit sub
case "bye", "quit": call getsym: call showtime: if errors then system 1 else system
case "clear": call getsym: call initvars: exit sub
case "edit": call getsym: call editstmt: exit sub
case "help": call getsym: call showhelp: exit sub
case "list": call getsym: call liststmt: exit sub
case "load", "old": call getsym: call loadprog(""): exit sub
case "new": call getsym: call initvars: call clearprog: tracing = false: exit sub
case "reload": call getsym: call loadprog(curr_filename): exit sub
case "run": call getsym: call runprog: interactive = false: goto restart_loop
case "save": call getsym: call saveprog: exit sub
case "stop", "end", "system": call getsym: exit sub
case "dump": call getsym: call dump_tables
case "chdir": call getsym: call chdircmd
case "circle": call getsym: call circlestmt
case "close": call getsym: call closestmt
case "cls": call getsym: cls
case "color": call getsym: call colorstmt
case "const": call getsym: call conststmt
case "dim": call getsym: call dimstmt
case "do": call getsym: call dostmt(true)
case "draw": call getsym: call drawstmt
case "else": call getsym: call elsestmt
case "elseif": call getsym: call elseifstmt
case "endif": call getsym: call endifstmt
case "environ": call getsym: call environstmt
case "exit": call getsym: call exitstmt
case "for": call getsym: call forstmt
case "gosub": call getsym: call gosubstmt: goto loop_top
case "goto": call getsym: call gotostmt: goto loop_top
case "if": call getsym: call ifstmt: goto loop_top
case "input": call getsym: call inputstmt
case "line": call getsym: call lineinputstmt
case "loop": call getsym: call loopstmt
case "locate": call getsym: call locatestmt
case "mid$": call getsym: call midstmt
case "next": call getsym: call nextstmt
case "open": call getsym: call openstmt
case "paint": call getsym: call paintstmt
case "palette": call getsym: call palettestmt
case "preset": call getsym: call presetstmt
case "print", "?": call getsym: call printstmt
case "pset": call getsym: call psetstmt
case "randomize": call getsym: call randomizer
case "rem", "'": call getsym: call skiptoeol
case "return": call getsym: call returnstmt: goto loop_top
case "screen": call getsym: call screenstmt
case "shell": call shellstmt
case "sleep": call getsym: call sleepstmt
case "swap": call getsym: call swapstmt
case "view": call getsym: call viewstmt
case "wend": call getsym: call wendstmt
case "while": call getsym: call whilestmt(true)
case "width": call getsym: call widthstmt
case "window": call getsym: call windowstmt
'case "screenres": call getsym: call screenresstmt
case "@": call getsym: call atassn
case "troff": call getsym: tracing = false
case "tron": call getsym: tracing = true
case "stoff": call getsym: stepping = false
case "ston": call getsym: stepping = true
' need to account for:
' - assignment
' let ...
' [str]ident = expression
' [str]ident(expression [, expression]) = expression
' - labels
' ident:
' - non-assignment, including labels
'
case else
if left$(sym, 1) = "_" then
print "Unknown command: "; sym: errors = true
elseif accept&("let") then
call assignment
elseif symtype = tyident or symtype = tystrident then
if peek_ch$ = "=" then
call assignment
elseif peek_ch$ = "(" and instr(mid$(pgm(curline), textp), "=") > 0 then
call array_assignment
elseif symtype = tyident and the_ch = ":" then
call getsym
elseif interactive then
call printstmt
elseif sym <> ":" and sym <> "" then
print at_line$; "Stmt expected, found:"; rest_of_line$: errors = true
end if
elseif interactive then
call printstmt
elseif sym <> ":" and sym <> "" then
print at_line$; "Stmt expected, found:"; rest_of_line$: errors = true
end if
end select
if errors then
exit sub
elseif sym = ":" then
call getsym
elseif sym <> "else" and sym <> "" then
print at_line$; "Extra stmts:"; rest_of_line$: errors = true
print "symtype:"; symtype; " sym:"; sym; " ch:"; the_ch
end if
loop
end sub
'------------------------------------------------------------------------
' variable storage/retrieval
'------------------------------------------------------------------------
' find position of vname in var_names
function find_vname&(vname as string)
dim i as integer
for i = 1 to var_names_max
if var_names(i).vname = vname then
find_vname& = i
exit function
end if
next
find_vname& = 0
end function
' helper function for 2d arrays
function ar_scale(i as long, lo as long)
ar_scale = i - (lo - 1)
end function
' get the index of "a" in either string_arr_store or numeric_arr_store
' pointing to: a(expr [, expr])
function get_array_index&(ident as string)
dim i as integer, index as long, index2 as long, lo as long, lo2 as long
dim x as long
call getsym
i = find_vname&(ident)
if i = 0 then print at_line$; "Array has not been declared: "; ident: errors = true: exit function
expect("(")
index = numeric_expr#
if var_names(i).multi then
expect(",")
index2 = numeric_expr#
end if
expect(")")
if var_names(i).a_len = 0 then
print at_line$; "'"; ident; "' is not declared as an array": errors = true: exit function
end if
' verfiy that the index is within range
if index < var_names(i).lo_bnd or index > var_names(i).hi_bnd then
print at_line$; "Index is out of range:"; index; "("; var_names(i).lo_bnd; ","; var_names(i).hi_bnd; ")": errors = true: exit function
end if
if var_names(i).multi then
if index2 < var_names(i).lo_bnd2 or index2 > var_names(i).hi_bnd2 then
print at_line$; "Index two is out of range:"; index2: errors = true: exit function
end if
end if
' compute the actual index
lo = var_names(i).lo_bnd
if var_names(i).multi then
lo2 = var_names(i).lo_bnd2
x = var_names(i).index + (var_names(i).a_width * (ar_scale(index2, lo2) - 1) + ar_scale(index, lo)) - 1
else
x = var_names(i).index + ar_scale(index, lo) - 1
'x = var_names(i).index + index - (var_names(i).lo_bnd - 1) - 1
end if
'print "index: "; x
if tracing then print ident;"(";index;")[";x;"] =";
get_array_index& = x
end function
' primary: if var does not exist, create it. Return the var store index
' sym is the numeric variable name
function getvarindex&(side as integer)
dim i as integer, ident as string, ident_type as integer
ident = sym: ident_type = symtype
call getsym
if ident_type = tystrident then
print at_line$; "type mismatch": errors = true
elseif ident_type <> tyident then
print at_line$; "getvarindex: not a variable": errors = true
else
' see if variable exists
i = find_vname&(ident)
if i > 0 then
getvarindex& = var_names(i).index
if side = left_side and var_names(i).is_const then print at_line$; "Cannot update const variable: "; ident: errors = true
exit function
end if
'if side = right_side then print at_line$; "Reference to unassigned variable: "; ident: errors = true
' create a new variable
num_store_max = num_store_max + 1
var_names_max = var_names_max + 1
var_names(var_names_max).vname = ident
var_names(var_names_max).symtype = ident_type
var_names(var_names_max).index = num_store_max
numeric_store(num_store_max) = 0 ' default value
getvarindex& = num_store_max
end if
end function
function getstrindex&(side as integer)
dim i as integer, ident as string, ident_type as integer
ident = sym: ident_type = symtype
call getsym
if ident_type = tyident then
print at_line$; "type mismatch": errors = true
elseif ident_type <> tystrident then
print at_line$; "getstrindex: not a variable": errors = true
else
' see if variable exists
i = find_vname&(ident)
if i > 0 then
getstrindex& = var_names(i).index
if side = left_side and var_names(i).is_const then print at_line$; "Cannot update const variable: "; ident: errors = true
exit function
end if
'if side = right_side then print at_line$; "Reference to unassigned variable: "; ident: errors = true
' create a new variable
str_store_max = str_store_max + 1
var_names_max = var_names_max + 1
var_names(var_names_max).vname = ident
var_names(var_names_max).symtype = ident_type
var_names(var_names_max).index = str_store_max
string_store(str_store_max) = "" ' default value
getstrindex& = str_store_max
end if
end function
' a(expr)
' when called, sym pointing at the ident
function get_numeric_array_value#
dim ident as string, ident_type as integer, x as long, n as double
ident = sym: ident_type = symtype
x = get_array_index&(ident)
n = numeric_arr_store(x)
get_numeric_array_value# = n
end function
' a(expr)
' when called, sym pointing at the ident
function get_string_array_value$
dim ident as string, ident_type as integer, x as long, s as string
ident = sym: ident_type = symtype
x = get_array_index&(ident)
s = string_arr_store(x)
get_string_array_value$ = s
end function
sub stridstmt
dim i as integer, vname as string
vname = sym
'print "stridstmt"
i = getstrindex&(left_side)
expect("=")
string_store(i) = strexpression$
if tracing then print vname, string_store(i)
end sub
sub idstmt
dim i as integer, vname as string
vname = sym
i = getvarindex&(left_side)
expect("=")
numeric_store(i) = numeric_expr#
if tracing then print "*** "; vname, " = "; numeric_store(i)
end sub
' ident = expression
sub assignment
if symtype = tyident then
call idstmt
elseif symtype = tystrident then
call stridstmt
else
print at_line$; "Expecting assignment stmt, found: "; sym: errors = true
end if
end sub
' ident(expression [, expression]) = expression
sub array_assignment
dim ident as string, ident_type as integer
dim s as string, n as double, x as long
ident = sym: ident_type = symtype
x = get_array_index&(ident)
expect("=")
if ident_type = tystrident then
s = strexpression$
'assign string
string_arr_store(x) = s
if tracing then print s
else
n = numeric_expr#
'assign number
numeric_arr_store(x) = n
if tracing then print n
end if
end sub
' array assignment: @(expr) = expr
sub atassn
dim atndx, n as long
expect("(")
atndx = numeric_expr#
expect(")")
expect("=")
n = numeric_expr#
atarray(atndx) = n
if tracing then print "*** @("; atndx; ") = "; n
end sub
'------------------------------------------------------------------------
' statement parsing
'------------------------------------------------------------------------
sub showhelp
print "bye or quit -- exit"
print "help -- show this screen"
print "clear -- clear variables"
print "edit -- edit current program"
print "list -- show source"
print "list vars -- show variables"
print "load -- load program from disk"
print "new -- clear program in memory"
print "reload -- reload program from disk"
print "run -- run program in memory"
print "save -- save program to disk"
print ""
print "cls -- clear screen"
print "tron -- tracing on"
print "troff -- tracing off"
print "ston -- stepping on"
print "stoff -- stepping off"
end sub
function getfn$(prompt as string)
dim filespec as string
if symtype = tystring or symtype = tystrident then
filespec = strexpression$
elseif sym <> "" then
filespec = sym ' gettoeol destroys sym
filespec = filespec + gettoeol$
else
print prompt; ": ";
line input filespec
end if
if filespec <> "" then
if instr(filespec, ".") = 0 then filespec = filespec + ".bas"
end if
getfn$ = filespec
end function
sub initvars
dim i as integer
for i = 1 to str_store_max
string_store(i) = ""
next
for i = 1 to num_store_max
numeric_store(i) = 0
next
for i = 1 to var_names_max
var_names(i).vname = ""
var_names(i).index = 0
next
str_store_max = 0: num_store_max = 0: var_names_max = 0
end sub
sub loadprog(fname as string)
dim n as integer, f as long
initvars
clearprog
if fname = "" then curr_filename = getfn$("Program file") else curr_filename = fname
if curr_filename = "" then exit sub
if instr(curr_filename, ".") = 0 then curr_filename = curr_filename + ".bas"
if not _fileexists(curr_filename) then
print at_line$; "File "; curr_filename; " not found": errors = true
exit sub
end if
f = freefile
open curr_filename for input as #f
n = 0
while not eof(f)
line input #f, pgm(0)
call initgetsym(0, 1)
if symtype = tynum and the_num > 0 and the_num <= pgmsize then
n = the_num
else
n = n + 1: textp = 1
end if
if pgm(n) <> "" then
dim s as string
print "line "; n; " is being overwritten, continue (y/n)": line input s
if s <> s then exit while
end if
pgm(n) = mid$(pgm(0), textp)
wend
close #f
curline = 0
end sub
sub editstmt
dim editor as string
editor = environ$("EDITOR")
if editor = "" then editor = "notepad.exe"
if curr_filename = "" then curr_filename = "default.bas"
shell editor + " " + curr_filename
call loadprog(curr_filename)
end sub
' DIM ff AS INTEGER, l$
' ff = FREEFILE
' OPEN file$ FOR BINARY AS ff
'
' currentLine = 0
' DO
' IF EOF(ff) THEN EXIT DO
' LINE INPUT #ff, l$
' currentLine = currentLine + 1
' IF currentLine > UBOUND(program) THEN REDIM _PRESERVE program(currentLine + 999) AS STRING
' program(currentLine) = l$
' LOOP
'
' CLOSE ff
' open filename for input as [#]1
sub openstmt
dim fname as string, b as integer
fname = strexpression$
expect("for")
expect("input")
expect("as")
b = accept("#")
if symtype <> tynum then
print at_line$; "file number expected, found: "; sym: errors = true
call getsym
exit sub
end if
call getsym
if not _fileexists(fname) then
print at_line$; "File "; fname; " not found": errors = true
exit sub
end if
filenum = freefile
open fname for input as #filenum
end sub
' close [#] number
sub closestmt
dim b as integer
b = accept("#")
if symtype <> tynum then
print at_line$; "file number expected, found: "; sym: errors = true
call getsym
exit sub
end if
call getsym
close #filenum
end sub
' eof([#]number)
function eoff%
dim b as integer
expect("(")
b = accept("#")
if symtype <> tynum then
print at_line$; "file number expected, found: "; sym: errors = true
call getsym
exit function
end if
call getsym
expect(")")
eoff% = eof(filenum)
end function
' line input #1, st$
' # has been parsed, pointing to
sub fileinput
dim st as string, i as long, x as long, ident as string, ident_type as integer
if symtype <> tynum then
print at_line$; "file number expected, found: "; sym: errors = true
call getsym
exit sub
end if
call getsym
expect(",")
ident = sym: ident_type = symtype
if ident_type <> tystrident then print at_line$; "String variable expected": errors = true: exit sub
line input #filenum, st
i = find_vname&(ident)
if i > 0 then
if ident_type <> var_names(i).symtype then
print at_line$; "Type mismatch: "; ident_type; " vs. table: "; var_names(i).symtype: errors = true: exit sub
end if
if var_names(i).a_len > 0 then ' array
x = get_array_index&(ident)
'assign string
string_arr_store(x) = st
if tracing then print st
exit sub
end if
end if
i = getstrindex&(left_side)
string_store(i) = st
end sub
sub runprog
if sym <> "" then call loadprog("")
call initgetsym(1, 1)
end sub
sub saveprog
dim filespec as string
dim i as integer
filespec = getfn$("Save as")