Skip to content

Instantly share code, notes, and snippets.

@nima
Created December 14, 2016 01:33
Show Gist options
  • Save nima/99018b3ec046aac7d5c7999f35c4e55c to your computer and use it in GitHub Desktop.
Save nima/99018b3ec046aac7d5c7999f35c4e55c to your computer and use it in GitHub Desktop.
Validates HGD string
function validate() {
let -i e=0
let -i balance=0
let -i opset=0
local ch
for (( i=0; i<${#1}; i++ )); do
ch="${1:$i:1}"
case "${ch}" in
'(')
if [ ${opset} -eq 1 ]; then
((balance++))
opset=0
else
e=1
break
fi
;;
')') ((balance--));;
'|'|'!'|'&') opset=1;;
esac
if [ ${balance} -lt 0 ]; then
e=2
break
fi
done
if [ $e -eq 0 ]; then
if [ ${balance} -ne 0 ]; then
e=3
fi
if [ ${ch} != ')' ]; then
e=4
fi
fi
return $e
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment