module Berp.Base.StdTypes.Object (object) where
import Prelude hiding (init)
import Berp.Base.SemanticTypes (Procedure, Object (..))
import Berp.Base.Monad (constantIO)
import Berp.Base.Attributes (mkAttributes)
import Berp.Base.StdNames (strName, eqName, initName)
import Berp.Base.Prims (primitive)
import Berp.Base.Object (identityOf)
import Berp.Base.StdTypes.Type (newType)
import Berp.Base.StdTypes.Tuple (emptyTuple)
import Berp.Base.StdTypes.String (string)
import Berp.Base.StdTypes.Bool (bool)
import Berp.Base.StdTypes.None (none)
object :: Object
object = constantIO $ do
dict <- attributes
newType [string "object", emptyTuple, dict]
attributes :: IO Object
attributes = mkAttributes
[ (strName, primitive 1 str)
, (eqName, primitive 2 eq)
, (initName, primitive 1 init)
]
init :: Procedure
init _ = return none
eq :: Procedure
eq (obj1:obj2:_) = return $ bool (identityOf obj1 == identityOf obj2)
eq _other = error "equality on objects applied to wrong number of arguments"
str :: Procedure
str (x:_) =
case x of
Object {} -> do
let objTypeNameStr = object_string $ object_type_name $ object_type x
let identity = identityOf x
return $ string $ "<" ++ objTypeNameStr ++ " object with identity " ++ show identity ++ ">"
Type {} -> do
let typeName = object_string $ object_type_name x
return $ string $ "<class " ++ typeName ++ ">"
Function {} -> do
let identity = identityOf x
return $ string $ "<function with identity " ++ show identity ++ ">"
_other -> return $ string "<unknown object>"
str _other = error "str conversion on object applied to wrong number of arguments"