Node:Example 1 - Calculator, Next:, Up:Examples



The code for the following examples are available in the directory library\vbsp\examples in the SICStus installation directory.

Example 1 - Calculator

This example contains a simple program that allows you to enter an arithmetic expression (conforming to Prolog syntax) as a string and displays the value of the given expression, as shown in the following figure: images/vbspcalcspec.png

The calculation itself will be done in Prolog.

We now we will go through the steps of developing this program.

Step 1: Start a new project called calculator

Step 2: Add the vbsp.bas file to the project

Step 3: Create a form window called calculator

Edit the calculator form window, adding two textboxes txtExpr and txtValue, and two command buttons, cmdCalc and cmdQuit: images/vbspcalcform1.png

Save the form window to the calculator.frm file. Then the project will contain the following two files: images/vbspproject.png

Step 4: Write the Prolog code

Write the Prolog code in the file calc.pl, evaluating the given expression with the is/2 predicate, and providing a minimal level of exception handling:

     prolog_calculate(Expr, Value) :-
        on_exception(Exc, Value is Expr, handler(Exc,Value)).
     
     handler(domain_error(_,_,_,_),'Incorrect expression').
     handler(Exc,Exc).
     

Note that this example focuses on a minimal implementation of the problem, more elaborate exception handling will be illustrated in the Train example (see Example 2 - Train).

Compile this file, and deposit the file calc in the directory where the calculator.vbp project is contained.

Step 5: Write the Visual Basic code with the Prolog calls

Now you have to write the Visual Basic code in which SICStus Prolog will be called at two points: