Basic: fix errors, reader, if form. Self-host 0-3
[jackhill/mal.git] / basic / qb2cbm.sh
index 4a3105e..b507853 100755 (executable)
@@ -3,6 +3,12 @@
 set -e
 
 DEBUG=${DEBUG:-}
+KEEP_REM=${KEEP_REM:-1}
+# 0 - drop all REMs
+# 1 - keep LABEL and INCLUDE REMs (and blank lines)
+# 2 - keep LABEL, INCLUDE, and GOTO REMs
+# 3 - keep LABEL, INCLUDE, GOTO, and whole line REMs
+# 4 - keep all REMS (end of line REMs too)
 
 infile=$1
 
@@ -33,7 +39,11 @@ while [[ ${input} =~ REM\ \$INCLUDE:\ \'.*\' ]]; do
             fi
             [ "${DEBUG}" ] && echo >&2 "including: ${include}"
             included[${include}]="done"
-            full="${full}\nREM vvv BEGIN '${include}' vvv\n$(cat ${include})\nREM vvv END '${include}' vvv\n"
+            if [ "${KEEP_REM}" -ge 1 ]; then
+                full="${full}\nREM vvv BEGIN '${include}' vvv\n$(cat ${include})\nREM vvv END '${include}' vvv\n"
+            else
+                full="${full}\n$(cat ${include})\n"
+            fi
         else
             full="${full}${line}\n"
         fi
@@ -47,27 +57,51 @@ done
 data=""
 declare -A labels
 
-lnum=10
+lnum=1
 while read -r line; do
-    if [[ ${line} =~ ^\ *$ ]]; then
-            [ "${DEBUG}" ] && echo >&2 "found blank line after $lnum"
+    if [[ ${line} =~ ^\ *# ]]; then
+        [ "${DEBUG}" ] && echo >&2 "ignoring # style comment at $lnum"
+        continue
+    elif [[ "${KEEP_REM}" -lt 3 && ${line} =~ ^\ *REM && \
+            ! ${line} =~ REM\ vvv && ! ${line} =~ REM\ ^^^ ]]; then
+        [ "${DEBUG}" ] && echo >&2 "dropping REM comment: ${line}"
+        continue
+    elif [[ ${line} =~ ^\ *$ ]]; then
+        if [ "${KEEP_REM}" -ge 1 ]; then
+            [ "${DEBUG}" ] && echo >&2 "found blank line at $lnum"
             data="${data}\n"
-            continue
-    elif [[ ${line} =~ ^[A-Za-z_]*:$ ]]; then
+        else
+            [ "${DEBUG}" ] && echo >&2 "ignoring blank line at $lnum"
+        fi
+        continue
+    elif [[ ${line} =~ ^[A-Za-z_][A-Za-z0-9_]*:$ ]]; then
         label=${line%:}
         [ "${DEBUG}" ] && echo >&2 "found label ${label} at $lnum"
         labels[${label}]=$lnum
-        data="${data}${lnum} REM ${label}:\n"
+        if [ "${KEEP_REM}" -ge 1 ]; then
+            data="${data}${lnum} REM ${label}:\n"
+        else
+            continue
+        fi
     else
         data="${data}${lnum} ${line}\n"
     fi
-    lnum=$(( lnum + 10 ))
+    lnum=$(( lnum + 1 ))
 done < <(echo -e "${input}")
 
+if [[ "${KEEP_REM}" -lt 4 ]]; then
+    [ "${DEBUG}" ] && echo >&2 "Dropping line ending REMs"
+    data=$(echo -e "${data}" | sed "s/: REM [^\n]*$//")
+fi
+
 for label in "${!labels[@]}"; do
     [ "${DEBUG}" ] && echo >&2 "Updating label: ${label}"
     lnum=${labels[${label}]}
-    data=$(echo "${data}" | sed "s/\(THEN\|GOTO\|GOSUB\) ${label}\>/\1 ${lnum}: REM \1 ${label}/g")
+    if [ "${KEEP_REM}" -ge 2 ]; then
+        data=$(echo "${data}" | sed "s/\(THEN\|GOTO\|GOSUB\) ${label}\>/\1 ${lnum}: REM ${label}/g")
+    else
+        data=$(echo "${data}" | sed "s/\(THEN\|GOTO\|GOSUB\) ${label}\>/\1 ${lnum}/g")
+    fi
 done
 
-echo -en "${data}"
+echo -e "${data}"