Squish 22.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
REM Copyright 2012 Castle Technology Ltd
REM
REM Licensed under the Apache License, Version 2.0 (the "License");
REM you may not use this file except in compliance with the License.
REM You may obtain a copy of the License at
REM
REM     http://www.apache.org/licenses/LICENSE-2.0
REM
REM Unless required by applicable law or agreed to in writing, software
REM distributed under the License is distributed on an "AS IS" BASIS,
REM WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
REM See the License for the specific language governing permissions and
REM limitations under the License.
REM
REM>squished.Squish
ON ERROR REPORT:PRINT " (internal error ";ERL;")":END
Robert Sprowson's avatar
Robert Sprowson committed
17
l$="v1.14 27-Oct-12"
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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
DIM aa% 1000
ba%=HIMEM-END-32768
DIM z% ba%
ca%=3

FOR A=0 TO 2 STEP 2
P%=aa%
[ OPT A
; call BASIC internal routine tokenaddr
.e
STMFD R13!,{R14}
ADD R2,R14,#19*4 ; tokenaddr
ADR R14,i
ADR R12,f
MOV PC,R2
.i
STR R1,c
LDMFD R13!,{PC}

.c  EQUD 0 ; tokenaddr result
.f  EQUD 0 ; Fake LINE buffer used by tokenaddr

; Identifiers are stored in a linked list in the following format:
; bytes 0-3 = address of next identifier, 0 for none
; byte 4 = usage count
; bytes 5+ = identifier name, CR-terminated

.asm_add_identifier
.asm_add_identifier_ident_loop
STR R0,d
ADR R2,b
ADD R1,R0,#5
.asm_add_identifier_char_loop
LDRB R3,[R2],#1
LDRB R4,[R1],#1
TEQ R3,R4
BNE asm_add_identifier_next_ident
TEQ R3,#13
BNE asm_add_identifier_char_loop
; Match found, increase usage count
LDRB R3,[R0,#4]
ADD R3,R3,#1
TEQ R3,#256
STRNEB R3,[R0,#4]
STR R0,g
MOV R3,#1
STR R3,d
MOV PC,R14

.asm_add_identifier_next_ident
LDR R0,[R0]
TEQ R0,#0
BNE asm_add_identifier_ident_loop
MOV PC,R14

.b  EQUS STRING$(206,"*") ; buffer to store string to match
ALIGN
EQUD 0

.d  EQUD 0  ; address of found identifier, or where to store link to new identifier
.g  EQUD -1 ; 1 = identifier found, 0 = identifier not found
]
NEXT A

x%=1
g%=-1
o%=0
da%=0
Q%=0
ea%=FALSE
Z%=TRUE
_%=0
`%=0
fa%=FALSE
m$=""
j$=""
PROCprocess_args(F$,f$)
IF F$="" INPUT "Input file: "F$
IF f$="" INPUT "Output file: "f$
SYS "OS_File",255,F$,z% TO,,ga%,ha%,ia%
ja%=(z%+ia%+256) AND NOT 3
ka%=ja%
la%=z%+ba%
IF FNtoupper(f$)=FNtoupper(F$) THEN
  VDU 7
  INPUT LINE "Output file same name as input - sure? "u$
  IF INSTR("YES",FNtoupper(u$))<>1 THEN END
ENDIF
IF g%>0 g%=OPENOUT (n$):IF g%=0:ERROR 1,"Can't open log file"
IF o% o%=OPENOUT (o$):IF o%=0 ERROR 1,"Can't open literal log file"
ON ERROR REPORT:PRINT " (internal error ";ERL;")":PROCtidyup:END
IF g%<0 AND ea%<>0:MODE 12:COLOUR 0,7:COLOUR 7,0
c$="Squish ("+l$+") squishing program '"+F$+"' to '"+f$+"'"
PROClog_nl
PROClog_str(c$)
PROClog_nl
PROClog_nl
IF g%>=0 PRINT'c$
TIME=0
PROClog_str("Scanning for variable names....")
PROClog_nl
R%=FNnew_identifier_list
B%=FNnew_identifier_list
D%=FNnew_identifier_list
S%=FNnew_identifier_list
F%=FNnew_identifier_list
G%=FNnew_identifier_list
T%=FNnew_identifier_list
U%=FNnew_identifier_list
ma%=FNnew_identifier_list
H%=0
J%=0
d%=z%
d$="@abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_`0123456789"
g$="@abcdefghijklmnopqrstuvwxyzABCDFGHIJKLMNOPQRSTUVWXZ_`0123456789"
w$="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"+CHR$ 128+CHR$ 130+CHR$ 132+CHR$ 236
ON ERROR REPORT:PRINT " (internal error ";ERL;")":PRINT "Line number in original = ";H%+256*J%:PROCtidyup:END
PROCread_program
c$="Source program read:  errors "+STR$ (`%)+", warnings "+STR$ (_%)
PROClog_nl
PROClog_str(c$)
IF g%>=0 PRINT'c$
IF `%<>0 OR (x%=2 AND _%<>0) THEN
  c$="** Conversion aborted: no output file produced."
  PROClog_nl
  PROClog_nl
  PROClog_str(c$)
  PROClog_nl
  IF g%>=0 PRINT'c$
  PROCtidyup
  END
ENDIF
x%=-1
ON ERROR REPORT:PRINT " (internal error ";ERL;")":PROCtidyup:END
PROCknockoutkept(R%,"","")
PROCknockoutkept(B%,"","%")
PROCknockoutkept(D%,"","$")
PROCknockoutkept(S%,"","()")
PROCknockoutkept(F%,"","%()")
PROCknockoutkept(G%,"","$()")
PROCknockoutkept(U%,"PROC","")
PROCknockoutkept(T%,"FN","")
PROClog_nl
PROClog_nl
PROClog_str("Allocating single letter variables....")
PROClog_nl
PROCallocate_single_letter(R%)
PROCallocate_single_letter(B%)
PROCallocate_single_letter(D%)
PROCallocate_single_letter(S%)
PROCallocate_single_letter(F%)
PROCallocate_single_letter(G%)
PROCallocate_single_letter(T%)
PROCallocate_single_letter(U%)
PROClog_nl
PROClog_nl
PROClog_str("Allocating double letter variables....")
PROClog_nl
PROCallocate_double_letter(R%)
PROCallocate_double_letter(B%)
PROCallocate_double_letter(D%)
PROCallocate_double_letter(S%)
PROCallocate_double_letter(F%)
PROCallocate_double_letter(G%)
PROCallocate_double_letter(T%)
PROCallocate_double_letter(U%)
PROClog_colour(7)
PROClog_nl
PROClog_str("Creating squished code....")
PROClog_nl
ON ERROR REPORT:PRINT " (internal error ";ERL;")":PRINT "Line number in original = ";H%+256*J%:PROCtidyup:END
PROCwrite_program
PROClog_colour(7)
c$="Program '"+F$+"' squished to '"+f$+"' on "+MID$( TIME $,5,11)+" at "+MID$( TIME $,17)
PROClog_nl
PROClog_nl
PROClog_str(c$)
PROClog_nl
IF g%>=0 PRINT'c$'
c$="Errors          : "+STR$ (`%)
PROClog_nl
PROClog_str(c$)
IF g%>=0 PRINT c$
c$="Warnings        : "+STR$ (_%)
PROClog_nl
PROClog_str(c$)
IF g%>=0 PRINT c$
c$="Original length : &"+STR$~(ia%)
PROClog_nl
PROClog_str(c$)
IF g%>=0 PRINT c$
c$="New length      : &"+STR$~(j%+2-V%)
PROClog_nl
PROClog_str(c$)
IF g%>=0 PRINT c$
c$="Saving          : &"+STR$~(ia%-(j%+2-V%))
PROClog_nl
PROClog_str(c$)
IF g%>=0 PRINT c$
c$="Size of squished program is "+STR$ (((j%+2-V%)*100) DIV ia%)+"% of original"
PROClog_nl
PROClog_nl
PROClog_str(c$)
PROClog_nl
IF g%>=0 PRINT'c$'
t%=TIME
c$="Time Taken      : "+STR$ (t% DIV 6000)+" mins, "+STR$ ((t% MOD 6000) DIV 100)+" secs"
PROClog_nl
PROClog_str(c$)
PROClog_nl
IF g%>=0 PRINT c$
PRINT "List of little used integers ..."
PROClist_identifiers(B%)
PRINT "List of little used integer arrays ..."
PROClist_identifiers(F%)
PRINT "List of little used strings ..."
PROClist_identifiers(D%)
PRINT "List of little used string arrays ..."
PROClist_identifiers(G%)
PROCtidyup
END

DEF PROCread_program
d%=z%
l%=0
WHILE d%?1<>255
  J%=d%?1
  H%=d%?2
  na%=d%?3
  q%=na%+d%
  d%+=4
  W%=0
  v%=0
  oa%=0
  K%=0
  f%=0
  k%=0
  r%=TRUE
  WHILE d%<q% AND f%<>8
    PROCget_token
    CASE f% OF
    WHEN 1
      IF fa%=FALSE OR NOT (FNkeep_identifier(b$)) THEN
        IF FNadd_identifier(b$,R%):PROClog_str(b$):PROClog_tab
      ENDIF
      IF x%>0 THEN PROCerror(FALSE,"Found real '"+b$+"'")
    WHEN 2
      IF j$<>"" AND LEN (b$)=1 AND b$>="@" AND b$<="Z" THEN
      ELSE
        IF fa%=FALSE OR NOT (FNkeep_identifier(b$+"%")) THEN
          IF FNadd_identifier(b$,B%):PROClog_str(b$+"%"):PROClog_tab
        ENDIF
      ENDIF
    WHEN 3
      IF fa%=FALSE OR NOT (FNkeep_identifier(b$+"$")) THEN
        IF FNadd_identifier(b$,D%):PROClog_str(b$+"$"):PROClog_tab
      ENDIF
    WHEN 4
      IF fa%=FALSE OR NOT (FNkeep_identifier(b$+"()")) THEN
        IF FNadd_identifier(b$,S%):PROClog_str(b$+"("):PROClog_tab
      ENDIF
    WHEN 12
      IF fa%=FALSE OR NOT (FNkeep_identifier(b$+"%()")) THEN
        IF FNadd_identifier(b$,F%):PROClog_str(b$+"%("):PROClog_tab
      ENDIF
    WHEN 13
      IF fa%=FALSE OR NOT (FNkeep_identifier(b$+"$()")) THEN
        IF FNadd_identifier(b$,G%):PROClog_str(b$+"$("):PROClog_tab
      ENDIF
    WHEN 5
      IF fa%=FALSE OR NOT (FNkeep_identifier("PROC"+b$)) THEN
        IF FNadd_identifier(b$,U%):PROClog_str("PROC"+b$):PROClog_tab
      ENDIF
    WHEN 6
      IF fa%=FALSE OR NOT (FNkeep_identifier("FN"+b$)) THEN
        IF FNadd_identifier(b$,T%):PROClog_str("FN"+b$):PROClog_tab
      ENDIF
    WHEN 7
    WHEN 10
      IF b$=""""v%=NOT v%
    ENDCASE
    r%=0
    IF v%=0 THEN
      IF f%=0 THEN
        IF b$=CHR$ 223 OR b$=CHR$ 236 OR b$=CHR$ 200+CHR$ 146 OR b$=CHR$ 200+CHR$ 144:K%=1
        IF Z% AND (b$=CHR$ 200+CHR$ 153) THEN
          WHILE ?d%=32
            d%+=1
          ENDWHILE
          IF ?d%=34 THEN
            h$=""
            WHILE d%?1<>34 AND d%?1>31
              d%+=1
              h$+=CHR$ ?d%
            ENDWHILE
            d%+=2
            SYS "XOS_SWINumberFromString",,h$ TO pa%;y%
            IF (y% AND 1) AND x%>0 THEN PROCerror(FALSE,"SYS - can't translate """+h$+""" to a number")
          ENDIF
        ENDIF
      ENDIF
      IF k%=2:IF b$=",":k%=1
      IF b$=CHR$ 222
      IF b$="["l%+=1:IF l%=1:r%=TRUE
      IF b$="]"l%-=1:IF l%<0:ERROR 1,"Too many ]"
      IF b$=":"r%=TRUE:k%=0:K%=0
      IF (f%=1 OR f%=2) AND W%:r%=1
      W%=b$="."
    ENDIF
  ENDWHILE
ENDWHILE
ENDPROC

DEF FNg(v,v$)
LOCAL e%
IF CHR$ ?d%=" " THEN
  e%=d%+1
  WHILE ?e%=32
    e%+=1
  ENDWHILE
  IF INSTR("0123456789.E%&",CHR$ ?e%):v$+=" "
ENDIF
=v$

DEF PROCget_token
f%=-1
IF v% THEN
  n%=?d%
  b$=CHR$ n%
  f%=10
  d%+=1
ELSE
  IF l%=0 OR r%<>0:WHILE ?d%=32:d%+=1:ENDWHILE
  IF d%=q%:b$="":f%=8:ENDPROC
  n%=?d%
  CASE n% OF
  WHEN 141,228,229,247,184,160,220
    IF x%>=0 PROCdangerous(n%)
  ENDCASE
  CASE n% OF
  WHEN 198,199,200
    f%=0
    b$=CHR$ n%+CHR$ d%?1
    d%+=2
  WHEN 242
    f%=5
    d%+=1
    b$=FNread_identifier(d$)
  WHEN 164
    f%=6
    d%+=1
    b$=FNread_identifier(d$)
  WHEN ASC "&"
    f%=7
    a=EVAL FNread_identifier("0123456789ABCDEFabcdef")
    b$=STR$ a
  WHEN ASC "%"
    f%=7
    a=EVAL FNread_identifier("01")
    b$=STR$ a
  WHEN 48,49,50,51,52,53,54,55,56,57
    f%=7
    b$=FNread_identifier("0123456789.")
    a=EVAL b$
  WHEN ASC "*"
    IF FNk:f%=11:b$=FNh ELSE b$="*":f%=10:d%+=1
  WHEN 220
    f%=11
    b$=FNh
  WHEN 244
    f%=8
    b$=""
    d%=q%
  OTHERWISE
    IF n%=239 AND d%?1=ASC ":" THEN
    f%=8
    b$=""
    d%=q%
    ELSE
    IF (r%<>0) AND l% AND n%<>ASC "." THEN
      WHILE ?d%=32
        d%+=1
      ENDWHILE
      b$=""
      WHILE ?d%<>32 AND d%<q% AND INSTR(w$,CHR$ ?d%)<>0 AND RIGHT$(b$,3)<>"opt" AND RIGHT$(b$,3)<>"OPT"
        b$+=CHR$ ?d%
        d%+=1
      ENDWHILE
      f%=10
      WHILE ?d%=32 AND d%<q%
        d%+=1
      ENDWHILE
      IF INSTR("SWIswi",LEFT$(b$,3)):b$+=" "
      IF d%<q% IF INSTR("ABCDEFGHIJKLMNOPQSTUVWXYZabcdefghijklmnopqstuvwxyz",CHR$ ?d%) OR (CHR$ ((?d%) AND 223)="R" AND INSTR("0123456789",CHR$ d%?1)=0) THEN b$+=" "
      IF d%<q%:IF INSTR("MUL MLA mul mla",LEFT$(b$,3)):IF RIGHT$(b$,1)<>" ":b$+=" "
      IF (ASC (LEFT$(b$,1)) AND 223)=ASC "B":IF RIGHT$(b$,1)<>" ":b$+=" "
      IF r%=1 b$=" "+b$
    ELSE
      IF l% IF (n% AND 223)=ASC "R":IF INSTR("0123456789",CHR$ d%?1):d%+=1:f%=7:b$=FNread_identifier("0123456789"):a=VAL b$
      IF l% THEN
        p$=FNtoupper2(CHR$ ?d%+CHR$ d%?1+CHR$ d%?2)
        CASE p$ OF
        WHEN "LSR","ASR","LSL","ROR","RRX","ASL"
          b$=p$
          d%+=3
          f%=10
        ENDCASE
      ENDIF
      IF K% IF n%=ASC "B":IF d%?1=ASC "Y":f%=10:b$="BY ":d%+=2
      IF f%=-1:IF INSTR(d$,CHR$ n%) PROCread_variable:IF l%:IF b$="PC" OR b$="pc":f%=10
      IF r%=0:IF n%=ASC ".":f%=7:b$=FNread_identifier("0123456789."):a=EVAL b$
    ENDIF
    IF l% AND (n%=ASC "\" OR n%=ASC ";") THEN
      WHILE ?d%<>ASC ":" AND d%<q%
        d%+=1
      ENDWHILE
      IF d%=q%:f%=8:b$=""
    ENDIF
    IF f%=-1 AND n%>126 THEN
      f%=0
      b$=CHR$ ?d%
      d%+=1
    ENDIF
  ENDIF
ENDCASE
ENDIF
IF f%=-1:f%=10:b$=CHR$ ?d%:d%+=1
ENDPROC

DEF FNk
LOCAL I%
IF r% THEN=TRUE
I%=0
REPEAT
  I%-=1
UNTIL d%?I%<>32
CASE d%?I% OF
WHEN 245,140,139,204,127,133,234
  I%=TRUE
OTHERWISE
  I%=FALSE
ENDCASE
=I%

DEF PROCread_variable
b$=FNread_identifier(d$)
CASE ?d% OF
WHEN ASC "%"
  f%=2
  d%+=1
WHEN ASC "$"
  f%=3
  d%+=1
OTHERWISE
  f%=1
ENDCASE
IF ?d%=ASC "(" THEN
  IF f%=1:f%=4
  IF f%=2:f%=12
  IF f%=3:f%=13
  d%+=1
ENDIF
ENDPROC

DEF FNtoupper2(A$)
LOCAL a%
FOR a%=1 TO LEN A$
  IF MID$(A$,a%,1)>="a":IF MID$(A$,a%,1)<="z":MID$(A$,a%,1)=CHR$ (ASC MID$(A$,a%,1)-32)
NEXT
=A$

DEF FNread_identifier(d$)
LOCAL T$
REPEAT
  T$+=CHR$ ?d%
  d%+=1
UNTIL INSTR(d$,CHR$ ?d%)=0
IF d$="0123456789." AND CHR$ ?d%="E" THEN
  d%+=1
  d$="0123456789"
  CASE CHR$ ?d% OF
  WHEN "+","-"
    d%+=1
    IF INSTR(d$,CHR$ ?d%):T$+="E"+CHR$ ?(d%-1):REPEAT:T$+=CHR$ ?d%:d%+=1:UNTIL INSTR(d$,CHR$ ?d%)=0
  WHEN "0","1","2","3","4","5","6","7","8","9"
    T$+="E"
    REPEAT
      T$+=CHR$ ?d%
      d%+=1
    UNTIL INSTR(d$,CHR$ ?d%)=0
  ENDCASE
ENDIF
=T$

DEF FNh
LOCAL T$
REPEAT
  T$+=CHR$ ?d%
  d%+=1
UNTIL d%=q%
=T$

DEF PROCm(A$)
A%=ASC A$
?f=ASC RIGHT$(A$,1)
CALL e
N%=!c
i$=""
WHILE ?N%<127 AND ?N%>31
  i$+=CHR$ ?N%
  N%+=1
ENDWHILE
IF A$=CHR$ 184+"P":i$+="P"
PROClog_str(i$)
ENDPROC

DEF PROCs(A%)
CALL e
N%=!c
ENDWHILE
ENDPROC

DEF PROCt(A%,?f)
CALL e
N%=!c
i$=""
WHILE ?N%<127 AND ?N%>31
  i$+=CHR$ ?N%
  N%+=1
ENDWHILE
PROClog_str(i$)
ENDPROC

DEF PROCwrite_program
qa%=5
j%=0
V%=ka%+16
h%=V%
PROCstart_next_line
PROCwrite(CHR$ 244+">"+f$)
PROClog_str("REM>"+f$)
PROCstart_next_line
d%=z%
l%=0
WHILE d%?1<>255
  J%=d%?1
  H%=d%?2
  na%=d%?3
  q%=na%+d%
  d%+=4
  W%=0
  v%=0
  oa%=0
  f%=0
  k%=0
  K%=0
  r%=TRUE
  ra%=0
  sa%=1
  m%=FALSE
  WHILE d%<q% AND f%<>8
    PROCget_token
    CASE f% OF
    WHEN 0
      m%=FALSE
      CASE ASC b$ OF
      WHEN 127,201,203,204,205,220,221
        IF sa%:IF h%-j%<>4:PROCstart_next_line
      ENDCASE
      PROClog_colour(1)
      IF b$=CHR$ 127:PROClog_str("OTHERWISE"):PROCwrite(CHR$ 127) ELSE IF b$<>CHR$ 244:PROCm(b$):PROClog_colour(7):PROCwrite(b$)
      IF b$=CHR$ 244:d%=q%
      IF b$=CHR$ 200+CHR$ 153 m%=TRUE
      IF b$=CHR$ 179:IF INSTR(":;,'=/><(""",CHR$ ?d%)=0:PROClog_str(" "):PROCwrite(" ")
      CASE ASC b$ OF
      WHEN 231,239,220,202,203,205,215,225,247,249,250,238,224
        ra%=1
      ENDCASE
      PROClog_colour(7)
    WHEN 1
      m%=FALSE
      PROClog_colour(5)
      PROClog_str(FNa(b$,R%))
      PROClog_colour(7)
      PROCwrite(FNa(b$,R%))
      IF k%=1:PROClog_str(" "):k%=2:PROCwrite(" ")
    WHEN 2
      m%=FALSE
      PROClog_colour(5)
      PROClog_str(FNa(b$,B%))
      PROClog_colour(7)
      PROCwrite(FNa(b$,B%))
      IF k%=1 IF d%?1<>ASC "!":PROClog_str(" "):k%=2:PROCwrite(" ")
    WHEN 3
      m%=FALSE
      PROClog_colour(5)
      PROClog_str(FNa(b$,D%))
      PROClog_colour(7)
      PROCwrite(FNa(b$,D%))
    WHEN 4
      m%=FALSE
      PROClog_colour(5)
      PROClog_str(FNa(b$,S%))
      PROClog_colour(7)
      PROCwrite(FNa(b$,S%))
      IF k%=1:k%=2
    WHEN 12
      m%=FALSE
      PROClog_colour(5)
      PROClog_str(FNa(b$,F%))
      PROClog_colour(7)
      PROCwrite(FNa(b$,F%))
      IF k%=1:k%=2
    WHEN 13
      m%=FALSE
      PROClog_colour(5)
      PROClog_str(FNa(b$,G%))
      PROClog_colour(7)
      PROCwrite(FNa(b$,G%))
      IF k%=1:k%=2
    WHEN 5
      m%=FALSE
      PROClog_colour(1)
      PROClog_str("PROC")
      PROClog_colour(5)
      PROClog_str(FNa(b$,U%))
      PROClog_colour(7)
      PROCwrite(CHR$ 242+FNa(b$,U%))
    WHEN 6
      m%=FALSE
      PROClog_colour(1)
      PROClog_str("FN")
      PROClog_colour(5)
      PROClog_str(FNa(b$,T%))
      PROClog_colour(7)
      PROCwrite(CHR$ 164+FNa(b$,T%))
    WHEN 7
      m%=FALSE
      PROClog_colour(4)
      PROClog_str(FNg(a,b$))
      PROClog_colour(7)
      PROCwrite(FNg(a,b$))
    WHEN 10
      IF l%:IF b$=":":WHILE h%?-1=32:h%-=1:ENDWHILE
      IF Z% AND m% THEN
        IF v% AND b$<>""""h$+=b$
      ELSE
        PROClog_str(b$)
        PROCwrite(b$)
        IF v% AND b$<>"""" AND o% BPUT #o%,b$;
      ENDIF
      IF b$="""" THEN
        v%=NOT v%
        IF NOT v% THEN
          IF o% AND da%<>PTR #o% BPUT #o%,10
          IF Z% AND m% THEN
            SYS "XOS_SWINumberFromString",,h$ TO pa%;y%
            IF (y% AND 1) THEN
              PROClog_str(""""+h$+"""")
              PROCwrite(""""+h$+"""")
            ELSE
              PROClog_colour(4)
              PROClog_str(STR$ (pa%))
              PROClog_colour(7)
              PROCwrite(STR$ (pa%))
            ENDIF
            m%=FALSE
          ENDIF
          PROClog_str(FNi)
          PROCwrite(FNi)
        ELSE
          IF m% h$=""
          IF o% da%=PTR #o%
        ENDIF
      ENDIF
    WHEN 11
      m%=FALSE
      IF sa%:IF h%-j%<>4:PROCstart_next_line
      IF LEFT$(b$,1)>CHR$ 126:PROClog_colour(1):PROCm(LEFT$(b$,1)):PROClog_colour(7):PROClog_str(RIGHT$(b$,LEN (b$)-1))
      IF LEFT$(b$,1)="*" PROClog_colour(2):PROClog_str(b$):PROClog_colour(7)
      PROCwrite(b$)
      ra%=1
    ENDCASE
    r%=0
    IF v%=0 THEN
      IF f%=0:IF b$=CHR$ 223 OR b$=CHR$ 236 OR b$=CHR$ 200+CHR$ 146 OR b$=CHR$ 200+CHR$ 144:K%=1
      IF k%=2:IF b$=",":k%=1
      IF b$=CHR$ 222
      IF b$="["l%+=1:IF l%=1:r%=TRUE
      IF b$="]"l%-=1:IF l%<0:ERROR 1,"Too many ]"
      IF b$=":"r%=TRUE:k%=0:K%=0:WHILE ?d%=32:d%+=1:ENDWHILE
      IF (f%=1 OR f%=2) AND W%:r%=1
      W%=b$="."
    ENDIF
    sa%=0
  ENDWHILE
  IF h%<>j%+4 THEN
    WHILE (h%?-1=32) AND (h%<>j%+4)
      h%-=1
    ENDWHILE
    IF ra%=0 AND (h%-j%+d%?3)<250 THEN
      IF CHR$ (h%?-1)<>":":PROClog_str(":"):PROCwrite(":")
    ELSE PROCstart_next_line
    ENDIF
  ENDIF
ENDWHILE
IF h%-j%<>4:PROCstart_next_line
j%?1=255
SYS "OS_File",0,f$,ga%,ha%,V%,j%+2
ENDPROC

DEF FNa($b,A%)
LOCAL d$,e%
IF USR asm_add_identifier=0 THEN
  d$=$b
ELSE
  d$=j$+$(!g+6+LEN ($(!g+5)))
ENDIF
CASE f% OF
WHEN 2
  d$+="%"
WHEN 3
  d$+="$"
WHEN 4
  d$+="("
WHEN 12
  d$+="%("
WHEN 13
  d$+="$("
ENDCASE
IF CHR$ ?d%=" " THEN
  CASE f% OF
  WHEN 1,5,6
    e%=d%+1
    WHILE ?e%=32
      e%+=1
    ENDWHILE
    IF ?e%>31 AND ?e%<127 AND INSTR(":;,'=/><""",CHR$ ?e%)=0:d$+=" "
  WHEN 2
    e%=d%+1
    WHILE ?e%=32
      e%+=1
    ENDWHILE
    IF INSTR("(!?|$",CHR$ ?e%):d$+=" "
  WHEN 3
    e%=d%+1
    WHILE ?e%=32
      e%+=1
    ENDWHILE
    IF CHR$ ?e%="(":d$+=" "
  ENDCASE
ENDIF
=d$

DEF FNalloc(s%)
IF s% AND 3 s%=(s% AND NOT 3)+4
ka%+=s%
IF ka%>la% ERROR 1,"Heap burst!"
=ka%-s%

DEF FNadd_identifier($b,A%)
CALL asm_add_identifier
ta%=!d
IF ta%=1:=0
u%=FNalloc(LEN ($b)+ca%+7)
!ta%=u%
!u%=0
u%?4=1
$(u%+5)=$b
?(u%+6+LEN $b)=13
=1

DEF FNnew_identifier_list
u%=FNalloc(16+128)
FOR A%=u%+8 TO u%+128+8
  ?A%=0
NEXT
u%?8=255
!u%=0
u%?4=0
$(u%+5)="**"+CHR$ 13+CHR$ 13
=u%

DEF PROClist_identifiers(i%)
@%=8
REPEAT
  IF i%?4<6 PRINT $(i%+5),i%?4/2
  b%=!i%
  IF !i%:i%=!i%
UNTIL b%=0
ENDPROC

DEF PROCunused(i%)
@%=8
REPEAT
  PRINT "NEXT = "~!i% " Cyc = "i%?4"  Old = "$(i%+5) " New = "$(LEN ($(i%+5))+6+i%)
  b%=!i%
  IF !i%:i%=!i%
UNTIL b%=0
ENDPROC

DEF PROCallocate_single_letter(e%)
LOCAL i%,L%,M%,ua%,O%
i%=e%
REPEAT
  L%=(!e%=0)
  v$=$(e%+5)
  IF LEN v$=1 THEN
    $(e%+5+LEN v$+1)=v$
    ?(i%+8+INSTR(g$,v$))=255
    e%?4=0
    PROClog_str(v$+" ")
  ENDIF
  e%=!e%
UNTIL L%
e%=i%
O%=2
REPEAT
  IF ?(i%+8+O%)=0 THEN
    e%=i%
    M%=0
    REPEAT
      L%=(!e%=0)
      IF e%?4>M%:M%=e%?4:va%=e%
      e%=!e%
    UNTIL L%
    IF M%<>0:$(va%+6+LEN ($(va%+5)))=MID$(g$,O%,1):va%?4=0:PROClog_str(MID$(g$,O%,1)+" ")
  ELSE M%=1
  ENDIF
  O%+=1
UNTIL O%=LEN g$-9 OR M%=0
ENDPROC

DEF PROCallocate_double_letter(i%)
LOCAL e%,X%,wa%
e%=i%
X%=2
wa%=2
REPEAT
  L%=(!e%=0)
  IF e%?4 THEN $(e%+6+LEN ($(e%+5)))=FNm
  e%=!e%
UNTIL L%
ENDPROC

DEF FNm
a$=MID$(g$,X%,1)+MID$(g$,wa%,1)
X%+=1
IF MID$(g$,X%,1)="0"X%=2:wa%+=1
IF (ASC (MID$(a$,1,1)) AND 223)=ASC ("P") AND (ASC (MID$(a$,2,1)) AND 223)=ASC ("C") THEN a$=MID$(g$,X%,1)+MID$(g$,wa%,1):X%+=1
IF wa%>LEN g$ ERROR 1,"out of vars!"
PROClog_str(a$+" ")
=a$

DEF PROCwrite(A$)
$h%=A$
h%+=LEN A$
IF h%>=la% ERROR 1,"Out of memory for new prog!"
ENDPROC

DEF PROCstart_next_line
PROClog_nl
PROClog_str(RIGHT$("    "+STR$ (qa%),5))
IF j%:WHILE h%?-1=ASC ":":h%-=1:ENDWHILE:WHILE h%?-1=32:h%-=1:ENDWHILE:WHILE h%?-1=ASC ":":h%-=1:ENDWHILE
IF j%:j%?3=h%-j%:IF h%-j%>252 ERROR 1,"dest line too long!"
j%=h%
?h%=13
h%?1=qa%>>8
h%?2=qa%
h%?3=0
h%+=4
qa%+=5
ENDPROC

DEF PROCprocess_args(RETURN q$,RETURN r$)
LOCAL z%,p%,w%,e$,a$()
DIM a$(20)
DIM z% 256
w%=0
q$=""
r$=""
n$=""
SYS "OS_GetEnv" TO e$
WHILE RIGHT$(e$,1)=" "
  e$=LEFT$(e$,LEN (e$)-1)
ENDWHILE
WHILE e$<>"" AND w%<=20
  WHILE LEFT$(e$,1)=" "
    e$=MID$(e$,2)
  ENDWHILE
  IF e$<>"" THEN
    p%=INSTR(e$," ")
    IF p%=0 THEN p%=1+LEN (e$)
    a$(w%)=LEFT$(e$,p%-1)
    e$=MID$(e$,p%+1)
    w%+=1
  ENDIF
ENDWHILE
p%=0
WHILE p%<w%
  CASE FNtoupper(a$(p%)) OF
  WHEN "-HELP"
    PRINT "Squish ("+l$+")"'
    PRINT "Syntax: *Squish -from <file> -to <file> [-warn <level>] [-literal <file>]"
    PRINT "           [-list <file> | -nolist] [-colour] [-nosys]"
    END
  WHEN "-FROM"
    IF p%<w%-1 THEN
      q$=a$(p%+1)
      p%+=1
    ENDIF
  WHEN "-TO"
    IF p%<w%-1 THEN
      r$=a$(p%+1)
      p%+=1
    ENDIF
  WHEN "-WARN"
    IF p%<w%-1 THEN
      x%=EVAL (a$(p%+1))
      p%+=1
      IF x%<0 x%=0 ELSE IF x%>2 x%=2
    ENDIF
  WHEN "-NOLIST"
    g%=0
  WHEN "-LITERAL"
    IF p%<w%-1 THEN
      o$=a$(p%+1)
      p%+=1
      o%=1
    ENDIF
  WHEN "-LIST"
    IF p%<w%-1 THEN
      n$=a$(p%+1)
      p%+=1
      g%=1
    ENDIF
  WHEN "-COLOUR"
    ea%=TRUE
  WHEN "-NOSYS"
    Z%=FALSE
  WHEN "-KEEP"
    IF p%<w%-1 THEN
      m$=a$(p%+1)
      p%+=1
      fa%=TRUE
    ENDIF
  WHEN "-PREFIX"
    IF p%<w%-1 THEN
      j$=a$(p%+1)
      p%+=1
    ENDIF
  ENDCASE
  p%+=1
ENDWHILE
ENDPROC

DEF FNkeep_identifier(x$)
LOCAL y%
IF fa% THEN
  y%=OPENIN (m$)
  IF y%<>0 THEN
    WHILE NOT EOF #y%
      IF GET$ #y%=x$ THEN
        CLOSE #y%
        =TRUE
      ENDIF
    ENDWHILE
    CLOSE #y%
  ENDIF
ENDIF
=FALSE

DEF FNtoupper(s$)
LOCAL c%,p%
IF s$<>"" THEN
  FOR p%=1 TO LEN (s$)
    c%=ASC (MID$(s$,p%,1))
    IF c%>=ASC ("a") AND c%<=ASC ("z") THEN MID$(s$,p%,1)=CHR$ (c%-32)
  NEXT p%
ENDIF
=s$

DEF PROClog_colour(C%)
IF g%<0 AND ea%<>0:COLOUR C%
ENDPROC

DEF FNi
LOCAL s$,e%
IF CHR$ ?d%=" " THEN
  e%=d%+1
  WHILE ?e%=32
    e%+=1
  ENDWHILE
  IF CHR$ ?e%="""":s$=" "
ENDIF
=s$

DEF PROCdangerous(xa%)
LOCAL e%
e%=d%+1
WHILE ?e%=32
  e%+=1
ENDWHILE
CASE xa% OF
WHEN 141
  d%+=3
  PROCerror(TRUE,"Line number reference")
WHEN 228,229
  IF ?e%=141 THEN d%=e%-1 ELSE PROCerror(TRUE,"Line number reference")
WHEN 247
  CASE ?e% OF
  WHEN 141
    d%=e%-1
  WHEN 133,13,ASC ":"
  OTHERWISE PROCerror(FALSE,"RESTORE - check DATA layout is safe")
  ENDCASE
WHEN 184
  IF d%?1=ASC ("P") AND d%?2<>32 THEN
    e%+=1
    PROCerror(FALSE,"TOP"+CHR$ (?e%)+"... - assuming ""TO P"+CHR$ (?e%)+"..."" - insert space if ""TOP "+CHR$ (?e%)+"...""")
  ENDIF
WHEN 160
  PROCerror(FALSE,"EVAL - check not evaluating variable names")
WHEN 220
  PROCerror(FALSE,"DATA - check READ will not evaluate variable names")
ENDCASE
ENDPROC

DEF PROCerror(ya%,t$)
IF ya% THEN
  `%+=1
  c$="** ERROR   at line "+STR$ (H%+256*J%)+": "+t$
  PROClog_nl
  PROClog_str(c$)
  PROClog_nl
  IF g%>=0 PRINT c$
ELSE
  IF x%:_%+=1:c$="** WARNING at line "+STR$ (H%+256*J%)+": "+t$
  IF x%:PROClog_nl:PROClog_str(c$):PROClog_nl:IF g%>=0 PRINT c$
ENDIF
ENDPROC

DEF PROClog_str(k$)
CASE TRUE OF
WHEN g%<0
  PRINT (k$);
WHEN g%>0
  BPUT #g%,k$;
ENDCASE
Q%+=LEN (k$)
IF Q%>70 PROClog_nl
ENDPROC

DEF PROClog_nl
CASE TRUE OF
WHEN g%<0
  PRINT
WHEN g%>0
  BPUT #g%,CHR$ 10;
ENDCASE
Q%=0
ENDPROC

DEF PROClog_tab
IF !d=1:ENDPROC
IF Q%>70 PROClog_nl ELSE PROClog_str(STRING$((256-Q%) MOD 16," "))
ENDPROC

DEF PROCtidyup
IF g%>0 CLOSE #g%:g%=0
IF o% CLOSE #o%:o%=0:SYS "OS_File",18,o$,4095
ENDPROC

DEF PROCknockoutkept(e%,pre$,post$):REM removes single-char keep names from list
LOCAL O%
FOR O%=2 TO LEN g$-10
  IF fa%=TRUE AND FNkeep_identifier(pre$+MID$(g$,O%,1)+post$) THEN ?(e%+8+O%)=255
NEXT
ENDPROC