evolve
Variables
Variables
Variables allow the storage of various data types within a program.
The variable name can be made up from alpha numeric characters and the '_' character. The first letter must be a capital.
If an Evolve statement returns a value in a passed variable then the variable does not need to be defined previously. For example the key_in command takes one argument which it sets to the ASCII value of a pressed key. So for the following line of code the variable Byte will be automatically created and the value returned in it.
key_in Byte
Variable Types
There are five variable types in evolve, variables are given a type when they are defined depending on the initial assignment, once a variable is created its type cannot be altered.
Byte Variables
The byte variable has a value of between 0 an 255. To define a variable as a byte assign it a value of between 0 and 255 using hex notation.
ByteVar = 0X12
Integer Variables
The integer has range of between -2147483648 and 2147483647. To define a variable as an integers
simply assign an integer value to it.
IntVar = 1234
Float Variables
The floating point variable has a value between approximately 1.24E-38 and 3.40E+38 . To define a variable as a floating point assign a value with a decimal point to it.
FloatVar = 34.66
String Variables
The string variable type allows you to store strings of any length. To a define string variable place the text in side inverted commas.
StringVar = "my text"
Variable Conversion
Evolve handles type conversion automatically for all variable types, however it is up to the programmer to check that the variables are compatible and also that the values are in range.
For example the following code will cause a run time error as evolve cannot convert 'testdata' to a number.
Test1 = “testdata”
Test2 = 0
Test2 = Test1
However the following code will not generate an error as the Test1 value can be converted to an integer, on completion Test2 will equal 456.
Test1 = “456”
Test2 = 0
Test2 = Test1
The following code example shows how the conversions work
//---------------------------------------
// File: VariableConversion.e
//---------------------------------------
//---------------------------------------
// Block: go
//---------------------------------------
block Go
ByteVar = 0x30
IntVar = 20
FloatVar = 34.7
StrVar = "56"
BoolVar = true
ByteOut = 0x0
IntOut = 0
FloatOut = 0.0
StrOut = ""
BoolOut = false
//Convert to Byte
ByteOut = IntVar
outl "Int to Byte " ByteOut
ByteOut = FloatVar
outl "Float to Byte " ByteOut
ByteOut = BoolVar
outl "Bool to Byte " ByteOut
ByteOut = StrVar
outl "Str to Byte " ByteOut
new_line
//Convert to Int
IntOut = ByteVar
outl "Byte to Int " IntOut
IntOut = FloatVar
outl "Float to Int " IntOut
IntOut = BoolVar
outl "Bool to Int " IntOut
IntOut = StrVar
outl "Str to Int " IntOut
new_line
//Convert to Float
FloatOut = ByteVar
outl "Byte to Float " FloatOut
FloatOut = IntVar
outl "Int to Float " FloatOut
FloatOut = BoolVar
outl "Bool to Float " FloatOut
FloatOut = StrVar
outl "Str to Float " FloatOut
new_line
//Convert to String
StringOut = ByteVar
outl "Byte to String " StringOut
StringOut = IntVar
outl "Int to String " StringOut
StringOut = BoolVar
outl "Bool to String " StringOut
StringOut = FloatVar
outl "Float to String " StringOut
new_line
//Convert to Bool
BoolOut = ByteVar
outl "Byte to Bool " BoolOut
BoolOut = IntVar
outl "Int to Bool " BoolOut
BoolOut = StrVar
outl "String to Bool " BoolOut
BoolOut = FloatVar
outl "Float to Bool " BoolOut
_block
Variable scope
Variables have visibility depending where they are defined, if they are defined in a block then they are local variables and can only be used within the block. Variables that are defined in a definitions block (see 7 ) are global variables and can be used anywhere within the code. In general it is good practice to keep global variable use to a minimum.
The following code shows variable scope in practice.
//---------------------------------------
// File: VariableScope.e
//---------------------------------------
//---------------------------------------
// def:
//---------------------------------------
def
J = 0
_def
//---------------------------------------
// Block: Test1
//---------------------------------------
block Test1
J = 1
I = 1
outl I " = " J
Test2
outl I " = " J
_block
//---------------------------------------
// Block: Test2
//---------------------------------------
block Test2
J = 2
I = 2
outl I " = " J
Test3
outl I " = " J
_block
//---------------------------------------
// Block: Test3
//---------------------------------------
block Test3
J = 3
I = 3
outl I " = " J
_block
//---------------------------------------
// Block: go
//---------------------------------------
block Go
I = 0
outl I " = " J
Test1
outl I " = " J
_block
Constant Variables
If the variable name is all capital letters then the variable will be constant and its value cannot be changed after it has been defined.
Command Line Variables
Command line variables are added to the stack automatically when a program is run they are added as a list with the variable name COMMANDLINE.
The following example will display any command line variables when it is run to see it in action run it from a command window.
evolve CommandLineVariable.e Var1 Var2 1224
The program should display.
Var1
Var2
1234
//---------------------------------------
// File: CommandLineVariables.e
//---------------------------------------
//---------------------------------------
// Block: go
//---------------------------------------
block Go
//get the number of arguments
com_numargs NumberArguments
//check there are some arguments to display
if NumberArguments != 0
//display the arguments
I = 0
while I < NumberArguments
outl COMMANDLINE[I]
++ I
_while
else
outl "No args found"
_if
_block
In the above case note that the function com_numargs is used to get the number of arguments although COMMANDLINE is a list and you can use lst_size to get the number of elements if there where no command line arguments COMMANDLINE would not exist so you will get an error message, using com_numargs handles this case and returns 0.