Programmer's Calculator V2.73 for the Palm OS

Syntax and Grammar for Programmer's Calc Programs


A program can be as simple as a single number 100 for example, or as complex as a series of statements separated by semi-colons and grouped by curly braces { } with looping constructs embedded in it such as while, etc. Essentially, everything in the calc is a program. Even what's displayed on the calc is represented by the program named Disp which always contains a numeric value. When you press the + button on the calc, it looks up the program that's assigned to the key and executes it. By default, the program is called Add and contains the program Op1+Op2 which is executed and returns a result (all programs return results) of the sum of the two operands which are themselves programs which have special meaning.

Programs are free form, meaning that you can enter them using as many or few whitespace characters (spaces, line feeds) as you'd like.

All programs have names, these names can be used symbolically as arguments in other programs. For example, if I define a program called sum and define it as 5+2, I can then define another program called sumsum and define it as sum+sum. If I execute sum, I get the result 7 and if I execute sumsum I get the result 14.

Programs can also be defined "on the fly" using a quoted string. For example, if I have a program called alpha which is defined as beta="5+2" then executing alpha defines the program called beta.

There's a built-in program called Inp("prompt") which brings up a dialog requesting input from the user and displays prompt. The ? key on the calculator has a program assigned to it that's defined as Inp("Enter Expression"). Pressing this key allows you to enter a program dynamically with the result of that program being displayed in the calculator's display area. You can also use the Inp() built-in in you're programs to prompt the user and assign the result to another program (variable, same thing!). The point is, that everything that's entered in the calc is a program so when the dialog comes up, you can enter 100 or { while(i<10) i++; } or any arbitrarily complex expression.

A program must always return a result. There are two ways this can happen, implicitly and explicitly. Implicitly means that the result of the last statement is returned, for example, 100+2 will implicitly have the result 102. Explicitly means using the return() statement. For example, return(100+2). The return statement also has the effect of ignoring the remainder of the program, i.e. the return is immediate.

A single statement such as 2+3*4 can be entered without ending with a semi-colon statement terminator. The rule is, if the end of program is reached, then an implicit semi-colon is assumed. You can also put the semi-colon in, if you want. But multiple statements such as A=2+3*4; B=A*2; can only be entered by using the curly brace grouping operators, for example, the correct way to enter the previous program is { A=2+3*4; B=A*2; } Since the assignment operator returns the result of the assignment and grouping using curly braces return the result (if any) of the last (right-most) statmement executed, then the result of this expression will be 28.

Looping and control instructions can be used to iterate and test but generally must be enclosed in curly braces since these statements don't return results but instead control the execution of programs that do. For example, lets say we want to get the sum of the first 10 whole numbers. You could do it the hard way using 1+2+3+4+5+6+7+8+9+10 or use a looping construct such as { sum=0; for(i=1;i< 11;i++) sum=sum+i; return(sum); }. For those not familiar with "C", the construct first executes the initial expression i=1 and then tests the relational operation i < 11 if that is true then the statement that follows is executed sum=sum+1; then the terminating expression is executed, i++ which increments i. The process continues until the relational operation is not true.

Here's the grammar for the Programmer's Calc language. One this to watch out for is that this is not "C". It's based on "C" and looks very similar to "C" but there are differences. For example, "C" is a typed language and makes you pre-define variables before they are used. The calc defines a variable on it's first use and defaults it's value to 0. Also, variables within the calc are really programs that are themselves, interpreted.

program stmt relop expr assignment-op shift-op addsub-op muldiv-op unary-op args number


Back