on bca019d help sign in
elk/src/AST/Kernel.hs
73 lines 1.2 kB history raw
1{-# OPTIONS_GHC -Wall #-}
2module AST.Kernel
3 ( Info(..)
4 , Chunk(..)
5 )
6 where
9import Control.Monad (liftM, liftM2)
10import Data.Binary
11import qualified Data.ByteString as BS
12import qualified Data.Text as Text
14import qualified AST.Module.Name as ModuleName
18-- INFO
21data Info =
22 Info
23 { _imports :: [(ModuleName.Raw, Text.Text)]
24 , _chunks :: [Chunk]
25 }
28data Chunk
29 = JS BS.ByteString
30 | Var ModuleName.Raw Text.Text
31 | Field Text.Text
32 | Prod Bool
36-- BINARY
39instance Binary Info where
40 put (Info imports chunks) =
41 put imports >> put chunks
43 get =
44 liftM2 Info get get
47instance Binary Chunk where
48 put chunk =
49 case chunk of
50 JS bytes ->
51 putWord8 0 >> put bytes
53 Var home name ->
54 putWord8 1 >> put home >> put name
56 Field field ->
57 putWord8 2 >> put field
59 Prod True ->
60 putWord8 3
62 Prod False ->
63 putWord8 4
65 get =
66 do word <- getWord8
67 case word of
68 0 -> liftM JS get
69 1 -> liftM2 Var get get
70 2 -> liftM Field get
71 3 -> return (Prod True)
72 4 -> return (Prod False)
73 _ -> error "problem deserializing Parse.Kernel.Chunk"