gdritter repos s-cargot / 4bf5361
Fixed type and semantic errors Getty Ritter 9 years ago
1 changed file(s) with 5 addition(s) and 4 deletion(s). Collapse all Expand all
135135 As pointed out above, there are three different carrier types that are
136136 used to represent S-expressions by the library, but you can use any
137137 type as a carrier type for a spec. This is particularly useful when
138 you want to do your own parsing. For example, if we wanted to parse
139 a small S-expression-based arithmetic language, we could define a
140 data type and transformations from and to an S-expression type:
138 you want to parse into your own custom tree-like type. For example, if
139 we wanted to parse a small S-expression-based arithmetic language, we
140 could define a data type and transformations from and to an S-expression
141 type:
141142
142143 ~~~~.haskell
143144 import Data.Char (isDigit)
151152 toExpr (RSAtom c)
152153 | T.all isDigit c = pure (Num (read (T.unpack c)))
153154 | otherwise = Left "Non-numeric token as argument"
154 toExpr _ = "Unrecognized s-expr"
155 toExpr _ = Left "Unrecognized s-expr"
155156
156157 fromExpr :: Expr -> RichSExpr Text
157158 fromExpr (Add x y) = RSList [RSAtom "+", fromExpr x, fromExpr y]