a simple tutorial
This page will describe all the things you can do with interpreter.
important
Some error checking is done, but not all errors from syntax to whatever else are checked for or found. For now, incorrect script has undefined behavior.
Script is parsed by each line. This means, unless given an exception below, all "statements" should be complete and on a single line.
comments
Use #
to start a comment. Comments must be one their own line, but can have whitespace before them.
variables
Variables are created once they are first referenced. To set a variable to some value, simply use an equals sign:
a = 0
b = 42.6
hello = "Hello, world!"
c = b * 4 - 2
Variables are not declared by type, but they do carry a type. So, numbers should only be used with other numbers and strings likewise. As seen in the 4th example, expressions can be evaluated for values. Expressions can include variables, numbers, parentheses, and function calls. Expressions also follow order of operations.
arrays
Arrays of numbers are now experimentally supported. To create an array:
array(newArray)
Arrays grow dynamically. Contents can be read using a '.'
operator:
newArray.0 = 15
newArray.4 = 18 / 5
Arrays can also be passed to the size()
function to get the size of the array.
Strings, being a sequence of characters, can be used like a readonly array. The '.'
operator can give number values for the nth character in the string, and the size()
function can give the string's length.
operators
The following operators are defined, and behave as would be expected in other languages (e.g. C):
= + - * / %
== != > < >= <=
& ^ | << >>
Different variable types should not appear in an operation.
For strings, only the =
and ==
operators are supported. Characters in a string can be accessed through the '.' operator, described in the arrays section above.
if/else
An if
can either be used by itself or with an accompanying else
:
if (a != 3) {
# do something
}
if ((flags & 17) == 17) {
# do another thing
} else {
# do a different thing
}
The bracket placement above is in the form the parser expects. Breaking away from this style creates undefined/unknown behavior.
while
While loops can be useful to loop on a certain condition:
counter = 0
while (counter < 10) {
# do stuff
counter = counter + 1
}
Bracket placement is important, as similarly described for if/else.
functions
Functions can be defined within script:
func(loopona) {
while (a >= 15) {
# probably work until the condition is false
}
}
Script functions are still being worked on. Parameters are accessed in the form arg0, arg1, etc. To return a value, have the last line of the function evaluate to what should be returned (e.g. a + 1
to return variable a plus one).
Examples of calling functions:
usethese(42, 16.5, "oink")
ret = increment(a)
If a function needs no arguments it may be called without parentheses. Calling a function with an unexpected number of arguments causes undefined behavior/segfaults.
functions from c
C functions can be exposed easily. Documentation on the C side of this will be added soon.
Calling c functions from script is not any different from normal functions:
dothingfromc("sample text")