-
Notifications
You must be signed in to change notification settings - Fork 1
Syntax
The XP Language's syntax is based on PHP. If you're coming from there as a background, the following will given a short overview of the differences:
public class HelloWorld {
public static void main(string[] $args) {
util.cmd.Console::writeLine('Hello World!');
}
}
The things you will have noticed are:
- Classes may also have modifiers.
- The
extends Object
is optional and added by the compiler if omitted. - The keyword
function
is gone and replaced by the return type. Because the main() method does not return anything, we use void. - An array type is written as component[]
- Variables still have dollar signs. This makes it easy to spot them, that's why we've decided to keep this!
- Fully qualified classnames are written with dots.
- The object operator is also a dot (at the same time, the string concatenation operator is now the tilde,
~
).
XP Language has literals for primitives, the void type, arrays and maps:
$i= 1; // an integer
$d= 1.0; // a double
$s= 'string'; // a string, inside single quotes
$s= "string"; // a string, inside double quotes, may contain escape sequences
$t= true; $f= false; // boolean values true and false
$n= null; // NULL
$a= [1, 2, 3]; // an array of any type as values
$m= [one: 1, two: 2]; // a map consisting of string keys and any type as values
The type literals for the above primitives, arrays and maps are as follows:
int $i;
double $d;
string $s;
bool $b;
void; // Only valid for return type
var $v; // Any type, including reference types
int[] $a; // An array of ints, var[] allows any component type
[:bool] $m; // A map with string keys and boolean values
The types in XP Language are declared by the keywords class
, interface
or enum
.
Classes may have a parent (if omitted, defaults to lang.Object
), can implement as many interfaces as they wish and declare members. Classes can be abstract (means abstract parts of their declaration need to be implemented by subclassing), or final (meaning they cannot be subclassed). Members can be declared using visibility operators public
(anyone may access), protected
(only the declaring class and subclasses have access) and private
(access only the declaring class), and may also be made abstract
or final
with the same meanings as for classes. Members can be static, meaning they relate to the class and not to the instance.
public class Person extends Identity implements Comparable {
const string PREFIX= 'P'; // A constant
protected string $name; // A field
public __construct(string $name= null) { // The constructor
$this.setName($name);
}
public void setName(string $name) { // A method
$this.name= $name;
}
// ...
}
Classes are instantiated using the new
operator:
$p1= new Person(); // Calls constructor, makes $name argument use NULL default
$p2= new Person('Timm'); // Calls constructor and passes 'Timm' as argument
Interfaces can optionally have multiple parents, and may declare member methods, which need to be declared by classes implementing them.
public interface Closeable {
void close(); // A method waiting to be implemented
}
Enums provide type-safe enumeration of values, can define a parent which must also be an enum (defaults to lang.Enum
), may implement interfaces, and can optionally have any member a class can also have. There are two types of enums: Simple and abstract enums.
This simple enum declares the members Weekday::MON
through Weekday::SUN
with the ordinals 0
.. 6
, and defines an accessor to easily test for weekends:
public enum Weekday {
MON, TUE, WED, THU, FRI, SAT, SUN; // The enum members
public bool isWeekend() {
return $this.ordinal > self::$FRI.ordinal;
}
}
This abstract enum declares two members, plus
and minus
, which implement the abstract evaluate()
method inside the enum itself.
public abstract enum Operation {
plus {
public int evaluate(int $x, int $y) { return $x + $y; }
},
minus {
public int evaluate(int $x, int $y) { return $x - $y; }
};
public abstract int evaluate(int $x, int $y);
}
Enums cannot be instantiated. Instead, their public static members are used:
$monday= Weekday::$MON;
foreach ($day in Weekday::values()) {
util.cmd.Console::writeLine($day, $day.isWeekend() ? ' (weekend)' : '');
}
$result= Operation::$plus.evaluate(1, 2); // 3
XP Language supports the if
and switch
flow control statements as well as two forms of the ternary operator:
If / Else:
if ($condition) {
// When condition is met
} else if ($other) {
// When other condition is met
} else {
// When neither condition is met
}
Switch:
switch ($condition) {
case 1:
// When $condition == 1
// No break: Falls through
case 2:
// When $condition == 2
break;
default:
// When neither of the above is met
}
Ternary:
$result= $condition ? 1 : 2; // Result will be 1 if condition is true-ish, 2 otherwise
$result= $value ?: 3; // Result will be $value if that is true-ish, 3 otherwise
XP Language supports four kinds of loops: for
, while
, do..while
and foreach
:
For:
for ($i= 0; $i < 10; $i++) {
// Runs ten times
}
While:
while ($element= $elements.next()) {
// Runs until element becomes false-ish
}
Do:
$element= $elements.first();
do {
// Runs until element becomes false-ish
} while ($element= $elements.next());
Foreach:
foreach ($method in $class.getMethods()) {
// Iterates over all of the class' methods
}
foreach ($key, $value in $map) {
// Iterates over all of the given map's keys and values
}
Inside loops, the break
keyword exits the loop, while the continue
keyword goes to the next element.
XP Language supports exceptions as the primary means of handling errors.
The most basic form of a try/catch-statement is as follows:
try {
throw new lang.IllegalStateException('Should not be here!');
} catch (lang.IllegalStateException $e) {
util.cmd.Console::writeLine('Caught expected ', $e);
}
Multiple catch blocks may be added, and multiple exceptions may be caught and processed in one catch
block. A finally
block may be added to be executed whether or not an exception was thrown:
try {
// Invoke an action which may throw an exception
} catch (lang.IllegalStateException | lang.IllegalArgumentException $e) {
// Handle ISE, IAE (or subclasses of any of the two)
} catch (lang.XPException $e) {
// Handle any exception subclassed from lang.XPException
} finally {
// Runs in any case
}
Methods may declare thrown exceptions with a throws
clause. This is used for documentation and reflection purposes only - callers need not catch and handle these exceptions.
Some of them have already been used above, but here's the complete overview:
$r= 1 + 2; // Addition, 3
$r= 2 - 1; // Subtraction, 1
$r= 2 * 4; // Multiplication, 8
$r= 4 / 2; // Division, 2
$r= 7 % 3; // Modulo division, 1
$r= 2 ** 3; // Exponentiation, 8
All of these have assignment counterparts, with an equals sign following them (e.g. $r+= 2;
is equivalent with $r= $r + 2;
, $r*= 3
is the same as $r= $r * 3
and so on).
$r= ~$a; // Negation
$r= $a & $b; // Bitwise and
$r= $a | $b; // Bitwise or
$r= $a ^ $b; // Xor
$r= 4 >> 2; // Shift right, 1
$r= 4 << 2; // Shift left, 16
All of these also have assignment counterparts, with an equals sign following them: $r &= 3;
is the same as $r = $r & 3;
.
$r= $a == $b; // True when a and b are equals
$r= $a === $b; // True when a and b are identical
$r= $a != $b; // Opposite of ==
$r= $a !== $b; // Opposite of !==
$r= $a > $b; // Greater than comparison
$r= $a >= $b; // Greater than or equal to comparison
$r= $a < $b; // Smaller than comparison
$r= $a <= $b; // Smaller than or equal to comparison
$r= $a && $b; // True when both a and b are true-ish
$r= $a || $b; // True when either a or b is true-ish, lazily evaluated from left to right
$r= !$a; // Negation
$r= $a ~ $b; // Concatenates the string representations of a and b
$r~= $b; // Concatenates the string representations of b to r
$r= $a.length; // Array length, only applies to array type
$r= $person instanceof Person; // True when person holds an instance of Person, or a subclass, or implementor
$p= $iter.next() as Person; // Casts expression to a Person
$p= $iter.next() as Person?; // Casts expression to a Person, compile-time only!