summaryrefslogtreecommitdiff
path: root/src/Test/Builtins.hs
blob: 774ad701ce3f7cc6a53ae5f39bc17175f5663ecc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
module Test.Builtins (
    builtins,
) where

import Data.Map qualified as M
import Data.Text (Text)
import Data.Typeable

import Process (Process)
import Test

builtins :: [ ( VarName, SomeVarValue ) ]
builtins =
    [ ( VarName "send", builtinSend )
    , ( VarName "guard", builtinGuard )
    , ( VarName "wait", builtinWait )
    ]

getArg :: Typeable a => FunctionArguments SomeExpr -> Maybe ArgumentKeyword -> a
getArg (FunctionArguments args) kw =
    case M.lookup kw args of
        Just (SomeExpr expr) | Just expr' <- cast expr -> expr'
        _ -> error "parameter mismatch"

builtinSend :: SomeVarValue
builtinSend = SomeVarValue (FunctionArguments $ M.fromList atypes) $
    \_ args -> TestBlock [ Send (getArg args (Just "to")) (getArg args Nothing) ]
  where
    atypes =
        [ ( Just "to", SomeArgumentType (ContextDefault @Process) )
        , ( Nothing, SomeArgumentType (NoDefault @Text) )
        ]

builtinGuard :: SomeVarValue
builtinGuard = SomeVarValue (FunctionArguments $ M.singleton Nothing (SomeArgumentType (NoDefault @Bool))) $
    \sline args -> TestBlock [ Guard sline (getArg args Nothing) ]

builtinWait :: SomeVarValue
builtinWait = SomeVarValue mempty $ const . const $ TestBlock [ Wait ]