SGScript tutorial


Table of Contents


The language

println( "Hello, world!" );

SGScript is a dynamic, procedural language, similar in syntax to many other programming languages, including C and JavaScript. It consists of various operations and structures that can be combined to specify operations and data that they use.


The basics

function printText()
{
	println( "text" );
}
printText();
y = 15.51;
x = 3 * y + 10;
x * y;
include "math";
function sinc( x )
{
	xpi = x * M_PI;
	return sin( xpi ) / xpi;
}
sinc3 = sinc( 3 );
x = 1; y = 2;
x += y;
++x;
--y;
x = 5; y = 3;
x *= y * ( y + x );
y -= x + x * y;
y += 5 * y += y;
printvar( y );
println( dumpvar( x ) );
x = 5;
global y = 6;
function z()
{
	a = 7;
	global b = 8;
	function c(){}
}
// testing the assignment operator
a = 5;
/* this ----------
-- should print --
------------- 5 */
println( a ); ////

Data types

a = null;
b = true;
c = 123;
d = 123.456;
e = "text";
f = function(){ return 4; };
g = println;
h = io_file(); // returns a file object
i = toptr(100);
arr = [ 1, 3, 5, 7 ];
println( arr[2] ); // prints 5
println( arr[4] ); // warning: index out of bounds
println( arr.size ); // prints 4
arr = [];
arr.push( 5 );
x = arr.pop();
arr.unshift( 4 );
y = arr.shift();
dct = { x = 5 };
dct[ "key" ] = "value";
println( dct ); // {x=5,key=value}
fnmap = map();
fnmap[ print ] = "print";
myObj = { x = 5, y = 7 };
function myObj.moveLeft(){ --this.x; }
myObj.moveLeft(); // method call
function myObj_moveRight(){ ++this.x; }
myObj!myObj_moveRight(); // compatible call

Flow control

if( a > 5 )
{
	println( "'a' is greater than 5" );
	if( a < 10 )
		println( "...but less than 10" );
}
else
	println( "'a' is not greater than 5" );
while( a > 5 )
{
	println( a );
	--a;
}
for( i = 0; i < 5; ++i )
	println( i );
foreach( name : [ "one", "two", "three" ] )
	println( name ); // prints one, two, three
foreach( key , : _G )
	println( key ); // prints names of all global variables
foreach( key, value : myObject )
{
	if( string_part( key, 0, 2 ) == "!_" )
		println( value ); // print all values for keys beginning with "!_"
}
foreach( value : data )
{
	if( value === false )
		continue;
	if( value )
		break;
}

Advanced concepts

Bitwise operations

hex = 0x12400;
bin = 0b10011010;
b_or = hex | bin ^ hex & bin;

The main difference from other languages is that the integer type by default is 64 bits long. Left/right shift rules are equal to those of the C language. Thus, it is safe to use only shift distances between 0 and 63.

Truth tables for AND/OR/XOR operations

AND01 OR01 XOR01
000 001 001
101 111 110


Building with SGScript

This section describes all supported ways to compile SGScript and integrate it into your project.


Downloading SGScript

There are two kinds of downloads - source and binaries. All downloads are available in the download page. Source files are hosted on Github.

Even though master branch is supposed to be the 'stable' branch, it is highly suggested that apidev branch is tried first since it is the most up-to-date branch, it is expected that generally less bugs are there. master branch is more thoroughly tested at the time of release and apidev may contain recent changes that subtly break the build.


Building with GNU Make

Required software

Building

@ECHO OFF
mingw32-make %*

Build options

Targets:

Options:

Additional build options & platforms


Building with IDEs

All IDE-related build data is under the build directory.

Supported IDEs

IDEs with necessary files but without support


Building with other tools

CMake

There is a file CMakeLists.txt in the root directory of the project. It only builds the dynamic library at the moment.

File integration

The code is made to support various compilers and compile under C and C++ so it is quite safe to just drag & drop it into your project.


Including SGScript into your project

There are generally two ways to include SGScript: link the library or include the files directly.

Note: Including the files isn't an option if SGScript modules are to be used.

For simplified inclusion of files, add src/ and ext/ to the include paths.