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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
|
{-# LANGUAGE DeriveFunctor, GeneralizedNewtypeDeriving, BangPatterns #-}
module Interpreter(interpret) where
import Control.Monad.Except
import Control.Monad.State.Strict
import Control.Monad.Writer.Strict
import Data.List
import Data.Maybe
import qualified Data.Map.Strict as Map
import qualified Data.Set as Set
-- import Debug.Trace
import AST
import qualified GCStore as GCS
interpret :: Program -> (Maybe String, String, VM) -- (Maybe error, output, vm)
interpret prog =
let res = evaluateProgram prog >> liftM show get >>= tellLine :: ExMonad ()
m1 = unExMonad res :: ExceptT String (WriterT String (State VM)) ()
m2 = runExceptT m1 :: WriterT String (State VM) (Either String ())
m3 = runWriterT m2 :: State VM (Either String (), String)
m4 = runState m3 makeVM :: ((Either String (), String), VM)
vm = snd m4 :: VM
merr = either Just (const Nothing) (fst (fst m4))
output = snd (fst m4)
in (merr, output, vm)
type Scope = Map.Map Name GCS.Id
data Value
= VNum Double
| VStr String
| VBlock BlockType ArgList Block
| VNil
deriving (Show)
type ObjectStore = GCS.Store Value
data VM = VM {scopeStack :: [Scope],
objStore :: ObjectStore,
tempValue :: Int}
deriving (Show)
newtype ExMonad a = ExContext {unExMonad :: ExceptT String (WriterT String (State VM)) a}
deriving (Functor, Applicative, Monad, MonadState VM, MonadWriter String, MonadError String)
typeNameOf :: Value -> String
typeNameOf (VNum _) = "Number"
typeNameOf (VStr _) = "String"
typeNameOf (VBlock _ _ _) = "Block"
typeNameOf VNil = "Nil"
niceThrowError :: String -> ExMonad a
niceThrowError s = tellLine s >> throwError s
getTopScope :: String -> ExMonad Scope
getTopScope mname = get >>= \(VM stk _ _) -> case stk of
[] -> niceThrowError $ mname ++ " on empty scope stack"
(sc:_) -> return sc
modifyScopeStack :: ([Scope] -> [Scope]) -> ExMonad ()
modifyScopeStack f = modify $ \vm@(VM stk _ _) -> vm {scopeStack = f stk}
putScopeStack :: [Scope] -> ExMonad ()
putScopeStack = modifyScopeStack . const
modifyObjStore :: (ObjectStore -> ObjectStore) -> ExMonad ()
modifyObjStore f = modify $ \vm@(VM _ os _) -> vm {objStore = f os}
putObjStore :: ObjectStore -> ExMonad ()
putObjStore = modifyObjStore . const
modifyTempValue :: (Int -> Int) -> ExMonad ()
modifyTempValue func = modify $ \vm -> vm {tempValue = func (tempValue vm)}
getTempValue :: ExMonad Int
getTempValue = liftM tempValue get
getRefCount :: GCS.Id -> ExMonad Int
getRefCount gcsid = get >>= \vm -> return (GCS.refcount (objStore vm) gcsid)
derefTell :: GCS.Id -> ExMonad ()
derefTell gcsid = do
get >>= \vm -> return (GCS.deref (objStore vm) gcsid)
>>= putObjStore
rc <- getRefCount gcsid
tellLine $ "GCS deref " ++ show gcsid ++ " -> rc=" ++ show rc
cleanupScope :: Scope -> ExMonad ()
cleanupScope sc = do
tellLine $ "## cleanupScope: " ++ show sc
mapM_ cleanupGcsid (Map.elems sc)
cleanupGcsid :: GCS.Id -> ExMonad ()
cleanupGcsid gcsid = do
vm <- get
case GCS.retrieve (objStore vm) gcsid of
Nothing -> niceThrowError $ "cleanupScope: gcsid " ++ show gcsid ++ " doesn't exist"
Just (VBlock _ _ b) -> cleanupBlock b
_ -> return ()
derefTell gcsid
cleanupBlock :: Block -> ExMonad ()
cleanupBlock block = do
let idset = collectBlock block
mapM_ derefTell idset
collectBlock :: Block -> Set.Set GCS.Id
collectBlock (Block sts) = foldl Set.union $ map collectStatement sts
collectStatement :: Statement -> Set.Set GCS.Id
collectStatement (Declaration _ e) = collectExpression e
collectStatement (Assignment _ e) = collectExpression e
collectStatement (Condition e b1 b2) =
foldl Set.union [collectExpression e, collectBlock b1, collectBlock b2]
collectStatement (Dive _ es b) =
Set.union (foldl Set.union (map collectExpression es)) (collectBlock b)
collectStatement (Expr e) = collectExpression e
collectExpression :: Expression -> Set.Set GCS.Id
collectExpression (EBin _ e1 e2) = Set.union (collectExpression e1) (collectExpression e2)
collectExpression (EUn _ e) = collectExpression e
collectExpression (ELit li) = collectLiteral li
collectLiteral :: Literal -> Set.Set GCS.Id
collectLiteral (LBlock _ _ b) = collectBlock b
collectLiteral (LGCSId gcsid') = cleanupGcsid gcsid'
collectLiteral _ = return ()
cleanupValue :: Value -> Set.Set GCS.Id
cleanupValue (VBlock _ _ b) = cleanupBlock b
cleanupValue _ = return ()
pushScope :: ExMonad ()
pushScope = tellLine "pushScope" >> modifyScopeStack (Map.empty :)
popScope :: ExMonad ()
popScope = do
vm <- get
case scopeStack vm of
[] -> niceThrowError $ "popScope on empty scope stack"
(sc:rest) -> do
cleanupScope sc
putScopeStack rest
tellLine "popScope"
findVar :: Name -> ExMonad (Maybe (Int, GCS.Id, Value))
findVar n = do
(VM stk os _) <- get
let mbs = map (\sc -> Map.lookup n sc) stk
dr = dropWhile isNothing mbs
gcsid = fromJust (head dr) -- not evaluated if dr == []
if null dr
then return Nothing
else maybe (niceThrowError $ "findVar: gcsid " ++ show gcsid ++ " doesn't exist") return
(GCS.retrieve os gcsid)
>>= \value -> return (Just (length mbs - length dr, gcsid, value))
createVarInScope :: Scope -> ObjectStore -> Name -> Value -> ExMonad (Scope, ObjectStore)
createVarInScope sc os n val =
let (gcsid, os') = GCS.store os val
sc' = Map.insert n gcsid sc
in tellLine ("GCS store in " ++ show gcsid ++ " value " ++ show val) >>
if isNothing (Map.lookup n sc)
then return (sc', os')
else error $ "Var " ++ n ++ " already exists in createVarInScope"
updateVarInScope :: Scope -> ObjectStore -> Name -> Value -> ExMonad ObjectStore
updateVarInScope sc os n val = do
let gcsid = maybe (error $ "Var " ++ n ++ " not found in updateVarInScope") id (Map.lookup n sc)
prev = maybe (error "Retrieve=Nothing in updateVarInScope") id (GCS.retrieve os gcsid)
cleanupValue prev
tellLine ("GCS update " ++ show gcsid ++ " to value " ++ show val)
return (GCS.update os gcsid val)
createVarLocal :: Name -> Value -> ExMonad ()
createVarLocal n val = do
vm <- get
let (VM (sc:rest) os _) = vm
(sc', os') <- createVarInScope sc os n val
put $ vm {scopeStack = sc' : rest, objStore = os'}
updateVarAt :: Int -> Name -> Value -> ExMonad ()
updateVarAt idx n val = do
vm <- get
let (VM stk os _) = vm
updateVarInScope (stk !! idx) os n val >>= putObjStore
tellLine :: String -> ExMonad ()
tellLine s = get >>= \vm ->
let lvl = length (scopeStack vm)
ind = if lvl >= 1 then take (4 * (lvl - 1)) (cycle "| ") else error "ZERO LEVEL"
in tell $ ind ++ replace "\n" ('\n' : ind) s ++ "\n"
tellLineStandout :: String -> ExMonad ()
tellLineStandout s = tell $ "\x1B[1m" ++ s ++ "\x1B[0m\n"
replace :: String -> String -> String -> String
replace _ _ "" = ""
replace a b subj =
if take (length a) subj == a
then b ++ replace a b (drop (length a) subj)
else head subj : replace a b (tail subj)
makeVM :: VM
makeVM = VM [Map.empty] GCS.empty 0
evaluateProgram :: Program -> ExMonad ()
evaluateProgram (Program (Block sts)) = mapM_ evStatement sts
evStatement :: Statement -> ExMonad ()
-- evStatement a | traceShow a False = unreachable
evStatement a = tellLine ("-# evStatement: " ++ astPretty a) >> evStatement' a
evStatement' :: Statement -> ExMonad ()
evStatement' (Declaration n e) =
getTopScope "evStatement" >>=
maybe (evExpression e >>= createVarLocal n)
(const $ niceThrowError $ "Variable " ++ n ++ " already exists in scope")
. Map.lookup n
evStatement' (Assignment n e) =
findVar n >>=
maybe (niceThrowError $ "Variable " ++ n ++ " assigned to but not found")
(\(idx, _, _) -> evExpression e >>= updateVarAt idx n)
evStatement' (Condition cond b1 b2) =
evExpression cond >>= truthValue >>= \bool -> evBlock $ if bool then b1 else b2
evStatement' (Dive "print" al b) = do
(show <$> mapM evExpression al) >>= tellLineStandout
evBlock b
evStatement' (Dive n al b) =
findVar n >>=
maybe (niceThrowError $ "Variable " ++ n ++ " dived into but not found")
(\(_, _, value) -> case value of
(VBlock BT2 val vb)
| length al == length val -> do
pushScope
evBlockNoScope $ Block [Declaration dn de | (dn, de) <- zip val al]
evBlockNoScope vb
evBlockNoScope b
popScope
| otherwise ->
niceThrowError $ "Invalid number of arguments to dived-in block " ++ n
_ -> niceThrowError $ "Cannot dive into " ++ n ++ " of invalid type " ++ typeNameOf value)
evStatement' (Expr e) = evExpression e >>= cleanupValue
evExpression :: Expression -> ExMonad Value
-- evExpression a | traceShow a False = unreachable
evExpression a = tellLine ("-# evExpression: " ++ astPretty a) >> evExpression' a
evExpression' :: Expression -> ExMonad Value
evExpression' (EBin bo e1 e2) = evBO bo e1 e2
evExpression' (EUn uo e) = evExpression e >>= evUO uo
evExpression' (ELit li) = evLiteral li
evBlock :: Block -> ExMonad ()
-- evBlock a | traceShow a False = unreachable
evBlock bl = do
pushScope
evBlockNoScope bl
popScope
evBlockNoScope :: Block -> ExMonad ()
evBlockNoScope a = tellLine ("-# evBlockNoScope: " ++ astPretty a) >> evBlockNoScope' a
evBlockNoScope' :: Block -> ExMonad ()
evBlockNoScope' (Block sts) = mapM_ evStatement sts
evLiteral :: Literal -> ExMonad Value
evLiteral (LNum m) = return $ VNum m
evLiteral (LStr s) = return $ VStr s
evLiteral (LVar n) =
findVar n >>= maybe (niceThrowError $ "Variable " ++ n ++ " referenced but not found") (return . thrd)
evLiteral (LBlock BT0 al bl)
| length al == 0 = evBlock bl >> return VNil
| otherwise = niceThrowError $ "Immediately invoked block literal cannot have parameters"
evLiteral (LBlock bt al bl) = liftM (VBlock bt al) $ processBlock bl
evLiteral (LGCSId gcsid) = get >>= \(VM _ os _) -> maybe (niceThrowError $ "evLiteral: gcsid " ++ show gcsid ++ " doesn't exist") return (GCS.retrieve os gcsid)
evLiteral LNil = return VNil
evUO :: UnaryOp -> Value -> ExMonad Value
evUO UONeg (VNum m) = return $ VNum (-m)
evUO UONeg value = niceThrowError $ "Operator '(-)' does not take a value of type " ++ typeNameOf value
evUO UONot value = (bool2VNum . not) <$> truthValue value
classifyBO :: BinaryOp -> (Bool, Bool, Bool)
classifyBO bo = case bo of
BOPlus -> (True, f, f)
BOMinus -> (True, f, f)
BOMul -> (True, f, f)
BODiv -> (True, f, f)
BOMod -> (True, f, f)
BOPow -> (True, f, f)
BOLess -> (f, True, f)
BOGreater -> (f, True, f)
BOEqual -> (f, True, f)
BOLEq -> (f, True, f)
BOGEq -> (f, True, f)
BOBoolAnd -> (f, f, True)
BOBoolOr -> (f, f, True)
where f = False
isArithBO, isCompBO {-, isBoolBO-} :: BinaryOp -> Bool
isArithBO bo = let (r, _, _) = classifyBO bo in r
isCompBO bo = let (_, r, _) = classifyBO bo in r
-- isBoolBO bo = let (_, _, r) = classifyBO bo in r
evArithBO :: BinaryOp -> Double -> Double -> Double
evArithBO BOPlus = (+)
evArithBO BOMinus = (-)
evArithBO BOMul = (*)
evArithBO BODiv = (/)
evArithBO BOMod = \a b -> a - fromIntegral (floor (a / b) :: Integer) * b
evArithBO BOPow = (**)
evArithBO _ = unreachable
evOrderingBO :: Ord a => BinaryOp -> a -> a -> Value
evOrderingBO bo a b =
let c = compare a b
in bool2VNum $ case bo of
BOLess -> c == LT
BOGreater -> c == GT
BOEqual -> c == EQ
BOLEq -> c == LT || c == EQ
BOGEq -> c == GT || c == EQ
_ -> unreachable
evBO :: BinaryOp -> Expression -> Expression -> ExMonad Value
evBO bo e1 e2
| isArithBO bo = evExpression e1 >>= \v1 -> evExpression e2 >>= \v2 -> case (v1, v2) of
(VNum m1, VNum m2) -> return $ VNum $ evArithBO bo m1 m2
(_, _) -> niceThrowError $ "Operator '" ++ astPretty bo ++ "' does not take types " ++
typeNameOf v1 ++ " and " ++ typeNameOf v2
| isCompBO bo = evExpression e1 >>= \v1 -> evExpression e2 >>= \v2 -> case (v1, v2) of
(VNum m1, VNum m2) -> return $ evOrderingBO bo m1 m2
(VStr s1, VStr s2) -> return $ evOrderingBO bo s1 s2
(_, _) -> niceThrowError $ "Operator '" ++ astPretty bo ++ "' does not take types " ++
typeNameOf v1 ++ " and " ++ typeNameOf v2
| bo == BOBoolAnd =
evExpression e1 >>= truthValue >>= \v1 ->
if not v1 then return (VNum 0)
else evExpression e2 >>= truthValue >>= return . bool2VNum
| bo == BOBoolOr =
evExpression e1 >>= truthValue >>= \v1 ->
if v1 then return (VNum 1)
else evExpression e2 >>= truthValue >>= return . bool2VNum
| otherwise = unreachable
truthValue :: Value -> ExMonad Bool
truthValue (VNum 0) = return False
truthValue (VNum _) = return True
truthValue (VStr "") = return False
truthValue (VStr _) = return True
truthValue (VBlock _ _ _) = niceThrowError "Block not valid as truth value"
truthValue VNil = return False
bool2VNum :: Bool -> Value
bool2VNum True = VNum 1
bool2VNum False = VNum 0
data ProcessState = ProcessState {psStack :: [Set.Set Name], psSet :: Set.Set GCS.Id}
newtype ProcessMonad a = ProcessMonad {unProcessMonad :: State ProcessState a}
deriving (Functor, Applicative, Monad, MonadState ProcessState)
pmModifyStack :: ([Set.Set Name] -> [Set.Set Name]) -> ProcessMonad ()
pmModifyStack f = modify $ \ps@(ProcessState s _) -> ps {psStack = f s}
pmModifyStackTop :: (Set.Set Name -> Set.Set Name) -> ProcessMonad ()
pmModifyStackTop f = modify $ \ps@(ProcessState (s:ss) _) -> ps {psStack = f s : ss}
pmModifySet :: (Set.Set GCS.Id -> Set.Set GCS.Id) -> ProcessMonad ()
pmModifySet f = modify $ \ps@(ProcessState _ s) -> ps {psSet = f s}
processBlockCollect :: [Scope] -> Block -> ProcessMonad Block
processBlockCollect gStack = goBlock
where
goBlock :: Block -> ProcessMonad Block
goBlock (Block sts) = do
pmModifyStack (Set.empty :)
b <- Block <$> mapM goStatement sts
pmModifyStack tail
return b
goStatement :: Statement -> ProcessMonad Statement
goStatement (Declaration n e) = do
d <- Declaration n <$> goExpression e
pmModifyStackTop (Set.insert n)
return d
goStatement (Assignment n e) = Assignment n <$> goExpression e
goStatement (Condition e b1 b2) = Condition <$> goExpression e <*> goBlock b1 <*> goBlock b2
goStatement (Dive n al b) = Dive n <$> mapM goExpression al <*> goBlock b
goStatement (Expr e) = Expr <$> goExpression e
goExpression :: Expression -> ProcessMonad Expression
goExpression (EBin bo e1 e2) = EBin bo <$> goExpression e1 <*> goExpression e2
goExpression (EUn uo e) = EUn uo <$> goExpression e
goExpression (ELit l) = ELit <$> goLiteral l
goLiteral :: Literal -> ProcessMonad Literal
goLiteral (LVar name) = do
(ProcessState st _) <- get
let midx1 = findIndex (Set.member name) st
midx2 = findIndex (Map.member name) gStack
case (midx1, midx2) of
(Nothing, Nothing) -> return (LVar name)
(Just _, _) -> return (LVar name)
(Nothing, Just idx) ->
let gcsid = fromJust $ Map.lookup name (gStack !! idx)
in pmModifySet (Set.insert gcsid) >> return (LGCSId gcsid)
goLiteral (LBlock bt al bl) = LBlock bt al <$> goBlock bl
goLiteral (LGCSId gcsid) = do
pmModifySet (Set.insert gcsid)
return (LGCSId gcsid)
goLiteral l = return l
processBlock :: Block -> ExMonad Block
processBlock block = do
tellLine ("## processBlock: " ++ astPretty block)
(VM stk os _) <- get
let (block', ProcessState _ psset) =
runState (unProcessMonad $ processBlockCollect stk block)
(ProcessState [] Set.empty)
let refitems = Set.toList psset
os' = foldl GCS.ref os psset
mapM_ (\gcsid -> tellLine ("GCS ref " ++ show gcsid)) refitems
putObjStore os'
return block'
-- processBlock :: Block -> ExMonad Block
-- processBlock b = tellLine ("## processBlock: " ++ show b) >> modifyTempValue (const 0) >> goBlock b
-- where
-- goStatement :: Statement -> ExMonad Statement
-- goStatement (Declaration n e) = do
-- d <- Declaration n <$> goExpression e
-- createVarLocal n VNil
-- return d
-- goStatement (Assignment n e) = Assignment n <$> goExpression e
-- goStatement (Condition e b1 b2) = Condition <$> goExpression e <*> goBlock b1 <*> goBlock b2
-- goStatement (Dive n al b') = Dive n <$> mapM goExpression al <*> goBlock b'
-- goStatement (Expr e) = Expr <$> goExpression e
-- goBlock :: Block -> ExMonad Block
-- goBlock (Block sts) = do
-- pushScope
-- modifyTempValue succ
-- b' <- Block <$> mapM goStatement sts
-- modifyTempValue pred
-- popScope
-- return b'
-- goExpression :: Expression -> ExMonad Expression
-- goExpression (EBin bo e1 e2) = EBin bo <$> goExpression e1 <*> goExpression e2
-- goExpression (EUn uo e) = EUn uo <$> goExpression e
-- goExpression (ELit l) = ELit <$> goLiteral l
-- goLiteral :: Literal -> ExMonad Literal
-- goLiteral (LVar name) = do
-- tv <- getTempValue
-- findVar name >>=
-- maybe (niceThrowError $ "Unknown variable " ++ name ++ " while processing block")
-- (\(idx, gcsid, _) ->
-- if idx >= tv
-- then modifyObjStore (\os -> GCS.ref os gcsid)
-- >> tellLine ("GCS ref " ++ show gcsid)
-- >> return (LGCSId gcsid)
-- else return (LVar name))
-- goLiteral (LBlock bt al bl) = LBlock bt al <$> goBlock bl
-- goLiteral l = return l
thrd :: (a, b, c) -> c
thrd (_, _, c) = c
unreachable :: a
unreachable = error "Unreachable"
|