2024-12-17 15:48:25 +08:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
FILE=$1
|
|
|
|
ITER=99
|
2024-12-17 17:09:19 +08:00
|
|
|
DEBUG=false
|
2024-12-17 15:48:25 +08:00
|
|
|
|
|
|
|
REG_A=$(grep 'Register A:' "$FILE" | cut -f2 -d: )
|
|
|
|
REG_B=$(grep 'Register B:' "$FILE" | cut -f2 -d: )
|
|
|
|
REG_C=$(grep 'Register C:' "$FILE" | cut -f2 -d: )
|
|
|
|
read -r -a INPUT <<< "$(grep 'Program:' "$FILE" | cut -f2 -d: | tr ',' ' ' )"
|
|
|
|
INPUT_LEN=${#INPUT[@]}
|
|
|
|
|
|
|
|
printf "Registers A: %s B: %s C: %s\n" "$REG_A" "$REG_B" "$REG_C" >&2
|
|
|
|
printf "Input : " >&2
|
|
|
|
printf "%s " "${INPUT[@]}" >&2
|
|
|
|
printf "\n" >&2
|
|
|
|
|
2024-12-17 17:09:19 +08:00
|
|
|
# Load machine operations
|
|
|
|
. machine.sh
|
2024-12-17 15:48:25 +08:00
|
|
|
|
2024-12-17 17:09:19 +08:00
|
|
|
POINTER=0
|
|
|
|
while [[ $POINTER -lt $INPUT_LEN ]]
|
2024-12-17 15:48:25 +08:00
|
|
|
do
|
2024-12-17 17:09:19 +08:00
|
|
|
OPCODE=${INPUT[$POINTER]}
|
|
|
|
OPERAND=${INPUT[$POINTER+1]}
|
|
|
|
read_opcode "$OPCODE" "$OPERAND"
|
|
|
|
printf "Registers A : %s B: %s C : %s\n" "$REG_A" "$REG_B" "$REG_C" >&2
|
|
|
|
printf "Pointer : %s\n" "$POINTER" >&2
|
|
|
|
(( ITER-- ))
|
|
|
|
if [[ $ITER -eq 0 ]] ; then break ;fi
|
|
|
|
done | paste -s -d ","
|