-
Notifications
You must be signed in to change notification settings - Fork 2
/
lex.sbl
1003 lines (681 loc) · 30.5 KB
/
lex.sbl
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
-title lex phase 1 translation from minimal to lexemes (lexemes)
-stitl initialization
* Copyright 1987-2012 robert b. k. dewar and mark emmer.
* Copyright 2012-2017 david shields
* This file is part of macro spitbol.
* macro spitbol is free software: you can redistribute it and/or modify
* it under the terms of the gnu general public license as published by
* the free software foundation, either version 2 of the license, or
* (at your option) any later version.
* macro spitbol is distributed in the hope that it will be useful,
* but without any warranty; without even the implied warranty of
* merchantability or fitness for a particular purpose. see the
* gnu general public license for more details.
* you should have received a copy of the gnu general public license
* along with macro spitbol. if not, see <http://www.gnu.org/licenses/>.
* usage:
* spitbol -u "infile<sep>condfile<sep>outfile" lex
* where:
* infile - minimal file name, less .min extension
* condfile - conditional file name, less .cnd extension
* outfile - output file name, less .lex extension.
* default is infile.lex.
* <sep> - ; or :
* note: <sep>outfile component is optional.
* This program takes minimal statements and parses them up into
* a stream of lexemes, or lexemes. It performs equ * substitution and
* conditional assembly.
* It is based on earlier translators written by David Shields, Steve Duff
* and Robert Goldberg.
-eject
* keyword initialization
&anchor = 1
&trim = 1
* useful constants
minlets = 'abcdefghijklmnopqrstuvwxy_z' 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
nos = '0123456789'
p.nos = span(nos) rpos(0)
p.exp = 'e' any('+-') span(nos)
p.real = span(nos) '.' (span(nos) | null) (p.exp | null) rpos(0)
tab = char(9)
* argform classifies arguments
define('argform(arg)')
* argtype checks argument types
define('argtype(op,typ)')
* crack parses stmt into a stmt data plex and returns it.
* stmt is the common data plex used to hold the components of
* a minimal statement during processing.
* it fails if there is a syntax error.
define('crack(line)operands,operand,char')
* error is used to report an error for current statement
define('error(text)')
* report writes summary message at end of translation
define('report(num,text)')
* labenter enters non-null label in labtab
define('labenter()tlab')
* outstmt is used to send a target statement to the target code
* output file (outfile <=> lu2)
define('outstmt(label,opcode,op1,op2,op3,comment)t,stmtout')
* rdline is called to return the next non-comment line from
* the minimal input file (infile <=> lu1). note that it will
* not fail on eof, but it will return a minimal end statement
define('rdline()')
* conditional assembly initialization
define('tblini(str)pos,cnt,index,val,lastval')
* catab is the transfer vector for routing control to generators
* for conditional assembly directives.
catab = table( 11,,.badop )
catab['.def'] = .defop; catab['.undef'] = .undefop
catab['.if'] = .ifop; catab['.then'] = .thenop
catab['.else'] = .elseop; catab['.fi'] = .fiop
* symtbl tracks defined conditional symbols. (undefined symbols
* are assigned null values in symtbl.)
symtbl = table( 11 )
* statestk maintains all state information while processing conditional
* statements. level indexes the top entry. another variable, top,
* has a copy of savestk[level].
statestk = array( 30 )
level = 0
top =
* each state entry in statestk contains state information about
* the processing for each active .if. the state is maintained
* as 2 fields:
* result the result of the .if expression evaluation-
* true, false, or bypass
* mode whether processing then or else portion of .if
data( 'state(result,mode)' )
false = 0
true = 1
bypass = 2
else = 0
then = 1
* processrec is indexed by the current result and mode to determine
* whether or not a statement should be processed and written to the
* output file.
processrec = array( false ':' bypass ',' else ' :' then,0 )
processrec[true,then] = 1
processrec[false,else] = 1
* p.condasm breaks up conditional assembly directives.
sep = ' '
p.condasm = ( break(sep) | rem ) . condcmd
. ( span(sep) | '' )
. ( break(sep) | rem ) . condvar
p.argskel1 = fence(break(',') | rem) $ argthis *differ(argthis)
p.argskel2 = len(1) fence(break(',') | rem) $ argthis *differ(argthis)
* ityptab is table mapping from common operands to gross type
ityptab = table(21)
ityptab['0'] = 1; ityptab['1'] = 1; ityptab['wa'] = 8
ityptab['wb'] = 8; ityptab['wc'] = 8; ityptab['xl'] = 7
ityptab['xr'] = 7; ityptab['xs'] = 7; ityptab['xt'] = 7
ityptab['(xl)'] = 9; ityptab['(xr)'] = 9; ityptab['(xs)'] = 9
ityptab['(xt)'] = 9; ityptab['-(xl)'] = 11; ityptab['-(xr)'] = 11
ityptab['-(xs)'] = 11; ityptab['-(xt)'] = 11;
ityptab['(xl)+'] = 10; ityptab['(xr)+'] = 10;
ityptab['(xs)+'] = 10; ityptab['(xt)+'] = 10
* opformtab is table mapping general op formats to row index for
* validform array.
opformtab = tblini(
+ 'val[1]reg[2]opc[3]ops[4]opw[5]opn[6]opv[7]addr[8]'
+ 'x[9]w[10]plbl[11](x)[12]integer[13]real[14]'
+ 'dtext[15]eqop[16]int[17]pnam[18]')
* validform is array that validates general op formats (opv, etc).
* the first index is named type val=1 reg=2 opc=3 ops=4 opw=5
* opn=6 opv=7 addr=8 x=9 w=10 plbl=11 (x)=12 integer=13 real=14
* dtext=15 eqop=16 int=17 pnam=18
* the second argument is gross type 01=int 02=dlbl ... 27=dtext
* the entry [i,j] is nonzero is gross type j is valid for named
* type i.
validform = array('18,27',0)
validform[1,1] = validform[1,2] = validform[2,7] = validform[2,8] =
. validform[3,9] = validform[3,10] = validform[3,11] = validform[4,3] =
. validform[4,4] = validform[4,9] = validform[4,12] = validform[4,13] =
. validform[4,14] = validform[4,15] = validform[5,3] = validform[5,4] =
. validform[5,8] = validform[5,9] = validform[5,10] = validform[5,11] =
. validform[5,12] = validform[5,13] = validform[5,14] = validform[5,15] =
. validform[6,3] = validform[6,4] = validform[6,7] = validform[6,8] =
. validform[6,9] = validform[6,10] = validform[6,11] = validform[6,12] =
. validform[6,13] = validform[6,14] = validform[6,15] = validform[7,3] =
. validform[7,4] = validform[7,7] = validform[7,8] = validform[7,9] =
. validform[7,10] = validform[7,11] = validform[7,12] = validform[7,13] =
. validform[7,14] = validform[7,15] = validform[7,18] = validform[7,19] =
. validform[7,20] = validform[7,21] = validform[7,22] = validform[8,1] =
. validform[8,2] = validform[8,3] = validform[8,4] = validform[8,5] =
. validform[8,6] = validform[9,7] = validform[10,8] = validform[11,6] =
. validform[12,9] = validform[13,16] = validform[14,17] = validform[15,27] =
. validform[16,24] = validform[17,1] = validform[18,6] = validform[18,23] = 1
* zero the counts
labcnt = noutlines = nlines = nstmts = ntarget = nerrors = 0
* p.minlabel is a pattern matching a valid minimal source label.
p.minlabel = any(minlets) any(minlets) any(minlets nos)
. any(minlets nos) any(minlets nos)
* p.csparse parses out the components of the input line in stmt,
* and puts them into the locals: label, opcode, operands, comment
p.csparse = (((p.minlabel . label) | (' ' '' . label)) ' '
. len(3) . opcode
. ((' ' (break(' ') | rtab(0)) . operands
. (span(' ') | '') rtab(0) . comment) |
. (rpos(0) . operands . comment))) |
. ('.' '' . label mincond . opcode
. ((tab(7) '.' len(4) . operands) | (rpos(0) . operands))
. '' . comment)
* p.csoperand breaks out the next operand in the operands string.
p.csoperand = (break(',') . operand ',') |
. ((len(1) rtab(0)) . operand)
* p.csdtc is a pattern that handles the special case of the
* minimal dtc op
p.csdtc = ((p.minlabel . label) | (' ' '' . label))
. len(7) (len(1) $ char break(*char) len(1)) . operand
. (span(' ') | '') rtab(0) . comment
* p.equ.rip is a pattern that parses out the components of an equ
* expression.
p.equ.rip = ( span(nos) . num1 | p.minlabel . sym1 )
. ( any('+-') . oprtr | '' )
. ( span(nos) . num2 | p.minlabel . sym2 | '' )
. rpos(0)
* optab is a table that maps opcodes into their argument
* types and is used for argument checking and processing.
optab = tblini(
. 'flc[w]'
. 'add[opn,opv]adi[ops]adr[ops]anb[w,opw]aov[opv,opn,plbl]atn[none]'
. 'bod[opn,plbl]bev[opn,plbl]'
. 'bct[w,plbl]beq[opn,opv,plbl]bge[opn,opv,plbl]bgt[opn,opv,plbl]'
. 'bhi[opn,opv,plbl]ble[opn,opv,plbl]blo[opn,opv,plbl]'
. 'blt[opn,opv,plbl]bne[opn,opv,plbl]bnz[opn,plbl]brn[plbl]'
. 'bri[opn]bsw[x,val,*plbl bsw]btw[reg]'
. 'bze[opn,plbl]ceq[ops,ops,plbl]'
. 'chk[none]chp[none]cmb[w]cmc[plbl,plbl]cne[ops,ops,plbl]cos[none]csc[x]ctb[w,val]'
. 'ctw[w,val]cvd[none]cvm[plbl]dac[addr]dbc[val]dca[opn]dcv[opn]'
. 'def[def]dic[integer]drc[real]dtc[dtext]dvi[ops]dvr[ops]ejc[none]'
. 'else[else]end[none end]enp[none]ent[*val ent]equ[eqop equ]'
. 'erb[int,text erb]err[int,text err]esw[none esw]etx[none]exi[*int]exp[int]fi[fi]'
. 'ica[opn]icp[none]icv[opn]ieq[plbl]if[if]iff[val,plbl iff]ige[plbl]'
. 'igt[plbl]ile[plbl]ilt[plbl]ine[plbl]ino[plbl]inp[ptyp,int inp]'
. 'inr[none]iov[plbl]itr[none]jsr[pnam]lch[reg,opc]lct[w,opv]lcp[reg]'
. 'lcw[reg]ldi[ops]ldr[ops]lei[x]lnf[none]lsh[w,val]lsx[w,(x)]mcb[none]'
. 'mfi[opn,*plbl]mli[ops]mlr[ops]mnz[opn]mov[opn,opv]mti[opn]'
. 'mvc[none]mvw[none]mwb[none]ngi[none]ngr[none]nzb[w,plbl]'
. 'orb[w,opw]plc[x,*opv]ppm[*plbl]prc[ptyp,val prc]psc[x,*opv]req[plbl]'
. 'rge[plbl]rgt[plbl]rle[plbl]rlt[plbl]rmi[ops]rne[plbl]rno[plbl]'
. 'rov[plbl]rsh[w,val]rsx[w,(x)]rti[*plbl]rtn[none]sbi[ops]'
. 'sbr[ops]sch[reg,opc]scp[reg]sec[none sec]sin[none]sqr[none]ssl[opw]sss[opw]'
. 'sti[ops]str[ops]sub[opn,opv]tan[none]then[then]trc[none]ttl[none ttl]'
. 'undef[undef]wtb[reg]xob[w,opw]zer[opn]zgb[opn]zrb[w,plbl]zzz[int]' )
* prctab is table of procedures declared in inp that is used to
* check for consistency of inp/prc statements.
prctab = table(60)
* equates is used by g.equ and . it contains a directory of
* all labels that were defined by equ instructions.
equates = table(257)
* labtab is a table that maps each label to the section in which
* it is defined, except labels defined in the definitions section
* (section 2).
labtab = table(150,150)
* bsw is a flag that indicates whether or not a bsw...esw range
* is being processed.
bsw = 0
-stitl main program
* here follows the driver code for the "main" program.
* loop until program exits via g.end
* dostmt is invoked to initiate processing of the next line from
* rdline.
* after doing this, dostmt branches to the generator routine indicated
* for this opcode if there is one.
* the generators all have entry points beginning
* with "g.", and can be considered a logical extension of the
* dostmt routine. the generators have the choice of branching back
* to dsgen to cause the thisstmt plex to be sent to outstmt, or
* or branching to dsout, in which case the generator must output
* all needed code itself.
* the generators are listed in a separate section below.
trandate = date()
* exit(-2)
* start execution
* reads for xxx.min, writes to xxx.lex, where xxx is a command line parameter.
* the command line parameter may optionally be expressed as xxx;yyy, where
* yyy.cnd is the name of a file containing .defs to override those in
* file xxx.min.
* get file name
* default the parameter string if none present
parm = "s"
filenamc = parm '.cnd'
filenami = parm '.min'
filenamo = parm '.lex'
* get file name
* flcflag = replace( input,'y','y' )
flcflag = 'n'
flcflag = 'y'
* output = 'full line comments passed to lexeme file? ' flcflag
* no page ejects without full line comments
* output = differ(flcflag,'n')
* ejcflag = replace( (differ(flcflag,'n') input, 'n'),'y','y' )
ejcflag = 'n'
ejcflag = 'y'
* output = 'ejcs passed to lexeme file? ' ejcflag
* associate input file to lu1. if a conditional file was specified,
* read it first.
input(.infile,1,(differ(filenamc) filenamc,filenami)):s(main1)
output = differ(filenamc) "cannot open conditional file: " filenamc
+ :s(end)
output = "cannot open minimal file: " filenami :(end)
* associate output file
main1
output(.outfile,2,filenamo) :s(main2)
output = "cannot open lexeme file: " filenamo :(end)
main2
* patterns used by dostmt
p.opsk1 = (break(' ') | rem) . argskel
-include "s.def"
* Initialize equ_defs from defintions in s.def
equ_defs = table(100)
equ_defs_next
equ_defs_init break(':') . equ_key ':' break(' ') . equ_value ' ' = :f(equ_defs_done)
equ_defs[equ_key] = equ_value :(equ_defs_next)
equ_defs_done
* &trace = 4000
* &ftrace = 4000
* &profile = 1
dsout
dostmt
thisline = rdline()
crack(thisline) :f(dsout)
differ(label) labenter()
argerrs = 0
opskel = optab[opcode] :f(ds01)
ident(opskel) error("opcode not known")
opskel p.opsk1 =
ident(argskel,'none') :s(dos10)
* here if arguments to verify
dos01
ident(argskel) :s(dos05)
argskel p.argskel1 =
* accept null argument if this argument optional
argthis '*' ident(op1) :s(dos05)
typ1 = argtype(op1,argthis)
argerrs = eq(typ1) argerrs + 1
ident(argskel) :s(dos05)
argskel p.argskel2 =
argthis '*' ident(op2) :s(dos05)
typ2 = argtype(op2,argthis)
argerrs = eq(typ2) argerrs + 1
ident(argskel) :s(dos05)
argskel p.argskel2 =
argthis '*' ident(op3) :s(dos05)
typ3 = argtype(op3,argthis) :(dos05)
argerrs = eq(typ3) argerrs + 1
dos10
dos05
gt(argerrs) error('arg type not known')
* here if an argument type not recognized
opskel ' ' = :f(dsgen)
* here if post-processing required
:($('g.' opskel))
* get generator entry point (less "g." prefix)
:(g.h)
* here if bad opcode
ds01
error('bad op-code') :(dsout)
* generate lexemes.
ds.typerr
error('operand type zero') :(dsout)
dsgen
outstmt(label,opcode,op1,op2,op3,comment) :(dsout)
-stitl argform(arg)
argform
argform = 0
* determine operand format type as follows
ident(t = ityptab[arg]) :s(argform1)
* ityptab has table of cases for types 07,08,09,10,11
* if entry in this table, type immediately available:
* w reg is 08 x reg is 07 (x)+ is 10 -(x) is 11 (x) is 09
argform = t :(return)
argform1
arg p.nos :s(argform.int)
arg '=' :s(argform.eq)
arg '*' :s(argform.star)
arg any('+-') :s(argform.snum)
arg break('(') :s(argform.index)
* here if the only possibility remaining is a name which must be lbl
* if the label not yet known, assume it is a plbl
ident(t = labtab[arg]) :s(argform.plbl)
argform = t :(return)
argform.plbl
labtab[arg] = 6
argform = 6 :(return)
argform.eq
arg len(1) rem . itypa
itypa = labtab[itypa]
argform = (eq(itypa,2) 18, eq(itypa,6) 22,
. gt(itypa,2) itypa + 17) :s(return)
* if =lbl and lbl not known, it must be elbl
argform = 22
labtab[itypa] = 5 :(return)
argform.star
arg len(1) rem . t :f(return)
eq(labtab[t],2) :f(return)
argform = 19 :(return)
argform.int
argform = 1 :(return)
argform.snum
arg len(1) p.nos :f(argform.sreal)
argform = 16 :(return)
argform.sreal
arg len(1) p.real :f(return)
argform = 17 :(return)
argform.index
arg break('(') . t '(x' any('lrst') ')' rpos(0)
. :f(return)
t p.nos :f(argform.index1)
* here if int(x)
argform = 12 :(return)
argform.index1
ident(t = labtab[t]) :s(return)
argform = (eq(t,2) 13, eq(t,3) 15, eq(t,4) 14) :(return)
-stitl argtype(op,typ)
* this module checks operand types of current operation,
* prefixing each operand with type code as given in
* minimal definition.
* initially classify as one of following:
* 01=int 02=dlbl 03=name 07=x 08=w 09=(x) 10=(x)+ 11=-(x)
* 12=int(x) 13=dlbl(x) 14=name(x) 16=signed-integer
* 17=real 18==dlbl 19=*dlbl 20==name 23=pnam 24=eqop
* 25=ptyp 26=text 27=dtext
argtype
argtype = 0
* typ may have initial'*' indicating argument optional. this
* code reached only if argument not null, so remove the '*'.
typ '*' =
ident(typ,'text') :s(arg.text)
ident(typ,'dtext') :s(arg.dtext)
ident(typ,'ptyp') :s(arg.ptyp)
ident(typ,'eqop') :s(arg.eqop)
itype = argform(op)
opform = opformtab<typ>
argtype = ne(validform<+opform,itype>) itype :(return)
* argtype = itype :(return)
arg.text
argtype = 26 :(return)
arg.dtext
argtype = 27 :(return)
arg.ptyp
op any('rne') :f(return)
argtype = 25 :(return)
arg.eqop
op1 = ident(op,'*')
. equ_defs[label]
argtype = 24 :(return)
-stitl crack(line)operands,operand,char
* crack is called to create a stmt plex containing the various
* entrails of the minimal source statement in line. for
* conditional assembly ops, the opcode is the op, and op1
* is the symbol. note that dtc is handled as a special case to
* assure that the decomposition is correct.
* crack will print an error and fail if a syntax error occurs.
crack
nstmts = nstmts + 1
line p.csparse :f(cs03)
op1 = op2 = op3 = typ1 = typ2 = typ3 =
ident(opcode,'dtc') :s(cs02)
* now pick out operands until none left
operands p.csoperand = :f(cs01)
op1 = operand
operands p.csoperand = :f(cs01)
op2 = operand
operands p.csoperand :f(cs01)
op3 = operand
cs01 :(return)
* dtc - special case
cs02 line p.csdtc :f(cs03)
op1 = operand
:(cs01)
* here on syntax error
cs03
error('source line syntax error') :(freturn)
-stitl error(text)
* this module handles reporting of errors with the offending
* statement text in thisline. comments explaining
* the error are written to the listing (including error chain), and
* the appropriate counts are updated.
error
outfile = '* *???* ' thisline
outfile = '* ' text
. (ident(lasterror),'. last error was line ' lasterror)
lasterror = noutlines
noutlines = noutlines + 2
nerrors = nerrors + 1
. :(dsout)
-stitl labenter()tlab
* labenter is called to make entry in labtab for a label
* current classification is 3 for wlbl, 4 for clbl and 5 for
* other labels
labenter
ident(label) :s(return)
labtab[label] = (eq(sectnow,2) 2, eq(sectnow,3) 4,
. eq(sectnow,4) 3 , gt(sectnow,4) 6) :(return)
-stitl outstmt(label,opcode,op1,op2,op3,comment)t,stmtout
outstmt
* send text to outfile
outfile = '{' label '{' opcode '{'
. (ident(typ1), typ1 ',') op1 '{'
. (ident(typ2), typ2 ',') op2 '{'
. (ident(typ3), typ3 ',') op3 '{' comment
. '{' nlines
ntarget = ntarget + 1
noutlines = noutlines + 1
. :(return)
-stitl rdline()
* this routine returns the next statement line in the input file
* to the caller. it never fails. if there is no more input,
* then a minimal end statement is returned.
* comments are passed through to the output file directly.
* conditional assembly is performed here.
* if we were reading from filenamc (conditional defs), then the
* input stream is switched to filenami, and the flag ignore_defs
* is set.
* lines beginning with ">" are treated as snobol4 statements
* and immediately executed.
rdline
rdline = infile :f(rl02)
nlines = nlines + 1
ident( rdline ) :s(rdline)
* transfer control to appropriate conditional assembly
* directive generator or other statement generator.
leq( substr( rdline,1,1 ),'.' ) :f(other)
rdline ? p.condasm :s( $catab[condcmd] )
rl00
leq( substr( rdline,1,1 ),'*' ) :f(rl01)
* only print comment if requested.
outfile = ident(flcflag,'y') rdline :f(rdline)
noutlines = noutlines + 1 :(rdline)
* here if not a comment line
rl01
leq( substr( rdline,1,1 ),'>' ) :f(return)
* here with snobol4 line to execute
c = code(substr( rdline, 2 ) "; :(rdline)") :s<c>
output = "error compiling snobol4 statement"
:(rl03)
* here on eof. filenamc is non-null if we were reading from it.
rl02
ident(filenamc) :s(rl03)
filenamc =
ignore_defs = 1
endfile(1)
input(.infile,1,filenami) :s(rdline)
output = "cannot open minimal file: " filenami :(end)
rl03
rdline = ' end' :(rl01)
* syntax error handler.
synerr output = incnt '(syntax error):' rdline :(rdline)
* process define
defop
ident( condvar ) :s(synerr)
differ( ignore_defs ) :s(rdline)
eq( level ) :s(defok)
eq( processrec[result(top),mode(top)] ) :s(rdline)
defok
symtbl[condvar] = 1 :(rdline)
* process undefine
undefop
ident( condvar ) :s(synerr)
eq( level ) :s(undok)
eq( processrec[result(top),mode(top)] ) :s(rdline)
undok
symtbl[condvar] = :(rdline)
* process if
ifop
ident( condvar ) :s(synerr)
eq( level ) :s(ifok)
* here for .if encountered during bypass state.
ne( processrec[result(top),mode(top)] ) :s(ifok)
level = level + 1
top = statestk[level] = state(bypass,then) :(rdline)
* here for .if to be processed normally.
ifok
level = level + 1
top = statestk[level] = state(
. ( differ( symtbl[condvar] ) true,false ),
. then ) :(rdline)
* process .then
thenop
differ(condvar) :s(synerr)
eq(level) :s(synerr)f(rdline)
* process .else
elseop
differ(condvar) :s(synerr)
mode(top) = ne( level ) else :s(rdline)f(synerr)
* process .fi
fiop
differ(condvar) :s(synerr)
level = ne( level ) level - 1 :f(synerr)
top = ( ne( level ) statestk[level],'' ) :(rdline)
* process statements other than conditional directives.
other
eq( level ) :s(rl00)
eq( processrec[result(top),mode(top)] ) :s(rdline)f(rl00)
-stitl tblini(str)pos,cnt,index,val,lastval
* this routine is called to initialize a table from a string of
* index/value pairs.
tblini pos = 0
* count the number of "[" symbols to get an assessment of the table
* size we need.
tin01
str (tab(*pos) break('[') break(']') *?(cnt = cnt + 1) @pos)
. :s(tin01)
* allocate the table, and then fill it. note that a small memory
* optimisation is attempted here by trying to re-use the previous
* value string if it is the same as the present one.
tblini = table(cnt)
tin02
str (break('[') $ index len(1) break(']') $ val len(1)) =:f(return)
val = convert( val,'integer' )
val = ident(val,lastval) lastval
lastval = val
tblini[index] = val :(tin02)
-stitl generators
* bsw processing begins by building an array that can hold all
* iff operands and comments.
g.bsw
* save prior vms code in case needed
ub = ( integer( op2 ) op2, equates[op2] )
iffar = integer( ub )
. array( '0:' ub - 1,'{{' ) :f(g.bsw1)
dplbl = op3
bsw = 1 :(dsgen)
g.bsw1
error("non-integer lower bound for bsw")
* iff processing sets the iffar[] element to the current
* value, plbl, and comment.
g.iff
(eq( bsw ) error("iff without bsw"))
ifftyp = ( integer(op1) '1', '2')
iffval = ( integer( op1 ) op1, equates[op1] )
iffar[iffval] = integer( iffval )
. ifftyp ',' op1 '{' typ2 ',' op2 '{' comment
. :s(dsout)
error("non-integer iff value")
* in order to support translation of minimal operands and
* bsw/iff/esw preprocessing, all equ expressions must be
* evaluated and kept in a symbol table.
g.equ
equates[label] = ident(op1,'*')
. equ_defs[label] :s(dsgen)
num1 = num2 = sym1 = sym2 = oprtr =
op1 p.equ.rip :f(g.equ2)
num1 = differ(sym1) equates[sym1]
num2 = differ(sym2) equates[sym2]
val = (differ(oprtr) eval( num1 ' ' oprtr ' ' num2 ), num1):f(g.equ3)
g.equ1
equates[label] = val :(dsgen)
g.equ2
error("equ operand syntax error")
g.equ3
error("equ evaluation failed : " num1 ' ' oprtr ' ' num2 ' "' op1 '"' )
* esw processing generates an iff for every value in the
* bsw range.
g.esw
(eq(bsw) error("esw without bsw"))
iffindx = 0
g.esw1
iffar[iffindx] break('{') $ val len(1)
. break( '{' ) $ plbl len(1)
. rem $ cmnt
. :f(g.esw2)
val = ident( val ) '1,' iffindx
plbl = ident( plbl ) '6,' dplbl
(ident(dplbl) ident(plbl) error("missing iff value : "
. val " without plbl in preceding bsw"))
outstmt(,'iff',val,plbl,,cmnt)
iffindx = iffindx + 1 :(g.esw1)
g.esw2
iffar = :(dsgen)
report
output = lpad(num,10) ' ' text :(return)
* end prints statistics on terminal then exits program
g.end
outstmt(,'end',,,,comment)
(ne(level) error(" unclosed if conditional clause"))
* report(nlines, 'lines read')
* report(nstmts, 'statements processed')
* report(ntarget, 'target code lines produced')
* report(&stcount, 'spitbol statements executed')
ne(nerrors) report(nerrors,'errors detected')
report =
+ differ(lasterror) ' the last error was in line ' lasterror
&code = ne(nerrors) 2001
t = convert(prctab,'array') :f(g.end.2)
* here if procedures declared by inp but not by prc
output = ' procedures with inp, no prc'
i = 1
g.end.1
output = t[i,1] ' ' t[i,2] :f(g.end.2)
i = i + 1 :(g.end.1)
g.end.2
:(end)
g.ent
* note program entry labels
* entfile = label ',' op1
labtab[label] = 5 :(dsgen)
g.h :(dsgen)
* keep track of sec statements
g.sec
sectnow = sectnow + 1 :(dsgen)
g.ttl
thisline len(10) rem . t
t span(' ') =
outstmt(,'ttl','27,' t) :(dsout)
g.erb
g.err thisline break(',') len(1) rem . t
outstmt(label,opcode,op1, t) :(dsout)
g.inp
ident(label) error('no label for inp')
differ(t = prctab[label]) error('duplicate inp')
prctab[label] = op1 :(dsgen)
g.prc
ident(label) error('no label for prc')
ident(t = prctab[label]) error('missing inp')
differ(t,op1) error('inconsistent inp/prc')
prctab[label] = :(dsgen)
* convert argument to lower case
lower
lower = replace(s,'ABCDEFGHIJKLMNOPQRSTUVWXYZ',