96 lines
1.0 KiB
Bash
96 lines
1.0 KiB
Bash
|
#!/bin/sh
|
||
|
|
||
|
alias :=_newword
|
||
|
alias '(=:'
|
||
|
alias ')=;'
|
||
|
|
||
|
_stack="" # top is
|
||
|
_builtin_dictionary="swap dup rot over drop add sub div mod mul put" # + - / * .
|
||
|
_dictionary=
|
||
|
|
||
|
_newword() {
|
||
|
name="$1"
|
||
|
shift
|
||
|
export "\$${name}_definition=\"$*\""
|
||
|
}
|
||
|
|
||
|
_execword() {
|
||
|
eval "def='$1_definition'"
|
||
|
for word in $def; do
|
||
|
case "$_builtin_dictionary" in
|
||
|
*"$word"*) "_$word" "$_stack" ;;
|
||
|
*) _execword "$word" ;;
|
||
|
esac
|
||
|
done
|
||
|
}
|
||
|
|
||
|
_swap() {
|
||
|
one="$1"
|
||
|
two="$2"
|
||
|
shift
|
||
|
_stack="$2 $1 $*"
|
||
|
}
|
||
|
|
||
|
_dup() {
|
||
|
_stack="$1 $*"
|
||
|
}
|
||
|
|
||
|
_rot() {
|
||
|
one="$1"
|
||
|
two="$2"
|
||
|
thr="$3"
|
||
|
shift 3
|
||
|
_stack="$3 $1 $2 $*"
|
||
|
}
|
||
|
|
||
|
_over() {
|
||
|
_stack="$2 $*"
|
||
|
}
|
||
|
|
||
|
_drop() {
|
||
|
shift
|
||
|
_stack="$*"
|
||
|
}
|
||
|
|
||
|
_add() {
|
||
|
one="$1"
|
||
|
two="$2"
|
||
|
shift 2
|
||
|
_stack="$(( one + two )) $*"
|
||
|
}
|
||
|
|
||
|
_sub() {
|
||
|
one="$1"
|
||
|
two="$2"
|
||
|
shift 2
|
||
|
_stack="$(( two - one )) $*"
|
||
|
}
|
||
|
|
||
|
_div() {
|
||
|
one="$1"
|
||
|
two="$2"
|
||
|
shift 2
|
||
|
_stack="$(( two / one )) $*"
|
||
|
}
|
||
|
|
||
|
_mod() {
|
||
|
one="$1"
|
||
|
two="$2"
|
||
|
shift 2
|
||
|
_stack="$(( two % one )) $*"
|
||
|
}
|
||
|
|
||
|
_mul() {
|
||
|
one="$1"
|
||
|
two="$2"
|
||
|
shift 2
|
||
|
_stack="$(( one * two )) $*"
|
||
|
}
|
||
|
|
||
|
_put() {
|
||
|
one="$1"
|
||
|
shift
|
||
|
printf '%s \n' "$one"
|
||
|
_stack="$*"
|
||
|
}
|