summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Semantic.hs29
-rw-r--r--SemanticMain.hs12
2 files changed, 29 insertions, 12 deletions
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