| Previous | Contents | Index |
When SheerPower evaluates an expression, it evaluates it in a specific order. SheerPower evaluates expressions from left to right.
1+Z+4 equals (1+Z)+4
1+Z-4 equals (1+Z)-4
3*4/QUANTITY equals (3*4)/QUANTITY
12/QUANTITY*3 equals (12/QUANTITY)*3
|
The following priorities take precedence over the left to right evaluation rule:
Z%-(X% / (Y% + AMOUNT))
|
NOT
AND
OR
XOR
IMP
EQV
Parentheses are used around every set of operations. This makes it easy to pick out the beginning and end of an operation, and will make absolutely clear what is intended without depending on the order of precedence of operators. If someone reading the code can know with absolute certainty how it was intended to be executed via the use of parentheses, then they will not have to wonder if a particular bug is due to the language used creating the code, other than what was intended. See Appendix A, Coding Principles and Standards for more on Coding Principles and Standards in SheerPower.
if a > b * c or d + 4 = f then x = x + 1 <--- hard to read! if ((a>(b * c)) or ((d + 4) = f)) then x = x + 1 <--- clarity with parenthesis |
Variables specify storage locations. Numeric and string variables are assigned values with the LET statement. String variables can also be assigned values with the LSET, RSET and CSET statements.
The LET statement assigns the value of an expression to a variable. The expression is evaluated and its value is stored in the location specified by the variable. The data types of the expression and the variable must match. Thus, you must assign a string variable string values and you must assign numeric variables numeric values. A string variable must end with a dollar sign "$" unless you declare it. An integer variable must end with a percent sign (%) unless you declare it.
The DECLARE statement allows you to dispense with data type designations. DECLARE declares a variable as either string, integer or real. Once the variable has been declared, you do not need to attach a $ or % to it. Functions, arrays, etc., can also be declared with the DECLARE statement.
DECLARE [STRING | INTEGER | REAL | BOOLEAN | OBJECT] var, var...
|
| Example 5-1 DECLARE statement |
|---|
declare string name, sex declare integer age declare real amount declare object anything declare boolean is_male input 'Enter your name': name input 'Enter your age': age input 'Enter your sex': sex input 'Enter an amount': amount if sex = 'male' then is_male = true else is_male = false print print name; ' is a'; age; 'year old '; sex print name; ' entered the amount'; amount if is_male then print name; ' is male' anything = 4 + 5 print 'This object (or dynamic) variable contains a numeric value: '; anything anything = 'kitty' print 'Now it contains a string value: '; anything end Enter your name? Sammy Enter your age? 28 Enter your sex? male Enter an amount? 25.38 Sammy is a 28 year old male Sammy entered the amount 25.38 Sammy is male This object (or dynamic) variable contains: 9 Now it contains a string value: kitty |
DECLARE is used to specify the data types of variables, functions, etc. Once the data type of a variable or function has been declared, it is not necessary to designate them with a trailing $ or %.
DECLARE declares the data type of a variable or function. The STRING option indicates that the following are string variables or functions. INTEGER declares integer numeric. REAL indicates real numeric. The BOOLEAN option indicates that the following are Boolean variables. Only one of the four data type options can be used in each DECLARE statement. Any number of variables can be declared with a DECLARE statement.
DECLARE OBJECT declares one or more variables to be of type OBJECT. A variable of type OBJECT receives the data type of the data that is put into it.
OBJECT and DYNAMIC are synonyms in this statement. DECLARE DYNAMIC is the same as DECLARE OBJECT. |
To find out the current data type of an object variable, the DTYPE function can be used (see Section 6.9.2).
Multiple data types can be declared by using the following format:
DECLARE datatype var, var, datatype var, var, datatype var, var, ...
|
For example:
| Example 5-2 Declaring multiple data types |
|---|
declare string name, sex, integer age, year, real amount input 'Enter your name': name input 'Enter your age': age input 'Enter your sex': sex input 'Enter the year': year input 'Enter an amount': amount print print name; ' is a'; age; 'year old '; sex; ' in'; year print name; ' entered the amount'; amount end Enter your name? Terry Enter your age? 25 Enter your sex? female Enter the year? 2000 Enter an amount? 582.69 Terry is a 25 year old female in 2000 Terry entered the amount 582.69 |
DECLARE STRUCTURE struc_name1 [, struc_name2 ...]
|
| Example 5-3 DECLARE STRUCTURE |
|---|
declare structure str
open structure cl: name 'sptools:client'
ask structure cl: id cl_id$
set structure str: id cl_id$
extract structure str
end extract
for each str
print str(#1); ' '; str(#2)
next str
end
20000 Smith
20001 Jones
20002 Kent
23422 Johnson
32001 Waters
43223 Errant
80542 Brock
80543 Cass
80544 Porter
80561 Derringer
80573 Farmer
|
DECLARE STRUCTURE declares one or more symbols to be of type STRUCTURE. Once a symbol has been declared to be of type STRUCTURE, it can be used in statements such as SET STRUCTURE...ID to write generalized routines where you do not know at compile time which structure you are going to use.
Usage example: this statement could be used in the situation where you have a transaction structure and a transaction history structure and, optionally, want a report on one or the other. You could use one report program and the DECLARE STRUCTURE statement to declare which structure to use when the user makes the report selection.
5.2 OPTION Statements
5.2.1 OPTION REQUIRE DECLARE
OPTION REQUIRE DECLARE
|
| Example 5-4 OPTION REQUIRE DECLARE statement |
|---|
option require declare declare string name, comment input 'Please enter your name': name line input 'Enter a comment in quotes': comment print name; ' says, '; comment end Please enter your name? George Enter a comment in quotes? 'Have a nice day!' George says, 'Have a nice day!' |
OPTION REQUIRE DECLARE causes SheerPower to require all variables in the program to be declared. If the OPTION REQUIRE DECLARE statement is used and a variable is left undeclared, SheerPower will return an error when program execution is attempted. The OPTION REQUIRE DECLARE statement should occur before any DECLARE statements and before any variables are assigned.
| Previous | Next | Contents | Index |