Derive functors for pretty much all phase 1 types

This commit is contained in:
Joscha 2020-04-03 01:15:35 +00:00
parent 0fea2b960a
commit bc52fafe63

View file

@ -1,3 +1,4 @@
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE OverloadedStrings #-}
module Mima.Asm.Phase1 module Mima.Asm.Phase1
@ -15,6 +16,7 @@ module Mima.Asm.Phase1
, JsonValue(..) , JsonValue(..)
, Directive(..) , Directive(..)
, Span(..) , Span(..)
, AsmToken(..)
-- * Phase1 -- * Phase1
, Phase1 , Phase1
, parsePhase1 , parsePhase1
@ -65,7 +67,7 @@ class Onion o where
{- Types -} {- Types -}
data Name a = Name a T.Text data Name a = Name a T.Text
deriving (Show) deriving (Show, Functor)
instance Onion Name where instance Onion Name where
peel (Name a _) = a peel (Name a _) = a
@ -73,7 +75,7 @@ instance Onion Name where
data Address a data Address a
= AddressAbsolute a Vm.MimaAddress = AddressAbsolute a Vm.MimaAddress
| AddressRelative a Integer | AddressRelative a Integer
deriving (Show) deriving (Show, Functor)
instance Onion Address where instance Onion Address where
peel (AddressAbsolute a _) = a peel (AddressAbsolute a _) = a
@ -82,20 +84,20 @@ instance Onion Address where
data Location a data Location a
= LocationAddress (Address a) = LocationAddress (Address a)
| LocationLabel (Name a) | LocationLabel (Name a)
deriving (Show) deriving (Show, Functor)
instance Onion Location where instance Onion Location where
peel (LocationAddress a) = peel a peel (LocationAddress a) = peel a
peel (LocationLabel a) = peel a peel (LocationLabel a) = peel a
data SmallOpcode a = SmallOpcode a Vm.SmallOpcode data SmallOpcode a = SmallOpcode a Vm.SmallOpcode
deriving (Show) deriving (Show, Functor)
instance Onion SmallOpcode where instance Onion SmallOpcode where
peel (SmallOpcode a _) = a peel (SmallOpcode a _) = a
data LargeOpcode a = LargeOpcode a Vm.LargeOpcode data LargeOpcode a = LargeOpcode a Vm.LargeOpcode
deriving (Show) deriving (Show, Functor)
instance Onion LargeOpcode where instance Onion LargeOpcode where
peel (LargeOpcode a _) = a peel (LargeOpcode a _) = a
@ -103,14 +105,14 @@ instance Onion LargeOpcode where
data MimaWord a data MimaWord a
= WordRaw a Vm.MimaWord = WordRaw a Vm.MimaWord
| WordLocation (Location a) | WordLocation (Location a)
deriving (Show) deriving (Show, Functor)
instance Onion MimaWord where instance Onion MimaWord where
peel (WordRaw a _) = a peel (WordRaw a _) = a
peel (WordLocation a) = peel a peel (WordLocation a) = peel a
data SmallValue a = SmallValue a Vm.SmallValue data SmallValue a = SmallValue a Vm.SmallValue
deriving (Show) deriving (Show, Functor)
instance Onion SmallValue where instance Onion SmallValue where
peel (SmallValue a _) = a peel (SmallValue a _) = a
@ -118,7 +120,7 @@ instance Onion SmallValue where
data Instruction a data Instruction a
= SmallInstruction a (SmallOpcode a) (Location a) = SmallInstruction a (SmallOpcode a) (Location a)
| LargeInstruction a (LargeOpcode a) (Maybe (SmallValue a)) | LargeInstruction a (LargeOpcode a) (Maybe (SmallValue a))
deriving (Show) deriving (Show, Functor)
instance Onion Instruction where instance Onion Instruction where
peel (SmallInstruction a _ _) = a peel (SmallInstruction a _ _) = a
@ -132,7 +134,7 @@ data RegisterDirective a
| RegRa a a (Location a) | RegRa a a (Location a)
| RegSp a a (Location a) | RegSp a a (Location a)
| RegFp a a (Location a) | RegFp a a (Location a)
deriving (Show) deriving (Show, Functor)
instance Onion RegisterDirective where instance Onion RegisterDirective where
peel (RegIar a _ _) = a peel (RegIar a _ _) = a
@ -142,7 +144,7 @@ instance Onion RegisterDirective where
peel (RegFp a _ _) = a peel (RegFp a _ _) = a
data JsonValue a = JsonValue a A.Value data JsonValue a = JsonValue a A.Value
deriving (Show) deriving (Show, Functor)
instance Onion JsonValue where instance Onion JsonValue where
peel (JsonValue a _) = a peel (JsonValue a _) = a
@ -157,7 +159,7 @@ data Directive a
| Meta a a (Name a) (JsonValue a) | Meta a a (Name a) (JsonValue a)
| MetaStart a a (Name a) (JsonValue a) | MetaStart a a (Name a) (JsonValue a)
| MetaStop a a (Name a) | MetaStop a a (Name a)
deriving (Show) deriving (Show, Functor)
instance Onion Directive where instance Onion Directive where
peel (Reg a _ _) = a peel (Reg a _ _) = a
@ -175,7 +177,7 @@ data AsmToken a
| TokenComment a T.Text Bool | TokenComment a T.Text Bool
-- ^ @'TokenComment' a text inline@ represents a comment. -- ^ @'TokenComment' a text inline@ represents a comment.
-- @inline@ is true if the comment is on the same line as an instruction or a label. -- @inline@ is true if the comment is on the same line as an instruction or a label.
deriving (Show) deriving (Show, Functor)
instance Onion AsmToken where instance Onion AsmToken where
peel (TokenLabel a) = peel a peel (TokenLabel a) = peel a
@ -191,7 +193,7 @@ instance Show Span where
showPos pos = showPos pos =
show (unPos $ sourceLine pos) ++ ":" ++ show (unPos $ sourceColumn pos) show (unPos $ sourceLine pos) ++ ":" ++ show (unPos $ sourceColumn pos)
type Phase1 = [AsmToken Span] type Phase1 s = [AsmToken s]
{- Parsing -} {- Parsing -}
@ -381,7 +383,7 @@ lineParser = do
comment $ not aloneOnLine comment $ not aloneOnLine
pure $ ls ++ toList t ++ toList c pure $ ls ++ toList t ++ toList c
parsePhase1 :: Parser Phase1 parsePhase1 :: Parser (Phase1 Span)
parsePhase1 = mconcat <$> sepBy lineParser newline <* eof parsePhase1 = mconcat <$> sepBy lineParser newline <* eof
{- Formatting -} {- Formatting -}
@ -448,7 +450,7 @@ formatToken (TokenInstruction ins) = " " <> formatInstruction ins
formatToken (TokenDirective dir) = formatDirective dir formatToken (TokenDirective dir) = formatDirective dir
formatToken (TokenComment _ text _) = ";" <> text formatToken (TokenComment _ text _) = ";" <> text
formatPhase1 :: Phase1 -> T.Text formatPhase1 :: Phase1 a -> T.Text
formatPhase1 (x:y@(TokenComment _ _ True):xs) = formatToken x <> " " <> formatPhase1 (y:xs) formatPhase1 (x:y@(TokenComment _ _ True):xs) = formatToken x <> " " <> formatPhase1 (y:xs)
formatPhase1 (x:xs) = formatToken x <> "\n" <> formatPhase1 xs formatPhase1 (x:xs) = formatToken x <> "\n" <> formatPhase1 xs
formatPhase1 [] = "" formatPhase1 [] = ""