#!/bin/bash
: '

  This is a syntagma engine using perl as the translator and executor.
  It is designed to work like sed: if a string is given, then that is
  interpreted as a syntagma expression and is executed with the input
  from stdin (echo or cat)

NOTES

  need a switch to recompile the syntagma.tonom.pl perl script, to 
  reflect new syntax in the syntagma language.

  should give this a short name like: synp for perl
  synr for rust, synl for lua.

TESTING

  Technically this is not interpreting, but rather compiling the syntagma
  script, into nom, and then into perl, and then running with stdin as
  input. But it appears as though it is.

  * interpret a basic syntagma expression
  >> echo "ab ab" | engine.perl.sh "wo:[a-z]+;ww=wo wo|ww wo;"

  * interpret a syntagma script.
  >> echo '123' | ./engine.perl.sh -f ../eg/s.json.numbers.syn

HISTORY

  27 may 2026 
    began. This perl-based syntagma interpreting engine appears
    to work for both expressions and files

'

# if any command fails in a pipe exit will be non zero. 
set -o pipefail
# Initialize variables
FILENAME=""
POSITIONAL_STRING=""
recompile="false";

# Loop through all arguments passed to the script
while [[ $# -gt 0 ]]; do
  case "$1" in
    -f)
      if [[ -n "$2" && "$2" != -* ]]; then
        FILENAME="$2"
        shift 2 # Shift past the -f flag and its value
      else
        echo "Error: -f requires a filename argument." >&2
        exit 1
      fi
      ;;
    -r)
      # recompile the syntagma to nom translator
      recompile="true"; shift;
      ;;
    -*) # Catch any other unknown switches
      echo "Unknown option: $1" >&2
      exit 1
      ;;
    *) # Anything that doesn't start with a '-' is treated as a positional string
      if [[ -z "$POSITIONAL_STRING" ]]; then
        POSITIONAL_STRING="$1"
      else
        echo "Error: Multiple text strings provided. Only one is allowed." >&2
        exit 1
      fi
      shift # Shift past the positional argument
      ;;
  esac
done

# --- Print the results ---
# echo "Parsed values:"
# echo "Filename:          ${FILENAME:-[Not Provided]}"
# echo "Positional String: ${POSITIONAL_STRING:-[Not Provided]}"

name="temp.pl"; 
out="$PEPNOM/tr/$name";
temp="$PEPNOM/tr/temp.pss";
file="$FILENAME";
expression="$POSITIONAL_STRING";

if [[ $recompile == "true" ]]; then
  pep -f $PEPNOM/tr/nom.tolua.pss $PEPNOM/tr/syntagma.pss > $PEPNOM/tr/syntagma.tonom.lua
fi

if [[ -n $file ]]; then
  # echo "Translating syntagma file to nom (temp.pss)..."
  cat $file | $PEPNOM/tr/syntagma.tonom.pl > $temp;
  if [ $? -ne 0 ]; then
    cat $temp
    echo "Syntagma did not compile to nom (via perl) well."
    exit 1;
  fi
elif [[ -n $expression ]]; then
  # echo "Translating syntagma expression to nom (temp.pss)..."
  echo "$expression" | $PEPNOM/tr/syntagma.tonom.pl > $temp;
  if [ $? -ne 0 ]; then
    cat $temp
    echo "Syntagma did not compile to nom (via perl) well."
    exit 1;
  fi

else 
  echo "Neither syntagma file (-f) nor expression '...' given.";
  echo
  echo "usage: cat input.txt | igmal -f <syntagma.script>";
  echo "  interpret a syntagma parsing script in lua "
  echo "Options: ";
  echo "  -r recompile the syntagma.tonom.lua script";
  exit 1;
fi

# interpreted 
# pep -f $PEPNOM/tr/nom.toperl.pss $temp > $out ; 

# perl native
cat $temp | $PEPNOM/tr/nom.toperl.pl > $out ; 
chmod a+x $out; 

if [ -t 0 ]; then
  # no input from stdin so just print script.
  cat $out;
else
  # run syntagma script with input from pipe 
  $out
fi

