|
1 |
| -module Parser where |
| 1 | +module Data.Compiler where |
2 | 2 |
|
3 | 3 | import qualified Data.Map as Map
|
4 | 4 |
|
5 |
| -import SExpr |
| 5 | +import Data.SExpr |
| 6 | +import Data.Token |
| 7 | +import Data.ParseTree |
6 | 8 | import Utils
|
7 | 9 |
|
8 |
| -data Token = IntegerType Integer |
9 |
| - | DoubleType Double |
10 |
| - | BoolType Bool |
11 |
| - | StringType String |
12 |
| - | SymbType String |
13 |
| - deriving (Show, Eq) |
14 |
| - |
15 |
| -data TokenTree = Empty |
16 |
| - | Leaf Token |
17 |
| - | Node TokenTree TokenTree deriving (Show, Eq) |
18 |
| - |
19 |
| -instance Read Token where |
20 |
| - readsPrec _ str = case dropLeadingBlanks str of |
21 |
| - "" -> [] |
22 |
| - '(':_ -> [] |
23 |
| - ')':_ -> [] |
24 |
| - '.':_ -> [] |
25 |
| - str' -> case reads str' :: [(String, String)] of |
26 |
| - [(x, s)] -> [(StringType x, s)] |
27 |
| - _ -> case reads str' :: [(Integer, String)] of |
28 |
| - [(x, s)] -> [(IntegerType x, s)] |
29 |
| - _ -> case reads str' :: [(Double, String)] of |
30 |
| - [(x, s)] -> [(DoubleType x, s)] |
31 |
| - _ -> case split str' of |
32 |
| - ("#t", s) -> [(BoolType True, s)] |
33 |
| - ("#f", s) -> [(BoolType False, s)] |
34 |
| - ('\"':_, _) -> [] -- a symbol cannot begin with a double quote |
35 |
| - (str'', s) -> [(SymbType str'', s)] |
36 |
| - |
37 |
| -instance Read TokenTree where |
38 |
| - readsPrec _ str = case dropLeadingBlanks str of |
39 |
| - "" -> [] |
40 |
| - ')':_ -> [] |
41 |
| - '.':_ -> [] |
42 |
| - '\'':str' -> case reads str' :: [(TokenTree, String)] of |
43 |
| - [(x, str'')] -> [(Node (Leaf (SymbType "\'")) (Node x Empty), str'')] |
44 |
| - _ -> [(Empty, '\'':str')] |
45 |
| - '(':str' -> case dropLeadingBlanks str' of |
46 |
| - ')':str'' -> [(Empty, str'')] |
47 |
| - _ -> case reads str' :: [(TokenTree, String)] of |
48 |
| - [(x, str'')] -> case dropLeadingBlanks str'' of |
49 |
| - "" -> [] |
50 |
| - ')':str''' -> [(Node x Empty, str''')] |
51 |
| - '.':str''' -> case reads ('(':str''') :: [(TokenTree, String)] of |
52 |
| - [(Node y Empty, str'''')] -> [(Node x y, str'''')] |
53 |
| - _ -> [] |
54 |
| - _ -> case reads ('(':str'') :: [(TokenTree, String)] of |
55 |
| - [(y, str''')] -> [(Node x y, str''')] |
56 |
| - _ -> [] |
57 |
| - _ -> [] |
58 |
| - _ -> case reads str :: [(Token, String)] of |
59 |
| - [(x, str')] -> [(Leaf x, str')] |
60 |
| - _ -> [] |
61 |
| - |
62 |
| -compile :: TokenTree -> Maybe SExpr |
| 10 | +compile :: ParseTree -> Maybe SExpr |
63 | 11 | compile Empty = Just Nil
|
64 | 12 |
|
65 | 13 | compile (Leaf (BoolType True)) = Just T
|
@@ -196,15 +144,15 @@ compile (Node x y) = do
|
196 | 144 | y' <- compile y
|
197 | 145 | return $ Pair x' y'
|
198 | 146 |
|
199 |
| -compileList :: TokenTree -> Maybe [SExpr] |
| 147 | +compileList :: ParseTree -> Maybe [SExpr] |
200 | 148 | compileList Empty = Just []
|
201 | 149 | compileList (Node x y) = do
|
202 | 150 | x' <- compile x
|
203 | 151 | y' <- compileList y
|
204 | 152 | return (x':y')
|
205 | 153 | compileList _ = Nothing
|
206 | 154 |
|
207 |
| -compileCoupleList :: TokenTree -> Maybe [(SExpr, SExpr)] |
| 155 | +compileCoupleList :: ParseTree -> Maybe [(SExpr, SExpr)] |
208 | 156 | compileCoupleList Empty = Just []
|
209 | 157 | compileCoupleList (Node (Node x (Node y Empty)) z) = do
|
210 | 158 | x' <- compile x
|
|
0 commit comments