| 1 | package Parse::Eyapp::Scope; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use Carp; | ||
| 5 | use Parse::Eyapp::Base qw(part valid_keys invalid_keys); | ||
| 6 | |||
| 7 | my %_new_scope = ( | ||
| 8 | SCOPE_NAME => 'STRING', | ||
| 9 | ENTRY_NAME => 'STRING', | ||
| 10 | SCOPE_DEPTH => 'STRING', | ||
| 11 | ); | ||
| 12 | my $valid_scope_keys = valid_keys(%_new_scope); | ||
| 13 | |||
| 14 | sub new { | ||
| 15 | my $class = shift; | ||
| 16 | my %args = @_; | ||
| 17 | |||
| 18 | if (defined($a = invalid_keys(\%_new_scope, \%args))) { | ||
| 19 | croak("Parse::Eyapp::Scope::new Error!:\n" | ||
| 20 | ."unknown argument $a. Valid arguments for new are:\n $valid_scope_keys") | ||
| 21 | } | ||
| 22 | $args{ENTRY_NAME} = 'entry' unless defined($args{ENTRY_NAME}); | ||
| 23 | $args{SCOPE_NAME} = 'scope' unless defined($args{SCOPE_NAME}); | ||
| 24 | $args{SCOPE_DEPTH} = '' unless defined($args{SCOPE_DEPTH}); | ||
| 25 | $args{PENDING_DECL} = []; | ||
| 26 | $args{SCOPE_MARK} = 0; | ||
| 27 | $args{DEPTH} = -1; # first depth is 0 | ||
| 28 | |||
| 29 | bless \%args, $class; | ||
| 30 | } | ||
| 31 | |||
| 32 | sub begin_scope { | ||
| 33 | my $self = shift; | ||
| 34 | |||
| 35 | # Set the mark for next scope to the level of the stack of instances | ||
| 36 | $self->{SCOPE_MARK} = @{$self->{PENDING_DECL}}; | ||
| 37 | |||
| 38 | # Save current mark in the stack of marks: it is an index | ||
| 39 | push @{$self->{SCOPE_STACK}}, $self->{SCOPE_MARK}; | ||
| 40 | $self->{DEPTH}++; # new scope, new depth | ||
| 41 | } | ||
| 42 | |||
| 43 | #################################################################### | ||
| 44 | # Usage : ($nondec, $declared) = $ids->end_scope($program->{symboltable}, $program, 'type'); | ||
| 45 | # ($nondec, $declared) = $ids->end_scope($while); | ||
| 46 | # ($nondec, $declared) = $ids->end_scope($symbol_table, 'label'); | ||
| 47 | # | ||
| 48 | sub end_scope { | ||
| 49 | my $self = shift; # The scope object | ||
| 50 | |||
| 51 | my $st; # reference to the hash holding the symbol table for this scope | ||
| 52 | my $block; # The node owning the current scope | ||
| 53 | # first arg can be the "block node" in which case the s.t. is omitted | ||
| 54 | if (UNIVERSAL::isa($_[0], 'Parse::Eyapp::Node')) { | ||
| 55 | # The call has the form: ($nondec, $declared) = $ids->end_scope($while); | ||
| 56 | $block = shift; | ||
| 57 | } | ||
| 58 | elsif (UNIVERSAL::isa($_[0], 'HASH')) { | ||
| 59 | # first arg can be the s.t. in which case the block node is expected | ||
| 60 | $st = shift; | ||
| 61 | if (UNIVERSAL::isa($_[0], 'Parse::Eyapp::Node')) { | ||
| 62 | $block = shift; | ||
| 63 | } | ||
| 64 | } | ||
| 65 | else { | ||
| 66 | croak "end_scope error: Specify a symbol table or a scope node\n" | ||
| 67 | } | ||
| 68 | |||
| 69 | # @_ = Remaining args hold key names for the entry that will be added to the instances | ||
| 70 | |||
| 71 | # Get the index pointing to the beginning of the current scope | ||
| 72 | my $scope = pop @{$self->{SCOPE_STACK}}; | ||
| 73 | croak "Error: end_scope called without matching begin_scope\n" unless defined($scope); | ||
| 74 | |||
| 75 | # Get tyhe instances ocurring in this scope | ||
| 76 | my @instances = splice @{$self->{PENDING_DECL}}, $scope; | ||
| 77 | |||
| 78 | if (defined($st)) { | ||
| 79 | |||
| 80 | # Partitions @instance based on the return value of BLOCK | ||
| 81 | my ($nodeclared, $declared) = part { exists $st->{$_->key} } @instances; | ||
| 82 | |||
| 83 | $declared = [] unless $declared; | ||
| 84 | $nodeclared = [] unless $nodeclared; | ||
| 85 | |||
| 86 | # Return non declared identifiers to the "pending of declarations" queue | ||
| 87 | push @{$self->{PENDING_DECL}}, @$nodeclared; | ||
| 88 | |||
| 89 | # Set the scope attribute for those instances that were declared | ||
| 90 | for my $i (@$declared) { | ||
| 91 | next unless UNIVERSAL::isa($i, 'HASH'); | ||
| 92 | $i->{$self->{SCOPE_NAME}} = $block if defined($block); | ||
| 93 | if (UNIVERSAL::can($i, 'key')) { | ||
| 94 | my $key = $i->key; | ||
| 95 | $i->{$self->{ENTRY_NAME}} = $st->{$key}; | ||
| 96 | for (@_) { | ||
| 97 | # $_ must be a string and a key of %$st | ||
| 98 | if (ref($_)) { | ||
| 99 | warn "end_scope warning! expecting a string key for symbol table entry $key not a reference\n"; | ||
| 100 | } | ||
| 101 | next unless exists $st->{$key}{$_}; | ||
| 102 | $i->{$_} = $st->{$key}{$_}; | ||
| 103 | } | ||
| 104 | } | ||
| 105 | } | ||
| 106 | |||
| 107 | $block->{$self->{SCOPE_DEPTH}} = $self->{DEPTH} if $self->{SCOPE_DEPTH}; | ||
| 108 | $self->{DEPTH}--; | ||
| 109 | |||
| 110 | return wantarray? ($nodeclared, $declared): $nodeclared; | ||
| 111 | } | ||
| 112 | |||
| 113 | # Not symbol table: Simple scope | ||
| 114 | |||
| 115 | # Set the scope attribute for those instances that were declared | ||
| 116 | my @r; | ||
| 117 | $block->{$self->{SCOPE_NAME}} = \@r; | ||
| 118 | for my $i (@instances) { | ||
| 119 | $i->{$self->{SCOPE_NAME}} = $block; | ||
| 120 | push @r, $i; | ||
| 121 | } | ||
| 122 | |||
| 123 | $block->{$self->{SCOPE_DEPTH}} = $self->{DEPTH} if $self->{SCOPE_DEPTH}; | ||
| 124 | $self->{DEPTH}--; | ||
| 125 | |||
| 126 | return \@instances; | ||
| 127 | } | ||
| 128 | |||
| 129 | # To be called for each ocurrence of an identifier | ||
| 130 | sub scope_instance { | ||
| 131 | my $self = shift; | ||
| 132 | |||
| 133 | my $NODE = shift; | ||
| 134 | |||
| 135 | push @{$self->{PENDING_DECL}}, $NODE; | ||
| 136 | } | ||
| 137 | |||
| 138 | 1; | ||
| 139 | |||
| 140 | __END__ |