lib/Language/Zcode/Translator/XML.pm untested

filter: Statementstmt 0.0% 0 / 50 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 79 CC 5 SCAR 17.1
CC
5
Cov
0%
translate_command
12.0
new
6.0
write_memory
6.0
CRAP 5.5
1 package Language::Zcode::Translator::XML;
2
3 =head1 NAME
4
5 Language::Zcode::Translator::XML - Translate Z-code into XML
6
7 =head1 DESCRIPTION
8
9 This is an extremely simple proof of concept. It just parses the Z-file
10 and outputs it as an XML file. With some really fancy css work, I suspect you
11 could do some neat stuff, though.
12
13 =cut
14
15 @Language::Zcode::Translator::XML::ISA = qw(Language::Zcode::Translator::Generic);
16 my $indent = ""; # indent subs for readability
17 # store commands until done with a routine
18 my ($save_name, $save_locals, @command_xml) = ("",[]);
19
20 sub new {
21 my ($class, @arg) = @_;
22 eval "use XML::Simple";
23 die "Can't output XML without XML::Simple module\n$@\n" if $@;
24 bless {}, $class;
25 }
26
27 sub program_start {
28 # Header for CSS: <?xml-stylesheet href="b2.css" type="text/css"?>
29 my $top = <<'ENDTOP';
30 <?xml version="1.0" standalone="yes"?>
31 <?xml-stylesheet type="text/xsl" href="zcode.xsl"?>
32 <zfile>
33
34 ENDTOP
35 my @xml_const = map {
36 { constant_key => $_, value => $Language::Zcode::Util::Constants{$_} }
37 } keys %Language::Zcode::Util::Constants;
38 my $xcref = {constants => [\@xml_const]};
39 #$top .= XMLout(@xml_const, rootname => "constants", noattr => 1);
40 $top .= XMLout($xcref, noattr => 1, keeproot => 1);
41 return $top . "\n";
42 }
43
44 sub program_end { "</zfile>\n" }
45
46 sub routine_start {
47 my ($self, $addr, @locals) = @_;
48 @command_xml = (); # (re)start collecting commands
49 ($save_addr, $save_locals) = ($addr, \@locals); # save for routine_end
50 return "";
51 }
52
53 sub routine_end {
54 my $self = shift;
55 my $save_xml = XMLout( {
56 addr => $save_addr, # addr of previous sub
57 name => "rtn$save_addr", # name of previous sub
58 locv => $save_locals, # locals of previous sub
59 command => \@command_xml, # stored commands of previous sub
60 },
61 noattr => 1, rootname => "subroutine");
62 return $save_xml . "\n";
63 }
64
65 # Translate Z op and args into XML
66 sub translate_command {
67 my ($self, $href) = @_;
68 my %parsed = %$href;
69 my $opcode = $parsed{opcode} or return; # totally unknown opcode?
70 my $command = "OOPS. No Command Here\n"; # command to return
71
72 # Leave ^'s in print strings, since XML will ignore literal \n's
73 # if (exists $parsed{print_string}) { $parsed{print_string} =~ s/\^/\n/g }
74
75 # pack addresses
76 foreach my $key (qw(packed_address_of_string routine)) {
77 if (exists $parsed{$key}) {
78 $parsed{$key} = $self->packed_address_str($parsed{$key}, $key);
79 }
80 }
81
82 # Turn variable number of args (if any) into a Perl list
83 # Btw, call_1n takes no args, so arg_list will be "" for call_1n, too
84 $parsed{args} = exists $parsed{args}
85 #? join(", ", map {$self->make_var($_)} @{$parsed{"args"}})
86 ? join(", ", @{$parsed{"args"}})
87 : "";
88
89 push @command_xml, \%parsed;
90 return "";
91 }
92
93 sub make_var {
94 my ($self, $a, $is_lval) = @_;
95 # XXX Help!
96 return $a;
97 }
98
99 sub newlineify {
100 my $s = pop;
101 $s =~ s/\n/\\n/g;
102 return $s;
103 }
104
105 sub write_memory {
106 # XXX should really move hexification to Translator::Generic
107 # change each byte to two hex digits
108 my $l = @Language::Zcode::Util::Memory;
109 my $hexed = "";
110 for (my $c = 0; $c < $l; $c+=16) {
111 # Add hex "line number" & \n's.
112 my $len = $l - $c;
113 $len = 16 if $len > 16;
114 $hexed .= sprintf("%06x " . " %02x" x $len . "\n", $c,
115 @Language::Zcode::Util::Memory[$c .. $c + $len -1]);
116 }
117 my $str = <<"ENDUNPACK";
118
119 <memory>
120 <!--
121 # Addr 0 1 2 3 4 5 6 7 8 9 a b c d e f -->
122 $hexed
123 </memory>
124 ENDUNPACK
125 return $str;
126 }
127
128 1;