AST, S-Expression, General List: same thing

The end-result of scanning and parsing is an S-expression, which is a Python object of type Expression. In compiler terminology the S-expression is also called an abstract syntax tree or AST. For Mathics, there are only very few different kinds of nodes or atoms in this tree or list:

  • Number

  • String

  • Symbol

  • Filename

With the exception of the addition of Filename these are almost the same atoms described in Basic Internal Architecture for WL.

Not listed above but found in the link is “general expressions”: the thing that glues everything together. In Mathics, I imagine this corresponds to Expression.

The class definitions for these are given in mathics.core.parser.ast.

If you compare the above four AST types with other languages, you’ll find this is pretty sparse. For example, Python’s AST has well over 30 different types.

So what’s the difference? Python specializes AST types for different kinds of programming language constructs, such as for loops, conditional statements, exception blocks, different kinds of expressions, and so on. In WL and Mathics, these are all simply functions.