-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSyntax.hs
110 lines (88 loc) · 2.64 KB
/
Syntax.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
module Syntax where
import Control.Monad.Except (ExceptT, MonadIO (liftIO))
import Data.HashMap.Strict qualified as Hash
import Data.IORef
import System.Environment (getEnv, getEnvironment)
data Expr
= Symbol String
| Number Integer
| Bool Bool
| List [Expr]
| Quote Expr
| Primitive PrimitiveFunction
| Lambda [String] Expr Environment
| Macro SyntaxClosure
typeOfExpr :: Expr -> String
typeOfExpr expr = case expr of
Symbol s -> "<Symbol>"
Number n -> "<Number>"
Bool b -> "<Bool>"
List lst -> "<List>"
Quote expr -> "<Quote>"
Primitive fn -> "<Primitive Function>"
Lambda params expr env -> "<Lambda>"
Macro closure -> "<Macro>"
showExpr :: Expr -> String
showExpr expr = case expr of
Symbol s -> "Symbol " ++ s
Number n -> "Number " ++ show n
Bool b -> "Bool " ++ show b
List lst -> "List [" ++ showaList (map showExpr lst) ++ "]"
Quote expr' -> "Quote " ++ showExpr expr'
Primitive fn -> "<primitive function>"
Lambda args expr env ->
"Lambda Args: ("
++ showaList (map show args)
++ ")\nBody: "
++ showExpr expr
++ ")"
Macro closure -> show closure
showaList :: [String] -> String
showaList lst = case lst of
[] -> ""
[x] -> x
(x : xs) -> x ++ ", " ++ showaList xs
data SyntaxClosure = SyntaxClosure [(Pattern, Template)] Environment
showSyntaxClosure :: SyntaxClosure -> String
showSyntaxClosure (SyntaxClosure rules env) = show rules
newtype SError
= Default String
deriving (Show, Eq)
-- Environment
type Environment = IORef (Hash.HashMap String Expr)
-- Macro
type MacroMap = Hash.HashMap (String, SyntaxClosure)
type IOThrowError = ExceptT SError IO
type PrimitiveFunction = [Expr] -> IOThrowError Expr
data Pattern
= PSymbol String
| PVariable String
| PList [Pattern]
| PRepeat Pattern
instance Show Pattern where
show :: Pattern -> String
show = showPattern
showPattern :: Pattern -> String
showPattern p = case p of
PSymbol s -> "PSymbol " ++ show s
PVariable v -> "PVariable " ++ show v
PList lst -> "PList [" ++ unwords (map showPattern lst) ++ "]"
PRepeat p -> "PRepeat " ++ showPattern p
data Template
= TSymbol String
| TVariable String
| TList [Template]
| TRepeat Template
instance Show Template where
show :: Template -> String
show = showTemplate
showTemplate :: Template -> String
showTemplate t = case t of
TSymbol s -> "TSymbol " ++ show s
TVariable v -> "TVariable " ++ show v
TList lst -> "TList [" ++ unwords (map showTemplate lst) ++ "]"
TRepeat p -> "TRepeat " ++ showTemplate p
type Rule = (Pattern, Template)
instance (Show SyntaxClosure) where
show :: SyntaxClosure -> String
show (SyntaxClosure rules env) = show rules