module EvalTree where

import HXML
import Char
import MyParserSpec
import System


skipPre s = if (elem ' ' s)
	    then after
	    else s
		where (before,_:after) = break (==' ') s



filColons string = filter (/='"') (filter (/=''') string) 

formatTicks [] = []
formatTicks (x:xs) | x==''' = ('"'):(formatTicks xs)
		   |otherwise = x:(formatTicks xs)


firstArg string = (init(reverse(dropWhile (/=' ') (reverse(tail string)))))

secondArg string = (init(reverse((takeWhile (/=' ') (reverse(tail string))))))

evalExpr (Tree (TXNode text) []) = if (head text)=='&'
				   then if ((takeWhile (/=' ') (tail text))=="match")
					then if (matchRe (firstArg (dropWhile (/=' ') (filColons text))) (secondArg (dropWhile (/=' ') (filColons text))))/=""
					     then do{
						     return (Tree (TXNode "'True'") [])}
					     else do{
						     return (Tree (TXNode "'False'") [])}
						
					else do{	
						writeFile "in" ":l Defs.hs\n";
						appendFile "in" (formatTicks((tail.reverse.tail.reverse) text));
						--cod<-system "ghci -ic:/Javi/EU-INDIA/RegexpLib98/src/basiclib < in > out";     
						cod<-system "ghci < in > out";     
						res<-readFile "out";
						linesol<-return(head (tail (reverse (lines res))));
						return (Tree (TXNode (''':(skipPre linesol)++"'")) [])
				        	}
				    else do{
					return (Tree (TXNode (''':(filColons text)++"'")) [])
					   }

evalExpress [] = do {return []}
evalExpress explist = do{first<-evalExpr (head explist);
			 rest<-evalExpress (tail explist);
			 return (first:rest)
			}
				





