summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorErin van der Veen2018-02-08 10:23:27 +0100
committerErin van der Veen2018-02-08 10:23:27 +0100
commit4776243a670916144f8de1f853b29b0b1d78fffe (patch)
treea71712176d2acb7decec5573d6f37b07ac9f3afe
parentImplement pprint for Nop (diff)
Create Program Serializer2-serialization
-rw-r--r--SPL-compiler.cabal4
-rw-r--r--src/SPL/Serialize.hs12
-rw-r--r--src/SPL/Syntax.hs36
3 files changed, 41 insertions, 11 deletions
diff --git a/SPL-compiler.cabal b/SPL-compiler.cabal
index adf84c7..9b4cedc 100644
--- a/SPL-compiler.cabal
+++ b/SPL-compiler.cabal
@@ -23,6 +23,8 @@ executable SPL-compiler
build-depends: base >=4.9 && <4.10,
MissingH >=1.4 && <1.5,
parsec >=3.1 && <3.2,
- mtl >=2.2 && <2.3
+ mtl >=2.2 && <2.3,
+ cereal >=0.5.5 && <0.5.6,
+ bytestring >=0.10 && <0.11
hs-source-dirs: src
default-language: Haskell2010
diff --git a/src/SPL/Serialize.hs b/src/SPL/Serialize.hs
new file mode 100644
index 0000000..5a82469
--- /dev/null
+++ b/src/SPL/Serialize.hs
@@ -0,0 +1,12 @@
+-- vim: et ts=2 sw=2 ai:
+module SPL.Serialize
+where
+
+import SPL.Syntax
+
+import GHC.Generics
+import Data.Serialize
+import Data.ByteString
+
+serializeProgram :: Program -> ByteString
+serializeProgram = encode
diff --git a/src/SPL/Syntax.hs b/src/SPL/Syntax.hs
index f52b866..d4775a4 100644
--- a/src/SPL/Syntax.hs
+++ b/src/SPL/Syntax.hs
@@ -1,14 +1,19 @@
-- vim: et ts=2 sw=2 ai:
+{-# LANGUAGE DeriveGeneric #-}
+{-# LANGUAGE DefaultSignatures #-}
module SPL.Syntax
where
+import GHC.Generics
+import Data.Serialize
+
type Name = String
data Program = Program
{ funs :: [Function]
, vars :: [Variable]
}
- deriving (Show)
+ deriving (Show, Generic)
data Function = Function
{ fname :: Name
@@ -17,14 +22,14 @@ data Function = Function
, fvars :: [Variable]
, fcode :: Statement
}
- deriving (Show)
+ deriving (Show, Generic)
data Variable = Variable
{ vname :: Name
, vtype :: Maybe Type
, vval :: Expression
}
- deriving (Show)
+ deriving (Show, Generic)
data Type
= TInt
@@ -35,7 +40,7 @@ data Type
| TTuple Type Type
| TArrow [Type] Type
| TVar Name
- deriving (Show)
+ deriving (Show, Generic)
data Statement
= If Expression Statement (Maybe Statement)
@@ -45,7 +50,7 @@ data Statement
| Return (Maybe Expression)
| Seq Statement Statement
| Nop
- deriving (Show)
+ deriving (Show, Generic)
data Expression
= Var Name
@@ -55,14 +60,14 @@ data Expression
| Literal Literal
| FunCall Name [Expression]
| Tuple Expression Expression
- deriving (Show)
+ deriving (Show, Generic)
data Field
= Hd
| Tl
| Fst
| Snd
- deriving (Show)
+ deriving (Show, Generic)
data Op2
= Add
@@ -79,16 +84,27 @@ data Op2
| And
| Or
| Cons
- deriving (Show)
+ deriving (Show, Generic)
data Op1
= Not
| Neg
- deriving (Show)
+ deriving (Show, Generic)
data Literal
= LInt Int
| LChar Char
| LBool Bool
| LNil
- deriving (Show)
+ deriving (Show, Generic)
+
+instance Serialize Program
+instance Serialize Function
+instance Serialize Variable
+instance Serialize Type
+instance Serialize Statement
+instance Serialize Expression
+instance Serialize Field
+instance Serialize Op2
+instance Serialize Op1
+instance Serialize Literal