-
Notifications
You must be signed in to change notification settings - Fork 0
Basic Tutorial
This tutorial is intended for users who already know at least one high level language such as lua or Python. WardScript claims to be simple and easy to learn, but it's recommended to have a basic knowledge on programming.
A variable name in wardscript can contain letters a-z, A-Z , numbers: 0-9, and a few special symbols: _ $ '.
a := 12;
b := a, c := b;
There is only 4 data types in wardscript:
- Byte
- Nil
- Node
- Function
A byte is an unsigned integer lesser than 256
b1 := 0,
b2 := 255;
The Nil datatype represents the absence of value and is useful in data-structure implementation.
root := nil;
Note: nil is not a keyword, it's only a variable predefined in all programs
In dynamic data structures, a node is a record that contains a data of interest and at least one pointer to reference to another node.
A node in wardscript is similar to a structure in c, but dynamically typed as in python or lua (dictionary and table), it differs from these by being immutable; which means that once a node is created, it can not have new members or eliminate those that already exist, only modify its content.
To create a node write a list of assignments (members) between curly brackets { }.
root := {
data := 0,
next := nil
};
To modify a member value
root.data := 1;
Functions in wardscript are first-class values (wikipedia). To declare a function we must assign it to a variable. The syntax to declare a function is the following:
func arg1, arg2, arg3: result:
? Block of code ?
end
More examples:
f := func x, y: x:
x := x + y;
end;
f2 := func x, y: nil:
if x > y then
present(x);
else
if y > x then
present(y);
else
present(x, y);
end
end
end,
f2' := func x, y: nil:
if x > y then
present(x);
exit;
end
if y > x then
present(y);
exit;
end
if x == y then
present(x, y);
exit;
end;
end;
hello := func : nil:
? A function does not necessarily have to have arguments. ?
print("Hello World!",10);
end;
Notes The argument list may be empty so the argument list won't take any argument at moment of call.
- The 'return variable' may or may not be declared inside the function body, but it has to exist in the scope at the momento of call. (an example is function f2)
- The 'exit' keyword it's like a return in the sense it breaks the flow of the function and the function return whatever the return variable has in that moment. (example of use in f2')
A loop is equivalent to a while True or a while (1)
This is a list of very useful functions that are essential when programming.
print(72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33, 10);
? print is a built-in function that takes any number of arguments and prints the byte character.
This would print out: "Hello world!" and a newline (because the last 10 represents a newline)
print always returns nil ?
- present
a := 10, x := { data := 16 };
present(a, x, x.data, present, nil);
? present is a built-in function that takes any number of arguments and prints a human readable representation.
This would print out: "10 <node at 0x23456789> 16 <function at 0x2248dd00> <nil> " and a newline
present always returns nil ?