2015-12-09 23:46:06 +09:00
## Language
SunScript is the name of the language parsed natively by _ssc_ . It closely resembles both JavaScript and the native byte-code used by Super Mario Sunshine's SPC interpreter. A SunScript file uses the extension ".sun" for convenience. Below is a brief summary of the language's syntax.
### Comments
Both single-line comments and multi-line comments are supported. A single line comment begins with `//` and ends at the next new-line character. A multi-line comment begins with `/*` and ends with `*/` .
### Variables
2015-12-19 09:53:06 +09:00
A variable is created by use of the `var` keyword and may initially be either_ declared_ or _defined_ .
A declaration leaves the variable with an undefined initial value, while a definition assigns a value to the variable explicitly:
2015-12-09 23:46:06 +09:00
```
2015-12-19 09:53:06 +09:00
var a; // declares a variable with an undefined value (may be assigned one later)
2015-12-09 23:46:06 +09:00
var b = 33; // defines a variable by explicitly giving starting value
```
2015-12-19 09:53:06 +09:00
---
There are three primitive types in SunScript, as shown in the following table.
All variables are dynamically typed and can change at any time by simply assigning it an expression of another type.
2015-12-09 23:46:06 +09:00
|Type | Example|
|:-----|-------------:|
|int |`132`, `-0xff` |
|float |`15.64`, `9.0` |
|string| `"foo\nbar"` |
SunScript also has the two boolean keywords `true` and `false` (currently defined as the integers one and zero, respectively).
---
2015-12-19 09:53:06 +09:00
To get the type of any variable or expression, use the `typeof` statement:
2015-12-09 23:46:06 +09:00
```
var a = 1.1 * 8;
2015-12-19 09:53:06 +09:00
var b = typeof(a);
var c = typeof(1.1 * 8); // you may also put expressions as the argument
2015-12-09 23:46:06 +09:00
```
2015-12-19 09:53:06 +09:00
Constants for the possible return values are defined in the [system.sun header ](stdlib/system.sun ).
---
2015-12-09 23:46:06 +09:00
To explicitly cast to the integer and floating-point types, use the `int` and `float` casting statements:
```
const PI = 3.14;
var piIsExactlyThree = int(PI); // *gasp*
```
#### Constants
2015-12-19 09:53:06 +09:00
Read-only variables may be created via the `const` keyword.
Only constant definitions are allowed; constant declarations are **not** supported.
2015-12-09 23:46:06 +09:00
2015-12-19 09:53:06 +09:00
To save space in the output binary, constants are not stored in the symbol table, nor are they compiled in the text section.
Instead, they are simply evaluated each time they are actually used (similar to macros in CPP):
2015-12-09 23:46:06 +09:00
```
const PI = 3.14;
const TWOPI = 2 * pi;
const R = 300.0;
var circ = R * TWOPI; // this actually compiles to 'var circ = r * (2 * (3.14));'
```
_**Note:** You may have function calls or any other valid expression assigned to a constant._
### Functions
2015-12-19 09:53:06 +09:00
There are two types of callables in SunScript: _builtins_ and _functions_ .
A builtin may only be declared (must not have a body), while a function may only be defined (must have a body).
Callables may have any number of parameters.
2015-12-09 23:46:06 +09:00
2015-12-19 09:53:06 +09:00
To define a function, use the `function` keyword.
For builtins, use the `builtin` keyword:
2015-12-09 23:46:06 +09:00
```
builtin getSystemFlag(flag);
builtin setSystemFlag(flag, value);
function setOnSystemFlag(flag) { setSystemFlag(flag, true); }
function setOffSystemFlag(flag) { setSystemFlag(flag, false); }
```
2015-12-19 09:53:06 +09:00
A callable may have any number of parameters.
Each parameter is dynamically typed.
A builtin may have a variadic signature by specifying an ellipsis keyword `...` as the final parameter (variadic functions are **not** supported).
2015-12-09 23:46:06 +09:00
2015-12-19 09:53:06 +09:00
A callable's return value is also dynamic.
Use a `return` statement to override the interpreter's default return value for a given code path.
2015-12-09 23:46:06 +09:00
---
2015-12-19 09:53:06 +09:00
Functions and builtins may be called either as standalone statements or in expressions.
To call a function or builtin, simply pass its name, followed by any arguments, each separated by a comma:
2015-12-09 23:46:06 +09:00
```
appearReadyGo(); // calls the function 'appearReadyGo' with no arguments
insertTimer(1, 0); // calls the function 'insertTimer' with two arguments, '1' and '0'
```
2015-12-19 09:53:06 +09:00
_**Note:** You cannot call a function or builtin in code preceding its definition or declaration, respectively.
All callable declarations and definitions must also be in the global scope._
2015-12-09 23:46:06 +09:00
### Operators
2015-12-19 09:53:06 +09:00
The following table describes the operators supported by SunScript, as well as their precedence and associativity:
2015-12-09 23:46:06 +09:00
|Precedence|Symbol|Name|Associativity|
|:--------:|:-----|:---|:-----------:|
|-1|`=`< br > `+=` `-=` `*=` `/=` `%=` `&=` `|=` |assignment< br > compound assignment|right|
|0|`||`|logical-OR|left|
|1|`& & `|logical-AND|left|
|2|`|`|bitwise-OR|left|
|3|`& `|bitwise-AND|left|
|4|`==` `!=` |equality and inequality|left|
|5|`< ` `>` `<=` `>=` |comparison|left|
|6|`<< ` `>>` |bitwise-shift|left|
|7|`+` `-` |addition and subtraction|left|
|8|`*` `/` `%` |multiplication, division, and modulo|left|
|9|`!`< br > `-`|logical-NOT< br > negation|right|
|10|`[?:]`|ternary conditional|right|
### Flow Control
SunScript has support for `while` , `do` , and `for` loops, as well as the `exit` , `break` , `continue` , and `return` statements:
```
function checkTime(time) {
if (time < 30 ) {
startMiss();
exit;
}
}
for (;;) {
checkTime();
if (stop) {
break;
}
}
```
#### Named Loops
2015-12-19 09:53:06 +09:00
Loops may be named.
To name a loop, simply prefix the loop with a label.
`break` and `continue` statements may be passed the name of the loop which they affect:
2015-12-09 23:46:06 +09:00
```
outer_loop:
2015-12-11 11:45:35 +09:00
for (var a; a < 4 ; + + a ) {
for (var b; b < 4 ; + + b ) {
2015-12-09 23:46:06 +09:00
if (b == 2) break outer_loop;
}
}
```
### Importing
2015-12-19 09:53:06 +09:00
You may split a script amongst several files.
Doing this requires the use of the `import` statement.
Simply pass the name of the SunScript file to import:
2015-12-09 23:46:06 +09:00
```
2015-12-19 09:53:06 +09:00
import "constants.sun"; // looks for 'constants.sun' in current directory, then in the compiler's directory
2015-12-09 23:46:06 +09:00
import "C:/math.sun"; // looks for 'math.sun' in the root C:/ drive only
```
2015-12-19 09:53:06 +09:00
_**Note:** It is recommended to use forward slashes as a path delimiter as a single backslash will be interpreted as an escape._
2015-12-09 23:46:06 +09:00
2015-12-19 09:53:06 +09:00
The `import` statement supports both relative and absolute path names.
If the path is relative, then the file will be looked in two places: first, the path to the file currently being compiled; otherwise, if not there, the path of the compiler executable.
If the path is absolute, only that path is searched as-is.
2015-12-09 23:46:06 +09:00
2015-12-19 09:53:06 +09:00
A compiler error will occur if the file cannot be found.
2015-12-09 23:46:06 +09:00
_**Note:** In order to prevent recursive inclusion, ssc keeps track of all imported files and will ignore `import` statements whose files have already been compiled before._