Skip to content
This repository has been archived by the owner on Jun 9, 2018. It is now read-only.

Commit

Permalink
have a nice OO interface
Browse files Browse the repository at this point in the history
(global variables are hidden)
  • Loading branch information
fperrad committed May 3, 2009
1 parent beba80a commit edf93a5
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 16 deletions.
2 changes: 1 addition & 1 deletion handler/xmlwriter/builtins/print.pir
Expand Up @@ -5,7 +5,7 @@
.sub 'print'
.param pmc args :slurpy
.local pmc stream
stream = get_global 'Stream'
stream = get_hll_global [ 'Xml';'Handler';'XmlWriter' ], 'Stream'
.local pmc iter
iter = new 'Iterator', args
iter_loop:
Expand Down
23 changes: 22 additions & 1 deletion handler/xmlwriter/xmlwriter.pir
@@ -1,6 +1,27 @@
# Copyright (C) 2009, Parrot Foundation.

.namespace [ 'Xml::Handler::XmlWriter' ]
.namespace [ 'Xml';'Handler';'XmlWriter' ]

.sub 'onload' :anon :load :init
load_bytecode 'PCT.pbc'

new $P0, 'P6metaclass'
$P0.'new_class'('Xml::Handler::XmlWriter', 'attr'=>'stream')
.end
.sub 'stream' :method
.param pmc value :optional
.param int has_value :opt_flag
if has_value goto set_value
value = get_hll_global [ 'Xml';'Handler';'XmlWriter' ], 'Stream'
unless null value goto end
value = new 'Undef'
goto end
set_value:
set_hll_global [ 'Xml';'Handler';'XmlWriter' ], 'Stream', value
end:
.return (value)
.end
.include 'handler/xmlwriter/gen_builtins.pir'
.include 'handler/xmlwriter/gen_actions.pir'
Expand Down
2 changes: 1 addition & 1 deletion sax/xml/builtins/fire.pir
Expand Up @@ -5,7 +5,7 @@
.sub 'fire'
.param string event
.param pmc args :slurpy :named
$P0 = get_global 'Handler'
$P0 = get_hll_global [ 'Xml';'Sax';'Xml';'Compiler' ], 'Handler'
if null $P0 goto L1
$P1 = find_method $P0, event
.tailcall $P1($P0, args :named :flat)
Expand Down
64 changes: 63 additions & 1 deletion sax/xml/xml.pir
Expand Up @@ -23,11 +23,14 @@ object.

=cut

.namespace [ 'Xml::Sax::Xml::Compiler' ]
.namespace [ 'Xml';'Sax';'Xml';'Compiler' ]

.sub 'onload' :anon :load :init
load_bytecode 'PCT.pbc'

new $P0, 'P6metaclass'
$P0.'new_class'('Xml::Sax::Xml::Compiler', 'attr'=>'comp')
$P0 = get_hll_global ['PCT'], 'HLLCompiler'
$P1 = $P0.'new'()
$P1.'language'('Xml')
Expand All @@ -38,6 +41,65 @@ object.
$P1.'removestage'('post')
.end
.sub 'init' :method :vtable
$P0 = compreg 'Xml'
self.'comp'($P0)
.end
.sub 'attr' :method
.param string attrname
.param pmc value
.param int has_value
if has_value goto set_value
value = getattribute self, attrname
unless null value goto end
value = new 'Undef'
goto end
set_value:
setattribute self, attrname, value
end:
.return (value)
.end
.sub 'comp' :method
.param pmc value :optional
.param int has_value :opt_flag
.tailcall self.'attr'('comp', value, has_value)
.end
.sub 'handler' :method
.param pmc value :optional
.param int has_value :opt_flag
if has_value goto set_value
value = get_hll_global [ 'Xml';'Sax';'Xml';'Compiler' ], 'Handler'
unless null value goto end
value = new 'Undef'
goto end
set_value:
set_hll_global [ 'Xml';'Sax';'Xml';'Compiler' ], 'Handler', value
end:
.return (value)
.end
.sub 'parse' :method
.param pmc source
$P0 = self.'comp'()
.tailcall $P0.'parse'(source)
.end
.sub 'parse_string' :method
.param pmc source
.tailcall self.'parse'(source)
.end
.sub 'parse_file' :method
.param pmc stream
$P0 = new 'FileHandle'
$S0 = $P0.'readall'(stream)
$P0.'close'()
.tailcall self.'parse'($S0)
.end
=item main(args :slurpy) :main
Start compilation by passing any command line C<args>
Expand Down
19 changes: 7 additions & 12 deletions xml.pir
Expand Up @@ -35,23 +35,19 @@ a simple handler
.local string filename
$P0 = shift args # progname
filename = shift args
.local string source
$P0 = new 'FileHandle'
source = $P0.'readall'(filename)
$P0.'close'()

.local pmc handler
handler = new ['Xml';'Handler';'XmlWriter']
set_global 'Handler', handler

.local pmc stream
stream = new 'StringHandle'
stream.'open'( 'xml', 'wr' )
set_global 'Stream', stream

.local pmc handler
handler = new ['Xml';'Handler';'XmlWriter']
handler.'stream'(stream)

.local pmc driver
driver = compreg 'Xml'
driver.'parse'(source)
driver = new [ 'Xml';'Sax';'Xml';'Compiler' ]
driver.'handler'(handler)
driver.'parse_file'(filename)

$S0 = stream.'readall'()
stream.'close'()
Expand All @@ -63,4 +59,3 @@ a simple handler
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4 ft=pir:

0 comments on commit edf93a5

Please sign in to comment.