Contents
Table of Contents |
---|
To use SIL like any other scripting language follow a few simple syntax rules.
General rules
...
Info |
---|
This topic provides an overview of the basic elements of the Simple Issue Language (SIL) scripting syntax. |
...
...
...
...
...
...
...
...
...
Types
Basic Types
SIL defines the following base types:
- string - defines a string, example literal is "abc"
- boolean - defines boolean values true and false
- number - any numeric (integer or real) value
- date - a date, like "2010-03-30"
- interval - a time interval, like "2d 3h"
Important notes
- interval - data type does not represent the working interval (i.e. 8h), but the full interval. When we created SIL™ we thought about if we should introduce a working interval type (i.e. 8h / day) but this would have certain undesired implications (how long is your workday? in France, for instance, Friday is only 6h). With our custom fields, it's a matter of interpretation of the interval, and we have routines to help you transform a 24h interval to a working interval, or you can simply write your own.
- date - data type can be used to represent dates and date-times.
- number - data type internal storage is represented only by real numbers (double). However, the language accepts for historical reasons type declarations as "integer", "int", "real", "double", "float" (treating them as "number", of course). Until now we have found no limitations regarding the uniform treatment of numbers.
Arrays
...
Complex data types
Arrays
Arrays are created by adding []
after any basic type. The following list represents valid arrays:
string[]
boolean[]
number[][]
integer[]
date[][][]
interval[]
You can also create arrays for a user-defined structure:
Person[][]
...
Key-
...
value arrays
Arrays
...
in SIL where positions in the list
...
act as keys. Key-value arrays (or maps) are declared
...
like regular arrays.
Learn more about
...
using the indexing operator to create key-value maps and retrieve values from them.
Structures
...
Structures are user-defined data types. A structure in SIL is like a container that can hold different types of data together. The syntax for defining a structure is:
Code Block |
---|
struct |
...
StructureName { |
...
type1 field1;
type2 field2;
// ... more fields
} |
The type of each field can be a
...
basic data type, a user-defined structure
...
, a self-referential structure, or arrays of these types.
...
The following examples show how to define a structure with each type:
Code Block |
---|
...
struct Address { string street; string city; string zipCode; //Basic data types in structure } struct Employee { string id; |
...
string name; Address workAddress; // Structure inside structure Address homeAddress; |
...
// Can use same type multiple times
}
struct Department {
string name;
Department parent; // A Department can reference itself
Department[] subDepartments; // Can have array of same type
} |
Panel | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
Best practices when creating structures:
|
To access the value of a field from a variable in a structure, use the syntax
...
title | Example |
---|
...
:
Code Block |
---|
<varName>.<fieldName> |
Code Block | ||
---|---|---|
| ||
// considering structure Person defined above
Person p;
p.name = "John Doe";
p.id = "1234567";
string hisManager = p.manager.name;
string employee = p.subordinates[0].name; |
Constants
Info |
---|
This topic is about constant values, NOT about the read-only attribute of variables. |
Single Value Constants
For numbers and boolean values, constants are represented without any additional alterations.
You can initialize structures field-by-field:
Code Block |
---|
Employee emp;
emp.id = "E123";
emp.name = "John Doe";
emp.workAddress.street = "123 Work St";
emp.workAddress.city = "Work City"; |
Alternatively, you can do a one-line initialization, including nested initialization:
Code Block |
---|
struct Point {
integer x;
integer y;
}
Point p = {10, 20}; // Initialize all fields at once
struct PersonInfo {
string name;
number salary;
Point location;
}
PersonInfo person = {"John Doe", 50000.00, {10, 20}}; // Nested initialization |
Literals
Literals are fixed values that appear directly in the code. In SIL, literals are used to represent constant values of different data types.
Note |
---|
Literals refer to the constant values used in scripts, NOT the use of a constant which is the readonly attribute of variables. |
Type | |||
---|---|---|---|
Basic type literals |
|
...
|
...
| |
String, Date, and Interval literals | These types require double quotes (" "). |
...
|
...
|
...
title | Example |
---|
...
|
...
|
...
|
...
|
...
|
...
|
...
|
...
| |
Array |
...
literals | Arrays |
...
use curly braces and commas:
|
...
title | Example |
---|
|
...
|
...
|
...
title | Example |
---|
...
| |||||
Special literals | You can use
|
Variables
Variables are named containers that use the data types. They are like labeled boxes that can hold specific values.
Code Block |
---|
// Variable format: <data type> <variable name> // Variable declaration: string name; // A container that can hold text number pi; // A container that can hold decimal numbers //You can also have an array of the type of variable# { variable1, variable2, variable3 }. // where variable# can also be an array |
Note | ||
---|---|---|
| ||
We currently do not support constant representation of key-value arrays. You will need to build one using the indexing operator. |
Variable Declaration
The general syntax for declaring a variable is:
Code Block |
---|
<type> varname; // declaration without initialization
// or
<type> varname = <expression>; // declaration with initialization |
You can create an array from variables as illustrated in the following example:
Code Block |
---|
// First, declare some variables
string name1 = "John";
string name2 = "Jane";
string name3 = "Bob";
// Create an array from these variables
string[] names = {name1, name2, name3};
// Result: names = {"John", "Jane", "Bob"} |
You can also create array variables:
Code Block |
---|
// First, declare some array variables
string[] teamA = {"John", "Jane"};
string[] teamB = {"Bob", "Alice"};
string[] teamC = {"Charlie", "David"};
// Create an array of arrays from these variables
string[][] allTeams = {teamA, teamB, teamC};
// Result: allTeams = {
// {"John", "Jane"},
// {"Bob", "Alice"},
// {"Charlie", "David"}
// } |
Variable declaration and initialization
To give value to your variables, you initialize them either when you declare them or later.
Code Block |
---|
string employeeName; // Just declaring employeeName = "John"; // Initializing after declaration string employeeAge = 25; // Declare and initialize at once // More examples of variable declaration integer random = 2; number pi = 3.14; boolean valid; date today = currentDate(); interval spent = "1h 30m"; interval estimate = "2d" - spent; number [][] matrix = {{0,1}, {2,3}, {4,5}}; |
...
Constants
Variables can be made
...
readonly by adding the keyword
...
const
...
before the data type when the variable is first defined.
Code Block |
---|
const string COMMA = ",";
const number PI = 3.14; |
If set to an array or structure, the
...
readonly attribute will be applied to all the elements of the array or all fields of the structure.
Routine Declaration
In addition to the routines provided out-of-the-box by SIL, you can define your local routines (user-defined routines or UDRs in short) that use the following syntax:
Code Block |
---|
function <name>(<type> param1, <type> param2, ...) {
Instruction1;
...
InstructionN;
return <value>;
} |
Explicit Type Casts
...
Explicit type casting
When types are compatible, SIL automatically converts one type to another. Automatic conversion can lead to ambiguous results. If you want to tell SIL how to convert a type, use explicit type casting with the following syntax:
Code Block |
---|
(<target_type>)varname |
The following example illustrates how explicit type casting returns accurate results:
Code Block |
---|
...
title | Example |
---|
integer n = 1; number x = 3.14; string s = "2"; //Ambiguous - could mean different things: return n + s; // |
...
SIL |
...
doesn't |
...
know |
...
if |
...
you |
...
want "12" |
...
or 3 //Explicit - clearn intentions: return (string)n + s; // |
...
Forces n to be treated as string "1" - clearly wants a string "12" return n + (number)s; // |
...
Forces s to be treated as number 2 |
...
- |
...
clearly |
...
wants |
...
a number (3) |
Note |
---|
...
Note |
...
: Not all type casts are valid. |
...
Converting an interval to a date or a date |
...
See also
...
to a string is an example of invalid type casting. |
User-defined functions
SIL comes with many out-of-the-box functions by SIL, but allows you to define local functions, referred to as user-defined routines (UDRs). To define and declare a function, use this general syntax:
Code Block |
---|
function <name>(<type> param1, <type> param2, ...) {
Instruction1;
...
InstructionN;
return <value>;
} |
For additional details, check the User-defined routines (UDRs) topic.