Menu

Calculate Language

Csaba Skrabák

The calculate field (as well as arrays inside the format fields and assign field values) in [The Templating Program Data Block] is written in a programming language for a stack machine. The bracy [Placeholders] use infix notation, but the trasplate script turns the operations into Polish notation. The tokens are in reversed order but it's still not a postfix notation as the operands appear reversed either, unlike in postfix notation. The reverse order makes the stack operations more efficient. The stack machine (that is part of the in-browser template engine) will first initialize its internal stack with the elements of the array in order.

Expression evaluation starts by pulling one value from the stack (i.e. the right end of the array). Depending on its type:

  • String value that is the name of an actually defined JavaScript variable. Action depends on its type:
    • string, number, bigint, boolean: evaluates to the value of the variable;
    • function: call it with zero arguments:
      • should be defined in backing script, or built-in, such as booing,
      • there is no function reference: every encounter of a function name results in calling it.
  • String value that is a valid JavaScript name but actually undefined: try accessing a configured backend API to get the value (full declarative mode).
  • String value that is not a valid JavaScript name:
    • numeric: evaluates to the number value;
    • built-in functions: ?, !, #, @, +1, -1, ~_:
      • at operator is indexing the current context object if it is an object, or, if not, just evaluates and takes the value;
    • binary/ternary operators: +, -, ~, *, |, (, [, ., ,, if, if!:
      • transplate script should prefer unary operators over binaries when generating the calculate program,
      • dot operator is field select: pull object name, pull field name (no evaluation), take the value,
      • square bracket is the indexingoperator: pull object name, pull and evaluate index, take the value,
      • the parenthesis is the function call operator: pull function name, pull argument, call,
      • comma operator is used to build an argument list for a function call - pull, evaluate, pull again, evaluate again, and push an array of both values on the stack.
  • Array value:
    • the 'if' and 'if!' binary operators accept such an array as an operand; these arrays contain a subprogram of the same structure as the calculate field itself;
    • the comma operator accepts an array as its first operand: an already started list to append the next value to.

Related

Wiki: Home
Wiki: Placeholders
Wiki: The Templating Program Data Block

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.