Content deleted Content added
No edit summary |
No edit summary |
||
Line 62:
The XL1 phase contains a large set of plug-ins, notably <code>XLSemantics</code>, that provide common abstractions like [[subroutine]], [[data type]] and [[variable]] [[declaration]] and [[definition]], as well as basic [[structured programming]] statements, like conditionals or loops.
=== Type System ===
XL1 type checking is [[data type|static]], with [[generic programming]] capabilities that are beyond those of languages like C++ or Ada. Types like arrays or pointers, which are primitive in languages like C++, are declared in the library in XL. For instance, an one-dimensional array type could be defined as follows:
generic [DataItem : type; Size : integer] type array
A ''validated generic type'' is a generic type where a condition indicates how the type can be used. Such types need not have generic parameters. For instance, one can declare that a type is <code>ordered</code> if it has a less-than operator as follows:
// A type is ordered if it has a less-than relationship
Line 68 ⟶ 74:
A, B : ordered
Test : boolean := A < B
It is then possible to declare a function that is implicitly generic because the type <code>ordered</code> itself is generic.
// Generic function for the minimum of one item
function Min(X : ordered) return ordered is
return X
This also applies to generic types that have parameters, such as <code>array</code>. A function computing the sum of the elements in any array can be written as follows:
function Sum(A : array) return array.DataItem is
I : integer
for I in 0..array.Size loop
result += A[I]
=== Type-safe variable argument lists ===
Functions can be [[polymorphism|overloaded]]. A function can be declared to use a variable number of arguments by using <code>other</code> in the parameter list. In such a function, <code>other</code> can be used to pass the variable number of arguments to another subroutine:
// Generic function for the minimum of N item
function Min(X : ordered; other) return ordered is
result := Min(other)
if X < result then
result := X
When such a function is called, the compiler recursively instantiates functiions to match the parameter list:
// Examples of use of the Min just declared
X : real := Min(1.3, 2.56, 7.21)
Y : integer := Min(1, 3, 6, 7, 1, 2)
=== Operator overloading ===
Operators can be defined using the <code>written</code> form of function declarations. Below is the code that would declare the addition of integers:
function Add(X, Y: integer) return integer written X+Y
Such ''written forms'' can have more than two parameters. For instance, a matrix linear transform can be written as:
function Linear(A, B, C : matrix) return matrix written A+B*C
A written form can use constants, and such a form is more specialized than a form without constants. For example:
function Equal(A, B : matrix) return boolean written A=B
function IsNull(A : matrix) return boolean written A=0
function IsUnity(A : matrix) return boolean written A=1
|