Skip to content
Browse files

- Logging in Translator added.

  • Loading branch information...
1 parent 142f683 commit 3d56a4bbd5f116570782f5faa7a15b8df4664120 @graninas committed Feb 16, 2014
Showing with 25 additions and 10 deletions.
  1. +24 −9 Amoeba/GameLogic/Language/Translator.hs
  2. +1 −1 Amoeba/Test/TranslatingTest.hs
View
33 Amoeba/GameLogic/Language/Translator.hs
@@ -6,6 +6,7 @@ import qualified GameLogic.Runtime.World as W
import qualified GameLogic.Data.Facade as D
import qualified Data.Map as M
+import Prelude hiding (log)
import Control.Monad.State
import Control.Monad.Trans
@@ -48,6 +49,7 @@ World "Pandora"
data TransRt = TransRt { trtNextId :: State TransRt Int
, trtItemMap :: M.Map String String -- TODO
, trtWorldConstructor :: String -- TODO
+ , trtLog :: [String]
}
type Trans a = EitherT String (State TransRt) a
@@ -61,20 +63,28 @@ nextId prevId = do
-initialRt = TransRt (nextId 1) M.empty "Empty"
+initialRt = TransRt (nextId 1) M.empty "Empty" []
(/>) trigger act = \token -> if trigger token
then act token
else return $ "Token not triggered: " ++ show token
+log s = do
+ ctx <- get
+ let newCtx = ctx { trtLog = (s : trtLog ctx) }
+ put newCtx
-- actions:
-skip :: RawToken -> Trans String
-skip t = return $ "Skip for: " ++ show t
-addItem t = return $ "Adding object template for: " ++ show t
-
+skip, addItem :: RawToken -> Trans String
+skip t = do
+ log $ "Skip for: " ++ show t
+ return ""
+addItem t = do
+ log $ "Adding object template for: " ++ show t
+ return ""
+
-- triggers:
onComment (RT.Comment _) = True
onComment _ = False
@@ -88,15 +98,20 @@ scheme = [ onComment /> skip
, onItem /> addItem
]
+apply sc t = mapM_ ($ t) sc
-translator _ [] = left "Test1."
-translator _ ts = left "Test2."
+translate _ [] = return ()
+translate sc (t:ts) = do
+ apply sc t
+ translate sc ts
-translateToWorld tokens = evalState (runEitherT (translator scheme tokens)) initialRt
+translateToWorld [] = Left "There are no tokens."
+translateToWorld tokens = return $ execState (runEitherT (translate scheme tokens)) initialRt
toWorld rawString = do
ts <- RP.parseRawTokens rawString :: Either String [RawToken]
- translateToWorld ts
+ ctx <- translateToWorld ts
+ return $ trtLog ctx
View
2 Amoeba/Test/TranslatingTest.hs
@@ -11,7 +11,7 @@ main = do
tokens <- readFile "./Data/Raws/World3.arf"
- let res = toWorld tokens :: Either String String
+ let res = toWorld tokens
print res

0 comments on commit 3d56a4b

Please sign in to comment.
Something went wrong with that request. Please try again.