Date : Sun, 22 Nov 2009 18:51:39 +0000
From : jgh@... (Jonathan Graham Harston)
Subject: Detokeniser, was Quine
Try this code.
10REM > DetokTest
20REM Test detokiser
30:
40A%=PAGE:REPEAT
50IFA%?1<>&FF:PRINTFNd(A%?2+256*A%?1,5);FNDetokenise($(A%+4)):A%=A%+A%?3
60UNTILA%?1=&FF
70END
80:
90DEFFNd(A%,N%)=RIGHT$(" "+STR$A%,N%)
100:
110GOTO 130
120:
130DEFFNDetokenise(A$):LOCAL A%,B%,C%,D%
140tok%=tok%:IFtok%=0:tok%=&8000:REPEATtok%=tok%+1:UNTIL!tok%=&80444E41
150B%=0:A%=0:REPEAT:A%=A%+1:C%=ASCMID$(A$,A%,1)
160IFC%=34:B%=B%EOR1:VDU34:UNTILA%>=LENA$:=""
170IFB%:VDUC%:UNTILA%>=LENA$:=""
180IFC%=&F4:B%=&80
190IFC%<128:VDUC%:UNTILA%>=LENA$:=""
200IFC%=141:B%=ASCMID$(A$,A%+1,1):C%=ASCMID$(A$,A%+2,1):D%=ASCMID$(A$,A%+3,
1):PRINT;(C%AND63)+256*(D%AND63)+4*((B%AND&30)EOR&10)+&1000*((B%AND&C0)EOR&40)
;:B%=0:A%=A%+3:UNTILA%>=LENA$:=""
210D%=tok%:REPEAT:B%=D%:REPEAT:D%=D%+1:UNTIL?D%>127:D%=D%+2
220UNTILD%?-2=C%:REPEATVDU?B%:B%=B%+1:UNTIL?B%>127
240B%=0:UNTILA%>=LENA$:=""
Also at http://mdfs.net/jgh/Develop/BBCBasic
I've been trying to develop a detokeniser library to compliment the
tokeniser routines at mdfs.net/blib, but the big problem with
detokenising is that the output string will almost always be longer
than the input string. That makes it difficult to return the
detokenised line as a string, as if the input string is 254 bytes
long with at least one token in it, the resultant string is going
to be longer than 255 characters.
--
J.G.Harston - jgh@... - mdfs.net/User/JGH
BBC IDE Hard Drive Interface - http://mdfs.net/Info/Comp/BBC/IDE