Types

From Pit
Jump to: navigation, search

Primitive

Primitive types mirror the types available in C. Memory to store them must be manually allocated. Garbage collection is not performed on them. An example declaration:

private string(unsigned int(16), 42) $foo;  // C equivalent:  unsigned short foo[42];
  • ptr: An integer containing the memory address of another primitive variable. (A pointer cannot point to an auto variable.)
  • char: An eight-bit unsigned int.
  • int: An integer. If unspecified, the size is the same as the size of a pointer. The size, specified in bits, has no restriction; it need not even be a multiple of eight, and there is no upper bound. For example, a 1024-bit integer may be specified. This is useful, for example, when writing crypto software.
  • float: A floating-point variable. A specific bit-size may be specified, but is restricted to the bit-sizes supported by the target architecture. If unspecified, the largest size available is used. Support for floats is architecture-dependent. Different machines support a different selection of bit-sizes, and some machines may not support floats at all.
  • string: An ordered collection of elements indexed with an integer. All elements must have the same type, and the index starts at zero.
  • struct: An ordered collection of named elements. Usually each element has a different type.
  • function: As with C, all executable code must be contained in functions. They support in, out, and inout parameters.
    • in: When passing an in-parameter, the caller copies the value to the stack and any modifications made by the callee are lost.
    • out: When passing an out-parameter, the caller stores a blank value to the stack and any modifications made by the callee are popped into a variable by the caller.
    • inout: When passing an inout-parameter, the caller copies the value to the stack and any modifications made by the callee are popped back into the variable passed by the caller. Note: This is not passing by-reference. The value passed is really a copy, so if the same variable is passed to multiple parameters at once, they are not aliases for the same variable. They are copies, and both will be copied back into the passed variable, with the second copy overwriting the first. However, it is indeterminate as to which is copied first and second.

Auto

Auto variables are dynamically typed. When writing code, the placeholder "auto" is used for the type of the variable. At run-time, the variable assumes whatever type is assigned to it. (They are not typeless like Perl.) An example declaration:

private auto $bar;
  • reference: An auto variable that can be used to reference another auto variable. Like a pointer, but arithmetic is not allowed and the referent is not deallocated while the reference exists.
  • integer: Like a primitive integer, except it cannot overflow. If a math operation would yield a result that is too large for the allocated memory, more memory is allocated automatically. If no more memory is available, an exception is raised.
  • fraction: Two integers representing a numerator and denominator.
  • string: A string stored in an auto must be an array of chars. This is different from an array only because allocating an auto for each char would be terribly inefficient. XXX: This might change to support any size of integer.
  • array: Like a string, except each element may have a different type. These can only be stored in auto variables.
  • hash: Like a struct, except the number and names of the elements are dynamic. Like structs, but unlike many other languages, hashes are ordered. These can only be stored in auto variables.
  • fnref: A reference to a function, for OO support. XXX: explain this better.

Notice that the list of primitive types is similar to the list of auto types. For example, reference is similar to ptr, integer is similar to int, fraction is similar to float, array is similar to string, and hash is similar to struct.