-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
handler2 - new custom method SubstituteRedex
- Loading branch information
Vince
committed
Feb 20, 2024
1 parent
d44bc48
commit 6c14663
Showing
2 changed files
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
|
||
module Server.Handler2.CustomMethod.SubstituteRedex (handler) where | ||
|
||
import qualified Syntax.Abstract as A | ||
import Error (Error(..)) | ||
|
||
import Server.Monad (ServerM, LoadedProgram(..)) | ||
import Server.CustomMethod (ResKind(..)) | ||
import Server.Handler2.Utils | ||
import Server.Handler2.CustomMethod.Utils | ||
|
||
|
||
import qualified GCL.Substitution as Substitution | ||
|
||
handler :: FilePath -> Int -> ([ResKind] -> ServerM ()) -> (Error -> ServerM ()) -> ServerM () | ||
handler filePath redexNumber onFinish onError = do | ||
maybeLoadedProgram <- dumpProgram filePath | ||
case maybeLoadedProgram of | ||
Nothing -> onError (Others "Please reload before inspect.") | ||
Just loadedProgram -> do | ||
let redexes :: IntMap (Int, A.Expr) = _redexes loadedProgram | ||
let abstract :: A.Program = _abstractProgram loadedProgram | ||
let variableCounter :: Int = _variableCounter loadedProgram | ||
case IntMap.lookup redexNumber redexes of | ||
Nothing -> onError (Others "Redex not found.") | ||
Just (_, redex) -> do | ||
let scope :: Map Text (Maybe Expr) = A.programToScopeForSubstitution abstract | ||
let (newExpr, variableCounter') = | ||
runState (Substitution.step scope redex) variableCounter | ||
let redexesInNewExpr = Substitution.buildRedexMap newExpr | ||
let loadedProgram' = loadedProgram | ||
{ _variableCounter = variableCounter' | ||
, _redexes = redexes <> redexesInNewExpr | ||
} | ||
_ <- cacheLoadedProgram filePath loadedProgram' | ||
onFinish [ResSubstitute i (render newExpr)] |