-
Notifications
You must be signed in to change notification settings - Fork 5
LRM 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.
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.
-
Addition
10 + 7; //17 x + y;
-
Substraction
10 - 7; //3 x - y;
-
Multiplication
10 * 7; //70 x * y;
-
Division
21 / 7; //3 x / y;
-
Modulus
10 % 7; //3 x % y;
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.
-
Equals
5 == 5 //true "abc" == "def" //false true == false //false
-
Not Equals
5 != 5 //false "abc" != "def" //true true != false //true
-
Less than
5 < 5 // false "abc" < "def" //true true < false //false
-
Less than or Equals
5 <= 5 //true "abc" <= "def" //true true <= false //false
-
Greater than
5 > 5 //false "abc" > "def" //false true > false //true
-
Greater than or Equals
5 >= 5 //true "abc" >= "def" //false true >= false //true
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.
-
And
true && false //false x && y
-
Or
true || false //true x || y
-
And
!true //false !x
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;
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
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
JSJS © 2016