sourcebrainfuck::Main.fan

using util

class Main : AbstractMain
{
  @Arg { help = "Brainfuck program"}
  File? program
  
  @Opt { help = "Show debug information" }
  Bool debug

  override Int run()
  {
    if (!program.exists)
    {
      log.err("File `${program}` does not exists")
      return 1
    }
    
    if (debug)
    {
      Main#.pod.log.level = LogLevel.debug
    }
    
    Interpreter? interpreter

    try
    {
      source := program.readAllStr
      lexer  := Lexer(source).tokenize
      interpreter = Interpreter(lexer.tokens)
      interpreter.run
      echo
      return 0
    }
    catch(BrainfuckErr e)
    {
      echo(e)
      echo(interpreter)
      return 1
    }
    catch(Err e)
    {
      echo(e.trace)
      return 1
    }
  }
}