-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: George Thomas <[email protected]>
- Loading branch information
Showing
16 changed files
with
600 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.