2.4. Expressions

2.4.1. Assignment

exp := exp '=' exp
exp := exp '<-' exp
exp := exp ':=' exp

daScript implements 3 kind of assignment: the copy assignment(=):

a = 10

copy assignment is equivalent of C++ memcpy operation:

template <typename TT, typename QQ>
__forceinline void das_copy ( TT & a, const QQ b ) {
    static_assert(sizeof(TT)<=sizeof(QQ),"can't copy from smaller type");
    memcpy(&a, &b, sizeof(TT));
}

“move” assignment

var b = new Foo
var a: Foo?
a <- b

move assignment nullifies source (b) It’s main purpose is to correctly move ownership, and optimize copying if you don’t need source for heavy types (such as arrays, tables). Some external handled types can be non assignable, but still moveable;

move assignment is equivalent of C++ memcpy + memset operations:

template <typename TT, typename QQ>
__forceinline void das_move ( TT & a, const QQ & b ) {
    static_assert(sizeof(TT)<=sizeof(QQ),"can't move from smaller type");
    memcpy(&a, &b, sizeof(TT));
    memset((TT *)&b, 0, sizeof(TT));
}

“clone” assignment

a := b

Clone assignment is syntactic sugar for calling clone(var a: auto&; b: auto&) if it exists or basic assignment for POD types. It is also implemented for das_string, array and table types, and creates ‘deep’ copy.

Some external handled types can be non assignable, but still cloneable (see Clone).

2.4.2. Operators

2.4.2.1. .. Operator

expr1 \.\. expr2

is equivalent to inverval(expr1,expr2). By default interval(a,b:int) is implemented as range(a,b), and interval(a,b:uint) is implemented as urange(a,b). Use can define their own interval functions or generics.

2.4.2.2. ?: Operator

exp := exp_cond '?' exp1 ':' exp2

conditionally evaluate an expression depending on the result of an expression. if expr_cond is true, only expr1 will be evaluated. similarly if false - only expr2.

2.4.2.3. ?? Null-coalescing operator

exp := exp1 '??' exp2

Conditionally evaluate an expression2 depending on the result of an expression1. Given code is equivalent to:

exp := (exp1 '!=' null) '?' *exp1 ':' exp2

It evaluates expressions until the first non-null value (just like | operators for the first ‘true’ one).

Operator precedence is also follows C# design, so that ?? has lower priority than |

2.4.2.4. ?. and ?[ - Null-propagation operator

exp := value '?.' key

If value is not null exists, return dereference of the field ‘key’ for struct, otherwise returns null.

struct TestObjectFooNative
    fooData : int

struct TestObjectBarNative
    fooPtr: TestObjectFooNative?
    barData: float

def test
    var a: TestObjectFooNative?
    var b: TestObjectBarNative?
    var idummy: int
    var fdummy: float
    a?.fooData ?? idummy = 1 // will return reference to idummy, since a is null
    assert(idummy == 1)

    a = new TestObjectFooNative
    a?.fooData ?? idummy = 2 // will return reference to a.fooData, since a is now not null
    assert(a.fooData == 2 & idummy == 1)

    b = new TestObjectBarNative
    b?.fooPtr?.fooData ?? idummy = 3 // will return reference to idummy, since while b is not null, but b.?barData is still null
    assert(idummy == 3)

    b.fooPtr <- a
    b?.fooPtr?.fooData ?? idummy = 4 // will return reference to b.fooPtr.fooData
    assert(b.fooPtr.fooData == 4 & idummy == 3)

Additionally null propagation of index ?[ can be used with tables:

var tab <- {{ "one"=>1; "two"=> 2 }}
let i = tab?["three"] ?? 3
print("i = {i}\n")

It checks both container pointer and availability of the key.

2.4.2.5. Arithmetic

exp:= 'exp' op 'exp'

daScript supports the standard arithmetic operators +, -, *, / and %. Other than that is also supports compact operators (+=, -=, *=, /=, %=) and increment and decrement operators(++ and --):

a += 2
// is the same as writing
a = a + 2
x++
// is the same as writing
x = x + 1

All operators are defined for numeric and vector types, i.e (u)int* and float* and double.

2.4.2.6. Relational

exp:= 'exp' op 'exp'

Relational operators in daScript are : ==, <, <=, >, >=, !=

These operators return true if the expression is false and a value different than true if the expression is true.

2.4.2.7. Logical

exp := exp op exp
exp := '!' exp

Logical operators in daScript are : &&, ||, ^^, !, &&=, ||=, ^^=

The operator && (logical and) returns false if its first argument is false, otherwise returns its second argument. The operator || (logical or) returns its first argument if is different than false, otherwise returns the second argument.

The operator ^^ (logical exclusive or) returns true if arguments are different, and false otherwise.

It is important to understand, that && and || would not necessarily ‘evaluates’ all arguments. Unlike C++ equivalents &&= and ||= would also cancel evaluation of the right side.

The ‘!’ operator will return false if the given value to negate was true or false otherwise.

2.4.2.8. Bitwise Operators

exp:= 'exp' op 'exp'
exp := '~' exp

daScript supports the standard C-like bitwise operators &, |, ^, ~, <<, >>, <<<, >>>. Those operators only work on integer values.

2.4.2.9. Pipe Operators

exp:= 'exp' |> 'exp'
exp:= 'exp' <| 'exp'

daScript supports pipe operators. Pipe operator is similar to ‘call’ expression with other expression is first argument.

def addX(a, b)
    assert(b == 2 || b == 3)
    return a + b
def test
    let t = 12 |> addX(2) |> addX(3)
    assert(t == 17)
    return true
def addOne(a)
    return a + 1

def test
    let t =  addOne() <| 2
    assert(t == 3)

lpipe macro allows piping to the previous line:

require daslib/lpipe

def main
    print()
    lpipe() <| "this is string constant"

In the example above string constant will be piped to the print expression on the previous line. This allows piping of multiple blocks while still using significant whitespace syntax.

2.4.2.10. Operators precedence

post++  post--  .   ->  ?. ?[ *(deref)

highest

|>  <|

is  as

-  +  ~  !   ++  --

??

/  *  %

+  -

<<  >> <<< >>>

<  <=  >  >=

==  !=

&

^

|

&&

^^

||

?  :

+=  =  -=  /=  *=  %=  &=  |=  ^=  <<=  >>=  <- <<<= >>>= &&= ||= ^^=

=>

',' comma

lowest

2.4.3. Array Initializer

exp := '[['type[] [explist] ']]'

Creates a new fixed size array:

let a = [[int[] 1; 2]]     // creates array of two elements
let a = [[int[2] 1; 2]]    // creates array of two elements
var a = [[auto 1; 2]]      // creates which fully infers its own type
let a = [[int[2] 1; 2; 3]] // error, too many initializers
var a = [[auto 1]]         // int
var a = [[auto[] 1]]       // int[1]

Arrays can be also created with array comprehensions:

let q <- [[ for x in range(0, 10); x * x ]]

Similar syntax can be used to initialize dynamic arrays:

let a <- [{int[3] 1;2;3 }]                      // creates and initializes array<int>
let q <- [{ for x in range(0, 10); x * x }]     // comprehension which initializes array<int>

Only dynamic multi-dimensional arrays can be initialized (for now):

var a <- [[auto [{int 1;2;3}]; [{int 4;5}]]]    // array<int>[2]
var a <- [{auto [{int 1;2;3}]; [{int 4;5}]}]    // array<array<int>>

(see Arrays, Comprehensions).

2.4.4. Struct, Class, and Handled type Initializer

struct Foo
  x: int = 1
  y: int = 2

let fExplicit = [[Foo x = 13, y = 11]]              // x = 13, y = 11
let fPartial  = [[Foo x = 13]]                      // x = 13, y = 0
let fComplete = [[Foo() x = 13]]                    // x = 13, y = 2 with 'construct' syntax
let aArray    = [[Foo() x=11,y=22; x=33; y=44]]     // array of Foo with 'construct' syntax

Initialization also supports optional inline block:

var c = [[ Foo x=1, y=2 where $ ( var foo ) { print("{foo}"); } ]]

Classes and handled (external) types can also be initialized using structure initialization syntax. Classes and handled types always require construct syntax, i.e. ().

(see Structs, Classes, Handles ).

2.4.5. Tuple Initializer

Create new tuple:

let a = [[tuple<int;float;string> 1, 2.0, "3"]]     // creates typed tuple
let b = [[auto 1, 2.0, "3"]]                        // infers tuple type
let c = [[auto 1, 2.0, "3"; 2, 3.0, "4"]]           // creates array of tuples

(see Tuples).

2.4.6. Variant Initializer

Variants are created with a syntax, similar to that of a structure:

variant Foo
    i : int
    f : float

let x = [[Foo i = 3]]
let y = [[Foo f = 4.0]]
let a = [[Foo[2] i=3; f=4.0]]   // array of variants
let z = [[Foo i = 3, f = 4.0]]  // syntax error, only one initializer

(see Variants).

2.4.7. Table Initializer

Tables are created via specifying key => value pairs separated by semicolon:

var a <- {{ 1=>"one"; 2=>"two" }}
var a <- {{ 1=>"one"; 2=>2 }}       // error, type mismatch

(see Tables).