Toy language syntax

leresidue edited this page Mar 25, 2018 · 14 revisions

The syntax is simple. Each line of code must be separated by a newline character, or a | bar. A comment may begin with an @ or a ©, and stops when a newline character or a | bar is encountered. But you can't have comments in a multi-line text variable definition.

A line of code is such:

FUNCTION [TARGET_VARIABLE] Param1, param2, param3...

that the function is the only mandatory part. If the function requests a TARGET_VARIABLE, and no target variable is provided, the function will resort to use the hidden integer variable. The hidden integer variable is always present. It is mandatory to use the hidden variable when using the if function, because the if function will not use a TARGET_VARIABLE other than the hidden variable.

If you want to use the hidden variable elsewhere, then you can use the $ character. Example:

=6|print text $

In that example, the = function takes the parameter '6', and put it in its TARGET_VARIABLE, which is $ because there's no target variable specified. Then the | bar starts a new line of code. The print function uses the 'text' type to convert the content of $ to text format. You must convert an integer or decimal variable to text if you want it to be interpreted as text within the function.

Parameters format depends upon the function.

A target variable is the variable which is modified by the function. Think of it as a return value. But, in Toy, the content of a target variable may also be used as a parameter. For example:

== [str1] str2

will compare str1 to str2, and put the result (1 for equality, 0 otherwise) in the hidden variable. That is because the == function cannot write its result into a text variable.

The parenthesis of a target variable may be local, or supralocal. For example:

integer [one]
integer <two>

The variable 'one' is local, and the variable 'two', is supralocal. The rationale behind using 'supralocal' instead of 'global' is that in Toy, every variable can be overloaded by another variable. Example:

integer <one> 1
integer [one] 2
print text one

The result will be '2'. You cannot access the first variable as a parameter of a function. But in certain cases, you can still access it. For example:

=<one> 3
=[one] 4
print text one

The result will be '4'. But the first variable 'one' will be assigned the value '3'. This is by design. But:

text <one>
text [one]
input<one> "what's the number? "

The function input will put your answer into the second variable 'one'. If you want to copy the result into the first variable, you do:

=<one>one

Another peculiarity of the input function, is that its supralocal version will execute immediately. And the local version (input[one]) will execute only if and when the 'one' variable will be used as a parameter later in the code. What this means, is that if you have a list of input local functions, not all of them will be executed.

The local variables of a context cannot be accessed inside another context. The only exception is with parameters. Example:

macro <funcA> <
    integer [a] 2
    macro <funcB> <
        =[a] 3
    />
    funcB
    print text a  |@ will print '2'
/>
funcA

You cannot call a macro outside of the context of its definition. You can only call funcB from inside funA, only after the definition of the funcB macro.

If you have supralocal variables defined inside of a macro, you can use them as the macro's parameters, but only if you specify them as 'types'. Example:

macro <printSuper> <
    text <super>
    print super, " super!"
/>
printSuper super "You are"   |@ will print "You are super!"

The idea is the same for the target variable:

macro <getSuper> <
    text <super>
    text <(super)>
    =<(super)>super, " super!"
/>
text [str]
getSuper[str] super "You are"
print str

The result will be "You are super!".

Clone this wiki locally
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.