Merge pull request #383 from asarhaddon/ada2tco-do
[jackhill/mal.git] / basic / printer.in.bas
CommitLineData
c756af81 1REM PR_STR(AZ, B) -> R$
11f94d2e 2PR_STR:
c756af81 3 R$=""
60270667 4 PR_STR_RECUR:
d7a6c2d6
JM
5 T=Z%(AZ)AND 31
6 U=Z%(AZ+1)
9d59cdb3 7 REM PRINT "AZ: "+STR$(AZ)+", T: "+STR$(T)+", U: "+STR$(U)
cc9dbd92 8 IF T=0 THEN R$="nil":RETURN
bbab5c5d 9 REM if metadata, then get actual object
d7a6c2d6 10 IF T>=14 THEN AZ=U:GOTO PR_STR_RECUR
a742287e 11 ON T GOTO PR_BOOLEAN,PR_INTEGER,PR_UNKNOWN,PR_STRING_MAYBE,PR_SYMBOL,PR_SEQ,PR_SEQ,PR_SEQ,PR_FUNCTION,PR_MAL_FUNCTION,PR_MAL_FUNCTION,PR_ATOM,PR_ENV,PR_RECUR,PR_FREE
11f94d2e 12
60ef223c 13 PR_UNKNOWN:
7895453b
JM
14 REM MEMORY DEBUGGING:
15 REM R$="#<unknown>"
60ef223c
JM
16 RETURN
17 PR_RECUR:
c756af81 18 AZ=U
60ef223c
JM
19 GOTO PR_STR_RECUR
20 PR_BOOLEAN:
21 R$="true"
c756af81 22 IF U=0 THEN R$="false"
60ef223c 23 RETURN
11f94d2e 24 PR_INTEGER:
c756af81 25 T$=STR$(U)
11f94d2e 26 REM Remove initial space
c756af81
JM
27 IF U>=0 THEN T$=RIGHT$(T$,LEN(T$)-1)
28 R$=R$+T$
11f94d2e 29 RETURN
a742287e 30 PR_STRING_MAYBE:
c756af81 31 R$=S$(U)
a742287e
JM
32 IF LEN(R$)=0 THEN GOTO PR_STRING
33 IF MID$(R$,1,1)=CHR$(127) THEN R$=":"+MID$(R$,2,LEN(R$)-1):RETURN
11f94d2e 34 PR_STRING:
f9f1cec9 35 IF B=1 THEN GOTO PR_STRING_READABLY
241d5d57
JM
36 RETURN
37 PR_STRING_READABLY:
c756af81
JM
38 S1$="\":S2$="\\":GOSUB REPLACE: REM escape backslash "
39 S1$=CHR$(34):S2$="\"+CHR$(34):GOSUB REPLACE: REM escape quotes "
115e430d
JM
40 #cbm S1$=CHR$(13):S2$="\n":GOSUB REPLACE: REM escape newlines
41 #qbasic S1$=CHR$(10):S2$="\n":GOSUB REPLACE: REM escape newlines
85d70fb7 42 R$=CHR$(34)+R$+CHR$(34)
11f94d2e
JM
43 RETURN
44 PR_SYMBOL:
c756af81 45 R$=S$(U)
11f94d2e 46 RETURN
b7b1787f 47 PR_SEQ:
85d70fb7 48 REM push the type and where we are in the sequence
93593012
JM
49 Q=T:GOSUB PUSH_Q
50 Q=AZ:GOSUB PUSH_Q
c756af81
JM
51 REM save the current rendered string
52 S$(S)=R$:S=S+1
b7b1787f 53 PR_SEQ_LOOP:
d7a6c2d6
JM
54 IF Z%(AZ+1)=0 THEN GOTO PR_SEQ_DONE
55 AZ=Z%(AZ+2):GOSUB PR_STR:GOSUB PEEK_Q_1:T=Q
c756af81
JM
56 REM append what we just rendered it
57 S$(S-1)=S$(S-1)+R$
9d59cdb3
JM
58
59 REM if this is a hash-map, print the next element
d7a6c2d6 60 IF T=8 THEN GOSUB PEEK_Q:AZ=Z%(Q+3):GOSUB PR_STR:S$(S-1)=S$(S-1)+" "+R$
9d59cdb3 61
85d70fb7 62 REM restore current seq type
93593012 63 GOSUB PEEK_Q_1:T=Q
11f94d2e 64 REM Go to next list element
93593012 65 GOSUB PEEK_Q
d7a6c2d6 66 AZ=Z%(Q+1)
93593012 67 Q=AZ:GOSUB PUT_Q
d7a6c2d6 68 IF Z%(AZ+1)<>0 THEN S$(S-1)=S$(S-1)+" "
b7b1787f
JM
69 GOTO PR_SEQ_LOOP
70 PR_SEQ_DONE:
c756af81
JM
71 REM restore the current string
72 S=S-1:R$=S$(S)
85d70fb7 73 REM pop where we are the sequence and type
93593012
JM
74 GOSUB POP_Q
75 GOSUB POP_Q:T=Q: REM get type
c756af81
JM
76 IF T=6 THEN R$="("+R$+")"
77 IF T=7 THEN R$="["+R$+"]"
78 IF T=8 THEN R$="{"+R$+"}"
11f94d2e 79 RETURN
b7b1787f 80 PR_FUNCTION:
7895453b 81 R$="#<fn"+STR$(U)+">"
b7b1787f 82 RETURN
241d5d57 83 PR_MAL_FUNCTION:
cc9dbd92 84 T1=AZ
d7a6c2d6 85 AZ=Z%(T1+2):GOSUB PR_STR
c756af81
JM
86 REM append what we just rendered it
87 S$(S)="(fn* "+R$:S=S+1
d7a6c2d6 88 AZ=Z%(T1+1):GOSUB PR_STR
c756af81
JM
89 S=S-1
90 R$=S$(S)+" "+R$+")"
241d5d57 91 RETURN
85d70fb7 92 PR_ATOM:
c756af81 93 AZ=U:GOSUB PR_STR
bf8d1f7d 94 R$="(atom "+R$+")"
85d70fb7 95 RETURN
412e7348 96 PR_ENV:
c756af81 97 R$="#<env"+STR$(AZ)+", data"+STR$(U)+">"
412e7348
JM
98 RETURN
99 PR_FREE:
c756af81 100 R$="#<free"+STR$(AZ)+", next"+STR$(U)+">"
412e7348 101 RETURN
b7b1787f 102
f9f1cec9
JM
103REM PR_STR_SEQ(AZ, B, B$) -> R$
104REM - B is print_readably
105REM - B$ is the separator
241d5d57 106PR_STR_SEQ:
f9f1cec9 107 V=AZ
c756af81 108 S$(S)="":S=S+1
241d5d57 109 PR_STR_SEQ_LOOP:
d7a6c2d6
JM
110 IF Z%(V+1)=0 THEN S=S-1:R$=S$(S):RETURN
111 AZ=Z%(V+2):GOSUB PR_STR
241d5d57 112 REM goto the next sequence element
d7a6c2d6
JM
113 V=Z%(V+1)
114 IF Z%(V+1)=0 THEN S$(S-1)=S$(S-1)+R$
115 IF Z%(V+1)<>0 THEN S$(S-1)=S$(S-1)+R$+B$
241d5d57 116 GOTO PR_STR_SEQ_LOOP