Various shell scripts for use on the RTC server. Not your official RTC stuff!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

79 lines
1.8 KiB

#! /bin/bash
readonly PROGNAME="${0##/}"
readonly VERSION="1.0"
sqr () { awk "BEGIN{print sqrt($1)}"; }
log () { awk "BEGIN{print log($1)}"; }
sin () { awk "BEGIN{print sin($1)}"; }
exp () { awk "BEGIN{print exp($1)}"; }
_POP () { ((SP--)); eval "let $1=$((STACK[SP]))"; unset STACK[$((SP))]; }
_PUSH () { STACK+=($1); ((SP++)); }
#ASK (prompt for input) - 2 operands
A () { read -p "> " A </dev/tty ; eval "let $1=$A"; }
#ARITHMETIC IF - 4 operands
C () { local Q=$(eval "echo $(( $1 - $2 ))"); [[ $Q -eq 0 ]] && T $3 || T $4 ; }
#END LOOP
E () { _POP IP ; }
#INPUT INTO VARIABLE - 1 operand
J () { eval "let $2=$1"; }
#PRINT A NEWLINE
N () { printf "\n" ; }
#PRINT VARIABLE - 1 operand
P () { eval "echo $(($1))" ; }
#JUMP - 1 operand
T () { IP="$1"; }
#FOR LOOP - 0(?) operands
Z () { _PUSH IP ; }
#+ - 3 operands
ADD () { eval "let $3=$(($1 + $2))" ; }
#- - 3 operands
SUB () { eval "let $3=$(($1 - $2))" ; }
#* - 3 operands
MUL () { eval "let $3=$(($1 * $2))" ; }
#/ - 3 operands
DIV () { eval "let $3=$(($1 / $2))" ; }
#EXP - 2 operands
EXP () { : XXX to be implemented ; }
#LOG - 2 operands
LOG () { : XXX to be implemented ; }
#SIN - 2 operands
SIN () { : XXX to be implemented ; }
#SQUARE ROOT - 2 operands
SQR () { : XXX to be implemented ; }
main () {
local -a CODE=("DOPE 1.0")
local -i IP=1
local -i SP=0
local -ai STACK
# A DOPE program example
# - reads user input
# - loops until 10 is entered
#
mapfile -t -O1 CODE <<-_code_
1 J 10 A1
2 Z
3 A A2
4 C A1 A2 8 5
5 P A1
6 P A2
7 E
8 N
_code_
#STEP="yes"
IP=1
while true ; do
[[ $IP -ge ${#CODE[@]} ]] && break
read L I O1 O2 O3 <<<"${CODE[IP]}"
[ -n "$STEP" ] && echo $IP: $I $O1 $O2 $O3 $O4 "::::" "${STACK[@]}"
eval "$I $O1 $O2 $O3 $O4"
[[ "$I" =~ ^(T|C|E)$ ]] || let IP+=1
[ -n "$STEP" ] && read Q < /dev/tty
done
}
main