Basic: add step3, vector/hash-map evaluation.
[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
8# 1 - keep LABEL and INCLUDE REMs
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
60lnum=10
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
b7b1787f 70 [ "${DEBUG}" ] && echo >&2 "found blank line at $lnum"
47def37e
JM
71 data="${data}\n"
72 continue
b7b1787f 73 elif [[ ${line} =~ ^[A-Za-z_][A-Za-z0-9_]*:$ ]]; then
47def37e
JM
74 label=${line%:}
75 [ "${DEBUG}" ] && echo >&2 "found label ${label} at $lnum"
76 labels[${label}]=$lnum
0cb556e0 77 if [ "${KEEP_REM}" -ge 1 ]; then
b7b1787f
JM
78 data="${data}${lnum} REM ${label}:\n"
79 else
80 continue
81 fi
47def37e
JM
82 else
83 data="${data}${lnum} ${line}\n"
84 fi
85 lnum=$(( lnum + 10 ))
86done < <(echo -e "${input}")
87
0cb556e0 88if [[ "${KEEP_REM}" -lt 4 ]]; then
b7b1787f
JM
89 [ "${DEBUG}" ] && echo >&2 "Dropping line ending REMs"
90 data=$(echo -e "${data}" | sed "s/: REM [^\n]*$//")
91fi
92
47def37e
JM
93for label in "${!labels[@]}"; do
94 [ "${DEBUG}" ] && echo >&2 "Updating label: ${label}"
95 lnum=${labels[${label}]}
0cb556e0 96 if [ "${KEEP_REM}" -ge 2 ]; then
b7b1787f
JM
97 data=$(echo "${data}" | sed "s/\(THEN\|GOTO\|GOSUB\) ${label}\>/\1 ${lnum}: REM ${label}/g")
98 else
99 data=$(echo "${data}" | sed "s/\(THEN\|GOTO\|GOSUB\) ${label}\>/\1 ${lnum}/g")
100 fi
47def37e
JM
101done
102
b7b1787f 103echo -e "${data}"