Skip to content

Commit 06b3aa0

Browse files
author
Sjoerd Visscher
committed
Allow a name and multiple pixel letters per type
Fixes #33
1 parent aa27cfa commit 06b3aa0

3 files changed

Lines changed: 36 additions & 29 deletions

File tree

src/InferType.purs

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import Data.Map (Map)
1414
import Data.Maybe (Maybe(..), maybe)
1515
import Data.Newtype (alaF)
1616
import Data.String (trim, split, joinWith)
17+
import Data.String.CodeUnits (singleton, toCharArray)
1718
import Data.String.Pattern (Pattern(..))
1819
import Data.String.Regex (regex, match)
1920
import Data.String.Regex.Flags (noFlags)
@@ -74,7 +75,7 @@ inferType ctx tm = { term, bounds, matches: matches <> typeToMatches, errors }
7475
replaceInferredType bounds $
7576
(acc <> step <> empty { bounds = bounds, matches = [Matched matches] })
7677
{ type = Ty (a <#> replaceBoxed bounds) (c <#> replaceBoxed bounds) }
77-
tmWithDecl = tm # traverse (\{ bid, box } -> Map.lookup bid ctx # maybe (Left $ "Undeclared name: " <> show bid) \decl -> Right { bid, box, decl })
78+
tmWithDecl = tm # traverse (\{ bid, box } -> Map.lookup bid ctx # maybe (Left $ "Undeclared name: " <> show bid) \decl -> Right { bid, box, decl: decl.type })
7879
fatTerm = tmWithDecl # either (const (Fix (Ann empty TUnit))) (reannotateFix alg)
7980
typeToMatches = case (getAnn fatTerm).type of Ty l r -> [Unmatched Valid Input l, Unmatched Valid Output r]
8081
{ bounds, matches, errors } = getAnn fatTerm
@@ -145,7 +146,7 @@ getSubTerm' _ _ mkTerm = mkTerm []
145146

146147

147148
defaultEnv :: Context String String
148-
defaultEnv = Map.fromFoldable
149+
defaultEnv = Map.fromFoldable $ map (\(name /\ ty) -> name /\ { name, type: ty })
149150
[ " " /\ Perm []
150151
, "-" /\ Perm [1]
151152
, "=" /\ Perm [1, 2]
@@ -167,22 +168,28 @@ parseContext = spl "\n" >>> alaF App foldMap toEntry
167168
where
168169
spl :: String -> String -> Array String
169170
spl p = trim >>> split (Pattern p) >>> map trim
171+
parseName :: String -> { name :: String, bids :: Array String }
172+
parseName nameBid = case nameBid # spl "@" of
173+
[name, bids] -> { name, bids: toCharArray bids <#> singleton }
174+
_ -> { name: nameBid, bids: toCharArray nameBid <#> singleton }
170175
toEntry :: String -> Either String (Context String String)
171176
toEntry line = case spl ":" line of
172-
[name, typ] -> case typ # spl "->" <#> (spl " " >>> filter (_ /= "")) of
173-
[left, right] -> pure $ Map.singleton name (Gen $ Ty left right)
174-
_ -> do
175-
permRe <- regex "^\\[(.*)\\]$" noFlags
176-
case match permRe typ # map toArray of
177-
Just [_, Just perm] ->
178-
pure $ Map.singleton name (Perm $ perm # (spl " " >>> filter (_ /= "") >>> map (readInt 10 >>> floor)))
179-
_ -> do
180-
spiderRe <- regex "^(\\d+)(o|\\.)(\\d+)$" noFlags
181-
case match spiderRe typ # map toArray of
182-
Just [_, Just ls, Just cs, Just rs] -> pure $ Map.singleton name (Spider
183-
(if cs == "." then Black else White)
184-
(readInt 10 ls # floor)
185-
(readInt 10 rs # floor)
186-
)
187-
_ -> Left $ "Invalid type: " <> typ
177+
[nameBid, typ] -> let { name, bids } = parseName nameBid in
178+
(\ty -> foldMap (\bid -> Map.singleton bid { name, type: ty }) bids) <$>
179+
case typ # spl "->" <#> (spl " " >>> filter (_ /= "")) of
180+
[left, right] -> pure $ Gen $ Ty left right
181+
_ -> do
182+
permRe <- regex "^\\[(.*)\\]$" noFlags
183+
case match permRe typ # map toArray of
184+
Just [_, Just perm] ->
185+
pure (Perm $ perm # (spl " " >>> filter (_ /= "") >>> map (readInt 10 >>> floor)))
186+
_ -> do
187+
spiderRe <- regex "^(\\d+)(o|\\.)(\\d+)$" noFlags
188+
case match spiderRe typ # map toArray of
189+
Just [_, Just ls, Just cs, Just rs] -> pure (Spider
190+
(if cs == "." then Black else White)
191+
(readInt 10 ls # floor)
192+
(readInt 10 rs # floor)
193+
)
194+
_ -> Left $ "Invalid type: " <> typ
188195
_ -> Left $ "Invalid signature: " <> line

src/Model.purs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ isGen :: ∀ bv. TypeDecl bv -> Boolean
138138
isGen (Gen _) = true
139139
isGen _ = false
140140

141-
type Context bv bid = Map bid (TypeDecl bv)
141+
type Context bv bid = Map bid { name :: String, type :: TypeDecl bv }
142142

143143

144144
data Var bv

src/View/Bricks.purs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -116,45 +116,45 @@ render { input: { bricks: { width, height, boxes }, matches, context, selectedBo
116116
] ]
117117
]
118118

119-
renderBrick :: m. InputOutput String -> Maybe (TypeDecl String) -> Brick String
119+
renderBrick :: m. InputOutput String -> Maybe { name :: String, type :: TypeDecl String } -> Brick String
120120
-> { className :: String, content :: Array (H.ComponentHTML Action () m) }
121-
renderBrick io (Just (Gen _)) b@{ box } =
121+
renderBrick io (Just { name, type: Gen _ }) b@{ box } =
122122
{ className: "box"
123123
, content:
124-
renderBox b
124+
renderBox name box
125125
<> maybe [] (foldMap (renderLines genLineSettings Input b)) (lookup (box /\ Input) io)
126126
<> maybe [] (foldMap (renderLines genLineSettings Output b)) (lookup (box /\ Output) io)
127127
}
128-
renderBrick io (Just (Perm perm)) b = { className: "wires", content: renderPerm io b perm }
129-
renderBrick io (Just (Spider c _ _)) b@{ box } =
128+
renderBrick io (Just { type: Perm perm }) b = { className: "wires", content: renderPerm io b perm }
129+
renderBrick io (Just { type: Spider c _ _ }) b@{ box } =
130130
{ className: "wires"
131131
, content:
132132
maybe [] (foldMap (renderLines spiderLineSettings Input b)) (lookup (box /\ Input) io) <>
133133
maybe [] (foldMap (renderLines spiderLineSettings Output b)) (lookup (box /\ Output) io) <>
134134
renderNode b c
135135
}
136-
renderBrick io (Just Cup) b@{ box } =
136+
renderBrick io (Just { type: Cup }) b@{ box } =
137137
{ className: "wires"
138138
, content:
139139
maybe [] (foldMap (renderLines cupcapLineSettings Input b)) (lookup (box /\ Input) io) <>
140140
maybe [] (foldMap (renderLines cupcapLineSettings Output b)) (lookup (box /\ Output) io)
141141
}
142-
renderBrick io (Just Cap) b@{ box } =
142+
renderBrick io (Just { type: Cap }) b@{ box } =
143143
{ className: "wires"
144144
, content:
145145
maybe [] (foldMap (renderLines cupcapLineSettings Input b)) (lookup (box /\ Input) io) <>
146146
maybe [] (foldMap (renderLines cupcapLineSettings Output b)) (lookup (box /\ Output) io)
147147
}
148148
renderBrick _ Nothing _ = { className: "box", content: [] }
149149

150-
renderBox :: m. Brick String -> Array (H.ComponentHTML Action () m)
151-
renderBox { bid, box: { topLeft: { x: xl, y: yt }, bottomRight: { x: xr, y: yb } } } =
150+
renderBox :: m. String -> Box -> Array (H.ComponentHTML Action () m)
151+
renderBox name { topLeft: { x: xl, y: yt }, bottomRight: { x: xr, y: yb } } =
152152
[ S.rect [ S.x (mx - 0.18), S.y (my - 0.25), S.width 0.36, S.height 0.5, svgClasses [ ClassName "inner-box" ] ]
153153
, S.text
154154
[ S.x mx, S.y (my + 0.12)
155155
, S.attr (AttrName "text-anchor") "middle"
156156
, svgClasses [ ClassName "inner-box-text" ]
157-
] [ text bid ]
157+
] [ text name, sub_ [ text "1" ] ]
158158
]
159159
where
160160
mx = (toNumber xl + toNumber xr) / 2.0

0 commit comments

Comments
 (0)