Expression syntax
The right hand side of variable declaration and assignments as well as the arguments to a method call all follow expression syntax. There are several ways to specify an expression depending on the type of the variable/parameter.
Variable references
An expression can refer to another variable accessible from the current scope. The scope is always the outermost model where the referenced is used in and the this
keyword can be used to resolve any disambiguity. When accessing variables inside a model expression, the .
operator allows for member access.
NestedModel1:
x is Real: 1.0
NestedModel2:
nested1 is NestedModel1
Model:
nested2 is NestedModel2
x is Real: 2.0
y is Real: this.x + nested2.nested1.x
Numeric expressions
Int
and Real
expressions support arithmetic expressions with the four binary operators +
, -
, *
, /
representing addition, subtraction, multiplication and division respectively and the unary operator -
representing negation. Expressions can be grouped using parenthesis ()
Model:
add is Real: 3.0 + 4.0
mult is Real: add * 1.2
div is Real: (mult + 2.0) / add
sub is Real: 3.0 - div
i is Int: 3.0
j is Int: -i
String expressions
There are three types of string syntax in OpenPLX.
Normal strings
Normal strings are enclosed in double quotes "<string>"
.
Model:
str is String: "Hello world"
Multi-line strings
Multi-line strings are enclodes in triple double quotes """<string>"""
.
Model:
multi_line_string is String: """I
am
a
multiline string"""
At-strings
When referencing a file path it is sometimes necessary to reference a file on a path relative to the current file, by enclosing a path in double quotes prefix by an @
sign the OpenPLX parser will expand the string to a fullpath relative to the current file.
Model:
path is String: @"../resources/asset.txt"
Boolean expressions
Booleans are specified with the true
and false
keywords.
Model:
bool1 is Bool: true
bool2 is Bool: false
Model initializers
When the left hand side of an assigment is a model, typically a simple model with mostly primitive attributes, you can use {}
to initialize it without having to name the attributes. The attributes are set in the order they are declared in the target model. Notice that the order might be ambiguous for complex models with inheritance and complex attributes in which case this syntax should be used with caution.
Vec2:
x is Real: 1.0
y is Real: 2.0
Model:
v1 is Vec2: { 3.0, 4.0 }
v2 is Vec2: {
3.0 + 5.0,
4.0 + v1.x
}
Method calls
A method can be called by referencing the method as a variable follow by (
, a comma separated list of arguments and finally a closing parenthesis )
.
ModelA:
fn my_method(p1: Int, p2: Bool) -> Real
x is Real: my_method(1 + 2, true)
Array expressions
Array expressions use brackets []
and a comma separated list of values.
Model:
x is Real[]: [1, 2.0, 3 + 4.2]
Multi-line expressions
Expressions inside ()
, []
or {}
can be distributed over multiple lines.
Model:
arr is Int[] = [
1,
2,
3
]
sum is Real: (3 +
2 +
5
)
v is Vec3: {
0.1,
0.0,
5.0
}
Grammar
expression → term
term → factor ( ( "-" | "+" ) factor )*
factor → call ( ( "/" | "*" ) call )*
call → primary ( ( "(" arguments? ")" ) | ( "." IDENTIFIER ) )*
arguments → expression ( "," expression )*
primary → IDENTIFIER | STRING | REAL | INT | "(" expression ")" | "[" elements "]"
elements → expression ( "," expression )*