Home | About | Partners | Contact Us

SourceForge Logo

Quick Links
Building XL
XL Mailing List

Understanding XL
Conceptual overview
XL examples
Inside XL
Concept Programming

In depth
Browse GIT
SourceForge Info

Other projects
GNU Project
The Mozart Project

XLR: Extensible Language and Runtime

The art of turning ideas into code

XL Operators

Prev: Expression reduction


Next: Compile-time variadics

XL doesn't really have a notion of "built-in operator" as most programming languages do. Instead, a precedence table drives the parsing of operators found in XL0 text into the appropriate parse tree structure. By default, this precedence table is specified by the xl.syntax input file. It is considered bad taste to change the priority of any of the operators defined in this table, as this would introduce significant syntactic noise. However, it is OK to add entries in this table, notably when extending the language. This can be done by compiler plug-ins.

From a parsing point of view, XL only classifies operators along two dimensions: name vs. symbol, and prefix vs. infix vs. block.

  • Names are made of sequences of alphanumeric characters (beginning with a letter), whereas symbols are made of sequences of non-alphanumeric characters. So not and in are names, whereas + or <=> are symbols. Beyond the parser, the two categories are treated identically.
  • Infix symbols sit between their two operands, and must be defined in the precedence table. Prefix symbols sit before their unique operand, and the default for a symbol not found in that table is to be considered as a prefix with the priority of a function name. Blocks sit around their unique operand. The * operator in A*B is infix (its priority being defined by xl.syntax whereas not in not Found is a prefix operator with a non-default priority. Finally, Factorial in Factorial 5 is a prefix operator with the default priority for function names. The [ and ] characters in [A,B,C] define a block (the content being made of the infix , with A on its left, and another infix , on its right).

XL (more precisely XL1) uses operators in roles which are typically played by keywords in older languages. In particular, the new-line and semi-colon ; are two operators, which are in general used to sequence instructions. Structures such as while loops, if-then-else, or indentation blocks, are also defined in terms of operators. For instance, consider the following text:

while X > 0 loop if not EOF then A := A+1 B *= (2.54 + MATH.sqrt(1-A))

Below is the C++ translation of the creation of the corresponding parse tree (this C++ code was generated by using quote and asking the XL compiler to emit C++ code). You can find the various operators from the source code replicated in that structure. Note the use of I+ and I- to denote indentation delimiters, and the use of a new-line operator (ascii::cr).

// C++ code generating the above parse tree newinfix(text("loop"), newprefix( newname(text("while")), newinfix(text(">"), newname(text("x")), newinteger(0))), newblock( newinfix(text("then"), newprefix( newname(text("if")), newprefix( newname(text("not")), newname(text("eof")))), newblock( newinfix(xl::textio::encoding::ascii::cr, newinfix(text(":="), newname(text("a")), newinfix(text("+"), newname(text("a")), newinteger(1))), newinfix(text("*="), newname(text("b")), newblock( newinfix(text("+"), newreal(2.54), newprefix( newinfix(text("."), newname(text("math")), newname(text("sqrt"))), newblock( newinfix(text("-"), newinteger(1), newname(text("a"))), "(", ")"))), "(", ")"))), "I+", "I-")), "I+", "I-")))

Prev: Expression reduction


Next: Compile-time variadics

Copyright 2008 Christophe de Dinechin (Blog)
E-mail: XL Mailing List (polluted by spam, unfortunately)