![]() | ![]() | |||||||||||||||
| ||||||||||||||||
Fundamental TypesAll AdaScript variables have a type. Variables must declared so that SparForte knows what that type is. When new variables are created by assignment at the command prompt, SparForte choses an appropriate type for the new variable. In scripts, all variables must be declared.
Universal TypesThere are three "universal types":
The first two types form the basis for all other AdaScript string and numeric types. Variables declared as universal_typeless change their type between universal_string and universal_numeric depending how they are used. If an enumerated value is assigned to a univeral_typeless variable, it becomes a number with the ordinal value of the enumerated item. If AdaScript cannot decide in a particular context, the variable type defaults to universal_string. Typeless variables correspond to variables in Bourne shells and are provided for as a command line convenience. Universal types automatically match any type derived from the same universal type. A universal_numeric variable accepts integer or floating point values. A universal_string variable accepts single characters or strings containing many characters. Universal types are used for all AdaScript literals. For example, a string literal like "hello" is a universal_string and be assigned to any string type. The numeric literal 45.5 is a universal_numeric can be used with float, long_float, or any other numeric type. Using these three built-in types will give you a minimum amount of type checking, suitable for short scripts or quick command line calculations. Universal types should not be used in large scripts because they will greatly reduce the number of errors SparForte can detect. Variables are declared as name, a colon and the type. More than one name can be used in a comma list to declare several variables of the same time at one time.
=> i : universal_numeric; Ada: Ada has no usable universal types but the Ada rationale uses universal types to describe the type system. Predefined, Non-Universal TypesFor more extensive scripts, AdaScript extends the universal string and numeric types into all the predefined Ada language types, plus some AdaScript-specific types:
The built-in packages may define additional types. By default, all numeric variables are initialized without any value. Any attempt to use uninitialized numeric variables in an expression will cause an error or exception. => i : integer -- no value specified Most types are incompatible with each other. Types may be typecast into related types. => i : integer := 5 You cannot typecast a numeric type into a string type directly. There are functions in the numerics and strings packages to do these conversions. Type AttributesThe System package contains information about the representation of types on your computer. You can use this package to determine the minimum integer, or the precision of a floating-point number. Array properties can be discovered with the arrays package. In this case, this is a 64-bit computer (with 8-bit bytes) and it has a maximum integer of 999,999,999,999,999. ? System.Min_Int See the description of the System package for more information. String GotchasSince all literals have a universal type, this can cause some unusual side-effects. A character variable can contain more than one character if you really want it to by assigning a string literal. Characters are stored as internally as a string and a string literal is a universal_string type. AdaScript will allow the assignment. However, the type checking will prevent a character variable from being assigned to a string variable. => i : integer := 5 In this example, "hello" is a literal so it is a universal_string. The variable c is a character, a type derived from universal_string. So SparForte allows the assignment. Internally, SparForte uses strings for characters so the entire value of "hello" is assigned to c and c is now a character variable with 5 characters in it. However, SparForte's type rules prevent c from being assigned to a string, and typecasting will check the length of a character variable. (This will likely get addressed in the future.) AdaScript strings are an unbounded string type--that is, they are stored as an Ada.Strings.Unbounded.Unbounded_String variable. They have an unlimited length and are not implemented as any sort of array. Ada "fixed" strings, the default string type for GCC Ada, are not used because they have limited length and can create array bounds exceptions. Strings are an integral part of scripts and unbounded strings are used instead. AdaScript includes an unbounded_string type (it has the same properties as the string type) which may make porting to GCC Ada easier. ConstantsConstants can be declared with the word "constant" for any type. The use of "constant" doesn't not affect the type of the variable--it simply prevents new values from being assigned by making the variable "read-only". program_name : constant string := "Nightly FTP Transfer"; Limited Typesfile_type and socket_type variables are known as limited type variables. Limited types cannot be assigned a new value with an assignment statement => f : file_type SparForte manages the contents of these variables and scripts are not allowed to change the contents. Complex TypesVariables declared complex represent complex numbers. A complex number is a record with long_float real and imaginary parts. The real field is designated "re" and the imaginary field is designated "im". (See below for more information about using records.) => c : complex := ( 1.0, 2.0) Some of the numerics package subprograms work with complex types. Complex types are not fully implemented in this version of SparForte: some of the Ada complex number functions are not available. Command TypesExternal operating system commands can be declared using command variables. When a command is declared, SparForte will ensure that the command exists and is runnable. Command types are similar to limited types and have several restrictions.
=> l : constant command := "/bin/ls" JSON and SparForte TypesJSON is an acronym for JavaScript Object Notaiton. It is an easier-to-use and more limited standard for data interchange compared to XML. Many programming languages support JSON. SparForte contains several functions to convert variable values into JSON, and JSON into variable values. JSON doesn't support enumerated types, so SparForte converts enumerated types into integer, the ordinal position of the enumerated values. Records are coverted into JSON objects. If an array or record has a json_string component within it, the content of the json_string is assumed to be JSON data and will be added as-is to the json-encoded aggregate. Use json_string to build up more complex JSON structures like arrays of arrays. Currently, SparForte doesn't support UTF-8. It doesn't support characters larger than 8 bits. Encoding arbitrary control characters with \u is not yet supported. Carriage returns, line feeds, and other backslash escapes are supported.
Exceptions are raised for a number of conditions:
type a_sample_enum is ( e1, e2 , e3 );
Example: Encoding a record as a JSON string
<?php
Example: Deccoding JSON string into a PHP Object
|
Block Statements and Subprograms |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||