Skip to content

Commit

Permalink
Add location information to subTerm error message
Browse files Browse the repository at this point in the history
  • Loading branch information
paulcadman committed Nov 6, 2024
1 parent 1a8b632 commit bde629b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
12 changes: 6 additions & 6 deletions src/Juvix/Compiler/Nockma/Evaluator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,17 @@ subTermT = go
L -> (\l' -> TermCell (set cellLeft l' c)) <$> go ds g (c ^. cellLeft)
R -> (\r' -> TermCell (set cellRight r' c)) <$> go ds g (c ^. cellRight)

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

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

parseCell ::
Expand Down Expand Up @@ -435,7 +435,7 @@ evalProfile inistack initerm =
goOpAddress :: Sem r (Term a)
goOpAddress = do
cr <- withCrumb (crumb crumbDecodeFirst) (asPath (c ^. operatorCellTerm))
withCrumb (crumb crumbEval) (subTerm stack cr)
withCrumb (crumb crumbEval) (subTerm stack cr (c ^. operatorCellTerm . termLoc))

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

goOpSequence :: Sem r (Term a)
goOpSequence = do
Expand Down
13 changes: 8 additions & 5 deletions src/Juvix/Compiler/Nockma/Evaluator/Error.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ data ExpectedAtom a = ExpectedAtom
data InvalidPath a = InvalidPath
{ _invalidPathCtx :: EvalCtx,
_invalidPathTerm :: Term a,
_invalidPathPath :: Path
_invalidPathPath :: Path,
_invalidPathLocation :: Maybe Interval
}

data KeyNotInStorage a = KeyNotInStorage
Expand Down Expand Up @@ -76,15 +77,16 @@ throwInvalidNockOp a = do
_invalidNockOp = a
}

throwInvalidPath :: (Members '[Error (NockEvalError a), Reader EvalCtx] r) => Term a -> Path -> Sem r x
throwInvalidPath tm p = do
throwInvalidPath :: (Members '[Error (NockEvalError a), Reader EvalCtx] r) => Maybe Interval -> Term a -> Path -> Sem r x
throwInvalidPath mi tm p = do
ctx <- ask
throw $
ErrInvalidPath
InvalidPath
{ _invalidPathCtx = ctx,
_invalidPathTerm = tm,
_invalidPathPath = p
_invalidPathPath = p,
_invalidPathLocation = mi
}

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

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

0 comments on commit bde629b

Please sign in to comment.