From 35a4cea8dafa280b1f7622fea0bffb12c84210b9 Mon Sep 17 00:00:00 2001 From: Jed Barber Date: Thu, 2 Aug 2012 09:38:15 +1000 Subject: Converted to module --- Semantic.hs | 29 +++++++++++++++++------------ SemanticMain.hs | 12 ++++++++++++ 2 files changed, 29 insertions(+), 12 deletions(-) create mode 100644 SemanticMain.hs diff --git a/Semantic.hs b/Semantic.hs index ed25c91..2390731 100644 --- a/Semantic.hs +++ b/Semantic.hs @@ -1,5 +1,10 @@ -import System( getArgs ) -import Text.Printf +module Semantic ( + eval, + doSemanticCheck + ) where + + + import Data.List import Data.Maybe import Data.Set( Set ) @@ -329,25 +334,25 @@ varType x = -doSemanticCheck :: [String] -> String -doSemanticCheck list = +eval :: [String] -> MachineState +eval list = let s = Stack.empty d = Map.empty a = Set.empty t = Set.empty op = (\x y -> case y of (Comment _) -> x (Command z) -> z x) + -- important to use foldl here so commands get applied in the correct order result = (foldl' (op) (Just (s,d,a,t))) . (map (parse)) $ list - in case (machineToString result) of - Just x -> x - Nothing -> "Error\n" + in result + +doSemanticCheck :: [String] -> String +doSemanticCheck list = + case (machineToString (eval list)) of + Just x -> x + Nothing -> "Error\n" -main = do - args <- getArgs - list <- getLines $ head args - let result = doSemanticCheck (map (stripReturn) list) - printf result diff --git a/SemanticMain.hs b/SemanticMain.hs new file mode 100644 index 0000000..7e7fd29 --- /dev/null +++ b/SemanticMain.hs @@ -0,0 +1,12 @@ +import System( getArgs ) +import Text.Printf +import Parse +import Semantic + + + +main = do + args <- getArgs + list <- getLines $ head args + let result = doSemanticCheck (map (stripReturn) list) + printf result -- cgit