| 1 | package Language::Zcode::Parser::Routine; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | =head1 NAME | ||||||||||
| 7 | |||||||||||
| 8 | Language::Zcode::Parser::Routine - A single Z-code subroutine | ||||||||||
| 9 | |||||||||||
| 10 | =head2 SYNOPSIS | ||||||||||
| 11 | |||||||||||
| 12 | # New routine at address $start, ends at $end | ||||||||||
| 13 | my $routine = new Language::Zcode::Parser::Routine $address; | ||||||||||
| 14 | $routine->end($end); | ||||||||||
| 15 | |||||||||||
| 16 | # Now actually parse it | ||||||||||
| 17 | $routine => parse(); | ||||||||||
| 18 | |||||||||||
| 19 | # ... and look at the parsed commands (which are simple hashes, not objects) | ||||||||||
| 20 | my @commands = $routine->commands(); | ||||||||||
| 21 | print map {$_->{opcode_address}, " ", $_->{opcode}, "\n"} @commands; | ||||||||||
| 22 | |||||||||||
| 23 | =head1 DESCRIPTION | ||||||||||
| 24 | |||||||||||
| 25 | A set of Z-code commands at a given address. | ||||||||||
| 26 | |||||||||||
| 27 | =cut | ||||||||||
| 28 | |||||||||||
| 29 | =head2 new (address) | ||||||||||
| 30 | |||||||||||
| 31 | Create a new subroutine at given address. The Z-code will not be parsed until | ||||||||||
| 32 | a parse() command is explicitly given. | ||||||||||
| 33 | |||||||||||
| 34 | =cut | ||||||||||
| 35 | |||||||||||
| 36 | 67 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 37 | 67 | my ($class, $address, %arg) = @_; | |||||||||
| 38 | 67 | my $self = { | |||||||||
| 39 | locals => [], # default values for local variables | ||||||||||
| 40 | commands => [], # parsed Z-code commands in this sub | ||||||||||
| 41 | txd_commands => [], # commands in this sub read by txd (for debugging) | ||||||||||
| 42 | %arg, | ||||||||||
| 43 | }; | ||||||||||
| 44 | 67 | bless $self, $class; | |||||||||
| 45 | 67 | $self->address($address); | |||||||||
| 46 | # print "New $address: ",%$self,"\n"; | ||||||||||
| 47 | 67 | return $self; | |||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | =head2 address (val) | ||||||||||
| 51 | |||||||||||
| 52 | get/set start address of the subroutine | ||||||||||
| 53 | |||||||||||
| 54 | =cut | ||||||||||
| 55 | |||||||||||
| 56 | 708 | ▶ | sub address { | ||||||||
|
Subroutine
address: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 57 | 708 | my ($self, $val) = @_; | |||||||||
| 58 | 708 | ▶ | $self->{address} = $val if defined $val; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | 708 | return $self->{address}; | |||||||||
| 60 | } | ||||||||||
| 61 | |||||||||||
| 62 | =head2 end (val) | ||||||||||
| 63 | |||||||||||
| 64 | get/set end address (including padding zeroes!) of the subroutine | ||||||||||
| 65 | |||||||||||
| 66 | =cut | ||||||||||
| 67 | |||||||||||
| 68 | 67 | ▶ | sub end { | ||||||||
|
Subroutine
end: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||
| 69 | 67 | my ($self, $val) = @_; | |||||||||
| 70 | 67 | ▶ | $self->{end} = $val if defined $val; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | 67 | return $self->{end}; | |||||||||
| 72 | } | ||||||||||
| 73 | |||||||||||
| 74 | =head2 last_command_address (val) | ||||||||||
| 75 | |||||||||||
| 76 | get/set address of last command in the subroutine (needed because "end" | ||||||||||
| 77 | may include padding zeroes) | ||||||||||
| 78 | |||||||||||
| 79 | =cut | ||||||||||
| 80 | |||||||||||
| 81 | 133 | ▶ | sub last_command_address { | ||||||||
|
Subroutine
last_command_address: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 82 | 133 | my ($self, $val) = @_; | |||||||||
| 83 | 133 | ▶ | $self->{last_command_address} = $val if defined $val; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | 133 | return $self->{last_command_address}; | |||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | =head2 locals (list of values) | ||||||||||
| 88 | |||||||||||
| 89 | get/set default values of this sub's local variables (returns list, not ref) | ||||||||||
| 90 | |||||||||||
| 91 | =cut | ||||||||||
| 92 | |||||||||||
| 93 | 134 | ▶ | sub locals { | ||||||||
|
Subroutine
locals: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 94 | 134 | my $self = shift; | |||||||||
| 95 | 134 | ▶ | $self->{locals} = [@_] if @_; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 96 | 134 | return @{ $self->{locals} } | |||||||||
| 97 | } | ||||||||||
| 98 | |||||||||||
| 99 | =head2 commands (list of values) | ||||||||||
| 100 | |||||||||||
| 101 | get/set parsed Z-code commands in this sub (returns list, not ref) | ||||||||||
| 102 | |||||||||||
| 103 | =cut | ||||||||||
| 104 | |||||||||||
| 105 | 132 | ▶ | sub commands { | ||||||||
|
Subroutine
commands: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 106 | 132 | my $self = shift; | |||||||||
| 107 | 132 | ▶ | $self->{commands} = [@_] if @_; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 108 | 132 | return @{ $self->{commands} } | |||||||||
| 109 | } | ||||||||||
| 110 | |||||||||||
| 111 | =head2 txd_commands (list of values) | ||||||||||
| 112 | |||||||||||
| 113 | get/set commands in this sub as returned by the txd Z-code parsing program, | ||||||||||
| 114 | to compare with my Pure Perl results. (returns list, not ref) | ||||||||||
| 115 | |||||||||||
| 116 | =cut | ||||||||||
| 117 | |||||||||||
| 118 | 0 | ▶ | sub txd_commands { | ||||||||
|
Subroutine
txd_commands: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 119 | 0 | my $self = shift; | |||||||||
| 120 | 0 | ▶ | $self->{txd_commands} = [@_] if @_; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 121 | 0 | return @{ $self->{txd_commands} } | |||||||||
| 122 | } | ||||||||||
| 123 | |||||||||||
| 124 | =head2 parse() | ||||||||||
| 125 | |||||||||||
| 126 | Parse (and store) the commands in this sub | ||||||||||
| 127 | |||||||||||
| 128 | =cut | ||||||||||
| 129 | |||||||||||
| 130 | 66 | ▶ | sub parse { | ||||||||
|
Subroutine
parse: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 131 | 66 | my $self = shift; | |||||||||
| 132 | 66 | my ($addr, $stop) = ($self->address, $self->last_command_address); | |||||||||
| 133 | # Side effect: moves PC to first command in the sub | ||||||||||
| 134 | 66 | $self->locals(&Language::Zcode::Parser::Opcode::parse_sub_header($addr)); | |||||||||
| 135 | 66 | my @commands; | |||||||||
| 136 | 66 | ▶ | push @commands, { &Language::Zcode::Parser::Opcode::parse_command() } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 137 | until $Language::Zcode::Parser::Opcode::PC > $stop; | ||||||||||
| 138 | 66 | $self->commands(@commands); | |||||||||
| 139 | 66 | return; | |||||||||
| 140 | } | ||||||||||
| 141 | |||||||||||
| 142 | 2 | 1; | |||||||||