<commit_before># Copyright 2005, The Perl Foundation.

=head1 NAME

TGE::Instance - the runtime engine for TGE

=head1 DESCRIPTION

=cut

.namespace [ "TGE::Instance" ]

.sub "__onload" :load
    # define the class
    .local pmc base
    newclass base, "TGE::Instance"
    addattribute base, "cell"  # a hash for storing values of tree nodes
    addattribute base, "visit" # arrays of rules that apply to each node type
    addattribute base, "data"  # the original unmodified tree
    .return ()
.end

=head2 new

Returns a simple initialized TGE::Instance object. Doesn't accept any
constructor parameters.

=cut

.sub __init method
    $P0 = new PerlHash
    $P1 = new PerlHash
    $P2 = new PerlUndef
    setattribute self, "cell", $P0
    setattribute self, "visit", $P1
    setattribute self, "data", $P2
.end

# Call all visitors for a given node
.sub _scan_node method
    .param pmc node
    .param pmc name
    .local string type

    # If the user passed in a special name, look up visit actions for that one,
    # otherwise look them up for the type name of the node.
    $I1 = defined name
    if $I1 goto name_from_arg
    type = typeof node
    goto name_set
name_from_arg:
    type = name
name_set:

    # Iterate over the elements of the visit hash for the given type
    .local pmc actions
    .local int index
    .local pmc currule
    $P2 = getattribute self, 'visit'
    actions = $P2[type]
    $I2 = defined actions
    unless $I2 goto end_loop
    index = actions
loop:
    dec index
    if index < 0 goto end_loop
    currule = actions[index]
    self._install_action(node, currule)
    goto loop
end_loop:
    .return()
.end

=head2 get

  value = AGI.get('attrname')

Fetches the value of a particular attribute from the root node of the
grammar.

  value = AGI.get('attrname', node)
  value = AGI.get('attrname', node, 'type')

Fetches the value of a particular attribute from the node passed in.
When working with a tree where the nodes don't know their type (PGE
match objects, for example), you must also tell C<get> the type of the
node.

=cut

.sub get method
    .param pmc name
    .param pmc node
    .param pmc type

    # If no node was passed in, use top level node.
    unless null node goto node_exists
    node = getattribute self, 'data'
node_exists:

    .local pmc id
    id = _lookup_id(node)

    .local pmc cell
    $P1 = getattribute self, "cell"
    # First check to see if cell is defined
    $P2 = $P1[name]
    $I1 = defined $P2
    if $I1 goto name_hash_exists
    $P2 = new PerlHash
    $P1[name] = $P2
    goto scan_name
name_hash_exists:
    cell = $P2[id]
    $I0 = defined cell
    if $I0 goto eval_cell
scan_name:
    self._scan_node(node,type)
    # Second check to see if _scan_node defined the cell
    cell = $P2[id]
    $I0 = defined cell
    if $I0 goto eval_cell
    # Cell still not defined, grammar is unresolvable.
    print "Cannot find the attribute '"
    print name
    print "' ("
    $S1 = typeof node
    print $S1
    print ") that you asked for.\n"
    .return ()
eval_cell:
    $P3 = self._eval_cell(cell,node)
    .return($P3)
.end

# Evaluate a thunk.
.sub _eval_cell method
    .param pmc cell
    .param pmc node<commit_msg>Marking optional parameters in TGE.<commit_after># Copyright 2005, The Perl Foundation.

=head1 NAME

TGE::Instance - the runtime engine for TGE

=head1 DESCRIPTION

=cut

.namespace [ "TGE::Instance" ]

.sub "__onload" :load
    # define the class
    .local pmc base
    newclass base, "TGE::Instance"
    addattribute base, "cell"  # a hash for storing values of tree nodes
    addattribute base, "visit" # arrays of rules that apply to each node type
    addattribute base, "data"  # the original unmodified tree
    .return ()
.end

=head2 new

Returns a simple initialized TGE::Instance object. Doesn't accept any
constructor parameters.

=cut

.sub __init method
    $P0 = new PerlHash
    $P1 = new PerlHash
    $P2 = new PerlUndef
    setattribute self, "cell", $P0
    setattribute self, "visit", $P1
    setattribute self, "data", $P2
.end

# Call all visitors for a given node
.sub _scan_node method
    .param pmc node
    .param pmc name     :optional
    .param int got_name :opt_flag
    .local string type

    # If the user passed in a special name, look up visit actions for that one,
    # otherwise look them up for the type name of the node.
    if got_name goto name_from_arg
    type = typeof node
    goto name_set
name_from_arg:
    type = name
name_set:

    # Iterate over the elements of the visit hash for the given type
    .local pmc actions
    .local int index
    .local pmc currule
    $P2 = getattribute self, 'visit'
    actions = $P2[type]
    $I2 = defined actions
    unless $I2 goto end_loop
    index = actions
loop:
    dec index
    if index < 0 goto end_loop
    currule = actions[index]
    self._install_action(node, currule)
    goto loop
end_loop:
    .return()
.end

=head2 get

  value = AGI.get('attrname')

Fetches the value of a particular attribute from the root node of the
grammar.

  value = AGI.get('attrname', node)
  value = AGI.get('attrname', node, 'type')

Fetches the value of a particular attribute from the node passed in.
When working with a tree where the nodes don't know their type (PGE
match objects, for example), you must also tell C<get> the type of the
node.

=cut

.sub get method
    .param pmc name
    .param pmc node     :optional
    .param int got_node :opt_flag
    .param pmc type     :optional
    .param int got_type :opt_flag

    # If no node was passed in, use top level node.
    if got_node goto node_exists
    node = getattribute self, 'data'
node_exists:

    .local pmc id
    id = _lookup_id(node)

    .local pmc cell
    $P1 = getattribute self, "cell"
    # First check to see if cell is defined
    $P2 = $P1[name]
    $I1 = defined $P2
    if $I1 goto name_hash_exists
    $P2 = new PerlHash
    $P1[name] = $P2
    goto scan_name
name_hash_exists:
    cell = $P2[id]
    $I0 = defined cell
    if $I0 goto eval_cell
scan_name:
    if got_type goto scan_with_type
    self._scan_node(node)
    goto done_scan
scan_with_type:
    self._scan_node(node,type)
done_scan:
    # Second check to see if _scan_node defined the cell
    cell = $P2[id]
    $I0 = defined cell
    if $I0 goto eval_cell
    # Cell still not defined, grammar is unresolvable.
    print "Cannot find the attribute '"
    print name
    print "' ("
    $S1 = typeof node
    print $S1
    print ") that you asked for.\n"
    .return ()
eval_cell:
    $P3 = self._eval_cell(cell,node)
    .return($P3)
.end

# Evaluate a thunk.
.sub _eval_cell method
    .param pmc cell
    .param pmc node
{'fraction_non_alphanumeric': 0.07884874831359616, 'fraction_numerical': 0.011692399940038975, 'mean_word_length': 2.7887563884156727, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4415136', 'n_tokens_mistral': 2231, 'n_tokens_neox': 2182, 'n_words': 869}