Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
State and Continuation
Using state and continuation in Tinymoe is not a simple topic. So if you are interested in it, you should read:
Here we only introduce the syntax part of state and continuation.
If you have read Manipulating Functions, you may know that there is a special kind of functions called continuation. In order to touch the real state object and real continuation object, we introduce the
cps for sentence and block.
- For sentences, the syntax is
cps (state) (continuation).
- For blocks, the syntax is
The declaration introduces two new variables. You can read and write fields of them. But changing these variables to reference to a new object does not work.
state is a variable storing extra information of the control flow event that is happening. The type of
state is always:
type continuation state flag argument continuation trap end
trap field, its type is always:
type continuation trap continuation previous trap end
If the current control flow event supports pause and resume, then the type of the
argument field should be:
type continuation fall back argument value trap fall back counter end
Coroutine is one application of the pause and resume feature.
Details of trapping can be found in Standard Library: Trap.
Details of coroutine can be found in Standard Library: Coroutine.
When there is no happening control flow event, the
continuation fields are all null. When there is, the
flag field stores a value of
symbol to specify the event name, and the values in
continuation depend on it.
continuation field is not null, it should be a continuation.
Continuations are only accessable in sentences with
cps (state) (continuation). If the
continuation field of the
state variable is not null, then the original value should comes from a cps sentence.
Continuation means "the next step". That means, if the following code is executing:
cps (state) (continuation) sentence do something xxx end phrase main set a to 1 do something set b to 2 print a + b end
When the running code reaches
xxx, then calling the
continuation means executing
set b to 2 print a + b
You are know understand what is a continuation. Continuation means "the next step", which means "everything that is going to be happened after the current cps sentence finished".
If you get the
continuation, you can implement any control flow that make sense. To do that, you should read:
###Notice Dealing with continuation is not difficult, but it is complicated. You should follow all rules that are described in the two links before. Standard library functions are always your first choice. You don't want to invent strange things. If you really want to invent your own pattern of using continuation, make sure that your code is compatible with those standard library things, like try-catch and coroutine.
You are suggested to use standard library functions for
continuation, if possible.