Enable splitting emitted tokens by new lines

This commit is contained in:
2021-11-03 21:11:17 +01:00
parent 674c574fda
commit ce5267a13f
2 changed files with 23 additions and 8 deletions

View File

@@ -2,8 +2,8 @@ module Assembler (
tokenize tokenize
) where ) where
import Data.Char as Char import qualified Data.Char as Char
import Data.Monoid as Monoid import qualified Data.Monoid as Monoid
import qualified VirtualMachine as VM (Op(..), Instruction, Command, instructionByOp) import qualified VirtualMachine as VM (Op(..), Instruction, Command, instructionByOp)
import qualified Util as U import qualified Util as U
@@ -77,7 +77,7 @@ tokenizeHex input = if isPrefix && len > 0
tokenizeChar :: Tokenizer tokenizeChar :: Tokenizer
tokenizeChar ('\'':'\\':x:'\'':_) = U.controlChar x >>= (\s -> return $ TokenizeResult (IntLiteral s) 4) tokenizeChar ('\'':'\\':x:'\'':_) = U.controlChar x >>= (\s -> return $ TokenizeResult (IntLiteral s) 4)
tokenizeChar ('\'':x:'\'':_) = Just $ TokenizeResult (IntLiteral . ord $ x) 3 tokenizeChar ('\'':x:'\'':_) = Just $ TokenizeResult (IntLiteral . Char.ord $ x) 3
tokenizeChar _ = Nothing tokenizeChar _ = Nothing
tokenizeString :: Tokenizer tokenizeString :: Tokenizer
@@ -121,10 +121,17 @@ anyTokenizer tokenizers input = Monoid.getFirst . Monoid.mconcat . map Monoid.Fi
tokenize :: String -> Either String [Token] tokenize :: String -> Either String [Token]
tokenize [] = Right [] tokenize [] = Right []
tokenize input = case tokenizers input of tokenize input = tokens >>= (\t -> Right $ filter tokenFilter t)
where
tokens = case tokenizers input of
(Just (TokenizeResult token chars)) -> tokenize (drop chars input) >>= (\rest -> return $ token : rest) (Just (TokenizeResult token chars)) -> tokenize (drop chars input) >>= (\rest -> return $ token : rest)
Nothing -> Left $ "Unknown token: " ++ take 20 input Nothing -> Left $ "Unknown token: " ++ take 20 input
tokenFilter :: Token -> Bool
tokenFilter (WhiteSpace) = False
tokenFilter (Comment _) = False
tokenFilter _ = True
tokenizers :: Tokenizer tokenizers :: Tokenizer
tokenizers = anyTokenizer tokenizers = anyTokenizer
[ keywordTokenizer False "\n" NewLine [ keywordTokenizer False "\n" NewLine

View File

@@ -4,7 +4,8 @@ module Util (
bytesStr, bytesStr,
head, head,
unescape, unescape,
controlChar controlChar,
explode
) where ) where
import Prelude hiding (head) import Prelude hiding (head)
@@ -58,3 +59,10 @@ controlChar x = case x of
'\'' -> Just 39 '\'' -> Just 39
'0' -> Just 0 '0' -> Just 0
_ -> Nothing _ -> Nothing
explode :: (Foldable f) => (a -> Bool) -> f a -> [[a]]
explode pred xs = filter (not . null) $ foldr split [[]] xs
where
split y (ys:yss)
| pred y = []:ys:yss
| otherwise = (y:ys):yss