Skip to content

Commit bde629b

Browse files
committed
Add location information to subTerm error message
1 parent 1a8b632 commit bde629b

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

src/Juvix/Compiler/Nockma/Evaluator.hs

+6-6
Original file line numberDiff line numberDiff line change
@@ -87,17 +87,17 @@ subTermT = go
8787
L -> (\l' -> TermCell (set cellLeft l' c)) <$> go ds g (c ^. cellLeft)
8888
R -> (\r' -> TermCell (set cellRight r' c)) <$> go ds g (c ^. cellRight)
8989

90-
subTerm :: (Members '[Reader EvalCtx, Error (NockEvalError a)] r) => Term a -> Path -> Sem r (Term a)
91-
subTerm term pos =
90+
subTerm :: (Members '[Reader EvalCtx, Error (NockEvalError a)] r) => Term a -> Path -> Maybe Interval -> Sem r (Term a)
91+
subTerm term pos posLoc =
9292
case term ^? subTermT pos of
93-
Nothing -> throwInvalidPath term pos
93+
Nothing -> throwInvalidPath posLoc term pos
9494
Just t -> return t
9595

9696
setSubTerm :: forall a r. (Members '[Error (NockEvalError a), Reader EvalCtx] r) => Term a -> Path -> Term a -> Sem r (Term a)
9797
setSubTerm term pos repTerm =
9898
let (old, new) = setAndRemember (subTermT' pos) repTerm term
9999
in if
100-
| isNothing (getFirst old) -> throwInvalidPath term pos
100+
| isNothing (getFirst old) -> throwInvalidPath Nothing term pos
101101
| otherwise -> return new
102102

103103
parseCell ::
@@ -435,7 +435,7 @@ evalProfile inistack initerm =
435435
goOpAddress :: Sem r (Term a)
436436
goOpAddress = do
437437
cr <- withCrumb (crumb crumbDecodeFirst) (asPath (c ^. operatorCellTerm))
438-
withCrumb (crumb crumbEval) (subTerm stack cr)
438+
withCrumb (crumb crumbEval) (subTerm stack cr (c ^. operatorCellTerm . termLoc))
439439

440440
goOpQuote :: Term a
441441
goOpQuote = c ^. operatorCellTerm
@@ -517,7 +517,7 @@ evalProfile inistack initerm =
517517
cellTerm <- withCrumb (crumb crumbDecodeFirst) (asCell (c ^. operatorCellTerm))
518518
r <- withCrumb (crumb crumbDecodeSecond) (asPath (cellTerm ^. cellLeft))
519519
t' <- evalArg crumbEvalFirst stack (cellTerm ^. cellRight)
520-
subTerm t' r >>= evalArg crumbEvalSecond t'
520+
subTerm t' r (cellTerm ^. cellLeft . termLoc) >>= evalArg crumbEvalSecond t'
521521

522522
goOpSequence :: Sem r (Term a)
523523
goOpSequence = do

src/Juvix/Compiler/Nockma/Evaluator/Error.hs

+8-5
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ data ExpectedAtom a = ExpectedAtom
3939
data InvalidPath a = InvalidPath
4040
{ _invalidPathCtx :: EvalCtx,
4141
_invalidPathTerm :: Term a,
42-
_invalidPathPath :: Path
42+
_invalidPathPath :: Path,
43+
_invalidPathLocation :: Maybe Interval
4344
}
4445

4546
data KeyNotInStorage a = KeyNotInStorage
@@ -76,15 +77,16 @@ throwInvalidNockOp a = do
7677
_invalidNockOp = a
7778
}
7879

79-
throwInvalidPath :: (Members '[Error (NockEvalError a), Reader EvalCtx] r) => Term a -> Path -> Sem r x
80-
throwInvalidPath tm p = do
80+
throwInvalidPath :: (Members '[Error (NockEvalError a), Reader EvalCtx] r) => Maybe Interval -> Term a -> Path -> Sem r x
81+
throwInvalidPath mi tm p = do
8182
ctx <- ask
8283
throw $
8384
ErrInvalidPath
8485
InvalidPath
8586
{ _invalidPathCtx = ctx,
8687
_invalidPathTerm = tm,
87-
_invalidPathPath = p
88+
_invalidPathPath = p,
89+
_invalidPathLocation = mi
8890
}
8991

9092
throwExpectedCell :: (Members '[Error (NockEvalError a), Reader EvalCtx] r) => Atom a -> Sem r x
@@ -147,7 +149,8 @@ instance (PrettyCode a, NockNatural a) => PrettyCode (InvalidPath a) where
147149
ctx <- ppCtx _invalidPathCtx
148150
path <- ppCode _invalidPathPath
149151
tm <- ppCode _invalidPathTerm
150-
return (ctx <> "The path" <+> path <+> "is invalid for the following term:" <> line <> tm)
152+
loc <- mapM ppCode _invalidPathLocation
153+
return (ctx <> "The path" <+> path <+> "is invalid for the following term:" <> line <> tm <>? ((line <>) <$> loc))
151154

152155
instance (PrettyCode a, NockNatural a) => PrettyCode (ExpectedAtom a) where
153156
ppCode ExpectedAtom {..} = do

0 commit comments

Comments
 (0)