2
Operators
Bartłomiej Przemysław Pluta edited this page 2020-04-01 21:09:18 +02:00

SMNP language provides a set of unary and binary operators that can be used to process supported data. Many of them have a different meaning for other data types.

Available operators

Unary operators

Unary operators support only one operand, which typically is placed on the right-hand side of the operator.

- (minus)

Supported data types:

  • - <int>: negate value: -4 == 0 - 4
  • - <string>: reverse string: -"Hey!" == "!yeH" (why not? ;-))
  • - <list>: reverse lists: -[1, 2, 3, 4] == [4, 3, 2, 1]

not

Supported data types:

  • not <bool>: negate value: not false == true

Binary operators

Binary operators support two operands, which typically are placed around the operator.

+ (plus)

Supported data types

  • <int, float> + <int, float>: sum: 2 + 4.5 < 7
  • <string> + <any> and <any> + <string>: concatenate strings (non-string operands are implicitly coerced to string - this is the only case of implicit type coercion): "he" + "llo" + 123 == "hello123"
  • <list> + <list>: concatenate lists: [1, 2] + [3, 4] == [1, 2, 3, 4]
  • <map> + <map>: concatenate maps: { a -> 1 } + { b -> 2 } == { a -> 1, b -> 2 }
  • <note> + <int>: extends the note duration by value: @g:8 + 16 == @g:8d

- (minus)

  • <int, float> - <int, float>: difference: 3 - 6.5 < 0

* (asterisk), / (slash)

  • <int, float> * <int, float>: product: 2 * 3 == 6
  • <int, float> / <int, float>: quotient: 12 / 2 == 6

** (double asterisk)

  • <int, float> ** <int, float>: power: Int(2**3) == 8.toInt()

. (dot)

  • <any>.identifier or <any>.identifier(...) - access to property or method invocation: "hello".length == 5

and, or (logic operators)

  • <bool> and <bool> - logical conjunction: true and false == false
  • <bool> or <bool> - logical alternative: true or false == true

==, != (equality operators)

  • <any> == <any> - equality of operands: 3 == 3
  • <any> != <any> - inequality of operands: @c != "hello"

>, <, >=, <= (relation operators)

  • <int, float> > <int, float> - greater than: 5 > 3.5
  • <int, float> < <int, float> - less than: 1 < 3.5
  • <int, float> >= <int, float> - greater than or equal to: 4 >= 3.5
  • <int, float> <= <int, float> - less than or equal to: 2.5 >= 3

Other operators

Apart from unary and binary operators, there is also ^ (caret) operator which is used in loop statement, however, loops are covered in next sections.

Operators precedence

SMNP language's operators have their unique priorities which determine operations' order in case of ambiguity.

Operator(s) Precedence
- (unary) 1
. 2
** 3
not 4
* / 5
+ - (binary) 6
== !=
<= >= 7
< >
and 8
or 9
^ 10

Remember, that in spite of operators precedence you can always force priority using parentheses:

a = -2+2;
b = -(2+2);

println(a);  # 0
println(b);  # -4

x = 2 + 2 * 2;
y = (2 + 2) * 2;

println(x);  # 6
println(y);  # 8