Skip to content
Neuron Teckid edited this page Mar 14, 2017 · 2 revisions

Expression

Factors

Boolean Literals

As the keywords

true
false

Number Literals

A number literal is a decimal. For example

0
1
1.0
2.71828
3.14

Underscores could be added between the digits for readability. For example

123_456_789
123_456.789_012

Single-quoted String Literals

String literals within quotes or double quotes. The same as in Javascript.

Triple-quoted String Literals

String literals enclosed in matching groups of three single or double quotes, in which, unescaped newlines and quotes are allowed. For example

'''<input type='text' value=''>'''  
"""
    The quick brown fox jumps over
    a lazy dog.
"""

Identifiers

An identifier starts with an underscore or an alphabet, followed by an arbitrary number of underscores, alphabets or digits.

Arrays and Objects

Arrays and objects have the same syntax structure as in Javascript, except that an extra is allowed after the last item (pair).

For example

[exprA, exprB, exprC]
[exprA, exprB, exprC, ]

{keyA: valueA, """keyB""": valueB}
{'keyA': valueA, keyB: valueB, }

Regular Expression

The same as in Javascript.

Pipeline Objects

Special factors that are only used in pipeline context.

Use $ for pipeline element, $k for pipeline key, $i for index.

Exception Object

A special factor that is only used in a catch scope.

Use $e for it.

This

The keyword this represents the context object.

Others

These literals are the same with their Javascript concept

null
undefined
NaN

Compound Expressions

Flatscript uses similar operators as in Javascript, with some precedence changes.

Arithmetics

Flatscript uses binary arithmetics operators and unary arithmetics operators that are available in Javascript for arithmetics expressions.

For example

1 + 1
2 - x
y * z
p / q
d % m

-n
+i

Unary + and - have a higher precedence than binary *, / and %, and the latter three have a higher precedence than binary + and -.

Array Concatenation

A shortcut for calling .concat of an array. For example, these two expressions are the same

[1, 2] ++ [x, y]
[1, 2].concat([x, y])

++ has the same precedence with binary + and -.

Bit Operations

Flatscript uses the bit operators as in Javascript, including unary ~ (bit reverse), binary | (bit or), binary & (bit and) and binary ^ (bit xor).

~ has the same precedence with unary - and +. Binary bit operators have a lower precedence than binary +, -, and ++.

Relation Operators

Relation operators include instanceof and typeof operators that are used in Javascript, and the owns operator that is used as a shortcut for hasOwnProperty.

For example

obj owns 'key'
obj.hasOwnProperty('key')

Relation operators have a lower precedence than bit operators.

Comparation

Comparation operators are =, !=, <, >, <= and >=.

= is translated to === and != is translated to !== in Javascript.

Comparation operators have a lower precedence than Relation operators.

Logical connectives

Logical connectives are unary negation !, "binary and" && and "binary or" ||.

Unary negation has the highest precedence among them, while its precedence is lower than any comparation. Then "binary and" has a higher precedence than "binary or".

For example

!a = b || c != d && e < f

evaluation order is (from to )

    !m = n || p != q && x < y
#   ^  a   ^    b    ^    c
#   d      ^         ^     
#          ^         e
#          f

Conditional

A ternary operator to choose a value from two expressions by a predicate. For example

x if predicate else y

if and else are the operator keywords. The value of this expression depends on the value of predicate. if predicate is a true value (true, a number which is not zero or NaN, non-empty string or any other object which is not null or undefined), the expression will be evaluated as x, otherwise y.

Member Access

A member access is an expression and a property name identiferied linked with a dot (.). For example

a.b
'the quick brown fox'.length

Member Lookup

As in Javascript, an expression enclosed in a pair of [ and ] following another expression is a member lookup. For example

list[0]
obj['key']

List slice

A list slice is to enclose two expressions "begin" and "end" separated by commas, or with another "step" expression, in a pair of [ and ], following another expression, which is expected as an array.

This syntax is used to obtain a subsequence of an array. The expression "begin" indicates where the subsequence starts and the expression "end" indicates indicates the stop.

The optional part "step" indicates how to sort or skip elements, which has a default value 1 when "begin" is greater than "end" or -1 otherwise. When it is negative, the array is iterated reversally. An exception is thrown when it is zero.

For example

[1, 1, 2, 3, 5, 8][2, 4]

is equivalent to

[2, 3]

A begin index of 2 and an end index of 4 will slice the array from the 2nd element to the 4th element.

Another example

[2, 3, 5, 7, 11][1, 4, 2]

is equivalent to

[3, 7]

A step of 2 will obtain an element in every 2 elements.

The begin index or end index could also be omitted. By default the begin index has the value 0 and the end index is the length of the sliced array. When omit these values, the comma should be left.

For example

[2, 3, 5, 7, 11][,, 2]

slice the array from it begin to end and pick 1 element in every 2, so the result of this expression is

[2, 5, 11]

Anonymous Functions

An inline anonymous function is a parameter list followed by a colon (:) and a returning expression.

A multiple-line anonymous function is a parameter list followed by a colon, and then immediately a line-break, before its function body scope.

See functions for details.

Function Calls

A function call is an expression followed by a list of arguments enclosed in a pair of ( and ). For example

parseInt('123')
Math.min(2, 9, 4, 3, 8)

Superclass Function Calls

Similar to a function call, a superclass function call is in a form of super.PROPERTY_NAME(ARGUMENTS) where super is a keyword, PROPERTY_NAME is an identifier and ARGUMENTS is a list of comma-separated expressions.

See classes for details.

Typeof

A typeof expression is a typeof keyword preceding an expression, to obtain its type as a string. For example

typeof x
typeof undefined

Wrapped Expression

Any expression wrapped in a pair of ( and ). For example

(a + b) * (c - d)