Skip to content

Advanced Tutorial

Luis Albizo edited this page Aug 29, 2018 · 15 revisions

Introduction

Strings

In wardscript there is no string data type, but with the help of nodes and bytes your can build one, even so there is a way for the interpreter to do this for you; with a syntactic sugar, just write the string you want between double quotes: "string". This will result in a node with a byte and another node referencing to the next char.

s := "My String";
? s is a node 
    The structure is: { $char := 77, $next := { $char := 121, $next := ... } }
    It is a linked list where $char is the byte value for the carácter and $next is another linked list or nil for the end of the string, like the following:
    77->121->32->3->116->114->105->110->103->nil
?
print(s); ? if the function print takes a node it wil treat it like a string and will print character by character. 
    the output will be "My String" (whitout end of line) ?

Lists

Another syntactic sugar on wardscript serves for creating linked list in an easy way.

my_list := [1, 2, 4, 8, 16];
? This generates a node with the folowing structure:
    { $root := { $data := 1, $next := { $data := 2, next := ... } } }
    as we see, the linked list is on the $root member which can be nil if the list is empty ( [] ),
    the $data member contains the value of the element and the member $next is the reference to the next node.

    root->(1->2->4->8->16->nil)
?

Lambdas

A lambda expression or an 'anonymous function' is a reduced syntaxis to create a function, a lambda just need a list of arguments and an expression, it automaticaly returns the result of that expresion.

$1 := func { x, y : x + y },
$2 := func { f, x : func { y : f(x, y) } };

Reduce operator

The reduce operator is unique to wardscript, is not an arithmetic operator. The function of this operator is to eval and reduce some expression, this is useful when is more appropiate to eval an expression just once. Internally the interpreter evaluate the expression and saves the result in the ast instead of just using it. An example is:

f := func x, y: x:
    x := x + y;
end,
f' := func x, y: x:
    x := #(x + y);
end;

? Non-reducing ?
present(f(2,4)); ? output: 6 ?
present(f(2,5)); ? output: 7 ?
? Reducing ?
present(f'(2,4)); ? output: 6 ?
present(f'(2,5)); ? output: 6 ?

Closures and the nonlocal keyword

Clone this wiki locally