From 5e9ef5170304ccaaced95382278ad4b7e522732e Mon Sep 17 00:00:00 2001 From: depsterr Date: Thu, 15 Apr 2021 13:40:48 +0200 Subject: [PATCH] added first prototype --- f.sh | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 f.sh diff --git a/f.sh b/f.sh new file mode 100644 index 0000000..30532d9 --- /dev/null +++ b/f.sh @@ -0,0 +1,95 @@ +#!/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="$*" +}