-
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.
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;
}
// ...
}
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);
}
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
}