Skip to content

Basic Tutorial

Luis Albizo edited this page Sep 13, 2020 · 30 revisions

Introduction

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.

WardScript has only 8 keywords: if then else end loop func nonlocal exit.

To run a wardscript program just run: ward script_name (you need to install the interpreter first)

Variables

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;

Data Types

There is only 4 data types in wardscript:

  • Byte
  • Nil
  • Node
  • Function

Byte

A byte is an unsigned integer lesser than 256

b1 := 0,
b2 := 255;

Nil

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

Node

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 access a member the syntax is: root.data (this is an expression) and to modify a member value: root.data := 1 (this is an statement)

Function

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 the 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 moment 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')

Operators

The operators in wardscript are the following: + - << >> < <= > >= && || #.

  • Arithmetic:
Operator Description Example
+ Addition. b := 17 + 90; (b is 107)
- Substraction. b := 170 - 90; (b is 80)
<< Left Shift. b := 3 << 2; (b is 12)
>> Right Shift. b := 30 >> 2; (b is 7)

Important: Only valid if the two operands are byte values

  • Relational:
Operator Description Example
< Less than b := 8 < 9; (b is 1)
<= Less than or equal to b := 8 <= 9; (b is 1)
> Greater than b := 2 > 2; (b is 0)
>= Greater than or equal to b := 2 >= 2; (b is 1)
== Equal to b := 3 == 2; (b is 0)
/= Not equal to b := 3 /= 2; (b is 1)

Note: the result is either 0 or 1, this serves as a boolean value. Important: Only valid if the two operands are byte values except for == and /=

  • Logical:
Operator Description Example
&& And b := 0 && 1; (b is 0)
|| Or b := 0 || 1; (b is 1)
  • Unary:
Operator Description Example
! Not b := !1; (b is 0)
# Reduce see Reduce Operator

Boolean Values

In wardscript there is no boolean datatype, instead it uses byte values as boolean, the 0 means False, and any other byte means True.

DataType Boolean Value
Byte (0) 0
Byte (1 to 255) 1
Nil 0
Node 1
Function 1

The 'boolean value' is used on the if-condition and can be obtained by using the function bool(obj).

Control Flow

If clauses

Use the if-statement to specify a block of code to be executed if a condition is true and add an 'else' block to execute if the condition is not met.

There are 2 syntax to create an if:

if cond then
    ? block-if ?
end

if cond then
    ? block-if ?
else
    ? block-else ?
end

Another example:

b := 20;

if b > 20 then
    print("b is over 20");
else
    print("b is not over 20");
end

Loops

A loop is a sequence of statements which is specified once but which may be carried out several times in succession until some condition is met, or indefinitely.

A loop in wardscript is equivalent to a while (true) in other languages and the syntaxis is the following:

loop
    ? Block of code ?
end

A few examples:

i := 0;
loop
    if i >= 200 then
        exit;
    else
        present(i);
        i := i + 1;
    end
end

The exit keyword its essential on loops, every loop should have a way to break the cycle.

Essential functions

This is a list of very useful functions that are essential when programming.

  • print
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 ?
  • finish
nonlocal exit_code := 23; 
finish();
finish(23); ? also valid ?
? finish is a built-in function that may take one argument and finish the program. ?
Clone this wiki locally