did more stuff

This commit is contained in:
Rachel Lambda Samuelsson 2021-04-15 14:09:33 +02:00
parent 5e9ef51703
commit f25131f77c

57
f.sh
View File

@ -1,25 +1,41 @@
#!/bin/sh #!/bin/sh
alias :=_newword alias :=_newword
alias '(=:' alias ']=_execbody'
alias ')=;' alias xshift='shift 2>/dev/null || printf "Stack underflow\n"'
_stack="" # top is _stack="" # top is
_builtin_dictionary="swap dup rot over drop add sub div mod mul put" # + - / * . _builtin_dictionary="swap dup rot over drop add sub div mod mul put" # + - / * .
_dictionary=
_newword() { _newword() {
name="$1" name="$1"
shift shift
export "\$${name}_definition=\"$*\"" eval "${name}_definition=\"$*\""
} }
_execword() { _execword() {
eval "def='$1_definition'" eval "def=\"\$$1_definition\""
for word in $def; do eval "def=\"\$$1_definition\""
[ "$def" ] || {
printf '%s\n' "Error: no word $1"
return
}
_execbody $def
}
_execbody() {
for word in "$@"; do
[ "$_debug" ] && {
printf 'word %s\nstack %s\n' "$word" "$_stack"
read
}
case "$_builtin_dictionary" in case "$_builtin_dictionary" in
*"$word"*) "_$word" "$_stack" ;; *"$word"*) "_$word" $_stack ;;
*) _execword "$word" ;; *) case "$word" in
*[0-9]*) _stack="$word $_stack" ;;
*) _execword "$word" ;;
esac
;;
esac esac
done done
} }
@ -27,7 +43,7 @@ _execword() {
_swap() { _swap() {
one="$1" one="$1"
two="$2" two="$2"
shift xshift
_stack="$2 $1 $*" _stack="$2 $1 $*"
} }
@ -39,7 +55,9 @@ _rot() {
one="$1" one="$1"
two="$2" two="$2"
thr="$3" thr="$3"
shift 3 xshift
xshift
xshift
_stack="$3 $1 $2 $*" _stack="$3 $1 $2 $*"
} }
@ -48,48 +66,53 @@ _over() {
} }
_drop() { _drop() {
shift xshift
_stack="$*" _stack="$*"
} }
_add() { _add() {
one="$1" one="$1"
two="$2" two="$2"
shift 2 xshift
xshift
_stack="$(( one + two )) $*" _stack="$(( one + two )) $*"
} }
_sub() { _sub() {
one="$1" one="$1"
two="$2" two="$2"
shift 2 xshift
xshift
_stack="$(( two - one )) $*" _stack="$(( two - one )) $*"
} }
_div() { _div() {
one="$1" one="$1"
two="$2" two="$2"
shift 2 xshift
xshift
_stack="$(( two / one )) $*" _stack="$(( two / one )) $*"
} }
_mod() { _mod() {
one="$1" one="$1"
two="$2" two="$2"
shift 2 xshift
xshift
_stack="$(( two % one )) $*" _stack="$(( two % one )) $*"
} }
_mul() { _mul() {
one="$1" one="$1"
two="$2" two="$2"
shift 2 xshift
xshift
_stack="$(( one * two )) $*" _stack="$(( one * two )) $*"
} }
_put() { _put() {
one="$1" one="$1"
shift xshift
printf '%s \n' "$one" printf '%s \n' "$one"
_stack="$*" _stack="$*"
} }