circuitus20250729.sh

#!/bin/bash

# (C) David Vajda
# 2025-07-29
# Circuit Excersize, US ...

# bakus naur - normalform - grammar rules (easiest level, linksrekursiv)
# expr ::= expr + term | term
# term ::= term * fact | fact
# fact ::= const | (expr)

# rechtzeitiges abbruchkriterium erzeugen sonst wird ausdruck etwas zu lange

date=$(date)
yes=1
no=0
op1="AND"
op2="OR"
op3="NOT"
MAXVAR=4

#decision=$(($RANDOM%2))
#if [ "$decision" == "$no" ]
#then
#    exit
#fi

if [[ "$1" == "expr" || -z "$1" ]]
then
    decision=$(($RANDOM%2))
    if [ $decision -eq "$yes" ]
    then
        echo -n " $op1 "
        /bin/bash "$0" "expr"
    else
        echo -n " $op1 "
        /bin/bash "$0" "term"
    fi
elif [ "$1" == "term" ]
then
    decision=$(($RANDOM%2))
    if [ $decision -eq "$yes" ]
    then
        echo -n " $op2 "
        /bin/bash "$0" "expr"
    else
        echo -n " $op2 "
        /bin/bash "$0" "fact"
    fi
elif [ "$1" == "fact" ]
then
    decision=$(($RANDOM%2))
    if [ $decision -eq "$yes" ]
    then
        if [ $decision -eq "$yes" ]
        then
            echo -n " $op3 "
        fi
        echo -n "x$(($RANDOM%$MAXVAR))"
        decision=$(($RANDOM%2))
    else
        echo -n "("
        /bin/bash "$0" "expr"
        echo -n ")"
    fi
fi