std / Program_Print
Available on macOS, Linux, Windows, Wasm
#import "Program_Print";
Turns compiler code nodes back into Jai source text.
Compiler :: #import "Compiler" ;
left := Compiler.make_integer_literal(-13 );
right := Compiler.make_integer_literal(29 );
sum: Compiler.Code_Binary_Operator;
sum.kind = .BINARY_OPERATOR;
sum.operator_type = cast (Compiler.Operator_Type) #char "+" ;
sum.left = left;
sum.right = right;
builder: String_Builder;
init_string_builder(*builder);
assert (print_expression (*builder, *sum));
assert (builder_to_string(*builder) == "(-13 + 29)" );
Procedures#
Writes s to builder as a quoted Jai string literal, escaping quotes, backslashes and control characters.
builder: String_Builder;
init_string_builder(*builder);
print_string_literal (*builder, "a\"b" );
assert (builder_to_string(*builder) == "\"a\\\"b\"" );Reset Copy ↗ Run ▸
Writes expression to builder as source text. Returns false for a node kind it cannot print, such as inline assembly.
Compiler :: #import "Compiler" ;
builder: String_Builder;
init_string_builder(*builder);
assert (print_expression (*builder, cast (*Compiler.Code_Node) Compiler.make_integer_literal(7 )));
assert (builder_to_string(*builder) == "7" );Reset Copy ↗ Run ▸
Returns decl as source text, or an empty string if it cannot be printed. The caller frees the string.
Compiler :: #import "Compiler" ;
decl: Compiler.Code_Declaration;
decl.kind = .DECLARATION;
decl.name = "answer" ;
decl.flags = .IS_CONSTANT;
decl.expression = Compiler.make_integer_literal(42 );
assert (print_declaration (*decl) == "answer:: 42" );Reset Copy ↗ Run ▸
Returns a metaprogram plugin that writes the loaded program back out as source, one pp_<file>.txt per file. Set its workspace and forward each message to its message procedure.
Compiler :: #import "Compiler" ;
plugin := get_plugin ();
plugin.workspace = Compiler.compiler_create_workspace("print" );Copy ↗ Run ▸ This one is only checked to compile, not run.
The plugin's message hook: collects each message for the final write.
The plugin's finish hook: writes the collected files.