Class: ASTTransform::Thunk
- Defined in:
- lib/ast_transform/thunk.rb
Overview
The reordering primitive: an eagerly built wrapper node, spliced wherever the wrapped statements must EXECUTE — statement position or composed inside an expression (e.g. an assert_raises block body). Its body keeps its own source locations, and the lowering derives the wrapper's textual placement from them (see ThunkLowering), so the statements still emit on their original lines even though execution waits.
Children are [id, *body_statements] and the invariants are enforced here in initialize, which every
construction path shares — s routing, the thunk helper, and Processor rebuilds (+updated+ re-initializes).
Build thunks with TransformationHelper#thunk; reuse the same node to execute one body from several points
(multiplexing).
Runtime semantics are near-transparent (proc lowering): return still returns from the enclosing method, and
locals the body assigns stay method-scope. See ThunkLowering for the full contract.
Defined Under Namespace
Classes: Id
Instance Attribute Summary collapse
-
#body ⇒ Array<Parser::AST::Node>
readonly
Retrieves the Thunk's body.
-
#id ⇒ ASTTransform::Thunk::Id
readonly
Retrieves the Thunk's id.
Instance Method Summary collapse
-
#initialize(type, children, properties = {}) ⇒ Thunk
constructor
A new instance of Thunk.
Methods inherited from Node
Constructor Details
#initialize(type, children, properties = {}) ⇒ Thunk
Returns a new instance of Thunk.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/ast_transform/thunk.rb', line 28 def initialize(type, children, properties = {}) id, *body = children unless id.is_a?(ASTTransform::Thunk::Id) raise ArgumentError, "a Thunk's first child must be its #{Thunk::Id} (got #{id.class}); build thunks with the " \ "thunk(*statements) helper" end raise ArgumentError, "a Thunk must wrap at least one statement" if body.empty? # Captured before super (which freezes the node); frozen so the shared array cannot be mutated out from under # +children+. Rebuilds via +updated+ re-run initialize, so the capture can never go stale. @id = id @body = body.freeze super end |
Instance Attribute Details
#body ⇒ Array<Parser::AST::Node> (readonly)
Same as the Parser::AST::Node#children
Retrieves the Thunk's body.
53 54 55 |
# File 'lib/ast_transform/thunk.rb', line 53 def body @body end |
#id ⇒ ASTTransform::Thunk::Id (readonly)
Retrieves the Thunk's id.
47 48 49 |
# File 'lib/ast_transform/thunk.rb', line 47 def id @id end |