git @ Cat's Eye Technologies Quylthulg / 92f6e26
Write format function as `show` for Term as instance of Show. Chris Pressey 1 year, 4 months ago
1 changed file(s) with 16 addition(s) and 16 deletion(s). Raw diff Collapse all Expand all
7676 | Label String Term
7777 | Goto String
7878 | Abort
79 deriving (Show, Ord, Eq)
79 deriving (Ord, Eq)
80
81 instance Show Term where
82 show (Int i) = show i
83 show (Str s) = "~" ++ "$" ++ s ++ "$"
84 show (Cons h t) = "[" ++ show h ++ showTail t
85 where showTail Null = "]"
86 showTail (Cons h t) = "," ++ show h ++ showTail t
87 showTail x = "|" ++ show x ++ "]"
88 show Null = "null"
89 show (Label s t) = ":" ++ s ++ ":" ++ show t
90 show (Goto s) = "goto " ++ "$" ++ s ++ "$"
91 show Abort = "abort"
92
8093
8194 isCons (Cons _ _) = True
8295 isCons _ = False
94107 else
95108 (follow lenv first, follow lenv rest)
96109
97 formatTerm (Int i) = show i
98 formatTerm (Str s) = "~" ++ formatIdent s
99 formatTerm (Cons h t) = "[" ++ (formatTerm h) ++ formatTail t
100 formatTerm Null = "null"
101 formatTerm (Label s t) = ":" ++ s ++ ":" ++ (formatTerm t)
102 formatTerm (Goto s) = "goto " ++ (formatIdent s)
103 formatTerm Abort = "abort"
104
105 formatTail Null = "]"
106 formatTail (Cons h t) = "," ++ (formatTerm h) ++ formatTail t
107 formatTail x = "|" ++ formatTerm x ++ "]"
108
109 formatIdent s = "$" ++ s ++ "$" -- not quite (ignores quoting), but close enough
110110
111111 --
112112 -- Terms support a number of operations which require the "meaning" of the
622622 in
623623 result
624624
625 showRun = formatTerm . run
625 showRun = show . run
626626
627627 mrun :: String -> IO Term
628628
632632 lenv <- return (collectExprLabels expr)
633633 env <- return (initialEnv expansions)
634634 result <- mInterpret env lenv expr
635 putStr (formatTerm result)
635 putStr $ show result
636636 return result