|
tplexpressions
trickonos template language (expressions).
Phase-User Expression Syntax EXPR: NIL | BOOL | INTEGER | STRING | LIST | DICTIONARY |
NAME_READ | NAME_WRITE | CALL | UNOP | BINOP |
INDEX_READ | INDEX_WRITE | ATTR_READ | ATTR_WRITE |
SCOR | SCAND | METHODCALL | TYPECHECK .List ConstructorThe list constructor (or list builder expression) defines a list object. Semi-Formal: LIST : '[' [ EXPR { ',' EXPR } ] ']' .Informal:
Example []; // valid empty list [1]; // valid list with one element (integer object 1) [1,2,3]; // valid list with three elements [].append(1,2,3); // equivalent to list constructor [1,2,3] [[1,2,3],[1,2,3]]; // valid list containing the above list [1,2,3] (twice) [; // invalid, missing ] [1 2]; // invalid, not comma separated Dictionary ConstructorThe dict constructor (or dict builder expression) defines a dictionary object. Semi-Formal: ASSOC : (IDENT | STRING) ':' EXPR .
DICTIONARY : '{' [ ASSOC { ',' ASSOC } ] '}' .Informal:
Example {}; // valid empty dictionary
{id:1}; // valid dictionary, where "ID" is associated integer object 1
{id:1,"OID":{oid:2}}; // valid dictionary with 2 elements
d := {}; // d = {}
d.id := 1; // d = {id:1}
d["OID"] := {}; // d = {id:1,"OID":{}}
d["OID"].oid := 2; // d = {id:1,"OID":{oid:2}}
{id:1,id:2}; // valid, one assoc (id:2)
{id:1,"ID":2}; // valid, one assoc (id:2)
{id:fun(),id:2}; // valid, one assoc (id:2), fun is called
{; // invalid, missing }
{id:1 id:2}; // invalid, not comma separated
{1:1}; // invalid, key must be ident or stringName ReadSemi-Formal: NAME_READ: IDENT . Informal:
Name WriteSemi-Formal: NAME_WRITE: IDENT ':=' EXPR . Informal:
Unary OperatorsSemi-Formal: UNOPER: '+' | '-' | 'not' . UNOP: UNOPER EXPR . Informal:
Example nobj := not obj; // Unary operation on Object obj Binary OperatorsSemi-Formal: BINOPER: '+' | '-' | '*' | 'div' | 'shl' | 'shr' | ... | '=' | '<>' | ... . BINOP: LEXPR BINOPER REXPR . Informal:
Example thesum := operand1 + operand2; // Binary operation operand1.^Add(operand2) Shortcut Binary Operator ANDSemi-Formal: SCAND: LEXPR 'and' REXPR . Informal:
Example x := a and b; // (mostly) equivalent if statement if a = nil then x := a; elseif a = false then x := a; elseif a = true then x := b; else x := a and b; // <=> binary operation a.^and(b) since a is <> false,true,nil end; Shortcut Binary Operator ORSemi-Formal: SCOR: LEXPR 'or' REXPR . Informal:
Example x := a or b; // (mostly) equivalent if statement if a = nil then x := b; elseif a = false then x := b; elseif a = true then x := a; else x := a or b; // <=> binary operation a.^or(b) since a is <> false,true,nil end; Object CallSemi-Formal: ARGS: [ EXPR { ',' EXPR } ] .
CALL: OEXPR '(' ARGS ')' .Informal:
Example 1(); // valid syntax, but creates a runtime error since Integer doesn't implement ^Call
function callme; // <- function decl. creates a function object and assigns it to callme
// do stuff..
Result := 1;
end;
x := callme; // not a call, this simply returns the function object
x(); // calls the above declared function, functions implement ^Call
callme(); // also calls the above declared function
Object Method CallSemi-Formal: ARGS: [ EXPR { ',' EXPR } ] .
METHODCALL: OEXPR '.' IDENT '(' ARGS ')' .Informal:
Example x.dosomething(); // valid method call x.dosomething; // not a method call (member access, see attribute read) x.(y)(); // invalid syntax, method name must be IDENT, not EXPR Object Attribute ReadSemi-Formal: ATTR_READ: OEXPR '.' IDENT . Informal:
NOTE: Member access does (in general) nothing with methods, its implementation dependent and a method call itself x.some_method_name(); // method call
x.some_method_name; // <=> x.^GetMember('SOME_METHOD_NAME'), nothing related to the some_method_name methodObject Attribute WriteSemi-Formal: ATTR_WRITE: OEXPR '.' IDENT ':=' EXPR . Informal:
Example x.someattr := y; // valid syntax, <=> x.^SetMember('SOMEATTR',y);
// dictionary example
d := {};
d.id := 1; // add an association as implemented in dictionary type
d.count := 1; // and another one
d.count; // = 1
d.count(); // = 2 -> method call implemented in dict Object Index ReadSemi-Formal: INDEX_READ: OEXPR '[' IEXPR { ',' IEXPR } ']' .Informal:
Object Index WriteSemi-Formal: INDEX_WRITE: OEXPR '[' IEXPR { ',' IEXPR } ']' ':=' EXPR .Informal:
TypecheckSemi-Formal: TYPECHECK: EXPR 'is' IDENT . Informal:
Example nil is None; // -> true nil is Integer; // -> false 1 is Integer; // -> true is func; // syntax error false is boolean is BOOLEAN; // -> true | ||||||||||||||||||||||||||||||||||||||||