Skip to content

Language Specification

Jing Lu edited this page Dec 10, 2013 · 52 revisions

A brief of Language Specification.

Language Elements

  • Expression
  • Statement
  • Expression Statement
  • Block

Expression

An expression contains Mathematical operations, variable assignment, function calling and primary expression. Expression should not end with semicolon:

1 + 2 + 3               // mathematical expression
a = 10                  // assignment expression
func()                  // function call expression
[1, 2, 3]               // array literal expression
{key: 'value'}          // object literal expression

Expression can be nested within '(' and ')':

func() * (1 + 2)

Primary Expression

Primary expression is a variable, digits const, object literal, array literal and etc. Primary expression used to build an entire expression.

a                       // variable
true                    // bool const
13.5                    // number const
0xA2F0                  // hex const
0b1101                  // binary const
[1,2,3]                 // array literal
{key:'value'}           // object literal

Expression could not be executed directly, it should be contained by Statement or Expression Statement.

Statement

Statement is language control syntax, commonly it indicates that what the code should be executed in what cases. The following statements are available in ReoScript:

e.g.

if (1 + 1 == 2) a = 3;

See Statements.

Expression Statement

Expression Statement is an assignment statement, function call expression or unary expression, etc. Unlike expression, an expression statement can finish an completed operation without other descriptions. Expression Statement should end with a semicolon:

a = 10;                  // assignment expression statement
func(1, 2);              // function call expression statement
return false;            // terminal and return expression statement

Assignment Expression

Syntax of Assignment Expression:

identifier = ( assignment-expression | expression )

For example:

a = 10;                  // OK
a = b = 10;              // OK (right to left)
10 = 10;                 // error: const value cannot be assigned

Function Call

Syntax of Function Call:

expression '(' [expression [, expression]*] ')'

For example:

function func1() { }
func1();                 // OK

var func2 = function() { };
func2();                 // OK

var arr = [ function() { } ];
arr[0]();                // OK

function func3() {
    return function() { }; 
}
func3()();               // OK

Unary Expression

Unary Expression is one of following expressions:

i++                      // post-self-increment-expression, equals i = i + 1, calc after accessing
++i                      // pre-self-increment-expression, equals i = i + 1, calc before accessing
-i                       // minus operation
~i                       // bitwise not operation
new expression           // create instance (where expression should be function)
typeof expression        // retrieve type of object

Block

Statement and Expression Statement grouped in a scope within '{' and '}' is called Block:

{
    a = func() * (1 + 2);
    console.log(a);
}

Note: It is same as ECMAScript/JavaScript, a block is not a variable scope. Only functions has call-scope, see more about Functions.

if (true) {
    var a = 10;
}

console.log(a);          // access variable outside a block

The result is:

10

See Variables and Functions.

Data Types

The 3 kind of data type are managed and can be processed in ReoScript.

  • Primitive type (null, number, boolean, string, object, function)
  • ReoScript Object (implemented by .Net type ObjectValue)
  • Any .Net Objects (available with DirectAccess)

Primitive type

Object Value

ReoScript uses standard ECMAScript/JavaScript data types and wrapper objects.

The following types are primitive type:

  • object
  • number (contains char, byte, int, long, float, double)
  • string
  • boolean
  • function

NOTE: typeof keyword can only return the primitive types above.

The following data types are wrapper objects, they all has mapped constructor:

  • Object
  • String
  • Function
  • Boolean
  • Number
  • Array
  • Date
  • Error

See more about wrapper object.

Operators

Unary Operators

-a                       // minus operator
++a                      // pre-self-increment operator (calc before accessing)
a++                      // post-self-increment operator (calc after accessing)
--a                      // pre-self-decrement operator (calc before accessing)
a--                      // post-self-decrement operator (calc after accessing)
a()                      // function call operator
new                      // creating instance operator
delete                   // delete property from object
typeof                   // retrieving data type operator

Calculating Operators

a + b                    // add a and b, or combine object a and b
a - b                    // subtract b from a
a * b                    // multiply a by b
a / b                    // divide a by b
a % b                    // get the remainder of division of a by b

Bitwise operators

~a                       // bitwise NOT - Inverts the bits of its operand.
a & b                    // bitwise a AND b
a | b                    // bitwise a OR b
a ^ b                    // bitwise a XOR b
a << b                   // shift a by b bits to the left
a >> b                   // shift a by b bits to the right

Relation operators

All relation operators return a boolean result.

a > b                    // check whether a is greater than b
a < b                    // check whether a is less than b
a == b                   // check whether a is equal to b
a >= b                   // check whether a is greater than or equal to b
a <= b                   // check whether a is less than or equal to b 
a === b                  // strict compare two operands (value and type)
a !== b                  // strict compare two operands NOT (value and type)

Condition control operator

boolean-expression ? expression : expression

Index access operator

expression '[' expression ']' 

Property access operator

expression '.' identifier

Keywords

All Keywords

break                       case                        catch                       continue
default                     delete                      else                        finally
for                         function                    if                          in
instanceof                  import                      new                         null
return                      switch                      this                        throw
try                         typeof                      undefined                   var
while

'undefined' is absolutely same as 'null' keyword in ReoScript.

Keywords not be supported by ReoScript

debugger                    do                         with

Keywords only be supported by ReoScript

import

See import keyword.

Reserved Keywords

public                     protected                   internal                     private

Built-in Objects

Math

Math object contains:

Math.PI     = 3.141592653589793
Math.E      = 2.71828182845904
Math.LN     = 0.6931471805599453
Math.LN10   = 2.302585092994046

Math.min = function(a, b) { return a > b ? b : a; };
Math.max = function(a, b) { return a > b ? a : b; };

Math.random(max)
Math.round(number)
Math.floor(number)
Math.sin(angle)
Math.cos(angle)
Math.tan(angle)
Math.abs(number)
Math.pow(number, pow)

console object

console.write(object)
console.log(object)
console.read()
console.readline()
console.clear()

debug object

debug object is available when ScriptDebugger is created for ScriptRunningMachine.

debug.assert(boolean)
debug.assert(but, expect)
debug.Stopwatch

JSON object

JSON.parse(string[, handler])
JSON.stringify(object[, handler])

See JSON.

Built-in Functions

Built-in Standard I/O functions:

__stdin__                  __stdinln__                 __stdout__                 __stdoutln__

Wrapper object and type converting functions:

Object                     Function                    String                     Number
Array                      Date                        Boolean                    Error
parseInt                   isNaN

Evaluation functions:

eval

Async-calling functions:

setTimeout                 clearTimeout               setInterval                 clearInterval              

See Enhanced Async-calling.

User interacting functions:

alert                      confirm                    

Tip: Some built-in objects and functions could be disabled by setting CoreFeatures.

See Also

Clone this wiki locally