# Variables, Files and Instant ON

A project log for Terminal-BASIC programmable calculator

A poket-to-laptop size microcomputer/programmable calculator, based on Arduino DUE and Terminal-BASIC

## Variables' implementation

In the calculator, variable names may start with a letter or an underscore (`_`) and can further contain letters and numbers, such as: `My_variable`, `_my_other_variable`, `_my_other_variable2`, `Catch_22`.

As in the standard BASIC, the variable type is defined by name ending.

• The name with no ending is a 64-bit double-precision Real: `The_Ultimate_Answer = 42`.
• The name ending with `%`  is a 64-bit sighed Integer: `The_Ultimate_Answer% = 42`.
• The name ending with `\$` is a string: `The_Ultimate_Answer\$ = "42"`.
• The above three definitions are treated as different variables and co-exist in the memory.
• Some versions of BASIC also support variables of different precision, e.g. `a!` designates a double-precision real. For the Calculator BASIC, these additional types are not yet implemented, and probably will not be - most of the time engineering calculations require double precision anyway.

BASIC supports 4 variable arrangements:

• Real or Integer numbers (scalars), e.g. `pi = 3.1415926`.
• Real or Integer vectors, e.g. `Fibonacci% = 0 : Fibonacci% = 1 : Fibonacci% = 1`.
• Real or Integer matrices, e.g. `ROT[0,0] = cos(a) : ROT[1,1] = cos(a) : ROT[1,0] = -sin(a) : ROT[0,1] = sin(a)`.
• Strings, e.g. `The_Ultimate_Answer\$ != "42"` evaluates to FALSE, `The_Ultimate_Answer\$` evaluates to "4".

## Standard variables and Instant On

The following variables are always available (in the final version will be more):

• stack[i] - the RPN stack. The index i can be from 0 to 19; 0-X, 1-Y, 2-Z. The default value is 0.
• prev - the RPN previous value. The default value is 0.
• amode - the angle mode - 0 - degrees, 1 - radians, 2 - gradians. The default value is 0.
• current_dir\$ - the current directory. The default value is "/" (root).
• scrMessage\$ - the current message displayed in the box. The default value is "RPN Ready".
• rpnLabelX\$ - the current message displayed above the X-register value in the RPN interface. The default value is "X".
• rpnLabelY\$ - the current message displayed above the Y-register value in the RPN interface. The default value is "Y".
• rpnLabelZ\$ - the current message displayed above the Z-register value in the RPN interface. The default value is "Z".

If the SD card is inserted, during a power-down all standard and user-defined variables are stored on the card in the file named /_RPN_SaveStatus.bin Upon the power-up, the calculator is automatically restored to the state just before the power-down. If the card is not present during power-down, all user-defined variables and other settings are lost. Upon the power-up, only the standard variables will be present and will be initialized with defaults.

This allows keeping separate working environments on different SD cards. Or, in some cases, it is important to have no stored environments on the calculator - for example, for math exams.

## Implementation details

The current variable space is defined in Variables.hpp as VARIABLE_SPACE = 32000 bytes.

Variables are placed in the variable space as following:

• byte 0 - variable type (0-undefined, 1-number, 2-vector, 3-matrix, 4-string)
• byte 1 - variable name length (N). The name cannot be longer than 254 bytes.
• bytes 2 to N+2 - variable name (null-terminated)

Then:

• byte N+3 - either an 8-byte double or an 8-byte integer

Or:

• byte N+3 - total vector/string length as a 2-byte unsigned integer (L)
• bytes N+5 and above - either a null-terminated string or a vector of 8-byte numbers

Or:

• byte N+3 - total matrix length as a 2-byte unsigned integer
• byte N+5 - row length as a 2-byte unsigned integer
• byte N+7 and above - a matrix of numbers composed by row

## Discussions 