Skip to content

Instantly share code, notes, and snippets.

@izabera
Created February 28, 2025 08:08
Show Gist options
  • Save izabera/ff958699fd910814da30daf95142af8c to your computer and use it in GitHub Desktop.
Save izabera/ff958699fd910814da30daf95142af8c to your computer and use it in GitHub Desktop.
basic rpn calculator with assignments
set -f
IFS=
rpn () {
for token do
case $token in
+|-|\*|/|%|\*\*) (( stack[-2] = stack[-2] $token stack[-1] )); unset stack[-1] ;;
=) (( ${stack[-2]} = stack[-1] )); unset stack[-1] stack[-1] ;;
*) stack+=($token) ;;
esac
done
echo $* ...... ${stack[-1]}
}
rpn 3 4 2 * 1 5 - 2 3 ** ** / +
rpn 3 4 2 * +
rpn 1 5 -
rpn 3 2 **
rpn 2 2 + a 3 2 ** = a + a +
declare -p a
@izabera
Copy link
Author

izabera commented Feb 28, 2025

$ bash rpn.bash 
3 4 2 * 1 5 - 2 3 ** ** / + ...... 3
3 4 2 * + ...... 11
1 5 - ...... -4
3 2 ** ...... 9
2 2 + a 3 2 ** = a + a + ...... 22
declare -- a="9"```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment