Gammapedia is archived. No new edits are allowed and no new accounts can be registered.

Ikepedia is the officially decreed successor to Gammapedia concerning Gammasphere canon.

Infinitypedia is another successor.

User:Abwayax/Abwayax programming language/Tutorial

From Gammapedia
Revision as of 08:21, 10 November 2007 by 24.243.13.121 (talk)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

Hello world program

println("Hello world!");

As you can see it is pretty much the same as in many other languages.

Variable assignment and usage

A variable is a name associated with a value. A variable consists of a name prefixed with a symbol that denotes what type the variable is. The five basic data types are scalar, array, hash, function, and object (the last two are covered later)

$string = "This is a string."; // The $ denotes a scalar (string or number) value.
@array = ["This","is","an","array"]; // The @ denotes an array (a collection of values stored under one variable name)
%hash = ["one" => 1, "two" => 2, "three" => 3]; // The % denotes a hash (or associative array; a collection of values associated with string keys)

println($string); // prints out This is a string.

println(@array); // prints out [This,is,an,array]
println(@array[0]); // prints out This
println(@array[1]); // prints out is
 
println(%hash); // prints out something like {one=1,two=2,three=3}
println(%hash["one"]); // prints out "one"
println(%hash["two"]); // prints out "two"

$one = %hash["one"]; // $one now equals 1
@array[0] = %hash["two"]; // this is also permissible; @array now holds [2,is,an,array]

Functions

A function is a subprogram that can be executed by calling its name and giving it parameters. Abwayaxlanguage considers functions to be data just like scalars, hashes, etc. Accordingly, they are defined by assigning them to a function variable, like so:

&foo = { // The & denotes a function variable
    println("Foo");
}; // Note the semicolon after the closing brace; most languages do not require this, but abwayaxlanguage does

After defined, the function foo() can be called at any time:

foo(); // prints out Foo

Like any other piece of data, functions can be stored in arrays or hashes, like so:

%funcmap = [
    "One" => { println("one") },
    "Two" => { println("two") },
    "Three" => { println("three")
 ];

Functions without their own function variable are called with the call() function, which takes a function as a parameter:

call(%funcmap["One"]); // calls the one function

Parameters

Functions usually take one or more parameters. A parameter is a value passed to a function, with which the function is expected to do something. Examples of paramters earlier in the tutorial include the strings passed to the println() function, which prints a line to the console, and the function passed to the call() function. In the body of the function, parameters are accessed through the built-in @args array.

&funcWithParams = {
    println(@args[0]); // prints out the first parameter given to it
};

Alternatively, you may use the list() function to assign names to the arguments. list() takes an argument followed by a number of strings; the values are taken from the array and assigned to variables with the names given.

&funcWithParams = {
    list(@args,"myParam"); // @args[0] is now assigned to $myParam
    println($myParam);
};

To call this function, you would use the following line:

funcWithParams("Hi there"); // prints out Hi there

Return

Usually, a function is expected to return something; that is, calling it will give a value that can be used in an expression. Consider the following function, which adds two numbers:

&addTwoNums = {
    list(@args,$num1,$num2);
    return $num1 + $num2;
};

Now we can use the function whenever we need to add two numbers, such as:

$twentySeven = addTwoNums(20,7); // returns 27

Objects

An object is an entity that contains pieces of data and functions that act on that data. Typical example of declaring an object:

*object = Object(); // * denotes an object
*object->ctr = 0; // -> is the object member access operator
*object->increment = { *this->ctr = *this->ctr + 1; };
finalize(*object);

*object->increment(); // raises *object->ctr to 1
*object->increment(); // raises it to 2

Our object *object has a field called ctr and a function (or method) called increment(). Each call to increment() increases ctr by 1. The function finalize() marks the object as final any further modification to the structure of the object outside its own scope (methods within the object ignore the final flag).

Constructor

Unless you plan to use a type of object only once in the code, it might help to define a constructor to create the object. A constructor is merely a function that creates and returns an object. Here is what a constructor for our *object might look like:

&myObject = {
    *object = Object();
    *object->ctr = 0;
    *object->increment = { *this->ctr = *this->ctr + 1; };
    finalize(*object);
    return *object; 
};

The constructor lets you easily create objects of this type (or instances). In some ways one might say it acts almost like a "class". Now, anyone wishing to create a myObject() must simply invoke the function:

*object = myObject();
*object2 = myObject();
*object->increment(); // affects only *object and not *object2

If

The if statement is a universally used statement to control program execution. It tests to see if a condition is true; if so, then it executes a statement or block of statements; else, it does not. In abwayaxlanguage, if looks like this:

if(condition) {
   // code
}; // note the semicolon

An example of if is as follows:

if(2 + 2 == 5)
    println("Two and two make five.");

In this example, the println is executed if and only if 2 + 2 equals 5 (which it doesn't).