diff options
author | Roman Smrž <roman.smrz@seznam.cz> | 2022-09-28 13:31:49 +0200 |
---|---|---|
committer | Roman Smrž <roman.smrz@seznam.cz> | 2022-09-30 22:45:58 +0200 |
commit | e6f8e2eeb66880950bd35fd82d439d87e7fa6bf5 (patch) | |
tree | d1c225b647bfea85749dc65e25e931f1457309c0 /src/Test.hs | |
parent | 8865c86aa904243ae91a598327e9dc1768ae8f3a (diff) |
Generic record member selection expression
Diffstat (limited to 'src/Test.hs')
-rw-r--r-- | src/Test.hs | 14 |
1 files changed, 10 insertions, 4 deletions
diff --git a/src/Test.hs b/src/Test.hs index 41649ef..a90035b 100644 --- a/src/Test.hs +++ b/src/Test.hs @@ -7,6 +7,7 @@ module Test ( VarName(..), textVarName, unpackVarName, ExprType(..), SomeVarValue(..), fromSomeVarValue, textSomeVarValue, + RecordSelector(..), Expr(..), eval, gatherVars, Regex, ) where @@ -22,7 +23,7 @@ import Data.Typeable import Text.Regex.TDFA import Text.Regex.TDFA.Text -import Network +import {-# SOURCE #-} Network import Process import Util @@ -32,7 +33,7 @@ data Test = Test } data TestStep = forall a. ExprType a => Let SourceLine VarName (Expr a) [TestStep] - | Spawn ProcName NodeName [TestStep] + | Spawn ProcName (Either NodeName (Expr Node)) [TestStep] | Send ProcName (Expr Text) | Expect SourceLine ProcName (Expr Regex) [VarName] [TestStep] | Guard SourceLine (Expr Bool) @@ -45,11 +46,11 @@ class MonadFail m => MonadEval m where lookupVar :: VarName -> m SomeVarValue -data VarName = VarName [Text] +newtype VarName = VarName Text deriving (Eq, Ord) textVarName :: VarName -> Text -textVarName (VarName name) = T.concat $ intersperse (T.singleton '.') name +textVarName (VarName name ) = name unpackVarName :: VarName -> String unpackVarName = T.unpack . textVarName @@ -60,6 +61,9 @@ class Typeable a => ExprType a where textExprValue :: a -> Text emptyVarValue :: a + recordMembers :: [(Text, RecordSelector a)] + recordMembers = [] + instance ExprType Integer where textExprType _ = T.pack "integer" textExprValue x = T.pack (show x) @@ -83,6 +87,8 @@ instance ExprType Regex where data SomeVarValue = forall a. ExprType a => SomeVarValue a +data RecordSelector a = forall b. ExprType b => RecordSelector (a -> b) + fromSomeVarValue :: forall a m. (ExprType a, MonadFail m) => VarName -> SomeVarValue -> m a fromSomeVarValue name (SomeVarValue value) = maybe (fail err) return $ cast value where err = T.unpack $ T.concat [ T.pack "expected ", textExprType @a Proxy, T.pack ", but variable '", textVarName name, T.pack "' has type ", textExprType (Just value) ] |