Parse assembly statements
This commit begins the rewrite of the assembly parser to use the new (not yet written down) syntax.
This commit is contained in:
parent
7def23284d
commit
f3b39f78f4
6 changed files with 259 additions and 1 deletions
65
src/Mima/Parse/Assembly/Directive.hs
Normal file
65
src/Mima/Parse/Assembly/Directive.hs
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
{-# LANGUAGE OverloadedStrings #-}
|
||||
|
||||
module Mima.Parse.Assembly.Directive
|
||||
( SetRegister(..)
|
||||
, Directive(..)
|
||||
, lDirective
|
||||
) where
|
||||
|
||||
import qualified Data.Set as Set
|
||||
import Text.Megaparsec
|
||||
|
||||
import Mima.Parse.Assembly.Common
|
||||
import Mima.Parse.Assembly.Lexeme
|
||||
import Mima.Parse.Common
|
||||
import Mima.Word
|
||||
|
||||
data SetRegister a
|
||||
= SetIAR a
|
||||
| SetACC MimaWord
|
||||
| SetRA a
|
||||
| SetSP a
|
||||
| SetFP a
|
||||
deriving (Show)
|
||||
|
||||
data Directive a
|
||||
= DReg (SetRegister a) -- .reg (iar|acc|ra|sp|fp) <initial value>
|
||||
| DOrg MimaAddress -- .org <address>
|
||||
| DLit MimaWord -- .lit <word>
|
||||
| DArr [MimaWord] -- .arr [<word>, ...]
|
||||
| DFlag (Set.Set Char) -- .flag <chars>
|
||||
| DFlagOn (Set.Set Char) -- .flagon <chars>
|
||||
| DFlagOff (Set.Set Char) -- .flagoff <chars>
|
||||
deriving (Show)
|
||||
|
||||
lSetRegister :: Parser (SetRegister Address)
|
||||
lSetRegister =
|
||||
SetIAR <$> sepBySpace "iar" address
|
||||
<|> SetACC <$> sepBySpace "acc" word
|
||||
<|> SetRA <$> sepBySpace "ra" address
|
||||
<|> SetSP <$> sepBySpace "sp" address
|
||||
<|> SetFP <$> sepBySpace "fp" address
|
||||
where
|
||||
sepBySpace name parser = symbol name *> lSpace *> lexeme parser
|
||||
|
||||
lWordArray :: Parser [MimaWord]
|
||||
lWordArray = open *> (word `sepBy` comma) <* close
|
||||
where
|
||||
open = lexeme $ symbol "["
|
||||
comma = lexeme $ symbol ","
|
||||
close = lexeme $ symbol "]"
|
||||
|
||||
lFlags :: Parser (Set.Set Char)
|
||||
lFlags = Set.unions <$> some (lexeme flag)
|
||||
|
||||
lDirective :: Parser (Directive Address)
|
||||
lDirective = label "assembler directive" $ undefined
|
||||
DReg <$> directive ".reg" lSetRegister
|
||||
<|> DOrg <$> directive ".org" (lexeme largeValue)
|
||||
<|> DLit <$> directive ".lit" (lexeme word)
|
||||
<|> DArr <$> directive ".arr" lWordArray
|
||||
<|> DFlag <$> directive ".flag" lFlags
|
||||
<|> DFlagOn <$> directive ".flagon" lFlags
|
||||
<|> DFlagOff <$> directive ".flagoff" lFlags
|
||||
where
|
||||
directive name parser = symbol name *> lSpace *> parser
|
||||
Loading…
Add table
Add a link
Reference in a new issue