1
1
mirror of https://github.com/github/semantic.git synced 2025-01-02 04:10:29 +03:00
semantic/test/Control/Abstract/Evaluator/Spec.hs

122 lines
4.8 KiB
Haskell
Raw Normal View History

{-# LANGUAGE TypeOperators #-}
2018-05-09 19:36:40 +03:00
module Control.Abstract.Evaluator.Spec
( spec
) where
2018-05-07 21:21:05 +03:00
2018-12-07 20:38:16 +03:00
import Control.Abstract as Abstract
import qualified Control.Abstract.Heap as Heap
2018-12-05 18:14:16 +03:00
import Data.Abstract.Address.Precise as Precise
import Data.Abstract.BaseError
import Data.Abstract.Evaluatable
import Data.Abstract.FreeVariables
import Data.Abstract.Module
import qualified Data.Abstract.Number as Number
import Data.Abstract.Package
import Data.Abstract.Ref
import qualified Data.Abstract.ScopeGraph as ScopeGraph
2018-12-05 18:14:16 +03:00
import Data.Abstract.Value.Concrete as Value
import Data.Algebra
import Data.Bifunctor (first)
import Data.Functor.Const
import qualified Data.Map.Strict as Map
import Data.Sum
import Data.Text (pack)
import SpecHelpers hiding (reassociate)
import System.IO.Unsafe (unsafePerformIO)
2018-05-07 21:21:05 +03:00
spec :: Spec
2018-05-07 22:10:41 +03:00
spec = parallel $ do
it "constructs integers" $ do
2018-12-07 22:46:41 +03:00
(_, (_, (_, expected))) <- evaluate (pure (integer 123))
expected `shouldBe` Right (Value.Integer (Number.Integer 123))
2018-05-07 23:06:21 +03:00
it "calls functions" $ do
2018-12-07 22:46:41 +03:00
(_, (_, (_, expected))) <- evaluate .withLexicalScopeAndFrame $ do
currentScope' <- currentScope
let lexicalEdges = Map.singleton Lexical [ currentScope' ]
x = SpecHelpers.name "x"
associatedScope <- newScope lexicalEdges
declare (ScopeGraph.Declaration "identity") emptySpan (Just associatedScope)
withScope associatedScope $ do
declare (Declaration x) emptySpan Nothing
identity <- function "identity" [ x ]
2018-12-07 23:20:55 +03:00
(SpecEff (Heap.lookupDeclaration (ScopeGraph.Declaration (SpecHelpers.name "x")) >>= deref)) associatedScope
2018-12-07 22:46:41 +03:00
val <- pure (integer 123)
call identity [val]
expected `shouldBe` Right (integer 123)
2018-05-07 23:06:21 +03:00
2018-05-07 22:10:11 +03:00
evaluate
2018-05-07 22:13:01 +03:00
= runM
2018-10-24 16:47:24 +03:00
. runTraceByIgnoring
. runState (lowerBound @(ScopeGraph Precise))
. runState (lowerBound @(Heap Precise Precise Val))
2018-10-24 16:47:24 +03:00
. runFresh
. runReader (PackageInfo (SpecHelpers.name "test") mempty)
2018-05-07 22:10:11 +03:00
. runReader (ModuleInfo "test/Control/Abstract/Evaluator/Spec.hs")
. evalState (lowerBound @Span)
2018-08-06 19:29:24 +03:00
. runReader (lowerBound @Span)
. runEvaluator
2018-10-24 17:01:55 +03:00
. runAllocator
. evalModule
where
evalModule action = do
scopeAddress <- newScope mempty
frameAddress <- newFrame scopeAddress mempty
val <- raiseHandler (runReader (CurrentScope scopeAddress))
. raiseHandler (runReader (CurrentFrame frameAddress))
. fmap reassociate
. runScopeError
. runHeapError
. runValueError
. runAddressError
. runEvalError
. runDeref @Val
. runAllocator
. runReturn
. runLoopControl
. runBoolean
. runFunction runSpecEff
$ action
pure ((scopeAddress, frameAddress), val)
reassociate :: Either (SomeError exc1) (Either (SomeError exc2) (Either (SomeError exc3) (Either (SomeError exc4) (Either (SomeError exc5) result)))) -> Either (SomeError (Sum '[exc5, exc4, exc3, exc2, exc1])) result
reassociate = mergeErrors . mergeErrors . mergeErrors . mergeErrors . mergeErrors . Right
type Val = Value SpecEff Precise
2018-09-25 22:41:57 +03:00
newtype SpecEff = SpecEff
{ runSpecEff :: Evaluator SpecEff Precise Val (FunctionC SpecEff Precise Val
2018-10-24 16:47:24 +03:00
(Eff (BooleanC Val
(Eff (ErrorC (LoopControl Val)
2018-12-07 20:38:16 +03:00
(Eff (ErrorC (Return Val)
2018-10-24 16:47:24 +03:00
(Eff (AllocatorC Precise
(Eff (DerefC Precise Val
(Eff (ResumableC (BaseError (EvalError Precise Val))
2018-10-24 16:47:24 +03:00
(Eff (ResumableC (BaseError (AddressError Precise Val))
(Eff (ResumableC (BaseError (ValueError SpecEff Precise))
(Eff (ResumableC (BaseError (HeapError Precise))
(Eff (ResumableC (BaseError (ScopeError Precise))
(Eff (ReaderC (CurrentFrame Precise)
(Eff (ReaderC (CurrentScope Precise)
(Eff (AllocatorC Precise
2018-10-24 16:47:24 +03:00
(Eff (ReaderC Span
(Eff (StateC Span
2018-10-24 16:47:24 +03:00
(Eff (ReaderC ModuleInfo
(Eff (ReaderC PackageInfo
(Eff (FreshC
(Eff (StateC (Heap Precise Precise Val)
(Eff (StateC (ScopeGraph Precise)
2018-10-24 16:47:24 +03:00
(Eff (TraceByIgnoringC
(Eff (LiftC IO)))))))))))))))))))))))))))))))))))))))))))))
2018-12-07 23:20:55 +03:00
Val
}
2018-09-25 22:41:57 +03:00
instance Eq SpecEff where _ == _ = True
instance Show SpecEff where show _ = "_"
instance FreeVariables SpecEff where freeVariables _ = lowerBound
instance Declarations SpecEff where
declaredName eff =
2018-12-07 23:20:55 +03:00
case unsafePerformIO (evaluate (runSpecEff eff)) of
2018-12-07 22:46:41 +03:00
(_, (_, (_, Right (Value.Symbol text)))) -> Just (SpecHelpers.name text)
_ -> error "declaredName for SpecEff should return an RVal"