make, swift3: fix parsing empty literal sequences.
[jackhill/mal.git] / basic / printer.in.bas
index ea83f5a..55c6360 100644 (file)
@@ -2,16 +2,17 @@ REM PR_STR(AZ, B) -> R$
 PR_STR:
   R$=""
   PR_STR_RECUR:
-  T=Z%(AZ,0)AND 31
-  U=Z%(AZ,1)
-  REM PRINT "AZ: "+STR$(AZ)+", T: "+STR$(T)+", C: "+STR$(U)
+  T=Z%(AZ)AND 31
+  U=Z%(AZ+1)
+  REM PRINT "AZ: "+STR$(AZ)+", T: "+STR$(T)+", U: "+STR$(U)
   IF T=0 THEN R$="nil":RETURN
   REM if metadata, then get actual object
-  IF T>=16 THEN AZ=U:GOTO PR_STR_RECUR
+  IF T>=14 THEN AZ=U:GOTO PR_STR_RECUR
   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
 
   PR_UNKNOWN:
-    R$="#<unknown>"
+    REM MEMORY DEBUGGING:
+    REM R$="#<unknown>"
     RETURN
   PR_RECUR:
     AZ=U
@@ -36,7 +37,8 @@ PR_STR:
   PR_STRING_READABLY:
     S1$="\":S2$="\\":GOSUB REPLACE: REM escape backslash "
     S1$=CHR$(34):S2$="\"+CHR$(34):GOSUB REPLACE: REM escape quotes "
-    S1$=CHR$(13):S2$="\n":GOSUB REPLACE: REM escape newlines
+    #cbm S1$=CHR$(13):S2$="\n":GOSUB REPLACE: REM escape newlines
+    #qbasic S1$=CHR$(10):S2$="\n":GOSUB REPLACE: REM escape newlines
     R$=CHR$(34)+R$+CHR$(34)
     RETURN
   PR_SYMBOL:
@@ -49,17 +51,21 @@ PR_STR:
     REM save the current rendered string
     S$(S)=R$:S=S+1
     PR_SEQ_LOOP:
-      IF Z%(AZ,1)=0 THEN GOTO PR_SEQ_DONE
-      AZ=AZ+1:GOSUB PR_STR
+      IF Z%(AZ+1)=0 THEN GOTO PR_SEQ_DONE
+      AZ=Z%(AZ+2):GOSUB PR_STR:GOSUB PEEK_Q_1:T=Q
       REM append what we just rendered it
       S$(S-1)=S$(S-1)+R$
+
+      REM if this is a hash-map, print the next element
+      IF T=8 THEN GOSUB PEEK_Q:AZ=Z%(Q+3):GOSUB PR_STR:S$(S-1)=S$(S-1)+" "+R$
+
       REM restore current seq type
       GOSUB PEEK_Q_1:T=Q
       REM Go to next list element
       GOSUB PEEK_Q
-      AZ=Z%(Q,1)
+      AZ=Z%(Q+1)
       Q=AZ:GOSUB PUT_Q
-      IF Z%(AZ,1)<>0 THEN S$(S-1)=S$(S-1)+" "
+      IF Z%(AZ+1)<>0 THEN S$(S-1)=S$(S-1)+" "
       GOTO PR_SEQ_LOOP
     PR_SEQ_DONE:
       REM restore the current string
@@ -72,15 +78,14 @@ PR_STR:
       IF T=8 THEN R$="{"+R$+"}"
       RETURN
   PR_FUNCTION:
-    T1=U
-    R$="#<function"+STR$(T1)+">"
+    R$="#<fn"+STR$(U)+">"
     RETURN
   PR_MAL_FUNCTION:
     T1=AZ
-    AZ=Z%(T1+1,0):GOSUB PR_STR
+    AZ=Z%(T1+2):GOSUB PR_STR
     REM append what we just rendered it
     S$(S)="(fn* "+R$:S=S+1
-    AZ=Z%(T1,1):GOSUB PR_STR
+    AZ=Z%(T1+1):GOSUB PR_STR
     S=S-1
     R$=S$(S)+" "+R$+")"
     RETURN
@@ -102,10 +107,10 @@ PR_STR_SEQ:
   V=AZ
   S$(S)="":S=S+1
   PR_STR_SEQ_LOOP:
-    IF Z%(V,1)=0 THEN S=S-1:R$=S$(S):RETURN
-    AZ=V+1:GOSUB PR_STR
+    IF Z%(V+1)=0 THEN S=S-1:R$=S$(S):RETURN
+    AZ=Z%(V+2):GOSUB PR_STR
     REM goto the next sequence element
-    V=Z%(V,1)
-    IF Z%(V,1)=0 THEN S$(S-1)=S$(S-1)+R$
-    IF Z%(V,1)<>0 THEN S$(S-1)=S$(S-1)+R$+B$
+    V=Z%(V+1)
+    IF Z%(V+1)=0 THEN S$(S-1)=S$(S-1)+R$
+    IF Z%(V+1)<>0 THEN S$(S-1)=S$(S-1)+R$+B$
     GOTO PR_STR_SEQ_LOOP