Last active
August 29, 2015 14:20
-
-
Save aabouzaid/aca5aa2ddeb22b4faf63 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/bash | |
# | |
# You can find more info about this competition at following url: | |
# https://community.trueability.com/t/the-sub-par-assembler-round-1/911 | |
# | |
#Main section. | |
spasm_dir_path="." | |
#spasm_dir_path="/var/lib/spasm" | |
print_error () { | |
echo "Operation not supported or syntax error!" | |
} | |
#Check if there is any syntax error. | |
if ! [[ $(echo "$1" | grep -P "\b(mov|add|sub|mul|eq|lt|gt)\b") ]]; then | |
print_error | |
exit 1 | |
elif ! [[ $(echo "$2" | grep -P "\bR(1|2|3|4)\b") ]]; then | |
print_error | |
exit 1 | |
elif [[ ! $(echo "$3" | grep -P "(R(1|2|3|4)\b|%\b)") ]]; then | |
print_error | |
exit 1 | |
fi | |
#Get target number. | |
target_number=$(cat ${spasm_dir_path}/$2 | grep -P -o "\d+") | |
#Get value number. | |
if [[ $(echo "$3" | grep -P "\bR(1|2|3|4)\b") ]]; then | |
value_number=$(cat ${spasm_dir_path}/$3 | grep -P -o "\d+") | |
elif [[ $(echo "$3" | grep -P "%\b") ]]; then | |
value_number=$(echo "$3" | grep -P -o "\d+") | |
fi | |
#Check Operations. | |
case $1 in | |
mov) | |
echo $value_number > ${spasm_dir_path}/$2 | |
;; | |
add) | |
[[ -f ${spasm_dir_path}/$2 ]] && echo $(( $target_number + $value_number )) > ${spasm_dir_path}/$2 || print_error; exit 1 | |
;; | |
sub) | |
[[ -f ${spasm_dir_path}/$2 ]] && echo $(( $target_number - $value_number )) > ${spasm_dir_path}/$2 || print_error; exit 1 | |
;; | |
mul) | |
[[ -f ${spasm_dir_path}/$2 ]] && echo $(( $target_number * $value_number )) > ${spasm_dir_path}/$2 || print_error; exit 1 | |
;; | |
eq) | |
[[ $target_number -eq $value_number ]] && echo -n "0" > ${spasm_dir_path}/CP || echo -n "1" > ${spasm_dir_path}/CP | |
;; | |
lt) | |
[[ $target_number -lt $value_number ]] && echo -n "0" > ${spasm_dir_path}/CP || echo -n "1" > ${spasm_dir_path}/CP | |
;; | |
gt) | |
[[ $target_number -gt $value_number ]] && echo -n "0" > ${spasm_dir_path}/CP || echo -n "1" > ${spasm_dir_path}/CP | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment