In my last semester, I studied about parser in compiler design. But that was all theoretical.I wanted to do something practical. So I decided to make Arithmetic Parser Library with all trigonometric, logarithmic functions which could also be used by others to evaluate expression. And kotlin language is very easy to write and idiomatic.
In Java, operators are tied to specific Java types. For example, String and numeric types in Java can use the + operator for concatenation and addition, respectively. No other Java type can reuse this operator for its own benefit. Kotlin, on the contrary, provides a set of conventions to support limited Operator Overloading.
Assignment Operator is used to assigning values to variables. The value after evaluation on the left-hand side is assigned to the variable of the right-hand side. Besides the basic = assignment operator, Kotlin provides a wide range of assignment Operators which are mentioned below.
Now for this we will create evaluate(expression:String) function.Now, what will be inside the evaluate function?Well, The Core idea is to search in the expression for an operator. Then split the expression in two parts. Part 1 (Left from operator) and Part 2 (Right from operator). Then recusively evaluate the value of Part 1 and Part 2 with evaluate() and then finally evaluate the answers of part1 and part2 with the found operator.
Increment and Decrement operators can be used before and after a variable but both have different meanings. If increment or decrement operator is used before the variable name then the value of that variable is changed first before any other operation on the variable. If the increment or decrement operator is used after a variable name then its value is changed after the other operation on that variable.
In the above example, the value of b is first incremented by 1 and then assigned to variable ‘increment’ whereas the value of c is first assigned to variable dec and then decreases by 1.
The following article, provide an outline of the most commonly used operators in Kotlin. Operators are basically special symbols that are used to perform specific operations on the operands. For an example (-) operator is used to perform a subtraction between two operands.