Skip to content

Commit

Permalink
[ fix ] Parse with-applications at LHS as PWithApp instead of PApp
Browse files Browse the repository at this point in the history
  • Loading branch information
buzden committed Sep 11, 2023
1 parent 7227096 commit f370566
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/Idris/Desugar.idr
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,7 @@ mutual
getClauseFn : RawImp -> Core Name
getClauseFn (IVar _ n) = pure n
getClauseFn (IApp _ f _) = getClauseFn f
getClauseFn (IWithApp _ f _) = getClauseFn f
getClauseFn (IAutoApp _ f _) = getClauseFn f
getClauseFn (INamedApp _ f _ _) = getClauseFn f
getClauseFn tm = throw $ GenericMsg (getFC tm) "Head term in pattern must be a function name"
Expand Down
3 changes: 3 additions & 0 deletions src/Idris/Elab/Implementation.idr
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,9 @@ elabImplementation {vars} ifc vis opts_in pass env nest is cons iname ps named i
updateApp ns (IApp fc f arg)
= do f' <- updateApp ns f
pure (IApp fc f' arg)
updateApp ns (IWithApp fc f arg)
= do f' <- updateApp ns f
pure (IWithApp fc f' arg)
updateApp ns (IAutoApp fc f arg)
= do f' <- updateApp ns f
pure (IAutoApp fc f' arg)
Expand Down
6 changes: 3 additions & 3 deletions src/Idris/Parser.idr
Original file line number Diff line number Diff line change
Expand Up @@ -1177,7 +1177,7 @@ mutual
mustWorkBecause b'.bounds "Not the end of a block entry, check indentation" $ atEnd indents
(rhs, ws) <- pure b.val
let fc = boundToFC fname (mergeBounds start b)
pure (MkPatClause fc (uncurry applyArgs lhs) rhs ws)
pure (MkPatClause fc (uncurry applyWithArgs lhs) rhs ws)
<|> do b <- bounds $ do
decoratedKeyword fname "with"
commit
Expand All @@ -1188,11 +1188,11 @@ mutual
pure (flags, wps, forget ws)
(flags, wps, ws) <- pure b.val
let fc = boundToFC fname (mergeBounds start b)
pure (MkWithClause fc (uncurry applyArgs lhs) wps flags ws)
pure (MkWithClause fc (uncurry applyWithArgs lhs) wps flags ws)
<|> do end <- bounds (decoratedKeyword fname "impossible")
atEnd indents
pure $ let fc = boundToFC fname (mergeBounds start end) in
MkImpossible fc (uncurry applyArgs lhs)
MkImpossible fc (uncurry applyWithArgs lhs)

clause : (withArgs : Nat) ->
IMaybe (isSucc withArgs) (PTerm, List (FC, PTerm)) ->
Expand Down
5 changes: 5 additions & 0 deletions src/Idris/Syntax.idr
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,11 @@ mutual
applyArgs f [] = f
applyArgs f ((fc, a) :: args) = applyArgs (PApp fc f a) args

export
applyWithArgs : PTerm' nm -> List (FC, PTerm' nm) -> PTerm' nm
applyWithArgs f [] = f
applyWithArgs f ((fc, a) :: args) = applyWithArgs (PWithApp fc f a) args

public export
PTypeDecl : Type
PTypeDecl = PTypeDecl' Name
Expand Down
3 changes: 3 additions & 0 deletions src/TTImp/Impossible.idr
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ mutual
= buildApp fc n mty exps autos named
mkTerm (IApp fc fn arg) mty exps autos named
= mkTerm fn mty (arg :: exps) autos named
mkTerm (IWithApp fc fn arg) mty exps autos named
= mkTerm fn mty (arg :: exps) autos named
mkTerm (IAutoApp fc fn arg) mty exps autos named
= mkTerm fn mty exps (arg :: autos) named
mkTerm (INamedApp fc fn nm arg) mty exps autos named
Expand Down Expand Up @@ -196,6 +198,7 @@ getImpossibleTerm env nest tm
-- the name to the proper one from the nested names map
applyEnv : RawImp -> RawImp
applyEnv (IApp fc fn arg) = IApp fc (applyEnv fn) arg
applyEnv (IWithApp fc fn arg) = IWithApp fc (applyEnv fn) arg
applyEnv (IAutoApp fc fn arg) = IAutoApp fc (applyEnv fn) arg
applyEnv (INamedApp fc fn n arg)
= INamedApp fc (applyEnv fn) n arg
Expand Down
5 changes: 4 additions & 1 deletion src/TTImp/Interactive/CaseSplit.idr
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,12 @@ updateArg allvars var con (IVar fc n)
updateArg allvars var con (IApp fc f a)
= pure $ IApp fc !(updateArg allvars var con f)
!(updateArg allvars var con a)
updateArg allvars var con (IWithApp fc f a)
= pure $ IWithApp fc !(updateArg allvars var con f)
!(updateArg allvars var con a)
updateArg allvars var con (IAutoApp fc f a)
= pure $ IAutoApp fc !(updateArg allvars var con f)
!(updateArg allvars var con a)
!(updateArg allvars var con a)
updateArg allvars var con (INamedApp fc f n a)
= pure $ INamedApp fc !(updateArg allvars var con f) n
!(updateArg allvars var con a)
Expand Down
4 changes: 4 additions & 0 deletions src/TTImp/Interactive/GenerateDef.idr
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ splittableNames (IApp _ f (IBindVar _ n))
= splittableNames f ++ [UN $ Basic n]
splittableNames (IApp _ f _)
= splittableNames f
splittableNames (IWithApp _ f (IBindVar _ n))
= splittableNames f ++ [UN $ Basic n]
splittableNames (IWithApp _ f _)
= splittableNames f
splittableNames (IAutoApp _ f _)
= splittableNames f
splittableNames (INamedApp _ f _ _)
Expand Down
4 changes: 4 additions & 0 deletions src/TTImp/WithClause.idr
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ matchFail loc = throw (GenericMsg loc "With clause does not match parent")
getHeadLoc : RawImp -> Core FC
getHeadLoc (IVar fc _) = pure fc
getHeadLoc (IApp _ f _) = getHeadLoc f
getHeadLoc (IWithApp _ f _) = getHeadLoc f
getHeadLoc (IAutoApp _ f _) = getHeadLoc f
getHeadLoc (INamedApp _ f _ _) = getHeadLoc f
getHeadLoc t = throw (InternalError $ "Could not find head of LHS: " ++ show t)
Expand Down Expand Up @@ -210,6 +211,9 @@ getNewLHS iploc drop nest wname wargnames lhs_raw patlhs
dropWithArgs (S k) (IApp _ f arg)
= do (tm, rest) <- dropWithArgs k f
pure (tm, arg :: rest)
dropWithArgs (S k) (IWithApp _ f arg)
= do (tm, rest) <- dropWithArgs k f
pure (tm, arg :: rest)
-- Shouldn't happen if parsed correctly, but there's no guarantee that
-- inputs come from parsed source so throw an error.
dropWithArgs _ _ = throw (GenericMsg iploc "Badly formed 'with' clause")
Expand Down
4 changes: 4 additions & 0 deletions tests/base/ttimp_show001/expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Main> Imported module Language.Reflection
Main> "[f a b with (expr) { f a b | x = one; f a b | y with (expr2) { f a a | y | z = e; f a a | y | zz = ee; f a a | y | zzz impossible } }]"
Main>
Bye for now!
2 changes: 2 additions & 0 deletions tests/base/ttimp_show001/input
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:module Language.Reflection
show `[f a b with (expr) { f a b | x = one; f a b | y with (expr2) { f a a | y | z = e; f a a | y | zz = ee; f a a | y | zzz impossible } }]
3 changes: 3 additions & 0 deletions tests/base/ttimp_show001/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
. ../../testutils.sh

idris2 < input

0 comments on commit f370566

Please sign in to comment.