Add break in each case alt in js backend (#1796)

* Add `break` in each case alt in js backend
Fixes #1795

* Remove some uneeded `break`s

* linter

* Follow @stefan-hoeck 's advice
This is neater
Note: I renamed breakAfterAssignment because it's too much work to type

* [ test ] Test for #1795

* cleanup: remove unneeded vcat
This commit is contained in:
Zoe Stafford 2021-07-30 07:16:23 +01:00 committed by GitHub
parent 4920601fe9
commit 8e1ca0eddf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 36 additions and 14 deletions

View File

@ -583,6 +583,10 @@ lambdaArgs : List Var -> Doc
lambdaArgs [] = "()" <+> lambdaArrow
lambdaArgs xs = hcat $ (<+> lambdaArrow) . var <$> xs
insertBreak : (r : Effect) -> (Doc, Doc) -> (Doc, Doc)
insertBreak Returns x = x
insertBreak (ErrorWithout _) (pat, exp) = (pat, vcat [exp, "break;"])
mutual
-- converts an `Exp` to JS code
exp : {auto c : Ref ESs ESSt} -> Exp -> Core Doc
@ -610,28 +614,31 @@ mutual
stmt (Declare v s) =
(\d => vcat ["let" <++> var v <+> ";",d]) <$> stmt s
stmt (Assign v x) =
(\d => vcat [hcat [var v,softEq,d,";"], "break;"]) <$> exp x
(\d => hcat [var v,softEq,d,";"]) <$> exp x
stmt (ConSwitch r sc alts def) = do
as <- traverse alt alts
as <- traverse (map (insertBreak r) . alt) alts
d <- traverseOpt stmt def
pure $ switch (minimal sc <+> ".h") as d
where alt : EConAlt r -> Core (Doc,Doc)
alt (MkEConAlt _ RECORD b) = ("undefined",) <$> stmt b
alt (MkEConAlt _ NIL b) = ("0",) <$> stmt b
alt (MkEConAlt _ CONS b) = ("undefined",) <$> stmt b
alt (MkEConAlt _ NOTHING b) = ("0",) <$> stmt b
alt (MkEConAlt _ JUST b) = ("undefined",) <$> stmt b
alt (MkEConAlt t _ b) = (tag2es t,) <$> stmt b
where
alt : {r : _} -> EConAlt r -> Core (Doc,Doc)
alt (MkEConAlt _ RECORD b) = ("undefined",) <$> stmt b
alt (MkEConAlt _ NIL b) = ("0",) <$> stmt b
alt (MkEConAlt _ CONS b) = ("undefined",) <$> stmt b
alt (MkEConAlt _ NOTHING b) = ("0",) <$> stmt b
alt (MkEConAlt _ JUST b) = ("undefined",) <$> stmt b
alt (MkEConAlt t _ b) = (tag2es t,) <$> stmt b
stmt (ConstSwitch r sc alts def) = do
as <- traverse alt alts
as <- traverse (map (insertBreak r) . alt) alts
d <- traverseOpt stmt def
ex <- exp sc
pure $ switch ex as d
where alt : EConstAlt r -> Core (Doc,Doc)
alt (MkEConstAlt c b) = do d <- stmt b
pure (Text $ jsConstant c, d)
where
alt : EConstAlt r -> Core (Doc,Doc)
alt (MkEConstAlt c b) = do
d <- stmt b
pure (Text $ jsConstant c, d)
stmt (Error x) = pure $ jsCrashExp (jsStringDoc x) <+> ";"
stmt (Block ss s) = do

View File

@ -251,7 +251,7 @@ nodeTests = MkTestPool "Node backend" [] (Just Node)
[ "node001", "node002", "node003", "node004", "node005", "node006"
, "node007", "node008", "node009", "node011", "node012", "node015"
, "node017", "node018", "node019", "node021", "node022", "node023"
, "node024", "node025"
, "node024", "node025", "node026"
, "perf001"
-- , "node14", "node020"
, "args"

View File

@ -0,0 +1,6 @@
import Data.String.Parser
main : IO ()
main = do
let res = parse (satisfy isDigit) "100"
printLn res

View File

@ -0,0 +1,3 @@
1/1: Building Fix1795 (Fix1795.idr)
Main> Right ('1', 1)
Main> Bye for now!

2
tests/node/node026/input Normal file
View File

@ -0,0 +1,2 @@
:exec main
:q

4
tests/node/node026/run Normal file
View File

@ -0,0 +1,4 @@
rm -rf build
$1 -p contrib --cg node --no-banner --no-color --console-width 0 Fix1795.idr < input