module Ermine.Builtin.Core
(
Lit(..)
, cons
, nil
, just
, nothing
, stringh
, inth
, longh
, cPutStrLn
, cShowInt
, cShowLong
, cShowLongHash
, cAddLong
, cFromIntegerToInt
, cFromIntegerToLong
) where
import Bound
import Control.Lens ((#), review)
import Data.Functor
import Data.Int
import qualified Data.Map as M
import Data.Text hiding (zip, length, concatMap, cons)
import Ermine.Builtin.Global
import Ermine.Syntax.Convention
import Ermine.Syntax.Core
import Ermine.Syntax.Global hiding (N)
import Ermine.Syntax.Id
import Ermine.Syntax.Literal
nil :: Core cc a
nil = Data [] 0 nilg []
cons :: AsConvention cc => Core cc a -> Core cc a -> Core cc a
cons a as = Data (review _Convention <$> [C,C]) 1 consg [a,as]
just :: AsConvention cc => Core cc a -> Core cc a
just a = Data [_Convention # C] 1 justg [a]
nothing :: Core cc a
nothing = Data [] 0 nothingg []
stringh :: AsConvention cc => Core cc a -> Core cc a
stringh s = Data [_Convention # N] 1 stringhg [s]
inth :: AsConvention cc => Core cc a -> Core cc a
inth i = Data [_Convention # U] 1 inthg [i]
longh :: AsConvention cc => Core cc a -> Core cc a
longh l = Data [_Convention # U] 1 longhg [l]
cPutStrLn :: AsConvention cc => Core cc a
cPutStrLn =
Lam [_Convention # C] . Scope $
Case (Var (B 0))
(M.singleton 0 . Match [_Convention # N] stringg
. Scope . App (_Convention # N) (_Id._Global # putStrLng) $ Var (B 1))
Nothing
cShowInt :: AsConvention cc => Core cc a
cShowInt =
Lam [_Convention # C] . Scope $
Case (Var (B 0))
(M.singleton 0 . Match [_Convention # U] inthg
. Scope . App (_Convention # U) (_Id._Global # showIntg) $ Var (B 1))
Nothing
cShowLong :: AsConvention cc => Core cc a
cShowLong =
Lam [_Convention # C] . Scope $
Case (Var (B 0))
(M.singleton 0 . Match [_Convention # U] longhg
. Scope . App (_Convention # U) (_Id._Global # showLongg) $ Var (B 1))
Nothing
cShowLongHash :: AsConvention cc => Core cc a
cShowLongHash = _Id._Global # showLongg
cAddLong :: AsConvention cc => Core cc a
cAddLong =
Lam (review _Convention <$> [C,C]) . Scope $
Case (Var (B 0))
(M.singleton 0 . Match [_Convention # U] longhg . Scope
$ Case (Var . F . Var . B $ 1)
(M.singleton 0 . Match [_Convention # U] longhg . Scope
$ App (_Convention # U)
(App (_Convention # U)
(_Id._Global # addLongg) $ (Var . F . Var . B $ 1))
(Var $ B 1))
Nothing)
Nothing
cFromIntegerToInt :: AsConvention cc => Core cc a
cFromIntegerToInt =
Lam [_Convention # C] . Scope $
Case (Var (B 0))
(M.singleton 0 . Match [_Convention # N] integerhg . Scope
. App (_Convention # N) (_Id._Global # fromIntegerToIntg) $ Var (B 1))
Nothing
cFromIntegerToLong :: AsConvention cc => Core cc a
cFromIntegerToLong =
Lam [_Convention # C] . Scope $
Case (Var (B 0))
(M.singleton 0 . Match [_Convention # N] integerhg . Scope
. App (_Convention # N) (_Id._Global # fromIntegerToLongg) $ Var (B 1))
Nothing
class Lit a where
lit :: AsConvention cc => a -> Core cc b
lits :: AsConvention cc => [a] -> Core cc b
lits = Prelude.foldr (cons . lit) nil
instance Lit Int64 where
lit l = Data [_Convention # U] 0 literalg [HardCore $ Lit $ Long l]
instance Lit Int32 where
lit i = Data [_Convention # U] 0 literalg [HardCore $ Lit $ Int i]
instance Lit Char where
lit c = Data [_Convention # U] 0 literalg [HardCore $ Lit $ Char c]
lits = lit . pack
instance Lit Text where
lit s = Data [_Convention # N] 0 stringg [HardCore $ Lit $ String s]
instance Lit Int8 where
lit b = Data [_Convention # U] 0 literalg [HardCore $ Lit $ Byte b]
instance Lit Int16 where
lit s = Data [_Convention # U] 0 literalg [HardCore $ Lit $ Short s]
instance (Lit a, Lit b) => Lit (a, b) where
lit (a,b) = Data (review _Convention <$> [C,C]) 0 (tupleg 2) [lit a, lit b]
instance Lit a => Lit [a] where
lit = lits
instance Lit a => Lit (Maybe a) where
lit = maybe nothing (just . lit)