prlg/app/Builtins.hs

74 lines
2.1 KiB
Haskell

module Builtins where
import Code
import Control.Monad.IO.Class
import Control.Monad.Trans.Class
import Control.Monad.Trans.State.Lazy
import qualified Data.Map as M
import Env hiding (PrlgEnv)
import qualified IR
import Interpreter (backtrack)
import qualified Operators as O
import System.Console.Haskeline
bi = Builtin
hello :: BuiltinFn
hello = do
liftIO $ putStrLn "hllo prlg"
return Nothing
printLocals :: BuiltinFn
printLocals = do
scope <- gets (gvar . cur)
Heap _ heap <- gets (heap . cur)
IR.StrTable _ _ itos <- gets strtable
flip traverse (M.elems scope) $ \(ref, name) ->
lift . outputStrLn $ (itos M.! name) ++ " = " ++ show (heap M.! ref)
return Nothing
promptRetry :: BuiltinFn
promptRetry = do
x <- lift $ getInputChar "? "
case x of
Just ';' -> backtrack
_ -> return Nothing
addBuiltins :: PrlgEnv ()
addBuiltins = do
a1 <- findStruct "a" 1
a <- findAtom "a"
b <- findAtom "b"
c <- findAtom "c"
varX <- findAtom "X"
b0 <- findStruct "b" 0
any1 <- findStruct "any" 1
eq2 <- findStruct "=" 2
hello0 <- findStruct "hello" 0
fail0 <- findStruct "fail" 0
true0 <- findStruct "true" 0
printlocals0 <- findStruct "print_locals" 0
debugprint0 <- findStruct "interpreter_state" 0
promptretry0 <- findStruct "prompt_retry" 0
modify $ \s ->
s
{ defs =
M.fromList
[ (eq2, [[U (LocalRef 0 varX), U (LocalRef 0 varX), NoGoal]])
, (a1, [[U (Atom a), NoGoal], [U (Atom b), NoGoal]])
, ( b0
, [ [Goal, U (Struct a1), U (Atom c), LastCall]
, [Goal, U (Struct a1), U (Atom b), LastCall]
])
, (any1, [[U VoidRef, NoGoal]])
, (hello0, [[Invoke $ bi hello]])
, (fail0, [[Invoke $ bi backtrack]])
, (true0, [[Invoke $ bi (pure Nothing)]])
, ( debugprint0
, [[Invoke $ bi (get >>= liftIO . print >> pure Nothing)]])
, (printlocals0, [[Invoke $ bi printLocals]])
, (promptretry0, [[Invoke $ bi promptRetry]])
]
, ops = [(O.xfy "," 1000), (O.xfx "=" 700)]
}