tagged release 0.6.4
[parrot.git] / runtime / parrot / library / yaml_dumper.pir
blob2d5dfcc9257aafd966519089a7fd20d09b03725d
1 # Copyright 2008, The Perl Foundation.
2 # $Id$
4 =head1 TITLE
6 yaml_dumper.pir - PIR version of a YAML dumper, ala Data::Dumper
8 =head1 VERSION
10 version 0.1
12 =head1 SYNOPSIS
14     ...
15     # dump the P0 register
16     yaml( P0 )
18     # dump the P0 register, with "name"
19     yaml( P0, "name" )
20     ...
22     END
23     .include "library/yaml_dumper.pir"
26 =head1 DESCRIPTION
28     PIR implementation of Perl 5's Data::Dumper module to dump YAML format.
30 =cut
32 # first method prints usage information
33 .sub __library_dumper_onload
34     print "usage:"
35     print "\tload_bytecode \"library/YAML/Dumper.pir\"\n"
36     print "\t...\n"
37     print "\tnew yaml, \"YAML::Dumper\"\n"
38     print "\tyaml.\"yaml\"( foo, \"foo\" )\n\n"
39     end
40 .end
42 .include "errors.pasm"
44 =head1 FUNCTIONS
46 This library provides the following functions:
48 =over 4
50 =item yaml( pmc, ?name, ?indent] )
52 This is the public (non object) interface to the yaml dumper library.
54 =over 4
56 =item pmc
58 Required. The PMC to dump.
60 =item name
62 Optional. The name of the PMC.
64 =item indent
66 Optional. The indent used at the start of each line printed.
68 =back
70 B<Note:> This function currently returns nothing. It should return
71 the dumped data as a string, like Perl's Data::Dumper. Instead,
72 everything is printed out using C<print>.
74 B<Note: #2> Hash keys are now sorted using C<_sort()> (library/sort.pir)
76 =cut
78 .sub yaml
79     .param pmc p
80     .param string name    :optional
81     .param int has_name   :opt_flag
82     .param string ident   :optional
83     .param int has_ident  :opt_flag
85     $P2 = _global_dumper()
86     if has_ident goto w_ident
87     unless has_name goto wo_name        # XXX argument order, opt 1st
88     $P2."yaml"(p, name)
89     goto ex
90 wo_name:
91     $P2."yaml"(p)
92     goto ex
93 w_ident:
94     $P2."yaml"(p, name, ident)
95 ex:
96 .end
98 =item _register_dumper( id, sub )
100 Registers a dumper for new PMC type. B<UNIMPLEMENTED>
101 But see B<method __dump> below.
103 =over 4
105 =item id
107 the PMC id, as returned by the C<typeof> op.
109 =item sub
111 a Sub pmc, that gets called in order to dump the content of the given PMC
113 =back
115 For example:
117     sub = find_name "_dump_PerlArray"
118     _register_dumper( .PerlArray, sub )
120 This function returns nothing.
122 =cut
124 .sub _register_dumper
125     .param int id
126     .param pmc s
127     $P2 = _global_dumper()
128     $P2."registerDumper"(id, s)
129 .end
131 =item __dump(pmc yaml, str label) method
133 If a method C<__dump> exists in the namespace of the class, it will be
134 called with the current dumper object and the label of the PMC.
136 =item yaml =_global_dumper() B<(internal)>
138 Internal helper function.
140 Returns the global dumper instance used by the non object interface.
142 =cut
144 .sub _global_dumper
145     .local pmc self
146     .local pmc yd_class
147     .local int is_defined
149     get_class yd_class, "YAML::Dumper"
150     if null yd_class goto load_yd_pir
151     goto TYPE_OK
153   load_yd_pir:
154     load_bytecode "library/YAML/Dumper.pir"
155     get_class yd_class, "YAML::Dumper"
156     if null yd_class goto no_class
157     goto TYPE_OK
159   no_class:
160     print "fatal error: failure while loading library/YAML/Dumper.pir\n"
161     end
162 TYPE_OK:
164     errorsoff .PARROT_ERRORS_GLOBALS_FLAG
165     find_global self, "YAML::Dumper", "global"
166     errorson .PARROT_ERRORS_GLOBALS_FLAG
167     if null self goto create_type
169 create_type:
170     new self, "YAML::Dumper"
171     store_global "YAML::Dumper", "global", self
173 END:
174     .return( self )
175 .end
177 =back
179 =head1 AUTHOR
181 Jens Rieks E<lt>parrot at jensbeimsurfen dot deE<gt> is the author
182 and maintainer.
183 Please send patches and suggestions to the Perl 6 Internals mailing list.
185 =head1 COPYRIGHT
187 Copyright (C) 2004-2008, The Perl Foundation.
189 =cut
191 # Local Variables:
192 #   mode: pir
193 #   fill-column: 100
194 # End:
195 # vim: expandtab shiftwidth=4 ft=pir: