A simple calculator with PANORAMIC and FreeBASIC

By jdebord

Introduction

In the previous issue of this magazine we have shown how PANORAMIC can call functions from dynamically loaded libraries (DLL). Here we describe a calculator made with PANORAMIC and using a FreeBASIC DLL.

Passing a PANORAMIC string to a DLL

In this example we need to pass to the DLL a string which contains the mathematical expression to be evaluated (ex. "1+1"). For instance, a function in a FreeBASIC DLL could be written as follows:

  function StrFunc(byref p as zstring ptr) as integer export
    dim as string s = *p
    ....................
  end function

The parameter p is a pointer to a null-terminated string. This pointer can be dereferenced inside the function and the resulting null-terminated string (*p) can be converted to a standard BASIC string (s) by a simple assignment.

This function can be called by the following PANORAMIC code:

  dim s$, i%
  s$ = "1+1"
  i% = dll_call1("StrFunc", adr(s$))
  

The EVAL DLL

The DLL used in this example is a simplified version of the "Simple Expression Evaluator" (SEE), a math parser written by Aleksandar Ruzicic (a.k.a. krcko) and published some years ago in the FreeBASIC forum. This DLL is available at:

http://www.unilim.fr/pages_perso/jean.debord/tpmath/eval.zip

The DLL exports 3 functions:

  • InitFunc
  • This function initializes the built-in DLL functions and returns their number. It is mandatory to call this function before using any of the built-in DLL functions.

    PANORAMIC call:

    	dim i%
    	i% = dll_call0("InitFunc")
    	print i%, " functions initialized in the EVAL DLL"
    	For a list of functions and operators available in the DLL, see the documentation file readme.txt in the archive.
    
  • SetVariable
  • This function sets a variable to a value. There are 26 variables available, from A to Z. The function returns the order number of the variable (1 for A, 2 for B etc.)

    PANORAMIC call:

    	dim variable$, x, i%
    	
    	variable$ = "x"
    	x = rnd(1)
    	
    	i% = dll_call2("SetVariable", adr(variable$), adr(x))
    	print "Name of variable : ", variable$
    	print "Order number     : ", i%
    	print "Value            : ", x
    
  • Eval
  • This function evaluates an expression passed as a character string and returns one of the following error codes:

    0. no error
    1. unexpected token
    2. unbalanced braces
    3. undefined variable
    4. undefined function

    PANORAMIC call:

    	dim expression$, result, i%
    	expression$ = "4 * atn(1)"
    	i% = dll_call2("Eval", adr(expression$), adr(result))
    	print "Expression : ", expression$
    	print "Result     : ", result
    	print "Error code : ", i%
    	

The PANORAMIC program

The following program uses the DLL to code a simple calculator with 4 variables:

  ' ---------------------------------------------
  ' Declarations
  ' ---------------------------------------------
  dim i%, variable$, expression$, value, result
  label On_Click_Button_12
  ' ---------------------------------------------
  ' Object description
  ' ---------------------------------------------
  left 0,225
  top 0,200
  width 0,470
  height 0,225
  caption 0,"Calculator"
  alpha 1
  left 1,20
  top 1,20
  caption 1,"Variables :"
  alpha 2
  left 2,20
  top 2,53
  width 2,27
  height 2,20
  caption 2,"A = "
  edit 3
  left 3,50
  top 3,50
  text 3, "1"
  alpha 4
  left 4,20
  top 4,83
  width 4,28
  caption 4,"B = "
  edit 5
  left 5,50
  top 5,80
  text 5, "2"
  alpha 6
  left 6,20
  top 6,113
  width 6,29
  caption 6,"C = "
  edit 7
  left 7,50
  top 7,110
  text 7, "3"
  alpha 8
  left 8,20
  top 8,143
  width 8,28
  caption 8,"D = "
  edit 9
  left 9,50
  top 9,140
  text 9, "4"
  alpha 10
  left 10,220
  top 10,20
  width 10,170
  caption 10,"Enter a formula, then click Evaluate:"
  edit 11
  left 11,220
  top 11,50
  width 11,225
  text 11, "A + B + C + D"
  button 12
  left 12,261
  top 12,90
  width 12,140
  caption 12,"Evaluate"
  on_click 12, On_Click_Button_12
  alpha 13
  left 13,220
  top 13,143
  width 13,62
  caption 13,"Result = "
  edit 14
  left 14,270
  top 14,140
  width 14,170
  text 14,""
  ' ---------------------------------------------
  ' Main program
  ' ---------------------------------------------
  dll_on "eval.dll"
  i% = dll_call0("InitFunc")
  end
  ' ---------------------------------------------
  ' Subprogram
  ' ---------------------------------------------
  On_Click_Button_12:
    variable$ = "A" : value = val(text$(3))
    i% = dll_call2("SetVariable", adr(variable$), adr(value))
    variable$ = "B" : value = val(text$(5))
    i% = dll_call2("SetVariable", adr(variable$), adr(value))
    variable$ = "C" : value = val(text$(7))
    i% = dll_call2("SetVariable", adr(variable$), adr(value))
    variable$ = "D" : value = val(text$(9))
    i% = dll_call2("SetVariable", adr(variable$), adr(value))
    expression$ = text$(11)
    i% = dll_call2("Eval", adr(expression$), adr(result))
    text 14, str$(result)
  return

Upon running, the program displays the calculator window. We can set the values of the four variables and enter a formula which may (or may not) depend on theses variables, then click the "Evaluate" button to get the result.

The compilation by PANORAMIC results in a huge executable which can be compressed by UPX to about 1/3 of its original size.

Conclusion

This simple example shows how PANORAMIC and FreeBASIC can be combined to develop GUI programs under Windows.

Acknowledgements

Many thanks to:

  • "krcko" for writing SEE and releasing it under a very permissive license
  • the members of the french PANORAMIC forum for their constant support
Powered by CMSimple | CMSimple Legal Notices | (X)html | css | Login | Template-Design: Lars Ellmauer | Template-Modified: Imortisoft ISD