Skip to content

Evaluating Expressions

Syst3ms edited this page Jan 5, 2018 · 2 revisions

VERY IMPORTANT : I'm currently working hard on a huge update, both on the Skript and Java sides.

This update will be so huge that :

  • It will basically be a 2.0
  • Skuared will be renamed to skript-math
  • Most importantly, the evaluation system will be radically different

(I really cba remaking this now)

Definition:

  • Postfix : Also known as Postfix Notation or RPN (Reverse Polish Notation). The postfix notation is a mathematical notation where operators are placed after their operands. Regular mathematical notation is called infix notation (aka 5 + 2). Infix 5 + 2 translates to 5 2 + in postfix. Postfix is commonly used as an intermediate step to evaluating infix expressions. Postfix is MUCH easier to understand for algorithms because there is no such thing as operator precedence (PEMDAS) in it. That also means there are no brackets in postfix. Note : a function such as cos(10) translates to 10 cos in postfix

Functioning of the evaluate expression

The evaluate expression respects operator priority (duh). Speaking of operators, here are all arithmetic operators : +, -, /, *, % (modulus, aka remainder of division), ^ (exponentation).

Skuared also provides bitwise operators for more complex operations. If you don't know what purpose these serve, you probably don't need them. Here they are : << (left bit shift), >> (right bit shift), >>> (unsigned/logical right bit shift). Skuared also provides default constants that you may encounter often. Here they are : pi, e, NaN (Not a Number), Infinity (positive) and phi (the golden number/ratio). All constant names are case insensitive.

If an operator has higher operator priority (also called operator precedence or just precendence) than another one, then it will be computed first. The higher the operator is in the list, the more it has priority. Here are operator priorities :

  • ^
  • *, / and %
  • + and -
  • >>, << and >>>.

Operators do not only have a precedence, but also an associativity, which can either be left or right. Associativity tells the evaluator in which direction to evaluate the expression when chaining the same operator multiple times. All operators provided in Skuared are left-associative, except exponentiation. This means the following :

  • 5 + 6 + 7 + 8 is interpreted as ((5 + 6) + 7) + 8
  • On the other hand, 2 ^ 3 ^ 4 ^ 5 is interpreted as 2 ^ (3 ^ (4 ^ 5))

Since I couldn't make an operator for every operation (not enough appropriate symbols), here are some functions Skuared registers and you can use anywhere, like Skript's default functions. They are bitwise operations. Same, you most likely don't need them if you don't know what they are. Here they are : or, and, xor, nor, nand, nxor. Now for the interesting part. You may use any function you like in the evaluate expression as long as it returns a number and has one or more arguments all consisting of a single number. NOTE : here, number includes all subtypes such as int, long, float, etc.

Implicit multiplication is supported. Meaning that 2(5 * pi), (5 + 2)(pi / 180) and 3pi is perfectly valid code. These expressions will be understood as 2 * (5 * pi), (5 + 2) * (pi / 180) and 3 * pi respectively.

Usage :

The evaluate expression syntaxes are :

eval[uate] [[math[ematic]] expr[ession]] %string% [with %number%]

The first %string% is obviously the expression to be evaluated. Now, the optional %number% in the second syntax is a value you give to Skuared, and to which you can refer using x in the expression. For example :

set {_res} to eval expr "4x^3" with 2 # 4*2^3 = 4*8 = 32

Erroring :

When something fucks up when parsing or evaluating an expression (most likely because of you), a descriptive error is stored in the last skuared error expression. Therefore, evaluating expressions with Skuared should follow this structure :

set {_res} to eval expr "EXPRESSION"
if last skuared error is not set:
    # proceed as usual
else:
    # something fucked up
    send "%last skuared error%"

Default functions :

Skuared registers default functions (like Skript's) :

  • The aforementioned bitwise functions
  • gamma : the gamma function, which is equal to (n - 1)! (! is the factorial). Please note this is an approximation, so the results might get more inexact as the parameter grows larger.
  • factorial : the factorial. This is basically a prettier form of gamma(n + 1), so this is also an approximation. Even though this function accepts all types of numbers, I would recommend keeping its usage restricted to integers and rather using gamma(x + 1) with floating-point/decimal numbers.