Skip to content

LRM Operators

Ayush Jain edited this page Feb 27, 2016 · 9 revisions

Operators

JSJS supports various operators for different data types. Broadly, JSJS includes Arithmetic Operators, Relational Operators, Boolean operators, Assignment Operator and String Operator. While most of these are binary operators, some are unary.

  | Binop of expr * op * expr
  | Unop of op * expr

The above code excerpt defines two types of expressions. The former defines a binary operator while the latter gives the format of a unary operator.

Arithmetic Operators

JSJS supports the following arithmetic operators: +, -, *, /, %.. All arithmetic operators require two operands of num data types. These can be either literals or variables or a combination of the two.

  1. Addition

    10 + 7; //17
    x + y;
  2. Substraction

    10 - 7; //3
    x - y;
  3. Multiplication

    10 * 7; //70
    x * y;
  4. Division

    21 / 7; //3
    x / y;
  5. Modulus

    10 % 7; //3
    x % y;

Relational Operators

The following relational operators are supported by JSJS: ==, !=, >=, <=, >, <. Relational operators require two operands. These operands can be of any primitive type namely, int, bool, string or unit. The only condition is that both the operands should be of the same type. These expressions always return a value of boolean data type.

  1. Equals

    5 == 5 //true
    "abc" == "def" //false
    true == false //false
  2. Not Equals

    5 != 5 //false
    "abc" != "def" //true
    true != false //true
  3. Less than

    5 < 5 // false
    "abc" < "def" //true
    true < false //false
  4. Less than or Equals

    5 <= 5 //true
    "abc" <= "def" //true
    true <= false //false
  5. Greater than

    5 > 5 //false
    "abc" > "def" //false
    true > false //true
  6. Greater than or Equals

    5 >= 5 //true
    "abc" >= "def" //false
    true >= false //true

Boolean operators

JSJS supports three boolean operators: &&, ||, !. &&and||act on two operands whereas!` is a unary operator. These act on boolean data types and return a single value of type boolean.

  1. And

    true && false //false
    x && y
  2. Or

    true || false //true
    x || y
  3. And

    !true //false
    !x

Assignment Operator

The assignment operator is used to assign a value to an identifier. The value of the expression on the right side is evaluated and assigned to the identifier on the left hand side.

val x : num = 5 + 3;
val y : string = "abc" ^ "def";
val z : bool = true;

String operator

JSJS includes an operator for strings as well. The ^ operator is the string concatenation operator, which takes two strings and returns an output string formed by the concatenation of the two.

"abc" ^ "def" //"abcdef"
x ^ y

Operator precedence

JSJS defines a precedence order in which operations are performed when more than one operators are present in a single expression. The operators sharing the same precedence are evaluated according to their associativity. Operators which are left associative are evaluated from left to right. Similarly, right associative operators are evaluated from right to left. In JSJS, all operators are left associative except for the assign(=) operator. Following is the chart of operator precedence.

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

The following OCAML code defines the operator precedence along with their associativity for our parser. The precedence increases from top to bottom which means that operators on the bottom are always evaluated first.

%right ASSIGN
%left CARET AND OR
%left NOT
%left LTE GTE LT GT EQUALS NEQ
%left PLUS MINUS
%left MULTIPLY DIVIDE MODULUS
%left NEG