Std. CodeGen
(Cure Standard Library v0.38.0)
View Source
First-class Cure code generator built on Std.Syntax and Cure.Compiler.Printer.
Every gen_* constructor returns a Std.Syntax.Syntax tree, so generated
code can either be rendered to formatted, parse-verified .cure source via
render/1, render_formatted/1, and render_verified/1, or spliced
directly into compile-time macros.
Examples
use Std.CodeGen
let body = gen_binop("+", gen_var("x"), gen_int(1))
let inc = gen_fn("inc", [gen_param("x", gen_type_ref("Int"))], gen_type_ref("Int"), body)
let m = gen_mod("Demo.Math", [gen_comment("Auto-generated"), inc])
render_verified(m)
Summary
Types
Errors produced when lowering, printing, or parse-verifying generated syntax.
A named, typed field specification for gen_rec/2.
A planned generated source file pairing an output path with its Syntax tree.
A rendered and parse-verified source file pairing an output path with its source text.
Callbacks
Lift a Cure runtime value into a Std.Syntax.Syntax literal expression.
Functions
Append a declaration decl to a module, proof, interface, implementation, or block container.
Append multiple declarations decls to a module, proof, interface, implementation, or block container.
Append a use <module_name> import to container.
Mark a function definition fn_node as private (local fn).
Construct an unguarded match arm <pattern> -> <body>.
Construct an atom literal syntax node.
Construct a standard generated-file banner comment attributing generator_name.
Construct a binary operator expression <left> <op> <right> (e.g. "+", "==", "*", "and").
Construct a sequential expression block.
Construct a boolean literal syntax node.
Construct a function call <name>(<args>).
Construct a character literal syntax node.
Construct a standalone # <text> line comment node (multi-line strings emit one # per line).
Construct a cons list expression [<head> | <tail>].
Construct a decorator @<name>(<args>) (or bare @<name> when args is empty).
Construct a standalone ## <text> doc comment node (multi-line strings emit one ## per line).
Attach @extern(<mod_name>, <fun_name>, <arity>) to a function signature sig.
Construct a FieldSpec pairing field name with type kind.
Construct a field access expression <target>.<field>.
Construct a float literal syntax node.
Construct a public function definition fn <name>(<params>) -> <ret> = <body>.
Construct a single clause | <patterns> -> <body> for a multi-clause function.
Construct a multi-clause function definition fn <name>(<params>) -> <ret> with clauses.
Construct a bodyless function signature fn <name>(<params>) -> <ret> (for interfaces and @extern).
Construct a function type (<domains>) -> <ret>.
Construct a guarded match arm <pattern> when <guard> -> <body>.
Construct a guarded clause | <patterns> when <guard> -> <body> for a multi-clause function.
Construct an if <cond> then <then_br> else <else_br> conditional expression.
Construct an implementation block impl <interface_name> for <target_type> with methods fns.
Construct an integer literal syntax node.
Construct a protocol/interface declaration proto <name>(<type_params>) with method signatures sigs.
Construct an anonymous function fn(<param_names>) -> <body>.
Construct a let <name> = <value> binding.
Construct a list literal [<items>].
Construct a private function definition local fn <name>(<params>) -> <ret> = <body>.
Construct a map literal %{<pairs>}.
Construct a match <scrutinee> expression with arms.
Construct a mod <name> container with body declarations decls.
Construct a key-value pair <key>: <value> for maps and record literals.
Construct a typed function parameter name: kind.
Construct a pickup conditional block with clauses.
Construct a pickup guard clause <guard> -> <body>.
Construct a pickup terminator clause else -> <body>.
Thread initial through a list of call expressions steps using the |> pipe operator.
Construct a single pipe step <lhs> |> <fn_name>(<extra_args>).
Construct a top-level program block containing items separated by canonical blank lines.
Construct a proof <name> container with proof declarations decls.
Construct a module-qualified function call <mod_name>.<fn_name>(<args>).
Construct a rec <name> record declaration with fields.
Construct a parameterized rec <name>(<type_params>) record declaration with fields.
Construct a record construction literal <name>{<field_pairs>}.
Construct an idiomatic section divider comment # -- <title> ---... padded to 76 columns.
Construct a string literal syntax node.
Construct a tuple literal (<items>).
Construct a tuple type Tuple(<items>).
Construct a sum type declaration type <name> = <variants>.
Construct a parameterized type application <name>(<args>) (e.g. List(Int)).
Construct a parameterized sum type declaration type <name>(<type_params>) = <variants>.
Construct a bare type reference <name> (e.g. Int, String).
Construct a typealias <name> = <rhs> declaration.
Construct a typed let <name>: <kind> = <value> binding.
Construct the unit value literal ().
Construct a unary operator expression <op> <operand> (e.g. "-", "not").
Construct a use <module_name> import declaration.
Construct an aliased use <module_name> as <alias_name> import declaration.
Construct a selective use <module_name>.{<items>} import declaration.
Construct a variable reference syntax node.
Construct a nullary enum variant name.
Construct an enum variant name(payload_types...) carrying typed payloads.
Construct a GeneratedFile entry pairing path with its Syntax tree content.
Return true when name is a Cure reserved keyword.
Lift a List(t) into a list literal [...] using elem_fn for each element.
Lift an Option(t) into Some(<val>) or None() using elem_fn.
Parse a single Cure expression string into a Syntax tree.
Parse a Cure source string (module, declaration list, or expression) into a Syntax tree.
Render a Syntax tree to Cure source using the default 2-space indentation.
Atomically render, format, and parse-verify every file in files, returning all RenderedFiles or the first error.
Render and format a Syntax tree with Cure's canonical formatter.
Render and format a Syntax tree. Pass true for the algebra formatter or false for the text formatter.
Render, format, and parse-verify a Syntax tree, guaranteeing syntactically valid Cure output.
Render a Syntax tree, optionally format it, and verify that it lexes and parses cleanly.
Render a Syntax tree to Cure source with an explicit indentation width (spaces, clamped to 1..8).
Lift a Result(t, e) into Ok(<val>) or Error(<err>) using ok_fn and err_fn.
Sanitize a foreign identifier into a valid Cure identifier, escaping reserved keywords with a trailing _.
Convert a snake_case or kebab-case name into PascalCase.
Convert a camelCase, PascalCase, or kebab-case name into snake_case.
Convert value into its Cure Syntax representation.
Request a blank line before node inside a block or module body.
Attach a # <text> leading comment above node (call repeatedly to stack lines).
Attach a decorator @<name>(<args>) above node (call repeatedly to stack decorators).
Attach a ## <text> doc comment above node.
Attach a when <guard> clause to a function definition fn_node.
Attach a trailing inline # <text> comment on the same line as node.
Types
enum CodeGenError
LowerError
PrintError
ParseError
Errors produced when lowering, printing, or parse-verifying generated syntax.
struct FieldSpec
?
?
A named, typed field specification for gen_rec/2.
struct GeneratedFile
?
?
A planned generated source file pairing an output path with its Syntax tree.
struct RenderedFile
?
?
A rendered and parse-verified source file pairing an output path with its source text.
Callbacks
Functions
fn add_decl(container: Syntax, decl: Syntax) -> Syntax
Append a declaration decl to a module, proof, interface, implementation, or block container.
fn add_decls(container: Syntax, decls: List(Syntax)) -> Syntax
Append multiple declarations decls to a module, proof, interface, implementation, or block container.
fn add_use(container: Syntax, module_name: String) -> Syntax
Append a use <module_name> import to container.
fn append_list_attr(values: List(Attr), key: Atom, item: SynLit) -> List(Attr)
fn append_node_list_attr(node: Syntax, key: Atom, item: SynLit) -> Syntax
fn append_synlit_list(existing: SynLit, item: SynLit) -> SynLit
fn as_local(fn_node: Syntax) -> Syntax
Mark a function definition fn_node as private (local fn).
fn char_or(cp: Int, fallback: Char) -> Char
fn field_nodes(fields: List(FieldSpec)) -> List(Syntax)
fn gen_arm(pattern: Syntax, body: Syntax) -> Syntax
Construct an unguarded match arm <pattern> -> <body>.
fn gen_atom(value: Atom) -> Syntax
Construct an atom literal syntax node.
fn gen_banner(generator_name: String) -> Syntax
Construct a standard generated-file banner comment attributing generator_name.
fn gen_binop(op: String, left: Syntax, right: Syntax) -> Syntax
Construct a binary operator expression <left> <op> <right> (e.g. "+", "==", "*", "and").
fn gen_block(exprs: List(Syntax)) -> Syntax
Construct a sequential expression block.
fn gen_bool(value: Bool) -> Syntax
Construct a boolean literal syntax node.
fn gen_call(name: String, args: List(Syntax)) -> Syntax
Construct a function call <name>(<args>).
fn gen_char(value: Char) -> Syntax
Construct a character literal syntax node.
fn gen_comment(text: String) -> Syntax
Construct a standalone # <text> line comment node (multi-line strings emit one # per line).
fn gen_cons(head: Syntax, tail: Syntax) -> Syntax
Construct a cons list expression [<head> | <tail>].
fn gen_decorator(name: String, args: List(Syntax)) -> Syntax
Construct a decorator @<name>(<args>) (or bare @<name> when args is empty).
fn gen_doc(text: String) -> Syntax
Construct a standalone ## <text> doc comment node (multi-line strings emit one ## per line).
fn gen_extern(mod_name: Atom, fun_name: Atom, arity: Int, sig: Syntax) -> Syntax
Attach @extern(<mod_name>, <fun_name>, <arity>) to a function signature sig.
fn gen_field(name: String, kind: Syntax) -> FieldSpec
Construct a FieldSpec pairing field name with type kind.
fn gen_field_access(target: Syntax, field: String) -> Syntax
Construct a field access expression <target>.<field>.
fn gen_float(value: Float) -> Syntax
Construct a float literal syntax node.
fn gen_fn(name: String, params: List(Syntax), ret: Syntax, body: Syntax) -> Syntax
Construct a public function definition fn <name>(<params>) -> <ret> = <body>.
fn gen_fn_clause(patterns: List(Syntax), body: Syntax) -> Syntax
Construct a single clause | <patterns> -> <body> for a multi-clause function.
fn gen_fn_clauses(name: String, params: List(Syntax), ret: Syntax, clauses: List(Syntax)) -> Syntax
Construct a multi-clause function definition fn <name>(<params>) -> <ret> with clauses.
fn gen_fn_sig(name: String, params: List(Syntax), ret: Syntax) -> Syntax
Construct a bodyless function signature fn <name>(<params>) -> <ret> (for interfaces and @extern).
fn gen_fn_type(domains: List(Syntax), ret: Syntax) -> Syntax
Construct a function type (<domains>) -> <ret>.
fn gen_guarded_arm(pattern: Syntax, guard: Syntax, body: Syntax) -> Syntax
Construct a guarded match arm <pattern> when <guard> -> <body>.
fn gen_guarded_fn_clause(patterns: List(Syntax), guard: Syntax, body: Syntax) -> Syntax
Construct a guarded clause | <patterns> when <guard> -> <body> for a multi-clause function.
fn gen_if(cond: Syntax, then_br: Syntax, else_br: Syntax) -> Syntax
Construct an if <cond> then <then_br> else <else_br> conditional expression.
fn gen_impl(interface_name: String, target_type: Syntax, fns: List(Syntax)) -> Syntax
Construct an implementation block impl <interface_name> for <target_type> with methods fns.
fn gen_int(value: Int) -> Syntax
Construct an integer literal syntax node.
fn gen_interface(name: String, type_params: List(String), sigs: List(Syntax)) -> Syntax
Construct a protocol/interface declaration proto <name>(<type_params>) with method signatures sigs.
fn gen_lambda(param_names: List(String), body: Syntax) -> Syntax
Construct an anonymous function fn(<param_names>) -> <body>.
fn gen_let(name: String, value: Syntax) -> Syntax
Construct a let <name> = <value> binding.
fn gen_list(items: List(Syntax)) -> Syntax
Construct a list literal [<items>].
fn gen_local_fn(name: String, params: List(Syntax), ret: Syntax, body: Syntax) -> Syntax
Construct a private function definition local fn <name>(<params>) -> <ret> = <body>.
fn gen_map(pairs: List(Syntax)) -> Syntax
Construct a map literal %{<pairs>}.
fn gen_match(scrutinee: Syntax, arms: List(Syntax)) -> Syntax
Construct a match <scrutinee> expression with arms.
fn gen_mod(name: String, decls: List(Syntax)) -> Syntax
Construct a mod <name> container with body declarations decls.
fn gen_pair(key: Syntax, value: Syntax) -> Syntax
Construct a key-value pair <key>: <value> for maps and record literals.
fn gen_param(name: String, kind: Syntax) -> Syntax
Construct a typed function parameter name: kind.
fn gen_pickup(clauses: List(Syntax)) -> Syntax
Construct a pickup conditional block with clauses.
fn gen_pickup_clause(guard: Syntax, body: Syntax) -> Syntax
Construct a pickup guard clause <guard> -> <body>.
fn gen_pickup_else(body: Syntax) -> Syntax
Construct a pickup terminator clause else -> <body>.
fn gen_pipe(initial: Syntax, steps: List(Syntax)) -> Syntax
Thread initial through a list of call expressions steps using the |> pipe operator.
fn gen_pipe_step(lhs: Syntax, fn_name: String, extra_args: List(Syntax)) -> Syntax
Construct a single pipe step <lhs> |> <fn_name>(<extra_args>).
fn gen_program(items: List(Syntax)) -> Syntax
Construct a top-level program block containing items separated by canonical blank lines.
fn gen_proof(name: String, decls: List(Syntax)) -> Syntax
Construct a proof <name> container with proof declarations decls.
fn gen_qual_call(mod_name: String, fn_name: String, args: List(Syntax)) -> Syntax
Construct a module-qualified function call <mod_name>.<fn_name>(<args>).
fn gen_rec(name: String, fields: List(FieldSpec)) -> Syntax
Construct a rec <name> record declaration with fields.
fn gen_rec_generic(name: String, type_params: List(String), fields: List(FieldSpec)) -> Syntax
Construct a parameterized rec <name>(<type_params>) record declaration with fields.
fn gen_record_lit(name: String, field_pairs: List(Syntax)) -> Syntax
Construct a record construction literal <name>{<field_pairs>}.
fn gen_section(title: String) -> Syntax
Construct an idiomatic section divider comment # -- <title> ---... padded to 76 columns.
fn gen_string(value: String) -> Syntax
Construct a string literal syntax node.
fn gen_tuple(items: List(Syntax)) -> Syntax
Construct a tuple literal (<items>).
fn gen_tuple_type(items: List(Syntax)) -> Syntax
Construct a tuple type Tuple(<items>).
fn gen_type(name: String, variants: List(Syntax)) -> Syntax
Construct a sum type declaration type <name> = <variants>.
fn gen_type_app(name: String, args: List(Syntax)) -> Syntax
Construct a parameterized type application <name>(<args>) (e.g. List(Int)).
fn gen_type_generic(name: String, type_params: List(String), variants: List(Syntax)) -> Syntax
Construct a parameterized sum type declaration type <name>(<type_params>) = <variants>.
fn gen_type_ref(name: String) -> Syntax
Construct a bare type reference <name> (e.g. Int, String).
fn gen_typealias(name: String, rhs: Syntax) -> Syntax
Construct a typealias <name> = <rhs> declaration.
fn gen_typed_let(name: String, kind: Syntax, value: Syntax) -> Syntax
Construct a typed let <name>: <kind> = <value> binding.
fn gen_unit() -> Syntax
Construct the unit value literal ().
fn gen_unop(op: String, operand: Syntax) -> Syntax
Construct a unary operator expression <op> <operand> (e.g. "-", "not").
fn gen_use(module_name: String) -> Syntax
Construct a use <module_name> import declaration.
fn gen_use_as(module_name: String, alias_name: String) -> Syntax
Construct an aliased use <module_name> as <alias_name> import declaration.
fn gen_use_items(module_name: String, items: List(String)) -> Syntax
Construct a selective use <module_name>.{<items>} import declaration.
fn gen_var(name: String) -> Syntax
Construct a variable reference syntax node.
fn gen_variant(name: String) -> Syntax
Construct a nullary enum variant name.
fn gen_variant_payload(name: String, payload_types: List(Syntax)) -> Syntax
Construct an enum variant name(payload_types...) carrying typed payloads.
fn generated_file(path: String, content: Syntax) -> GeneratedFile
Construct a GeneratedFile entry pairing path with its Syntax tree content.
fn is_ascii_digit(cp: Int) -> Bool
fn is_ascii_lower(cp: Int) -> Bool
fn is_ascii_upper(cp: Int) -> Bool
fn is_ident_char(cp: Int) -> Bool
fn is_reserved_word(name: String) -> Bool
Return true when name is a Cure reserved keyword.
fn lambda_params(names: List(String)) -> SynLit
fn list_to_syntax(items: List(t), elem_fn: Function(t, Syntax)) -> Syntax
Lift a List(t) into a list literal [...] using elem_fn for each element.
fn lower_cp(cp: Int) -> Int
fn map_to_syntax(items: List(t), f: Function(t, Syntax)) -> List(Syntax)
fn nil_binders(items: List(Syntax)) -> SynLit
fn option_to_syntax(opt: Option(t), elem_fn: Function(t, Syntax)) -> Syntax
Lift an Option(t) into Some(<val>) or None() using elem_fn.
fn parse_expr(source: String) -> Result(Syntax, CodeGenError)
Parse a single Cure expression string into a Syntax tree.
fn parse_source(source: String) -> Result(Syntax, CodeGenError)
Parse a Cure source string (module, declaration list, or expression) into a Syntax tree.
fn pascal_chars(chars: List(Char), cap_next: Bool) -> List(Char)
fn put_attr(values: List(Attr), key: Atom, value: SynLit) -> List(Attr)
fn qualified_name(mod_name: String, fn_name: String) -> String
fn render(syntax: Syntax) -> Result(String, CodeGenError)
Render a Syntax tree to Cure source using the default 2-space indentation.
fn render_all(files: List(GeneratedFile)) -> Result(List(RenderedFile), CodeGenError)
Atomically render, format, and parse-verify every file in files, returning all RenderedFiles or the first error.
fn render_formatted(syntax: Syntax) -> Result(String, CodeGenError)
Render and format a Syntax tree with Cure's canonical formatter.
fn render_formatted_with(syntax: Syntax, use_algebra: Bool) -> Result(String, CodeGenError)
Render and format a Syntax tree. Pass true for the algebra formatter or false for the text formatter.
fn render_verified(syntax: Syntax) -> Result(String, CodeGenError)
Render, format, and parse-verify a Syntax tree, guaranteeing syntactically valid Cure output.
fn render_verified_with(syntax: Syntax, format: Bool) -> Result(String, CodeGenError)
Render a Syntax tree, optionally format it, and verify that it lexes and parses cleanly.
fn render_with_indent(syntax: Syntax, indent_spaces: Int) -> Result(String, CodeGenError)
Render a Syntax tree to Cure source with an explicit indentation width (spaces, clamped to 1..8).
fn repeat_dash(n: Int) -> String
fn result_to_syntax(res: Result(t, e), ok_fn: Function(t, Syntax), err_fn: Function(e, Syntax)) -> Syntax
Lift a Result(t, e) into Ok(<val>) or Error(<err>) using ok_fn and err_fn.
fn safe_ident(name: String) -> String
Sanitize a foreign identifier into a valid Cure identifier, escaping reserved keywords with a trailing _.
fn sanitize_chars(chars: List(Char)) -> List(Char)
fn set_node_attr(node: Syntax, key: Atom, value: SynLit) -> Syntax
fn snake_chars(chars: List(Char), prev_sep: Bool) -> List(Char)
fn string_list(items: List(String)) -> SynLit
fn to_pascal_case(name: String) -> String
Convert a snake_case or kebab-case name into PascalCase.
fn to_snake_case(name: String) -> String
Convert a camelCase, PascalCase, or kebab-case name into snake_case.
fn to_syntax(value: t) -> Syntax
Convert value into its Cure Syntax representation.
fn upper_cp(cp: Int) -> Int
fn with_blank_before(node: Syntax) -> Syntax
Request a blank line before node inside a block or module body.
fn with_comment(node: Syntax, text: String) -> Syntax
Attach a # <text> leading comment above node (call repeatedly to stack lines).
fn with_decorator(node: Syntax, name: String, args: List(Syntax)) -> Syntax
Attach a decorator @<name>(<args>) above node (call repeatedly to stack decorators).
fn with_doc(node: Syntax, text: String) -> Syntax
Attach a ## <text> doc comment above node.
fn with_guard(fn_node: Syntax, guard: Syntax) -> Syntax
Attach a when <guard> clause to a function definition fn_node.
fn with_trailing_comment(node: Syntax, text: String) -> Syntax
Attach a trailing inline # <text> comment on the same line as node.