Tests run again.

This commit is contained in:
Erik Svedäng 2018-11-14 21:21:56 +01:00
parent 30fff46963
commit cd96ccbf39
9 changed files with 36 additions and 14 deletions

View File

@ -5,8 +5,8 @@
(register find (Fn [&Pattern &String] Int))
(doc find-all "Finds all indices of a pattern in a string. Returns [] otherwise.")
(register find-all (Fn [&Pattern &String] (Array Int)))
(doc match "Finds the match groups of the first match of a pattern in a string. Returns [] otherwise.")
(register match (Fn [&Pattern &String] (Array String)))
(doc match-groups "Finds the match groups of the first match of a pattern in a string. Returns [] otherwise.")
(register match-groups (Fn [&Pattern &String] (Array String)))
(doc match-str "Finds the first match of a pattern in a string. Returns [] otherwise.")
(register match-str (Fn [&Pattern &String] String))
(doc global-match "Finds all matches of a pattern in a string as a nested array. Returns [] otherwise.")

View File

@ -492,7 +492,7 @@ Array Pattern_find_MINUS_all(Pattern* p, String* s) {
}
Array Pattern_match(Pattern* p, String* s) {
Array Pattern_match_MINUS_groups(Pattern* p, String* s) {
String str = *s;
Pattern pat = *p;
int lstr = strlen(str);

View File

@ -281,9 +281,9 @@
</p>
</div>
<div class="binder">
<a class="anchor" href="#match">
<h3 id="match">
match
<a class="anchor" href="#match-groups">
<h3 id="match-groups">
match-groups
</h3>
</a>
<div class="description">

View File

@ -14,14 +14,13 @@
(defn g []
(Flip.B true))
;; (defn f []
;; ())
;; (deftype (Blah t)
;; [x Int])
(defn m []
(Maybe.Just 123))
(defn n []
(the (Maybe Int) (Maybe.Nothing)))
(defn main []
(match (Flip.A 100)
(A a) 1
(B b) 2))

View File

@ -77,6 +77,10 @@ genConstraints root = fmap sort (gen root)
wholeStatementConstraint : insideConditionConstraints ++
insideTrueConstraints ++ insideFalseConstraints)
-- Match
XObj Match _ _ : expr : cases ->
return []
-- While
[XObj While _ _, expr, body] ->
do insideConditionConstraints <- gen expr

View File

@ -233,6 +233,22 @@ initialTypes typeEnv rootEnv root = evalState (visit rootEnv root) 0
XObj If _ _ : _ -> return (Left (InvalidObj If xobj))
-- Match
matchExpr@(XObj Match _ _) : expr : cases ->
do visitedExpr <- visit env expr
visitedCases <- fmap sequence $ mapM (\(tag, x) -> do visitedX <- visit env x
case visitedX of
Left e -> return (Left e)
Right okX -> return (Right (tag, okX)))
(pairwise cases)
returnType <- genVarTy
return $ do okExpr <- visitedExpr
okCases <- visitedCases
return (XObj (Lst ([matchExpr, okExpr] ++ concatMap (\(a, b) -> [a, b]) okCases))
i (Just returnType))
XObj Match _ _ : _ -> return (Left (InvalidObj Match xobj))
-- While (always return Unit)
[whileExpr@(XObj While _ _), expr, body] ->
do visitedExpr <- visit env expr

View File

@ -63,6 +63,7 @@ data Obj = Sym SymPath SymbolMode
| While
| Break
| If
| Match
| Mod Env
| Typ Ty -- TODO: Rename to Deftype!
| DefSumtype Ty
@ -268,6 +269,7 @@ pretty = visit 0
Def -> "def"
Fn _ captures -> "fn" -- ++ " <" ++ joinWithComma (map getName (Set.toList captures)) ++ ">"
If -> "if"
Match -> "match"
While -> "while"
Do -> "do"
Let -> "let"

View File

@ -224,6 +224,7 @@ symbol = do i <- createInfo
"let" -> return (XObj Let i Nothing)
"break" -> return (XObj Break i Nothing)
"if" -> return (XObj If i Nothing)
"match" -> return (XObj Match i Nothing)
"true" -> return (XObj (Bol True) i Nothing)
"false" -> return (XObj (Bol False) i Nothing)
"address" -> return (XObj Address i Nothing)

View File

@ -41,11 +41,11 @@
"find-all works as expected if not found")
(assert-equal test
&[@"12"]
&(match #"(\d+)" " 12")
&(match-groups #"(\d+)" " 12")
"match works as expected")
(assert-equal test
&[]
&(match #"(\d+)" " ")
&(match-groups #"(\d+)" " ")
"match works as expected if not found")
(assert-equal test
true