module Main where

import CppToken
import CppAst
import TestProgs
import Language.Haskell.Pretty
import Control.Monad
import Debug.Trace
import CppGrammar

fromTokList a = zip (map line [1..length a]) a

startState = newCompilerState

testParser p = (runParser p startState) . fromTokList

printRes (decls, state, tail) =
    do	putStrLn "\n--- Declarations:"
    	print decls
    	putStrLn "\n--- State:"
    	print state
    	if (not.null) tail
       		then putStrLn $ "\n*** Unparsable tail of input:\n"++(show tail)
       		else putStrLn "\nEntire input successfully parsed"

testP p inp = do
	putStrLn "--- Test case:"
	print inp
	printRes (testParser p inp)

testParse = testP translationUnit

test n = testP translationUnit $ testProg n

interpTest = do
	let res = testParser translationUnit $ testProg 8
	let (decls, state, tail) = res
	printRes res
	when (null tail) (mapM_ printBytecode decls)

printBytecode decl = putStrLn ("bytecode for " ++ show decl)

main = sequence $ map test [0..6]
