root/src/Pugs/Types/Scalar.hs

Revision 15615, 1.7 kB (checked in by audreyt, 21 months ago)

* Pugs.Types: Make Array elements truly bindable:

my @x;
@x[10] := 123;
@x[10] = 5; # can't assign into Int

also make cloning an STM action rather than an Eval action.

  • Property svn:mime-type set to text/plain; charset=UTF-8
  • Property svn:eol-style set to native
Line 
1
2class (Typeable a) => ScalarClass a where
3    scalar_iType :: a -> Type
4    scalar_iType = const $ mkType "Scalar"
5    scalar_fetch :: a -> Eval VScalar
6    scalar_store :: a -> VScalar -> Eval ()
7    scalar_const :: a -> Maybe VScalar
8    scalar_clone :: a -> STM a
9    scalar_clone = return
10    scalar_fetch' :: a -> Eval VScalar
11    scalar_fetch' x = scalar_fetch x
12    scalar_type  :: a -> Eval Type
13    scalar_type x = do
14        v <- scalar_fetch x
15        evalValType v
16
17instance ScalarClass IScalarProxy where
18    scalar_iType = const $ mkType "Scalar::Proxy"
19    scalar_fetch = fst
20    scalar_store = snd
21    scalar_const = const Nothing
22
23instance ScalarClass IScalar where
24    scalar_fetch = stm . readTVar
25    scalar_store = (stm .) . writeTVar
26    scalar_const = const Nothing
27    scalar_clone sv = newTVar =<< readTVar sv
28
29instance ScalarClass IScalarLazy where
30    scalar_iType = const $ mkType "Scalar::Lazy"
31    scalar_fetch = return . maybe undef id
32    scalar_store d _ = retConstError $ VStr $ show d
33    scalar_const = const Nothing
34
35instance ScalarClass IScalarCwd where
36    scalar_iType = const $ mkType "Scalar::Cwd"
37    scalar_fetch _ = do
38        str <- io getCurrentDirectory
39        return $ VStr str
40    scalar_store _ val = do
41        str <- fromVal val
42        tryIO () $ setCurrentDirectory str
43    scalar_const = const Nothing
44
45instance ScalarClass VScalar where
46    scalar_iType = const $ mkType "Scalar::Const"
47    -- scalar_fetch v@(VRef (MkRef IPair{})) = return v
48    scalar_fetch (VRef (MkRef (IScalar sv))) = scalar_fetch sv
49    scalar_fetch v = return v
50    scalar_store d _ = retConstError d
51    scalar_const = Just
52    scalar_type (VRef (MkRef (IScalar sv))) = scalar_type sv
53    scalar_type v = return $ valType v
54    scalar_fetch' v = return v
Note: See TracBrowser for help on using the browser.