Skip to main content

Module parser

Module parser 

Source
Expand description

Recursive descent parser for the Polydat DSL.

Parses a token stream (from the lexer) into an AST. Infix arithmetic expressions (+, -, *, /, %, ^) are handled by a Pratt (precedence-climbing) parser that produces Expr::BinOp nodes, later desugared by the compiler into function calls.

§String interpolation

String literals containing { … } placeholders (polydat_grammar.md §9) are desugared to a printf call over the placeholder bodies. The bodies are parsed as full Polydat expressions via parse_expression — same entry the rest of the language uses — so anything that can appear on a binding right-hand side can appear inside a placeholder.

Examples:

  • "hello" — no placeholders → Expr::StringLit("hello").
  • "{name}" — bare identifier → printf("{}", name).
  • "x={a + b}" — infix expression → printf("x={}", a + b).
  • "{format_u64(hash(cycle), 10)}@example.com" — nested call → printf("{}@example.com", format_u64(hash(cycle), 10)).
  • "{row.id}" — field access → printf("{}", row.id).
  • "{{literal braces}}" — escaped → stays a StringLit (printf emits { / } from {{ / }} at format time).
  • "x={:05}" — printf format spec, not a Polydat expression → stays a StringLit; the user is calling printf by hand.
  • "missing close {abc" — unterminated placeholder → stays a StringLit.

The desugaring is pure syntactic sugar. The resulting printf call goes through the standard binding/assembly path: each placeholder expression compiles to a node, the printf node ingests their outputs as wires, and at evaluation time Value::to_display_string() renders each input into its slot. No special runtime support is needed beyond printf.

Functions§

binary_operator
The binary operator a token spells, if it spells one.
binding_power
The binding powers (left, right) of a binary operator: the one precedence table of the language (polydat_grammar.md §6.1), whose levels parse_expr_bp lists. Expressions and comprehension where predicates (crate::comprehension::predicate) both parse with it. A higher power binds tighter; left < right associates to the left and left > right to the right. The unary operators - and ! bind tighter than every binary operator: each applies to the atom after it.
for_source_from_text
Classify and parse the text after for.
parse
Parse a token stream into a PolydatFile AST.
parse_expression
Parse a token stream as a single Polydat expression.
parse_with_tile_defaults
parse reading every tile that names no delimiters or sigil of its own under tile_defaults.