piet-0.1: A Piet interpreter

Language.Piet.PietMonad

Contents

Description

A module implementing the Piet interpreter as a monad. The monad encapsulates the interpreter's status, i. e. the side-effects of Piet programs.

Synopsis

The Piet interpreter monad

data PietMonad a Source

A monad encapsulating the status of a Piet interpreter.

data InterpreterStatus Source

The status of a Piet interpreter.

data LogLevel Source

Describes the importance of a log message.

Constructors

Verbosed

Rather verbosed output.

Info

Usual log level.

Error

A recoverable error has occured.

Fatal

A fatal error has occured.

Status access

Direction Pointer, Codel Chooser and position

getDP :: PietMonad DirectionPointerSource

Returns the current Direction Pointer.

setDP :: DirectionPointer -> PietMonad ()Source

Sets the Direction Pointer.

getCC :: PietMonad CodelChooserSource

Returns the current Codel Chooser.

setCC :: CodelChooser -> PietMonad ()Source

Sets the current Codel Chooser.

getPosition :: PietMonad (Int, Int)Source

Returns the current position.

setPosition :: Int -> Int -> PietMonad ()Source

Sets the current position.

Stack primitives

stackPush :: Int -> PietMonad ()Source

Pushes a given Int value on the stack.

stackPop :: PietMonad (Maybe Int)Source

Pops the top value from the stack. If the stack was empty, Nothing is returned, Just the top value otherise.

stackRollSource

Arguments

:: Int

Roll number

-> Int

Depth

-> PietMonad () 

Performs the roll operation on the stack.

I/O

printNumber :: Int -> PietMonad ()Source

Prints a number to STDOUT.

printChar :: Int -> PietMonad ()Source

Converts a given number to a character and prints it to STDOUT.

readNumber :: PietMonad IntSource

Reads a number from STDIN.

readChar :: PietMonad IntSource

Reads a character from STDIN. Note that it is returned as an Int.

logMessage :: LogLevel -> String -> PietMonad ()Source

Issue log message with given priority.

Termination

terminate :: PietMonad ()Source

Quit a program. Any command following this one will be ignored.

Execution

runPietMonadSource

Arguments

:: (PietType -> IO Int)

Callback to read from STDIN

-> (PietType -> Int -> IO ())

Print callback

-> (LogLevel -> String -> IO ())

Logging callback

-> PietMonad a

The program to be executed

-> IO (Either String a)

Result of the PietMonad or an error message

Executes a program represented by a PietMonad. I/O operations (reading and writing numbers or characters) is delegated to callback functions.