Basic: fix errors, reader, if form. Self-host 0-3
[jackhill/mal.git] / basic / qb2cbm.sh
CommitLineData
47def37e
JM
1#!/bin/bash
2
3set -e
4
5DEBUG=${DEBUG:-}
0cb556e0
JM
6KEEP_REM=${KEEP_REM:-1}
7# 0 - drop all REMs
bf8d1f7d 8# 1 - keep LABEL and INCLUDE REMs (and blank lines)
0cb556e0
JM
9# 2 - keep LABEL, INCLUDE, and GOTO REMs
10# 3 - keep LABEL, INCLUDE, GOTO, and whole line REMs
11# 4 - keep all REMS (end of line REMs too)
47def37e
JM
12
13infile=$1
14
15die () {
16 echo >&2 "$*"
17 exit 1
18}
19
20[ "${infile}" ] || die "Usage: <infile>"
21
22input=$(cat ${infile})
23
24[ "${DEBUG}" ] && echo >&2 "Processing includes"
25
26full="${input}"
27declare -A included
28
29while [[ ${input} =~ REM\ \$INCLUDE:\ \'.*\' ]]; do
30 full=""
31 while read -r line; do
32 if [[ ${line} =~ REM\ \$INCLUDE:\ \'.*\' ]]; then
33 include=${line#REM \$INCLUDE: \'}
34 include=${include%\'}
35 # Only include it once
36 if [ "${included[${include}]}" ];then
37 [ "${DEBUG}" ] && echo >&2 "already included: ${include}"
38 continue
39 fi
40 [ "${DEBUG}" ] && echo >&2 "including: ${include}"
41 included[${include}]="done"
0cb556e0 42 if [ "${KEEP_REM}" -ge 1 ]; then
b7b1787f
JM
43 full="${full}\nREM vvv BEGIN '${include}' vvv\n$(cat ${include})\nREM vvv END '${include}' vvv\n"
44 else
45 full="${full}\n$(cat ${include})\n"
46 fi
47def37e
JM
47 else
48 full="${full}${line}\n"
49 fi
50 done < <(echo -e "${input}")
51 input="${full}"
52done
53
54
55[ "${DEBUG}" ] && echo >&2 "Renumbering"
56
57data=""
58declare -A labels
59
85d70fb7 60lnum=1
47def37e 61while read -r line; do
11f94d2e 62 if [[ ${line} =~ ^\ *# ]]; then
b7b1787f
JM
63 [ "${DEBUG}" ] && echo >&2 "ignoring # style comment at $lnum"
64 continue
0cb556e0
JM
65 elif [[ "${KEEP_REM}" -lt 3 && ${line} =~ ^\ *REM && \
66 ! ${line} =~ REM\ vvv && ! ${line} =~ REM\ ^^^ ]]; then
b7b1787f 67 [ "${DEBUG}" ] && echo >&2 "dropping REM comment: ${line}"
11f94d2e
JM
68 continue
69 elif [[ ${line} =~ ^\ *$ ]]; then
bf8d1f7d 70 if [ "${KEEP_REM}" -ge 1 ]; then
b7b1787f 71 [ "${DEBUG}" ] && echo >&2 "found blank line at $lnum"
47def37e 72 data="${data}\n"
bf8d1f7d
JM
73 else
74 [ "${DEBUG}" ] && echo >&2 "ignoring blank line at $lnum"
75 fi
76 continue
b7b1787f 77 elif [[ ${line} =~ ^[A-Za-z_][A-Za-z0-9_]*:$ ]]; then
47def37e
JM
78 label=${line%:}
79 [ "${DEBUG}" ] && echo >&2 "found label ${label} at $lnum"
80 labels[${label}]=$lnum
0cb556e0 81 if [ "${KEEP_REM}" -ge 1 ]; then
b7b1787f
JM
82 data="${data}${lnum} REM ${label}:\n"
83 else
84 continue
85 fi
47def37e
JM
86 else
87 data="${data}${lnum} ${line}\n"
88 fi
85d70fb7 89 lnum=$(( lnum + 1 ))
47def37e
JM
90done < <(echo -e "${input}")
91
0cb556e0 92if [[ "${KEEP_REM}" -lt 4 ]]; then
b7b1787f
JM
93 [ "${DEBUG}" ] && echo >&2 "Dropping line ending REMs"
94 data=$(echo -e "${data}" | sed "s/: REM [^\n]*$//")
95fi
96
47def37e
JM
97for label in "${!labels[@]}"; do
98 [ "${DEBUG}" ] && echo >&2 "Updating label: ${label}"
99 lnum=${labels[${label}]}
0cb556e0 100 if [ "${KEEP_REM}" -ge 2 ]; then
b7b1787f
JM
101 data=$(echo "${data}" | sed "s/\(THEN\|GOTO\|GOSUB\) ${label}\>/\1 ${lnum}: REM ${label}/g")
102 else
103 data=$(echo "${data}" | sed "s/\(THEN\|GOTO\|GOSUB\) ${label}\>/\1 ${lnum}/g")
104 fi
47def37e
JM
105done
106
b7b1787f 107echo -e "${data}"