THE SLANG PROGRAMMING LANGUAGE

ABOUT SLANG   LANGUAGE   REPOSITORY   PROJECTS   CONTACT

Sample program


		
#!/usr/local/bin/slang

// Extension imports
import extension extJson;

// Library imports
import libParam;
import System.IO.File;


public void Main( int argc, string args ) modify {
	if ( argc < 2 ) {
		print( "usage: program [arg1 [arg2 [...] ] ]" );
		print( "" );
		return;
	}

	try {
		var params = new ParameterHandler( argc, args );

		foreach ( Parameter filename : params ) {
			if ( params.size() > 1 ) {
				print( filename.Key + ":" );
			}

			readFile( filename.Key );
		}
	}
	catch ( string e ) {
		print( "Exception: " + e );
	}
	catch ( IException e ) {
		print( "Exception: " + e.what() );
	}
}

private void readFile( string filename ) const {
	var file = new System.IO.File( filename, System.IO.File.AccessMode.ReadOnly );

	string text;
	while ( !file.isEOF() ) {
		text += file.readChar();
	}

	print( text );
}		
		
	

public void Main


As with many other programming languages Main is the entry point for your scripts. Either returning void or int is possible.

Imports and Extensions


Two types of imports are available:

- importing a file by providing its path (with . as path separator)

import Folder.Folder.Filename;

- importing an extension (without .so file extension)
import extension extensionName;


A typical way to import all needed files of a library one simply imports the base folder of it (like `import libParam;`) and the Slang interpreter automatically looks for an `All.os` file which defines all the needed imports. These `All.os`files are similar to an `*` in an Java import statement, with the exception that the library author manually creates it and thus can specify which files to import (typically unit tests or executables are filtered by that).

Methods


Methods can either modify data or are declared as const and are thus not allowed to make modifications. By default all methods are const, so the user needs to specifically mark a method with modify to be able to modify data from within.

Const (or: design by contract)


Not only methods can be const but almost everything else as well:

- objects: allows only const members and const methods as part of an object

public object AConstObject const
- methods: prevents data modification and allows only calls to const methods
public int getValue() const
- members: prevents data modification (except from within constructor and destructor)
public int mValue const
- parameters: prevents data modification
public void Method( int param const )
- return values: prevents data modification
public List const provideList()
- local variables: prevents data modification
int value const = 42

Exceptions


Slang uses a similar concept of checked exceptions like Java although it`s way more relaxed. If a method throws exceptions that are not caught directly within the method, it has to be marked with throws but the user does not need to provide the type of exception.

public void MethodThatThrows() modify throws

To throw an exception one simply needs to throw
throw 42

To catch an exception one can catch it

- by type
try { ... } catch ( string e ) { ... }

- by type hierarchy
try { ... } catch ( IException e ) { ... }

- catch everything
try { ... } catch { ... }

- or ignore it completely
try { ... }

Loops


Similar to Java to following loop constructs are available in Slang:

- indexed for loop

for ( int i = 0; i < 10; i++ ) { ... }
- Iterator based foreach loop
foreach ( Item item : collection ) { ... }
- while loops
while ( true ) { ... }

Ranges


A Range is a special collection which allows iterating over a predefined set of numbers with a custom step (default: 1). Ranges can be used either explicitly by using

new Range( from, to [, step] )
or implicitly by using
1..10

In both cases `System.Collections.Range` needs to be imported.

switch/case


Switch/case statements differ in some key aspects from what one is used to from other languages.


switch ( i ) {
	case 0: { ... break; }
	case 1: { ... continue; }
	default: { break; }
}
		

Each case expression is ended with a colon (:) and requires a separate code block ({}), which itself is required to have a controlflow statement (break, continue, return). With that one can create a loop as long as the switch statement is not exited with a break or return statement.