OneScript Overview
This provides a basic overview of the OneScript language and how it relates to the Macro Virtual Machine.
In reverence to all other languages OneScript offers the same capabilities as many other languages:
General Terms
These general terms refer to all elements of OneScript
ident
- refers to an identifier (for a variable or constant) that can contain almost any character, including Unicode characters. Identifiers can’t contain whitespace characters, mathematical symbols, arrows, private-use Unicode scalar values, or line- and box-drawing characters. Nor can they begin with a number, although numbers may be included elsewhere within the name.For example, 😺🙈, ⿅⿇⿌ and
init
.literal
- a literal can refer to a value of numeric, string, boolean, date or set value.numeric
- numeric values can be integers or floating point values.string
- string values.date
- date and/or time values.set
- sets are a special collection of values that can be used with the categorical or multiple choice type questions in a survey.enum
- a value type defined by a set of named constants of the underlying integral numeric type.
Numeric Constants
Numerical constants can be in decimal, hexadecimal, binary, octal or float point. These can take the following forms:
Type
Example
decimal-value
1, 2, -3
hexadecimal-value
0x12F
binary-value
0b10111
octal-value
0o1234567
floating-point-value
0.23, -.12, -123.45, 0f23
Date Constants
Because OneScript is a researchers programming language dates and times are handled in a specific manner.
A basic date can be defined
Set Constants
Set constants can be an alpha optionally followed by an alphanumeric separated by a comma and surrounded by braces. For example:
{a}
{_1,_2}
{a1}
{a,b1}
{abc}
Enum Constants
Enum constants are a way to using an identifier to reference a constant value rather that placing the constant value everywhere in the code. For example:
Whitespace
OneScript programs are defined through the OneScript language which is case sensitive. It supports each keyword and associated operator can be separated with whitespace. The following can be considered to be whitespace:
Space
Tab
//
followed by a comment/*
with a comment*/
Keywords
The following keywords are reserved and may not be used as identifiers:
Basic Data Types
The following basic data types are built into OneScript
Type
Description
bool
a boolean value that be either true or false.
byte
an unsigned byte value
int
a signed integer value
float
a floating point value
date
a date value
string
a string value
enum
an enum numeric value.
Set
a sets value
object
an object
void
used to define the absence of a value (for example a method with no return value)
Objects
object
refer to instances of classes that cannot be defined by the base types (bool, int, float, date, enum and string).
Void
void
refers to a 'no value' and is used to explain what is returned in a method.
Null
null
refers to a reference that has no value. This can be used to detect whether a value has been instantiated or not. If an object has not yet been instantiated then it is automatically set to null.
String Constants
String constants are delimited with a double quote "
. String constants are Unicode with the ability to add encoded values from the following list:
\n
- new line\r
- return\t
- tab\\
- back slash\"
- double quote""
- double quote
Program Structure
OneScript is a "Light Object Orientated Language". This means it does not support inheritance within its own language, but does support it through the objects it can create from libraries.
Basic statements and declarations in a OneScript program are added to a "Main" static class if they are not declared explicitly within a class. The "Main" class is always used as the entry point to a Macro Virtual Machine program.
Declarations
Basic declarations take the following form:
Collections
OneScript supports the concept of collections in a number of different ways. This section covers them.
Arrays
Declare arrays using brackets ([]
), and create the array using the new
statement.
Array values can also be initialised as part of the declaration.
Access the array elements using brackets:
Currently only one dimensional arrays are supported. Multi dimensional arrays can be supported by creating nested arrays.
Ranges
Ranges are used to refer to set of values to reference. For example:
The above statement declares a range of integers from 4 to 6 inclusive of the 4 and 6. Ranges can include any basic types. For example:
Ranges can be used in the place of for
statements:
The above statements create an array and set values 3 to 6 to specific values.
Statements
Statements are used to control the flow of a program and manipulate data defined in declarations. The following demonstrates a simple OneScript program:
Blocks
Statements can be grouped into blocks by using the {
and }
braces. This allows statements to refer to a group of statements rather just one. For example:
In some instances the a single statement can be used in place of a block. For example:
Flow Control
OneScript contains the following program flow controls.
If Statements
A basic if
statement requires a condition and a block referring to a statement that is executed if the statement results in a boolean value of true
. For example:
A more extensive if
statement includes the else
statement. For example:
It is possible to chain if statements together by using the else if
phrasing:
Immediate If Statements
To save coding time it is possible to place an if statement within a statement:
If the condition is true then the first part of the immediate if phrase is performed otherwise the second phrase. In the case above, a
equals 0
so the "0"
will be chosen.
Do..While Statements
Do while statements follow the flow of performing a statement or block while an expression is true. For example:
While Statements
While statements will perform the following statement or block whilst an expression is true. For example:
For Statements
For statements consist of a set of statements that control a loop. 1. A declaration initializer 2. A test 3. A changer
The simplest form of this is:
In this example the int i = 0
is the declaration initializer, the i < 5
is the test and the i++
is the changer.
Iterator For Statements
Iterator for statements are based on an iterator being implemented by the object be assessed. OneScript has a standard set of properties and methods that allow the iterator for
statement to be to used.
Range For Statements
Range for statements take the following form:
In the above example the total
will equal 15.
Array For Statements
Array for statements take the following form:
In the above example the array of animals are wirtten to the console.
Break and Continue
The break
statement will force the for loop to finish and move onto the next statement. The continue
statement will force the process to jump to the next changer, if present, statement continuing the loop.
Implied For Statements
Implied range for statements use range specifications:
In the above example all values in the array are set to value 5. The ..
is a special range specifier that refers to all elements.
Switch Statements
Switch statements supports any kind of data type and is designed to support testing for for equality of a value against other values:
Like many other languages the break statement will route the logic to the next statement outside of the switch block. Without the break statement the flow will continue to the next case statement.
Goto Statements
Goto statements rely on a label. They are basic representation of branching to a new statement. The following is an example:
Methods
Methods are declared by using parentheses and a block of statements. For example:
A method will automatically return after the last statement in the block or when a return statement (or throw statement) is performed.
It is important the return value matches the declaration of the method itself.
Return Statements
Return statements are used to exit methods and return to the call include method. When the end of the statements are reached in a method an automatic return is carried out.
Error Handling
Error handling automatically handled by the Macro Virtual Machine. By default, when the application generates an error it will fail with unpredicted results and the user of the application must deal with it.
To take control of any errors it will be necessary to implement a try..catch
coding control. For example:
It is possible to provide a set of catch clauses for different errors. For example:
Ultimately if the errors are not met then the error falls through to the next error handler in the stack. The catch statement can optional declare a variable that is store at the private method block level.
It is possible to force an error with the throw
statement. for example:
Assertions
Assertions are checks that happen at runtime. They are used to make sure an essential condition is satisfied before executing any further code.
In the above example, the assertion will fail and cause an exception, which if not handled will force the program to stop.
Snapshots
Snapshots are a unique feature of OneScript to support the ability to move back and forth in an application by defining a snapshot
of the application to jump back to later.
A snapshot will hold information about the virtual machine position in the code and the restoration of a snapshot will automatically jump the flow of the application back to the statement directly after the save snapshot statement with the equivalent name.
State Management
For full access to state management the OneScript Software Development is available. For general access we have built a set of environments that support the running of a survey.
Operators and Expressions
OneScript supports a number of operators that can be split into the following groups:
Arithmetic Operators
Arithmetic operators can be applied to byte
, int
and float
. All types are signed.
Unary Operators
Unary operators can be applied as prefixes or suffixes.
Name
Description
++
Increment operator. Applied as a prefix the value is incremented by 1 before it is used. Applied as a suffix it is incremented by 1after the value is used.
--
Decrement operator. Applied as a prefix the value is decremented by 1 before it is used. Applied as a suffix it is decremented by 1 after the value is used.
Binary Operators
Name
Description
+
Addition operator can be used to add integers or floating point number. It can also be used to concatenate strings and sets.
-
Subtraction operator can be used to subtract integers, floating point numbers and sets.
/
Division
*
Multiplication
%
Modulus
Bitwise Operators
Logical operators can be applied to byte
and int
.
Name
Description
&
Bitwise and
|
Bitwise or
^
Bitwise exclusive or
Conditional Operators
Condition operators can be applied to ...
Name
Description
==
Equal to
!=
Not equal to
>
Greater than
>=
Greater than or equal to
<
Less than
<=
Less than or equal to
It is also possible to compare a value to null
is Operator
The is
operator can be used to check the type of an object and returns a boolean value to indicate a match:
?? Operator
The ??
operator (also known as the null coalescing operator) can be used to check whether a value is null
and if provide another value to use as the expression result:
If the question.Response.Value is equal to null
then use an empty string as the result.
Operator Precedence
The following table shows the operator precedence. This can be overridden with the use of brackets around those parts of the expression that should take precedence.
Name
Description
-- ++ ! (type)
unary operators
*
/ %
Multiplication, division, modulus
-
+
Subtraction, addition
is
is operator
< > <= >=
Comparison operators
== !=
Comparison operators
& | ^
Bitwise operators
&&
Logical And
||
Logical Or
??
Null coalescing
?
Immediate if
Fields
To make OneScript a researchers language it supports the definition of fields that hold the definitions of questions to be asked and results to be stored for analysis. Fields are declared in a field block:
A field can describe the options, language and controls to be used in the asking of a question, but is also accessible through the object interfaces (IInterview, IQuestion, ICategory, etc.) automatically generated for OneScript.
When the Ask
method of a question is used a Snapshot is automatically generated to support the ability to move back and forth through questions. A feature that is supported by DIY Surveys.
Classes
Classes are a way of encapsulating methods and properties together. Unlike other languages OneScript classes only have basic capabilities and cannot be inherited or used in polymorphisms.
An instance of a class can be created in the following way:
Structures
Structures are similar to classes in that they can contain methods and properties.
Structures are not instantiated in the same way as a class:
Last updated