Skip to content

Commit

Permalink
feat: Add support for animations
Browse files Browse the repository at this point in the history
Signed-off-by: George Thomas <[email protected]>
  • Loading branch information
georgefst committed Oct 24, 2023
1 parent a482844 commit 0b1e1c8
Show file tree
Hide file tree
Showing 16 changed files with 600 additions and 5 deletions.
2 changes: 2 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,8 @@
# https://github.com/input-output-hk/haskell.nix/issues/1242
packages.mtl-compat.writeHieFiles = false;
packages.bytestring-builder.writeHieFiles = false;
packages.fail.writeHieFiles = false;
packages.diagrams.writeHieFiles = false;
}
{
#TODO This shouldn't be necessary - see the commented-out `build-tool-depends` in primer.cabal.
Expand Down
18 changes: 18 additions & 0 deletions primer-service/test/outputs/OpenAPI/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,24 @@
"contents"
],
"type": "object"
},
{
"properties": {
"contents": {
"type": "string"
},
"tag": {
"enum": [
"PrimAnimation"
],
"type": "string"
}
},
"required": [
"tag",
"contents"
],
"type": "object"
}
]
},
Expand Down
1 change: 1 addition & 0 deletions primer/gen/Primer/Gen/Core/Raw.hs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ genPrimCon =
_ = \case
PrimChar _ -> ()
PrimInt _ -> ()
PrimAnimation _ -> ()

genType :: ExprGen Type
genType =
Expand Down
3 changes: 2 additions & 1 deletion primer/gen/Primer/Gen/Core/Typed.hs
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ genChk ty = do
brs0 <- Gen.list (Range.linear 0 5) $ do
p <- pg
(p,) . CaseBranch (PatPrim p) [] <$> genChk ty
let brs = nubSortOn ((\case PrimInt n -> Left n; PrimChar c -> Right c) . fst) brs0
let brs = nubSortOn ((\case PrimInt n -> Left (Left n); PrimChar c -> Left (Right c); PrimAnimation b -> Right b) . fst) brs0
fb <- genChk ty
pure $ Case () e (snd <$> brs) (CaseFallback fb)

Expand Down Expand Up @@ -676,6 +676,7 @@ genPrimCon = catMaybes <$> sequence [whenInScope PrimChar 'a' genChar, whenInSco
_ = \case
PrimChar _ -> ()
PrimInt _ -> ()
PrimAnimation _ -> ()

-- We bias the distribution towards a small set, to make it more likely we
-- generate name clashes on occasion
Expand Down
4 changes: 4 additions & 0 deletions primer/primer.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ library
Primer.App.Utils
Primer.Builtins
Primer.Builtins.DSL
Primer.Builtins.Picture
Primer.Core
Primer.Core.DSL
Primer.Core.Meta
Expand Down Expand Up @@ -109,8 +110,11 @@ library
, aeson >=2.0 && <2.2
, assoc ^>=1.1
, base >=4.12 && <4.19
, base64-bytestring ^>=1.2.1
, containers >=0.6.0.1 && <0.7.0
, deriving-aeson >=0.2 && <0.3.0
, diagrams-lib ^>=1.4.6
, diagrams-rasterific ^>=1.4.2
, exceptions >=0.10.4 && <0.11.0
, extra >=1.7.10 && <1.8.0
, generic-optics >=2.0 && <2.3.0
Expand Down
43 changes: 43 additions & 0 deletions primer/src/Primer/Builtins.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ module Primer.Builtins (
cRight,
eitherDef,
builtinModuleName,
tPicture,
cCircle,
cRect,
cColour,
cRotate,
cTranslate,
cCompoundPicture,
pictureDef,
) where

import Primer.Core.Meta (
Expand All @@ -50,6 +58,21 @@ builtinModuleName = mkSimpleModuleName "Builtins"
builtin :: Name -> GlobalName k
builtin = qualifyName builtinModuleName

tPicture :: TyConName
tPicture = builtin "Picture"
cCircle :: ValConName
cCircle = builtin "Circle"
cRect :: ValConName
cRect = builtin "Rectangle"
cColour :: ValConName
cColour = builtin "Colour"
cRotate :: ValConName
cRotate = builtin "Rotate"
cTranslate :: ValConName
cTranslate = builtin "Translate"
cCompoundPicture :: ValConName
cCompoundPicture = builtin "Compound"

tBool :: TyConName
tBool = builtin "Bool"
cTrue, cFalse :: ValConName
Expand Down Expand Up @@ -151,3 +174,23 @@ eitherDef =
, astTypeDefConstructors = [ValCon cLeft [TVar () "a"], ValCon cRight [TVar () "b"]]
, astTypeDefNameHints = []
}

pictureDef :: ASTTypeDef () ()
pictureDef =
ASTTypeDef
{ astTypeDefParameters = []
, astTypeDefConstructors =
[ ValCon cCircle [TCon () tInt]
, ValCon cRect [TCon () tInt, TCon () tInt]
, ValCon cColour [TCon () tInt, TCon () tInt, TCon () tInt, TCon () tPicture]
, ValCon cRotate [TCon () tInt, TCon () tPicture]
, ValCon cTranslate [TCon () tInt, TCon () tInt, TCon () tPicture]
, -- Pictures are ordered foreground to background, i.e. those earlier in the list appear on top.
ValCon cCompoundPicture [TApp () (TCon () tList) (TCon () tPicture)]
]
, astTypeDefNameHints = []
}
where
-- TODO This can't currently be imported due to cyclic module issues.
tInt :: TyConName
tInt = qualifyName (mkSimpleModuleName "Primitives") "Int"
55 changes: 55 additions & 0 deletions primer/src/Primer/Builtins/Picture.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{-# LANGUAGE ViewPatterns #-}

-- | A Haskell model of our built-in `Picture` type.
-- Using this type can make working with pictures more convenient,
-- including by giving us compile-time exhaustiveness checks.
module Primer.Builtins.Picture (Picture (..), exprToPicture) where

import Foreword

import Primer.Builtins (
cCircle,
cColour,
cCompoundPicture,
cCons,
cNil,
cRect,
cRotate,
cTranslate,
)
import Primer.Core (Expr' (..), PrimCon (..))

data Picture
= Circle Integer
| Rect Integer Integer
| Colour Integer Integer Integer Picture
| Rotate Integer Picture
| Translate Integer Integer Picture
| CompoundPicture [Picture]

exprToPicture :: Expr' a b c -> Maybe Picture
exprToPicture = \case
Con _ c [PrimCon _ (PrimInt r)]
| c == cCircle ->
Just $ Circle r
Con _ c [PrimCon _ (PrimInt w), PrimCon _ (PrimInt h)]
| c == cRect ->
Just $ Rect w h
Con _ c [PrimCon _ (PrimInt r), PrimCon _ (PrimInt g), PrimCon _ (PrimInt b), exprToPicture -> Just p]
| c == cColour ->
Just $ Colour r g b p
Con _ c [PrimCon _ (PrimInt a), exprToPicture -> Just p]
| c == cRotate ->
Just $ Rotate a p
Con _ c [PrimCon _ (PrimInt x), PrimCon _ (PrimInt y), exprToPicture -> Just p]
| c == cTranslate ->
Just $ Translate x y p
Con _ c [exprToList -> Just (traverse exprToPicture -> Just ps)]
| c == cCompoundPicture ->
Just $ CompoundPicture ps
_ -> Nothing
where
exprToList = \case
Con _ c [] | c == cNil -> Just []
Con _ c [x, exprToList -> Just xs] | c == cCons -> Just $ x : xs
_ -> Nothing
2 changes: 2 additions & 0 deletions primer/src/Primer/Core/Meta.hs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ instance HasMetadata (Meta a) where
data PrimCon
= PrimChar Char
| PrimInt Integer
| -- | Contains a base-64 encoding of an animated GIF.
PrimAnimation Text
deriving stock (Eq, Show, Read, Data, Generic)
deriving (FromJSON, ToJSON) via PrimerJSON PrimCon
deriving anyclass (NFData)
Expand Down
4 changes: 4 additions & 0 deletions primer/src/Primer/Module.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ import Primer.Builtins (
maybeDef,
natDef,
pairDef,
pictureDef,
tBool,
tEither,
tList,
tMaybe,
tNat,
tPair,
tPicture,
)
import Primer.Core (
GVarName,
Expand Down Expand Up @@ -148,6 +150,7 @@ builtinModule = do
maybeDef' <- generateTypeDefIDs $ TypeDefAST maybeDef
pairDef' <- generateTypeDefIDs $ TypeDefAST pairDef
eitherDef' <- generateTypeDefIDs $ TypeDefAST eitherDef
pictureDef' <- generateTypeDefIDs $ TypeDefAST pictureDef
pure
$ Module
{ moduleName = builtinModuleName
Expand All @@ -159,6 +162,7 @@ builtinModule = do
, (baseName tMaybe, maybeDef')
, (baseName tPair, pairDef')
, (baseName tEither, eitherDef')
, (baseName tPicture, pictureDef')
]
, moduleDefs = mempty
}
Expand Down
1 change: 1 addition & 0 deletions primer/src/Primer/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ prettyExpr opts = \case
prim = \case
PrimChar c -> "Char" <+> pretty @Text (show c)
PrimInt n -> "Int" <+> pretty @Text (show n)
PrimGlossProg n -> pretty @Text (show n)
typeann e t = brac Round Yellow (pE e) <+> col Yellow "::" <> line <> brac Round Yellow (pT t)

-- When grouped: " x "
Expand Down
Loading

0 comments on commit 0b1e1c8

Please sign in to comment.