This documentation is incomplete. Changes will be made without notice.

Language Specification

Reserved Words

Any word on the following list cannot be used for naming a class, variable, property, or method.

catch class do else elseif extends false final
for if import new null private protected public
return static throw true try void while  

Operators

Arithmetic

object_1 + object_2 Addition
object_1 - object_2 Subtraction
object_1 * object_2 Multiplication
object_1 / object_2 Division
object_1 %object_2 Modulus

Assignment

variable = expression Simple assignment
variable += expression Addition assignment
variable -= expression Subtraction assignment
variable *= expression Multiplication assignment
variable /= expression Division assignment
variable %= expression Modulus assignment

Comparison

object_1 == object_2 Equal
object_1 != object_2 Not equal
object_1 > object_2 Greater than
object_1 < object_2 Less than
object_1 >= object_2 Greater than or equal
object_1 <= object_2 Less than or equal

Logical

expression_1 && expression_2 And
expression_1 || expression_2 Or

String

object_1 + object_2 Concatenation

Classes and Objects

Class Definitions

A class is defined by the keyword class followed by a space and the name of class. Curly braces must then follow and enclose all members of that class. The class name can be a string with uppercase and lowercase characters. The class name must also reflect the name of the file where it is stored, omitting the file extension.

//
// Filename: SampleClass.swn
//
class SampleClass
{

}

A class may be extended from any other defined class which allows the child class to access the (accessible) members of the parent class. You can extend a class by putting 'extends' and the parent class name after the class definition.

class SampleClass extends ParentClassName
{

}

Class Aliases

Class aliases are used for shorting class names, based off of their fullname. The alias of a class is the characters to the right of the rightmost set of semicolons (Example: Core::IO::Console aliases to Console). Class aliases are made before the class definition and use the following syntax:

import (classname);

Accessing Members

Members inside of a class is done by specifing the class or object variable, followed by a period, followed by the name of the member. An example of this is:

(Classname or object variable).(Member name)

Member Visibility

swn supports three types of visibility modifiers: public, private, and protected. Public members are accessible from inside and outside the class, private members are only visible from inside the current class, and protected members are visible from the current and any sub-classes. If a member does not have an explicit visibility modified, it is assumed it is private.

class SampleClass
{
    public Core::String name = "Tim";
}

Static Members

By using the keyword static, the selected member is accessible without creating an instance of the class.

class SampleClass
{
    public static Core::String name = "Tim";
}
Core::IO::Console.printLine (SampleClass.name);

Final Members

When a member is declared with the keyword final, any class which descends said class is not allowed to overwrite the member.

class ParentClass
{
    public final Core::Number age = 16;
}
//
// An error would occur when trying to use this class
//
class ChildClass extends ParentClass
{
    public Core::Number age = 17;
}

Properties

When creating a property for a class, use the follow structure:

[visibility] [static] [final] (data type) (name) = (expression);

Methods

Creating methods inside your class can be done using the following syntax:

[visibility] [static] [final] (return type) (name) ( [arguments] )
{
    [method body]
}

Arguments in the class definition are defined by: specifying the data type, supplying a name for the variable, and then, if there is a succeeding argument, a comma should be placed.

If the method does not return any data, 'void' is used as the data type.

'this'

Inside any of your non-static methods, you will have access to a pre-assigned variable named 'this'. 'this' is the instance of the current class and allows you access to the members of said class.

class SampleClass
{
    public Core::Number age = 16;
    public void setAge ( Core::Number age )
    {
        this.age = age;
        this.refreshProfile ( );
    }
    private final void refreshProfile ( )
    {
        ...
    }
}

Constructors

Class constructors are used to run code when an object is first created. The constructor method(s) should have the same name as the class and should not have a return type nor should they be static.

class SampleClass
{
    private Core::String name;
    public SampleClass ( Core::String name )
    {
        this.name = name;
    }
}
SampleClass instance = new SampleClass ( "Tim" );

Method Overloading

Overloading methods allows multiple methods to share a common name but have different arguments, thus, different code will be executed based on how many arguments are passed.

class SampleClass
{
    public void sayHello ( Core::String name )
    {
        Core::IO::Console.printLine ( "Hello" + name );
    }
    public void sayHello ( Core::String name, Core::String name2 )
    {
        Core::IO::Console.printLine ( "Hello" + name + " and " + name2 );
    }
}

Control Structures

if

The use of the if control structure is to test if an expression evaluates true. If it does, the code between the following curly braces is parsed.

if ( (expression) )
{
    ...
}
if ( "Tim".length <= 3 )
{
    Core::IO::Console.printLine ( "That's a short name!" );
}

else

Used in conjunction with if, else is used to execute code only when all the expressions in the above if statements evaluate false.

if ( (expression) )
{
    ...
}
else
{
    ...
}
if ( "Tim".length <= 3 )
{
    Core::IO::Console.printLine ( "That's a short name!" );
}
else
{
    Core::IO::Console.printLine ( "That name is relatively long!" );
}

elseif

Used after an if control structure, but before an optional else structure, elseif allows you to evaluate multiple expressions if any of the previous evaluate false.

if ( (expression) )
{
    ...
}
elseif ( (expression) ) // You can add more of these statements if needed
{
    ...
}
else // An else structure is optional
{
    ...
}

for

This control structure allows code to be iterated over while a condition is met. The for structure allows statements to be executed before the expression and at the end of each of iteration.

for ( (pre-loop expression) ; (loop condition expression) ; (end-of-iteration expression) )
{
    ...
}

while

Like the for structure, the while structure allows you to loop through the same code over until a condition is met. The only difference is that while does not help with explicitly stating pre-loop and post-iteration statements.

while ( (loop condition) )
{
    ...
}

do ... while

An addition onto the while loop, the do ... while loop checks the condition after the code has gone through the iteration. This guarantees that the code will always be run at least once.

do
{
    ...
} while ( (loop condition) );

Exceptions

Exceptions are used to handle non-fatal errors. These errors are usually related to invalid input.

try ... catch

The try code block runs code as normal; however, if an exception is thrown inside of the block, the appropriate catch block is run, depending on the exception data type. If there is a possibility of code throwing an exception, you should wrap it in a try ... catch block.

try
{
    ...
{
catch ( (Exception class) (Exception Variable) )
{
    ...
}
try
{
    Core::IO::File file_handler = new Core::IO::File ( "this_file_does_not_exist" );
}
catch (Core::IO::FileNotFoundException e)
{
    Core::IO::Console.printLine( "The file " + e.getFilename() + " could not be found" );
}

Core Library

Core::Boolean

Core::IO::Console

Methods

public static void

beep ( )

If supported, the computer plays an internal beep.

public static Core::Number

readNumber ( )

Returns a number which was inputed to standard input

public static Core::String

readLine ( )

Returns a string which was inputed to standard input

public static void

print ( Core::String str )

Displays str in the command line window

public static void

printLine ( Core::String str )

Displays str in the command line window, followed by a newline character

Core::Number

Methods

public Core::Number

abs ( )

Returns the absolute value of the current number object

public Core::Number

ceil ( )

Returns the rounded up value of the current number object

public Core::Number

cos ( )

Returns the cos value of the current number object

public Core::Number

floor ( )

Returns the rounded down value of the current number object

public Core::Number

log ( Core::Number base )

Returns the logarithmic value of the current object to the base of base

public Core::Number

pow ( Core::Number p )

Returns the value of the current number object to the power of p

public Core::Number

sqrt ( )

Returns the square root of the current number object

public Core::Number

sin ( )

Returns the sin value of the current number object

public Core::Number

tan ( )

Returns the tan value of the current number object

public Core::String

toString ( [ Core::Number base ] )

Returns a string of the current number object to the base of base. Default is to base 10.

Core::Object

Methods

Core::String

Properties

public Core::Number

length

Returns the number of characters in the string object

public static Core::Number

TRIM_AFTER = 1

Sets the trim method to remove whitespace after a string

public static Core::Number

TRIM_BEFORE = 2

Sets the trim method to remove whitespace before a string

public static Core::Number

TRIM_BOTH = 3

Sets the trim method to remove whitespace from before and after a string

Methods

public Core::Number

characterAt ( Core::Number index )

Returns the ASCII value of the chracter at position index in the current string.

public Core::Number

indexOf ( Core::String search [, Core::Number offset ] )

Starting at offset, this returns the location of search from the beginning of the string. Returns -1 if search cannot be found

public Core::String

lowercase ( )

Returns a string object with all uppercase characters changed to lowercase characters

public Core::String

repeat ( Core::Number i )

Returns a string repeated i times

public Core::String

replace ( Core::String search, Core::String replace )

Returns a string object with instances of search replaced with replace

public Core::String

reverse ( )

Returns a string object from back to front

public Core::String[]

split ( [ Core::String delimiter ] )

Returns an array of string tokens splitted by delimiter

public Core::String

substring ( Core::Number start [, Core::Number length ] )

Returns the string object from index start for length characters. If end is not given, it goes on to the end of the string. If start is negative, it starts from +start characters from the end of the string.

public Core::String

trim ( [ Core::Number type ] )

Returns the string object with the whitespace cut off from either before the string (Core::String.TRIM_BEFORE), after the string (Core::String.TRIM_AFTER), or both (Core::String.TRIM_BOTH), which is the default.

public Core::String

uppercase ( )

Returns a string object with all lowercase characters changed to uppercase characters

Core::System

Methods

public static void

exit ( Core::Number code )

Stops execution of the script and returns a program return code of code