#! /usr/bin/ruby

require './tokenizer.rb'
require './checker.rb'
require './parser.rb'
require './translator.rb'
require './testcases.rb'

# test a ruby tokenizer, checker, parser, and translator for the IttyBitty language

# assumes the included testcases.rb file contains two arrays:
#    ValidTests and ErrorTests,
# each of which consists of a series of pairs [ codeblock, name ]
#    (these are the test cases this script iterates across)

# testcode(code,name)
# --------------
# function to try tokenizing, checking, parsing, and translating an IttyBitty program,
#     displaying results as you go
# expects the code block and a (string) name for it as parameters
def testcode(code, name)
   puts "========================================================="
   puts "testing code block #{name}"
   puts "========================================================="
   puts "Press enter to continue"
   cont = gets

   puts "\n----The original source code was----"
   puts "#{code}"

   # get the token list
   tokenlist = tokenize(code)
   puts "\n----The resulting token list was----"
   puts "#{tokenlist}"

   # try to check the given tokenlist
   puts "\n----The result of checking was----"
   check,pos = parseChecker(tokenlist)
   if check == false then
      puts "   parse checking failed"
   elsif pos < tokenlist.length then
      puts "   parse checking succeeded, but only #{pos}/#{tokenlist.length} tokens were parsed?"
   else
      puts "   parse checking passed"
   end

   # if it passed syntax checking, try to parse the given tokenlist
   if check then
      parsetree,pos = parse(tokenlist)
      puts "\n----parsed #{pos}/#{tokenlist.length} tokens-----"
      puts "----The resulting parse tree was----"
      puts "#{parsetree}"

      # if parsing succeeded, try to translate the parse tree
      if parsetree != false then
         puts "\n----The translated code is----"
         result = translate(parsetree, $ymtable)
         puts "#{result}"
      end
   else
      puts "not parsing (failed check)\n\n"
   end
end


# iterate through each array of test cases
ValidTests.each do | code, name |
    testcode(code, name)
end

ErrorTests.each do | code, name |
    testcode(code, name)
end

