Sheerpower Logo
B.1  Variables, Data Types, Database Access, Declaring Variables, and Comments

First and foremost, have fun programming with Sheerpower! It’s fast, intuitive, and designed to help you be productive.

Variable Names

User variable names must begin with a letter and can include letters, numbers, underscores ("_"), and embedded dollar signs ("$"). Names are case-insensitive, meaning Account and AccOUNT are treated as the same. By convention, multi-word variable names are separated by underscores.

Primitive Data Types

  • REAL – A numeric data type that supports up to 18 digits before the decimal point and up to 16 digits after the decimal point. SheerPower leverages Perfect Precision Math, which ensures that basic arithmetic operations are completely free of rounding errors. This level of precision makes SheerPower particularly well-suited for business applications, where even small numerical discrepancies can result in significant financial consequences. To achieve this precision, SheerPower internally stores REAL numbers in two separate memory locations: one for the integer part and one for the fractional part.
  • STRING – Characters, such as a person's name, city name, or arbitrary 8-bit binary data.
  • INTEGER – A whole number without fractional parts.
  • BOOLEAN – A logical value, either true (1) or false (0), used in conditional statements.
  • DYNAMIC – A flexible type that can adjust its data type based on its usage.
  • Database Access – Expressed as table_name(field_name).

The most commonly used data types in Sheerpower are REAL, STRING, and BOOLEAN. INTEGER and DYNAMIC are less frequently used. By default, variables are of the REAL type.

Consistent Variable Types

In Sheerpower, variable types must remain consistent after they are declared. For example, if you declare a = 18, attempting to use a in a string context, like a + '9', will result in a compile-time error. This type safety ensures predictable results. However, table field references (e.g., payroll(salary)) are handled differently since the data type of a field can only be determined at runtime. This allows dynamic type handling for table fields but does not apply to regular variables.

Database Access in Sheerpower

All database access uses the table_name(field_name) syntax. For instance, print payroll(salary) retrieves the current record from the payroll table, locks it if necessary, extracts the salary field, and prints its value.

When assigning a value to a database field, such as payroll(salary) = 1234.56, Sheerpower retrieves the current record, locks it, assigns the value 1234.56 to the salary field, and writes the modified record back to the database using a "lazy write" mechanism. This mechanism ensures database integrity by delaying the write operation until it is most efficient.

Dynamic Adjustment of table_name(field_name)

In Sheerpower, the treatment of table_name(field_name) is dynamic and adjusts based on the context in which it is used. This flexibility allows developers to handle different data types without the need for explicit conversions, simplifying code and reducing potential errors.

  1. Numerical Context:

    When a field from a database table is used in a numerical context, such as in mathematical operations, Sheerpower automatically interprets and converts the field's value to the appropriate numeric type. This makes it easy to perform calculations on database fields without requiring any manual type conversions.

    mysalary = 1.5 * payroll(salary)

    In this case, payroll(salary) retrieves the salary field from the payroll table. The value is automatically converted into a number, allowing it to be multiplied by 1.5 seamlessly. This dynamic conversion eliminates the need for explicit type management in mathematical operations.

  2. String Context:

    When a field is used in a string context, Sheerpower dynamically converts the value to a string. This is particularly useful when concatenating or formatting data from the database with other strings.

    mysalary$ = 'my ' + payroll(salary)

    Here, payroll(salary) retrieves the salary field from the payroll table, and the value is automatically treated as a string for concatenation with the text 'my '. The result, mysalary$, will hold the concatenated string without needing explicit conversion.

  3. Advantages:
    • Seamless Type Handling: Sheerpower dynamically adjusts the type of the database field based on its usage context, reducing the need for manual type conversion and simplifying the code.
    • Context-Aware Operations: Whether the field is used in mathematical calculations or string operations, Sheerpower intelligently determines how to handle the field, ensuring that it behaves correctly in both contexts.
    • Efficient Database Interactions: The table_name(field_name) syntax provides a clean and efficient way to access and modify database fields, with built-in protections against SQL injection attacks, further enhancing security and performance.

This dynamic type conversion, coupled with the ease of use of table_name(field_name), allows developers to focus more on business logic rather than handling cumbersome type declarations or conversions.


Variables with a dollar sign "$" suffix default to being a string. Those with a percent sign "%" suffix default to being an integer. And those with a question mark "?" suffix default to being a boolean. Unsuffixed, undeclared variables are assumed to be declared as real.
print 1.2 - 1.0 tax_rate = 5.5 fullname$ = 'Sally Sue' is_wonderful? = true counter% = 50 declare dynamic answer declare real inches declare string address declare integer frogs declare boolean is_great inches = 55.6 print inches

Defining Constants
You can also define constants in your code. Constants cannot be changed once given a value.
const speed_of_light_mps = 299_792_458
You can also require that all variables be declared. In this case, undeclared variables will cause compile-time errors.
option require declare

Declaring Default Output Formatting
There are some variables that when printed you always want specific formatting to be used. For example, with a variable that contains a money amount, every time it is printed, you might want to include a dollar sign, commas, and also rounded to two decimal digits. For details on print formats, see the SPRINTF() function
declare format '$%.2m' cash cash = 123456.7 print 'The value is: ';cash The value is: $123,456.70
Custom Data Types
A custom data type is defined by: TYPE typename datatype [format 'format string']

After being defined, custom data types can be utilized in place of any primitive data type. If you declare a variable with a custom data type, any attempt to assign a different custom data type to that variable will generate a compile-time error. Custom data types also improve the readability of your code, making it easier to maintain in the long run.
type money format '$%.2m' declare refund money refund = 123456.728 print 'Your refund is: ';refund >> Your refund is: $123,456.73 type miles declare distance miles distance = 45 // The code below generates a compiler error // because refund is a type of money and distance is a type of miles refund = distance
Comments in your code

Comments in your code should explain the reasoning behind specific logic. Sheerpower ignores all comments since they are intended for your reference and for future developers who need to understand your code.

Comments are prefixed with either ! or //
! this is a comment
// this is a comment


Hide Description

    

       


      

Enter or modify the code below, and then click on RUN

Looking for the full power of Sheerpower?
Check out the Sheerpower website. Free to download. Free to use.