QScript

Quick Introduction

What is 2 + 2?

If you don't know the answer is 4 then you definitely need this library or a good calculator. To display the answer in the Java console using this library then the syntax is :-

System.out.println(Solver.evaluate("2+2"));
      

in Processing this can be simplified to

println(Solver.evaluate("2+2"));
      

I will continue with using the shorter (Processing) syntax since seasoned Java programmers will find it easy enough to make the translation.

Remember Pythagoras’s theorem - in a right angled triangle the square of the hypotenuse equals the sum of the squares of the other two sides. So the hypotenuse of a right angle triangle with other sides 5 and 12 can be found with.

println(Solver.evaluate("sqrt(5^2 + 12^2)"));
      

In QScript ^ means 'raise to the power of' and has a higher precedence than + so is calculated first. By the way the answer is 13.

QScript also supports all the common mathematical operations and applies the standard order of precedence.

So now you have seen the library in action I should mention - QScript is case sensitive. So sqrt, SQRT and Sqrt are all treated differently. Lets make that clearer -

QScript is case sensitive

QScript also supports variables, multiple expressions and output to the Java console window e.g.

Solver.evaluate("$x=2; println($x + ' cubed is ' + $x^3) ");
      

In QScript all variable names start with the $ symbol so in this script we have a single variable called $x. There are also two statements, they can be identifed because all statements must be separated by a semi-colon (;).

The first statement simply assigns the value 2 to the variable $x. In the second statement the println is a QScript function that behaves in the same way as it does in Java. In QScript literal strings are surrounded with 'single quotes' unlike Java where we use "double quotes". So the output from this script is

2 cubed is 8.0

Notice the + operator is overloaded to perform string concatenation when needed.

It gets better, this code calculates and displays all prime numbers <= 90

 String[] lines = {
    "$maxPrime = 90",
    "println('Prime numbers <= ' + $maxPrime)",
    "println('2')",
    "$n = 3",
    "REPEAT",
    "  $rootN = int(sqrt($n))",
    "  $notPrime = false;",
    "  $i = 3",
    "  WHILE($i <= $rootN && NOT($notPrime))",
    "    $notPrime = ($n % $i == 0)",
    "    $i = $i + 1",
    "  WEND",
    "  IF($notPrime == false)",
    "    println($n)",
    "  ENDIF",
    "  $n = $n + 2",
    "UNTIL($n > $maxPrime)"
};
Solver.evaluate(lines);

Not only can a script have multiple expressions on a line, they can also be over multiple lines. You might also notice that QScript has support for both conditional and loop constructs so can be used to solve many algorithms. The identation of the code is not required by QScript but it does make it easier for a human reader to follow the programming logic.

This script will not take long to execute but if we wanted it to display all prime numbers <= 100,000 this would take many seconds to complete. If the script was being evaluated in the main thread then the application would not be able to respond to mouse or keyboard events and so become unusable.

There is a solution, evaluate the script in a separate thread. QScript evan takes the hard work out of that, simply change the lsat line to

Solver$.evaluate(lines);
      

Before you delve deeper into the workings of QScript you might look at the operators and functions available to you.

 

What Next?

If you could guarentee that all scripts will be free of syntax and evaluation errors then thats all you need to know, but what's the chance of that? QScript detects and reports both syntax and evaluation errors. In basic mode any errors are reported in the Java console window but in expert mode the programmer can catch and deal with the errors him/herself.

In a later guide you will get an insight into how QScript works and how to handle syntax and evaluation errors.