diff options
author | Josh Rahm <joshuarahm@gmail.com> | 2024-10-05 17:13:26 -0600 |
---|---|---|
committer | Josh Rahm <joshuarahm@gmail.com> | 2024-10-05 17:13:26 -0600 |
commit | 3ceedaf5f5193fadadcb011c40df1688cfed279d (patch) | |
tree | 772c8a0c607d68e287addc59bdde71172edd10b1 /src/Language/Fiddle/Compiler.hs | |
parent | 407e41489cc22fbf0518fd370530f8857b8c3ed0 (diff) | |
download | fiddle-3ceedaf5f5193fadadcb011c40df1688cfed279d.tar.gz fiddle-3ceedaf5f5193fadadcb011c40df1688cfed279d.tar.bz2 fiddle-3ceedaf5f5193fadadcb011c40df1688cfed279d.zip |
Implement qualification.
Big change. Implements qualification, which separates the qualification
concerns from the ConsistencyCheck phase.
I'm getting close to implementing a backend.
Diffstat (limited to 'src/Language/Fiddle/Compiler.hs')
-rw-r--r-- | src/Language/Fiddle/Compiler.hs | 43 |
1 files changed, 36 insertions, 7 deletions
diff --git a/src/Language/Fiddle/Compiler.hs b/src/Language/Fiddle/Compiler.hs index c029765..049d533 100644 --- a/src/Language/Fiddle/Compiler.hs +++ b/src/Language/Fiddle/Compiler.hs @@ -6,6 +6,7 @@ module Language.Fiddle.Compiler where import Control.Arrow import Control.Monad (when) +import Control.Monad.Identity (Identity) import Control.Monad.RWS import Control.Monad.Trans.Maybe import Control.Monad.Writer @@ -21,6 +22,15 @@ data Level = Error | Warning | Info data Diagnostic = Diagnostic Level String SourceSpan +emitDiagnosticError :: String -> Commented SourceSpan -> Compile a () +emitDiagnosticError str a = tell [Diagnostic Error str (unCommented a)] + +emitDiagnosticWarning :: String -> Commented SourceSpan -> Compile a () +emitDiagnosticWarning str a = tell [Diagnostic Warning str (unCommented a)] + +emitDiagnosticInfo :: String -> Commented SourceSpan -> Compile a () +emitDiagnosticInfo str a = tell [Diagnostic Info str (unCommented a)] + newtype Compile s a = Compile (MaybeT (RWS () [Diagnostic] s) a) deriving newtype (Functor, Applicative, Monad) @@ -103,13 +113,13 @@ pureCompilationPhase :: (CompilationStage stageFrom) => ( FiddleUnit stageFrom - (StageFunctor stageFrom) + Identity (StageAnnotation stageFrom) -> Compile () ( FiddleUnit stageTo - (StageFunctor stageTo) + Identity (StageAnnotation stageTo) ) ) -> @@ -138,7 +148,7 @@ data CompilationPhase stageFrom stageTo where -- only time a side effect may be performed. ioAction :: privateFlags -> - TreeType FiddleUnit Parsed -> + FiddleUnit Parsed Identity (StageAnnotation Parsed) -> IO ([Diagnostic], Maybe privateState), -- | 'nextStage' is the function that transforms a 'FiddleUnit' from -- the current stage ('stageFrom') to the next stage ('stageTo'). It @@ -149,13 +159,13 @@ data CompilationPhase stageFrom stageTo where privateState -> FiddleUnit stageFrom - (StageFunctor stageFrom) + Identity (StageAnnotation stageFrom) -> Compile () ( FiddleUnit stageTo - (StageFunctor stageTo) + Identity (StageAnnotation stageTo) ) } -> @@ -198,11 +208,11 @@ thenPhase execCompilationPipelineWithCmdline :: CompilationPhase Parsed s' -> Parser - ( FiddleUnit Parsed (StageFunctor Parsed) (StageAnnotation Parsed) -> + ( FiddleUnit Parsed Identity (StageAnnotation Parsed) -> IO ( [Diagnostic], Maybe - ( FiddleUnit s' (StageFunctor s') (StageAnnotation s') + ( FiddleUnit s' Identity (StageAnnotation s') ) ) ) @@ -216,3 +226,22 @@ execCompilationPipelineWithCmdline Nothing -> return (diags, Nothing) ) flagsParser + +squeezeDiagnostics :: (Alter t) => t (Either [Diagnostic]) a -> Compile () (t Identity a) +squeezeDiagnostics ast = do + _ <- + alter + ( \case + (Left l) -> tell l >> return (Left l) + r -> return r + ) + return + ast + + case squeeze ast of + (Left _) -> compilationFailure + (Right a) -> return a + +resolveOrFail :: Either [Diagnostic] a -> Compile s a +resolveOrFail (Right a) = return a +resolveOrFail (Left l) = tell l >> compilationFailure |