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.

  1. Start a new project called calculator.
  2. Add the vbsp.bas file to the project.
  3. Create a form window called calculator. Edit it, 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

  4. 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.

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