Gamend.GDScript.Codegen (gamend_plugin_tools v1.0.1213)

Copy Markdown View Source

AST into Elixir source text.

Two things make this more than a pretty-printer.

Mutation. Elixir allows rebinding, but a rebinding inside a block does not escape it. So for every if, codegen computes the set of already-declared variables assigned in any branch and threads them out as a tuple:

var bonus = 0          bonus = 0
if score > 100:   =>   bonus = if gd_truthy(score > 100), do: 50, else: bonus
    bonus = 50

A var declared inside a branch is block-scoped in GDScript too, so it is deliberately not lifted.

Semantics that differ. + concatenates strings in GDScript, / is integer division between two ints, and 0/""/[] are falsy. Elixir agrees with none of those, so those operators emit small private helpers rather than the native operator. They are emitted into the module itself only when used, so the output stays dependency-free and readable.

Summary

Functions

Generate Elixir source for module from parsed statements.

Does anything here mutate a collection in place? That is what needs the heap, and Gamend.GDScript.compile_all/2 asks across every script in a plugin.

Functions

generate(statements, module, opts \\ [])

@spec generate([tuple()], module :: String.t(), keyword()) :: String.t()

Generate Elixir source for module from parsed statements.

mutates?(node)

@spec mutates?(term()) :: boolean()

Does anything here mutate a collection in place? That is what needs the heap, and Gamend.GDScript.compile_all/2 asks across every script in a plugin.