-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parser.hs
30 lines (21 loc) · 975 Bytes
/
Parser.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
module Parser(parseProgram) where
import LParse
import Program
parseProgram :: Parser r Program
parseProgram = star parseCommand
parseCommand :: Parser r Command
parseCommand = cParse (not . null) (parseInv <|> parseSwap <|> parsePush <|> parsePop <|> parseNE <|> parseNZ <|> pFail "Expected Command") "Expected Command"
parseInv :: Parser r Command
parseInv = dPrefixParse "!" (constParse Inv)
parseSwap :: Parser r Command
parseSwap = dPrefixParse "_" (constParse Swap)
parsePush :: Parser r Command
parsePush = dPrefixParse "v" (constParse Push)
parsePop :: Parser r Command
parsePop = dPrefixParse "^" (constParse Pop)
parseNE :: Parser r Command
parseNE = dPrefixParse "(" (fmap NE parseProgram <.const.> remCB)
where remCB = cParse (not . null) (pParse tail noopParse) "Expected ')'"
parseNZ :: Parser r Command
parseNZ = dPrefixParse "[" (fmap NZ parseProgram <.const.> remCB)
where remCB = cParse (not . null) (pParse tail noopParse) "Expected ']'"