
# mbStr.py
# Andrew Davison, ad@coe.psu.ac.th, April 2026

'''
> python mbStr.py factors.bas
"P!Incn<2JcEDi2Cdi*ied>nJeAfn/igf*ihg=nJhBii+1J1CAPnJ1EBnn/iPiJ1DEP!"

> python minBASIC.py "P!Incn<2JcEDi2Cdi*ied>nJeAfn/igf*ihg=nJhBii+1J1CAPnJ1EBnn/iPiJ1
DEP!"

?? 1001
7 11 13

>
'''

import sys, os
import re

# Regex pattern: matches anything inside double quotes "..."
# The [^"]* ensures it matches the shortest possible string
stringRE = r'"[^"]*"'


if len(sys.argv) != 2:
  print("Usage: python mbStr.py <file.bas>")
  sys.exit(1)

fnm = sys.argv[1]
if not fnm.endswith('.bas'):
  print("Error: input file must have a .bas extension")
  sys.exit(1)

if not os.path.isfile(fnm):
  print(f"File '{fnm}' not found.")
  sys.exit(1)

with open(fnm, 'r') as f:
  print("\"", end="")
  for line in f:
    # Replace all double-quoted strings with '-' (print nothing)
    line2 = re.sub(stringRE, '-', line)
    
    # Find first occurrence of '#' and delete
    if '#' in line2:
      line2 = line2.split('#')[0]
    
    # Remove whitespace
    line3 = "".join(line2.split())
    
    # print if there's something left
    if line3:
      print(line3, end="")
  print("\"")
                    

