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

Per SRD 10 §“String Interpolation”, string literals containing { … } placeholders are desugared to a printf call over the placeholder bodies. The bodies are parsed as full GK 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§

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.