Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add location information to subTerm error message #3151

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading