Skip to content

Commit

Permalink
refactor: Add strict version of the diagnostics monad.
Browse files Browse the repository at this point in the history
This should help performance a bit.
  • Loading branch information
iphydf committed Dec 31, 2021
1 parent 8bab416 commit dae75f1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
8 changes: 8 additions & 0 deletions src/Language/Cimple.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ module Language.Cimple
( module X
, AstActions
, defaultActions
, AstActions'
, defaultActions'
) where

import Control.Monad.State.Lazy (State)
import qualified Control.Monad.State.Strict as SState
import Data.Text (Text)

import Language.Cimple.Annot as X
Expand All @@ -18,3 +21,8 @@ type AstActions a = X.IdentityActions (State a) Text

defaultActions :: AstActions state
defaultActions = X.identityActions

type AstActions' a = X.IdentityActions (SState.State a) Text

defaultActions' :: AstActions' state
defaultActions' = X.identityActions
27 changes: 19 additions & 8 deletions src/Language/Cimple/Diagnostics.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@
{-# LANGUAGE StrictData #-}
module Language.Cimple.Diagnostics
( Diagnostics
, Diagnostics'
, HasDiagnostics (..)
, warn
, warn'
, sloc
) where

import Control.Monad.State.Lazy (State)
import qualified Control.Monad.State.Lazy as State
import Data.Fix (foldFix)
import Data.Text (Text)
import qualified Data.Text as Text
import Language.Cimple.Ast (Node)
import qualified Language.Cimple.Flatten as Flatten
import Language.Cimple.Lexer (Lexeme (..), lexemeLine)
import Control.Monad.State.Lazy (State)
import qualified Control.Monad.State.Lazy as State
import qualified Control.Monad.State.Strict as SState
import Data.Fix (foldFix)
import Data.Text (Text)
import qualified Data.Text as Text
import Language.Cimple.Ast (Node)
import qualified Language.Cimple.Flatten as Flatten
import Language.Cimple.Lexer (Lexeme (..), lexemeLine)

type DiagnosticsT diags a = State diags a
type Diagnostics a = DiagnosticsT [Text] a
Expand All @@ -25,6 +28,14 @@ warn
=> FilePath -> at -> Text -> DiagnosticsT diags ()
warn file l w = State.modify (addDiagnostic $ sloc file l <> ": " <> w)

type DiagnosticsT' diags a = SState.State diags a
type Diagnostics' a = DiagnosticsT' [Text] a

warn'
:: (HasLocation at, HasDiagnostics diags)
=> FilePath -> at -> Text -> DiagnosticsT' diags ()
warn' file l w = SState.modify (addDiagnostic $ sloc file l <> ": " <> w)


class HasDiagnostics a where
addDiagnostic :: Text -> a -> a
Expand Down

0 comments on commit dae75f1

Please sign in to comment.