#!/bin/bash
: '

  This is a syntagma engine using lua 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.lua perl script, to 
  reflect new syntax in the syntagma language.

  should give this a short name like: igmal for lua, igmapy for python.

TESTING

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

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

  * interpret a syntagma script.
  >> echo '123' | ./engine.lua.sh -f ../eg/s.json.numbers.syn
  >> cat input.txt | igmal -f ../eg/s.json.numbers.syn

HISTORY

  29 may 2026 
    began. 

'

# 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.lua"; 
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


cat "$file" | "$PEPNOM/tr/syntagma.tonom.lua" > "$temp"

if [[ -n $file ]]; then
  # echo "Translating syntagma file to nom (temp.pss)..."
  cat $file | $PEPNOM/tr/syntagma.tonom.lua > $temp;

  if [ $? -ne 0 ]; then
    cat $temp
    echo "Syntagma did not compile to nom (via lua) well."
    exit 1;
  fi

elif [[ -n $expression ]]; then
  # echo "Translating syntagma expression to nom (temp.pss)..."
  echo "$expression" | $PEPNOM/tr/syntagma.tonom.lua > $temp;
  if [ $? -ne 0 ]; then
    cat $temp
    echo "Syntagma did not compile to nom (via lua) 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.tolua.pss $temp > $out ; 

# lua native
cat $temp | $PEPNOM/tr/nom.tolua.lua > $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

