Programming Language for Old Timers


by David A. Moon
February 2006 .. September 2008

Comments and criticisms to dave underscore moon atsign alum dot mit dot edu.


Previous page   Table of Contents   Next page


Expression Syntax

The syntax of expressions can be explained notionally as:

defsyntax expression
  ?lhs is unit { ~^ ?:infix-operator ?rhs is unit | ~^ ?:infix-macro ... }* => ...

defparser unit
  { ?:prefix-operator }* { ?:variable | ?:literal | ?:macro-call | ?:expression } => ...

However, it can't really be defined this way because operator precedence and associativity must be used to resolve how the infix-operators and units in an expression roll up into invocations, and to resolve the infinite recursion of expression -> unit -> expression. The definition shown above is ambiguous.

Note that because of the infix/prefix operator ambiguity the syntax does not allow two expressions in a row, except when separated by a newline. Hence the ~^ in expression syntax, which means no newline is allowed here.

Unlike many languages, any name (whether punctuation or not) can be defined as an operator. There is no restriction to a built-in set of operators such as +, -, etc.

The syntactic types used but not defined above are as follows. All of these types are in the compiler module.

infix-operator a name with a visible constant definition that is an operator that permits infix calls.
prefix-operator a name with a visible constant definition that is an operator that permits prefix calls.
variable a name that is not punctuation and is not visibly defined as an operator or macro, or else a \ followed by any name (including punctuation) or string. We say "variable" although often this will have a fixed definition, i.e. does not allow its value to be changed by assignment.
literal a self-quoting literal number, character, or string.
macro-call a name with a visible constant definition that is either a macro or an operator that is a prefix macro, followed by the idiosyncratic syntax parsed by that macro.
infix-macro a name with a visible constant definition that is an operator that is an infix macro. The ... in the defsyntax stands for the idiosyncratic syntax parsed by that macro when called with the left-hand-side and a token stream.

Some expressions are informally called "statements." There is no difference between statements and other expressions in where they can appear in programs. Like any expression, a statement returns one or more values when executed. It is just a convenient classification, especially for expressions that are customarily executed for their side-effects rather than their value.


Previous page   Table of Contents   Next page