]> git.rkrishnan.org Git - yorgey.git/blob - hw11/SExpr.hs
c3f7d70f757a8cc7e00ac31f33b882974d6df8e7
[yorgey.git] / hw11 / SExpr.hs
1 {- CIS 194 HW 11
2    due Monday, 8 April
3 -}
4
5 module SExpr where
6
7 import AParser
8 import Control.Applicative
9
10 ------------------------------------------------------------
11 --  1. Parsing repetitions
12 ------------------------------------------------------------
13 sequenceA  :: Applicative f => [f a] -> f [a]
14 sequenceA [] = pure []
15 sequenceA (x:xs) = (:) <$> x <*> sequenceA xs
16 -- sequenceA (x:xs) = liftA2 (:) x (sequenceA xs)
17
18 replicateA :: Applicative f => Int -> f a -> f [a]
19 replicateA n x = sequenceA (replicate n x)
20
21 zeroOrMore :: Parser a -> Parser [a]
22 zeroOrMore p = oneOrMore p <|> pure []
23
24 oneOrMore :: Parser a -> Parser [a]
25 oneOrMore p = liftA2 (:) p (zeroOrMore p)
26
27 ------------------------------------------------------------
28 --  2. Utilities
29 ------------------------------------------------------------
30
31 spaces :: Parser String
32 spaces = undefined
33
34 ident :: Parser String
35 ident = undefined
36
37 ------------------------------------------------------------
38 --  3. Parsing S-expressions
39 ------------------------------------------------------------
40
41 -- An "identifier" is represented as just a String; however, only
42 -- those Strings consisting of a letter followed by any number of
43 -- letters and digits are valid identifiers.
44 type Ident = String
45
46 -- An "atom" is either an integer value or an identifier.
47 data Atom = N Integer | I Ident
48   deriving Show
49
50 -- An S-expression is either an atom, or a list of S-expressions.
51 data SExpr = A Atom
52            | Comb [SExpr]
53   deriving Show