State and Main

From Esolang
Jump to navigation Jump to search

State and Main is an esolang by User:PythonshellDebugwindow.

Syntax

Programs in State and Main are made up of a main function and a series of state definitions.

Main function

The main function can have as much or as little code as you like, and is written like so:

main
(code)
(more code)
(...)
(code)

It is executed once, at the beginning of the program, and can't be called again. Main can optionally take an argument (denoted by a lowercase letter):

main n
(code)
(more code)
(...)
(code)

This argument is an integer read from STDIN or the command-line args of the implementation.

State definitions

Like the main function, a state definition can have as much or as little code as you like, and is written like so where n is the state number:

state n
(code)
(more code)
(...)
(code)

If the same state is defined more than once in a program, an error should be raised before execution begins.

Inside main and state definitions

Lines of code inside of main and state definitions can be state changes or state returns.

State changes take this form, where n is a number:

(state n!)

This changes the current state to n. In the main function, if it takes an argument, that argument can be used as well. There's also a special state called H, which halts the program when it's the current state.

State returns take this form:

(return)

This returns to the previous state (or the main function), e.g. the one which changed the state to the current one. If a state return is reached in the main function, an error is raised.

Examples

Truth-machine

No output.

main n
(state n!)
state 0
(state H!)
state 1
(state 1!)