Permalink
Browse files

[Perl6]:

* Moved p6shell features directly into perl6.pbc (lib/main.pir).
* perl6.pbc can be invoked directly from parrot command line (like APL).
* Changed infix:<,> to be list associative.
* Updated STATUS and README files.


git-svn-id: http://svn.perl.org/parrot/trunk/languages/perl6@12194 d31e2699-5ff4-0310-a27c-f18f2fbe73fe
  • Loading branch information...
1 parent 419e8a8 commit c13e9609107977515723fe13fb0a9c273ab9780b @pmichaud pmichaud committed Apr 12, 2006
Showing with 223 additions and 103 deletions.
  1. +18 −10 README
  2. +1 −0 STATUS
  3. +123 −0 lib/main.pir
  4. +2 −2 lib/parse.pir
  5. +0 −87 p6shell.pir
  6. +79 −4 perl6.pir
View
28 README
@@ -24,18 +24,22 @@ file, simply issue the command
$ make
-This directory comes with F<p6shell.pir>, which is a simple parrot
-script designed to exercise the parser, both on static input files
-and interactively via command line input. To run the parser
-on a perl 6 input file named "foo.p6" and display the resulting
-parse tree:
+To invoke the parser from a shell prompt on a (Perl 6)
+input file named "foo.p6", use:
- $ parrot p6shell.pir foo.p6
+ $ parrot perl6.pbc foo.p6
-To run the parser interactively, entering single-line statements
+To run interactively, entering single-line statements
and displaying the parse tree for each:
- $ parrot p6shell.pir
+ $ parrot perl6.pbc
+
+At the moment perl6.pbc only displays the parse tree.
+
+To get a dump of the parser's operator precedence table,
+use --dump-optable:
+
+ $ parrot perl6.pbc --dump-optable
=head2 Files
@@ -59,13 +63,17 @@ parsing Perl 6 that are better written directly in PIR instead
of using the top-down rules syntax or bottom-up operator
precedence parser.
-The PIR files in F<lib/> are then included as part of compiling
+The F<lib/main.pir> file controls what happens when the perl6.pbc
+file is invoked directly from parrot (as opposed to being
+loaded via the load_bytecode op).
+
+The PIR files in F<lib/> are included as part of compiling
F<perl6.pir> to produce F<perl6.pbc>. One can then parse Perl 6
source by doing:
$P0 = compreg "Perl6"
$S0 = "...perl 6 source code..."
- $P1 = $P0($S0)
+ $P1 = $P0($S0, target=>'parse')
# $P1 holds the parse tree of the Perl 6 source code
View
1 STATUS
@@ -14,6 +14,7 @@ Parser implemented
* Basic blocks
* Perl 6 comments (excluding POD)
* Listop parsing
+* List associative parsing
Parser to-do
View
123 lib/main.pir
@@ -0,0 +1,123 @@
+=head1 NAME
+
+main.pir -- code for running Perl 6 from command line
+
+=head1 DESCRIPTION
+
+See Perl6.pir for a synopsis.
+
+=head1 Functions
+
+=item C<main(PMC args)>
+
+Handles program control when the Perl6.pbc file is executed
+directly from the Parrot command line. It calls all of
+the submodule :load routines, then processes commands from
+the file specified on the command line or standard input.
+
+The --target= command line option allows the compilation
+to display a parse tree or abstract syntax tree in lieu of
+executing program statements.
+
+=cut
+
+.namespace [ 'Perl6' ]
+
+.sub 'main' :main
+ .param pmc args
+ $P0 = find_global 'Perl6', '__onload'
+ $P0()
+ $P0 = find_global 'Perl6::Grammar', '__onload_parse'
+ $P0()
+
+ load_bytecode 'dumper.pbc'
+ load_bytecode 'PGE/Dumper.pbc'
+ load_bytecode 'Getopt/Obj.pbc'
+
+ .local pmc getopts, opts
+ .local string arg0
+ arg0 = shift args
+ getopts = new 'Getopt::Obj'
+ getopts.'notOptStop'(1)
+ push getopts, 'target=s'
+ push getopts, 'dump-optable'
+ push getopts, 'help|h'
+ opts = getopts.'get_options'(args)
+
+ $S0 = opts['dump-optable']
+ if $S0 goto dump_optable
+ $S0 = opts['help']
+ if $S0 goto usage
+
+ .local string target
+ target = opts['target']
+
+ .local pmc perl6
+ perl6 = compreg 'Perl6'
+
+ $I0 = elements args
+ if $I0 > 0 goto file_arg
+
+ .local pmc stdin
+ stdin = getstdin
+ push stdin, 'utf8'
+
+ stmt_loop:
+ .local string stmt
+ stmt = readline stdin
+ unless stmt goto end
+ bsr perl6_eval
+ goto stmt_loop
+
+ file_arg:
+ .local string filename
+ filename = args[1]
+ $P0 = open filename, '<'
+ unless $P0 goto err_no_file
+ push $P0, 'utf8'
+ stmt = read $P0, 65535
+ close $P0
+ bsr perl6_eval
+ goto end
+
+ perl6_eval:
+ $P0 = perl6(stmt, 'target' => target)
+ if target == 'PIR' goto dump_pir
+ if target goto dump_object
+ goto dump_object
+ $P0()
+ ret
+ dump_pir:
+ print $P0
+ ret
+ dump_object:
+ '_dumper'($P0, target)
+ ret
+
+ err_no_file:
+ print 'Cannot open file '
+ print filename
+ print "\n"
+ end
+
+ dump_optable:
+ $P0 = find_global "Perl6::Grammar", "$optable"
+ "_dumper"($P0, "$Perl6::Grammar::optable")
+ goto end
+
+ usage:
+ print "usage: perl6.pbc [--dump-optable] [--target=OUT] [file]\n"
+
+ end:
+.end
+
+=back
+
+=head1 LICENSE
+
+Copyright (c) 2006 The Perl Foundation
+
+This is free software; you may redistribute it and/or modify
+it under the same terms as Parrot.
+
+=cut
View
4 lib/parse.pir
@@ -18,14 +18,14 @@ appear here as the parser grows.
.include "cclass.pasm"
-=item C<__onload()>
+=item C<__onload_parse()>
This subroutine creates an operator precedence parser
and initializes it with the operators defined by Perl 6.
=cut
-.sub "__onload" :load
+.sub "__onload_parse" :load
.local pmc optable
$I0 = find_type "PGE::OPTable"
optable = new $I0
View
87 p6shell.pir
@@ -1,87 +0,0 @@
-=head1 TITLE
-
-p6shell.pir - Program to exercise the Perl 6 parser
-
-=head2 SYNOPSIS
-
- parrot p6shell.pir [file]
-
-=head2 Description
-
-This is a simple program that exercises the Perl 6 parser
-in perl6.pbc. If given a file argument on the command line,
-it will run the Perl 6 parser on the file and dump the
-resulting parse tree.
-
-If no file is provided, then p6shell enters an interactive
-mode, reading Perl 6 statements from the command line and
-dumping the resulting parse trees to the standard output.
-
-=cut
-
-.sub main :main
- .param pmc args
- .local pmc p6compile
- .local string code
-
- load_bytecode "PGE.pbc"
- load_bytecode "perl6.pbc"
- load_bytecode "dumper.pir"
- load_bytecode "PGE/Dumper.pir"
- load_bytecode "Getopt/Obj.pir"
-
- p6compile = compreg "Perl6"
-
- .local pmc getopts, opts
- .local string arg0
- arg0 = shift args
- getopts = new "Getopt::Obj"
- getopts."notOptStop"(1)
- push getopts, "dump-optable"
- push getopts, "help|h"
- opts = getopts."get_options"(args)
-
- $S0 = opts["dump-optable"]
- if $S0 goto dump_optable
- $S0 = opts["help"]
- if $S0 goto usage
-
- $I0 = elements args
- if $I0 > 1 goto usage
- if $I0 < 1 goto read_stdin
-
- .local string filename
- .local pmc filehandle
- filename = args[1]
- filehandle = open filename, "<"
- code = read filehandle, 65535
- close filehandle
-
- $P0 = p6compile(code)
- unless $P0 goto end
- "_dumper"($P0)
- goto end
-
- read_stdin:
- .local pmc stdin
- stdin = getstdin
-
- stdin_loop:
- code = readline stdin
- if code == "" goto end
- $P0 = p6compile(code)
- unless $P0 goto end
- "_dumper"($P0)
- goto stdin_loop
-
- dump_optable:
- print "$Perl6::Grammar::optable :\n"
- $P0 = find_global "Perl6::Grammar", "$optable"
- "_dumper"($P0)
- goto end
-
- usage:
- print "usage: p6shell.pir [--dump-optable] [file]\n"
-
- end:
-.end
View
83 perl6.pir
@@ -16,16 +16,20 @@ Just to make sure we aren't fooling anyone -- at the
moment the "Perl6" compiler here only parses Perl 6 code.
But that should change soon.
-=over 4
-
=cut
.include "lib/parse.pir"
+
+.namespace [ "Perl6::Grammar" ]
.include "lib/grammar.pir"
.namespace [ "Perl6" ]
-=item C<onload()>
+=over 4
+
+=head2 Functions
+
+=item C<__onload()>
Loads the PGE libraries needed for running the parser,
and registers the "parse" subroutine as the "Perl6"
@@ -37,11 +41,82 @@ compiler.
load_bytecode "PGE.pbc"
load_bytecode "PGE/Text.pir"
+ $I0 = find_type "Perl6::Grammar"
+ if $I0 != 0 goto onload_1
+ load_bytecode "PGE.pbc"
+ $P0 = getclass "PGE::Rule"
+ $P0 = subclass $P0, "Perl6::Grammar"
+ onload_1:
+
$P0 = compreg "PGE::P6Rule"
$P1 = $P0("^<Perl6::Grammar::program>")
- compreg "Perl6", $P1
+ store_global "Perl6", "&parse", $P1
+
+ $P0 = find_global "Perl6", "compile"
+ compreg "Perl6", $P0
.end
+
+=item C<compile(STR code [, 'target' => target])>
+
+Compile the Perl6 C<code>. The C<target> named parameter
+allows the caller to specify the degree of compilation to
+be performed; a value of C<parse> returns the parse tree,
+C<PAST> returns the abstract syntax tree, C<PIR> returns
+the generated PIR code, and other values return the
+compiled code as a PMC.
+
+=cut
+
+.sub 'compile'
+ .param pmc code
+ .param string target :named('target') :optional
+ .param int has_target :opt_flag
+
+ if has_target goto parse
+ target = 'pbc'
+
+ parse:
+ .local pmc parse
+ .local pmc match
+ parse = find_global 'Perl6', '&parse'
+ match = parse(code)
+
+ unless match goto return_match
+ if target == 'parse' goto return_match
+ goto return_match
+
+ build_ast:
+ match = match['Perl::Grammar::statement_list']
+ .local pmc astgrammar, astbuilder, ast
+ astgrammar = new 'ASTGrammar'
+ astbuilder = astgrammar.apply(match)
+ ast = astbuilder.get('result')
+ if target == 'PAST' goto return_ast
+
+ build_pir:
+ .local pmc pirgrammar, pirbuilder
+ .local string pir
+ pirgrammar = new 'PIRGrammar'
+ pirbuilder = pirgrammar.'apply'(ast)
+ pir = pirbuilder.get('pir')
+ if target == 'PIR' goto return_pir
+
+ compile_pir:
+ $P0 = compreg "PIR"
+ $P1 = $P0(pir)
+ .return ($P1)
+
+ return_match:
+ .return (match)
+ return_ast:
+ .return (ast)
+ return_pir:
+ .return (pir)
+.end
+
+.include "lib/main.pir"
+
=pod
=back

0 comments on commit c13e960

Please sign in to comment.