Commits

allison committed 42c488d

Stock generated language shell

Comments (0)

Files changed (13)

+$Revision$
+# $Id$
+
+=head1 TITLE
+
+parvik.pir - A Parvik compiler.
+
+=head2 Description
+
+This is the entry point for the Parvik compiler.
+
+=head2 Functions
+
+=over 4
+
+=item main(args :slurpy)  :main
+
+Start compilation by passing any command line C<args>
+to the Parvik compiler.
+
+=cut
+
+.sub 'main' :main
+    .param pmc args
+
+    load_language 'parvik'
+
+    $P0 = compreg 'Parvik'
+    $P1 = $P0.'command_line'(args)
+.end
+
+=back
+
+=cut
+
+# Local Variables:
+#   mode: pir
+#   fill-column: 100
+# End:
+# vim: expandtab shiftwidth=4 ft=pir:
+
+#!/usr/bin/env parrot
+# $Id$
+
+=head1 NAME
+
+setup.pir - Python distutils style
+
+=head1 DESCRIPTION
+
+No Configure step, no Makefile generated.
+
+=head1 USAGE
+
+    $ parrot setup.pir build
+    $ parrot setup.pir test
+    $ sudo parrot setup.pir install
+
+=cut
+
+.sub 'main' :main
+    .param pmc args
+    $S0 = shift args
+    load_bytecode 'distutils.pbc'
+
+    .local int reqsvn
+    $P0 = new 'FileHandle'
+    $P0.'open'('PARROT_REVISION', 'r')
+    $S0 = $P0.'readline'()
+    reqsvn = $S0
+    $P0.'close'()
+
+    .local pmc config
+    config = get_config()
+    $I0 = config['revision']
+    unless $I0 goto L1
+    unless reqsvn > $I0 goto L1
+    $S1 = "Parrot revision r"
+    $S0 = reqsvn
+    $S1 .= $S0
+    $S1 .= " required (currently r"
+    $S0 = $I0
+    $S1 .= $S0
+    $S1 .= ")\n"
+    print $S1
+    end
+  L1:
+
+    $P0 = new 'Hash'
+    $P0['name'] = 'Parvik'
+    $P0['abstract'] = 'the Parvik compiler'
+    $P0['description'] = 'the Parvik for Parrot VM.'
+
+    # build
+    $P1 = new 'Hash'
+    $P1['parvik_ops'] = 'src/ops/parvik.ops'
+    $P0['dynops'] = $P1
+
+    $P2 = new 'Hash'
+    $P3 = split ' ', 'src/pmc/parvik.pmc'
+    $P2['parvik_group'] = $P3
+    $P0['dynpmc'] = $P2
+
+    $P4 = new 'Hash'
+    $P4['src/gen_actions.pir'] = 'src/Parvik/Actions.pm'
+    $P4['src/gen_compiler.pir'] = 'src/Parvik/Compiler.pm'
+    $P4['src/gen_grammar.pir'] = 'src/Parvik/Grammar.pm'
+    $P4['src/gen_runtime.pir'] = 'src/Parvik/Runtime.pm'
+    $P0['pir_nqp-rx'] = $P4
+
+    $P5 = new 'Hash'
+    $P6 = split "\n", <<'SOURCES'
+src/parvik.pir
+src/gen_actions.pir
+src/gen_compiler.pir
+src/gen_grammar.pir
+src/gen_runtime.pir
+SOURCES
+    $S0 = pop $P6
+    $P5['parvik/parvik.pbc'] = $P6
+    $P5['parvik.pbc'] = 'parvik.pir'
+    $P0['pbc_pir'] = $P5
+
+    $P7 = new 'Hash'
+    $P7['parrot-parvik'] = 'parvik.pbc'
+    $P0['installable_pbc'] = $P7
+
+    # test
+    $S0 = get_parrot()
+    $S0 .= ' parvik.pbc'
+    $P0['prove_exec'] = $S0
+
+    # install
+    $P0['inst_lang'] = 'parvik/parvik.pbc'
+
+    # dist
+    $P0['doc_files'] = 'README'
+
+    .tailcall setup(args :flat, $P0 :flat :named)
+.end
+
+
+# Local Variables:
+#   mode: pir
+#   fill-column: 100
+# End:
+# vim: expandtab shiftwidth=4 ft=pir:
+

src/Parvik/Actions.pm

+class Parvik::Actions is HLL::Actions;
+
+method TOP($/) {
+    make PAST::Block.new( $<statementlist>.ast , :hll<parvik>, :node($/) );
+}
+
+method statementlist($/) {
+    my $past := PAST::Stmts.new( :node($/) );
+    for $<statement> { $past.push( $_.ast ); }
+    make $past;
+}
+
+method statement($/) {
+    make $<statement_control> ?? $<statement_control>.ast !! $<EXPR>.ast;
+}
+
+method statement_control:sym<say>($/) {
+    my $past := PAST::Op.new( :name<say>, :pasttype<call>, :node($/) );
+    for $<EXPR> { $past.push( $_.ast ); }
+    make $past;
+}
+
+method statement_control:sym<print>($/) {
+    my $past := PAST::Op.new( :name<print>, :pasttype<call>, :node($/) );
+    for $<EXPR> { $past.push( $_.ast ); }
+    make $past;
+}
+
+method term:sym<integer>($/) { make $<integer>.ast; }
+method term:sym<quote>($/) { make $<quote>.ast; }
+
+method quote:sym<'>($/) { make $<quote_EXPR>.ast; }
+method quote:sym<">($/) { make $<quote_EXPR>.ast; }
+
+method circumfix:sym<( )>($/) { make $<EXPR>.ast; }
+

src/Parvik/Compiler.pm

+class Parvik::Compiler is HLL::Compiler;
+
+INIT {
+    Parvik::Compiler.language('Parvik');
+    Parvik::Compiler.parsegrammar(Parvik::Grammar);
+    Parvik::Compiler.parseactions(Parvik::Actions);
+}

src/Parvik/Grammar.pm

+=begin overview
+
+This is the grammar for Parvik in Perl 6 rules.
+
+=end overview
+
+grammar Parvik::Grammar is HLL::Grammar;
+
+token TOP {
+    <statementlist>
+    [ $ || <.panic: "Syntax error"> ]
+}
+
+## Lexer items
+
+# This <ws> rule treats # as "comment to eol".
+token ws {
+    <!ww>
+    [ '#' \N* \n? | \s+ ]*
+}
+
+## Statements
+
+rule statementlist { [ <statement> | <?> ] ** ';' }
+
+rule statement {
+    | <statement_control>
+    | <EXPR>
+}
+
+proto token statement_control { <...> }
+rule statement_control:sym<say>   { <sym> [ <EXPR> ] ** ','  }
+rule statement_control:sym<print> { <sym> [ <EXPR> ] ** ','  }
+
+## Terms
+
+token term:sym<integer> { <integer> }
+token term:sym<quote> { <quote> }
+
+proto token quote { <...> }
+token quote:sym<'> { <?[']> <quote_EXPR: ':q'> }
+token quote:sym<"> { <?["]> <quote_EXPR: ':qq'> }
+
+## Operators
+
+INIT {
+    Parvik::Grammar.O(':prec<u>, :assoc<left>',  '%multiplicative');
+    Parvik::Grammar.O(':prec<t>, :assoc<left>',  '%additive');
+}
+
+token circumfix:sym<( )> { '(' <.ws> <EXPR> ')' }
+
+token infix:sym<*>  { <sym> <O('%multiplicative, :pirop<mul>')> }
+token infix:sym</>  { <sym> <O('%multiplicative, :pirop<div>')> }
+
+token infix:sym<+>  { <sym> <O('%additive, :pirop<add>')> }
+token infix:sym<->  { <sym> <O('%additive, :pirop<sub>')> }

src/Parvik/Runtime.pm

+# language-specific runtime functions go here
+
+sub print(*@args) {
+    pir::print(pir::join('', @args));
+    1;
+}
+
+sub say(*@args) {
+    pir::say(pir::join('', @args));
+    1;
+}

src/ops/parvik.ops

+/*
+ * $Id$
+ * Copyright (C) 20xx, Parrot Foundation.
+ */
+
+BEGIN_OPS_PREAMBLE
+
+#include "parrot/dynext.h"
+
+END_OPS_PREAMBLE
+
+/* Op to get the address of a PMC. */
+inline op parvik_pmc_addr(out INT, invar PMC) :base_core {
+    $1 = (int) $2;
+    goto NEXT();
+}
+
+/*
+ * Local variables:
+ *   c-file-style: "parrot"
+ * End:
+ * vim: expandtab shiftwidth=4:
+ */
+
+# $Id$
+
+=head1 TITLE
+
+parvik.pir - A Parvik compiler.
+
+=head2 Description
+
+This is the base file for the Parvik compiler.
+
+This file includes the parsing and grammar rules from
+the src/ directory, loads the relevant PGE libraries,
+and registers the compiler under the name 'Parvik'.
+
+=head2 Functions
+
+=over 4
+
+=item onload()
+
+Creates the Parvik compiler using a C<PCT::HLLCompiler>
+object.
+
+=cut
+
+.HLL 'parvik'
+.loadlib 'parvik_group'
+
+.namespace []
+
+.sub '' :anon :load
+    load_bytecode 'HLL.pbc'
+
+    .local pmc hllns, parrotns, imports
+    hllns = get_hll_namespace
+    parrotns = get_root_namespace ['parrot']
+    imports = split ' ', 'PAST PCT HLL Regex Hash'
+    parrotns.'export_to'(hllns, imports)
+.end
+
+.include 'src/gen_grammar.pir'
+.include 'src/gen_actions.pir'
+.include 'src/gen_compiler.pir'
+.include 'src/gen_runtime.pir'
+
+=back
+
+=cut
+
+# Local Variables:
+#   mode: pir
+#   fill-column: 100
+# End:
+# vim: expandtab shiftwidth=4 ft=pir:
+

src/pmc/parvik.pmc

+/*
+Copyright (C) 20xx, Parrot Foundation.
+$Id$
+
+=head1 NAME
+
+src/pmc/Parvik.pmc - Parvik
+
+=head1 DESCRIPTION
+
+These are the vtable functions for the Parvik class.
+
+=cut
+
+=head2 Helper functions
+
+=over 4
+
+=item INTVAL size(INTERP, PMC, PMC)
+
+*/
+
+#include "parrot/parrot.h"
+
+static INTVAL
+size(Interp *interp, PMC* self, PMC* obj)
+{
+    INTVAL retval;
+    INTVAL dimension;
+    INTVAL length;
+    INTVAL pos;
+
+    if (!obj || PMC_IS_NULL(obj)) {
+        /* not set, so a simple 1D */
+        return VTABLE_get_integer(interp, self);
+    }
+
+    retval = 1;
+    dimension = VTABLE_get_integer(interp, obj);
+    for (pos = 0; pos < dimension; pos++)
+    {
+        length = VTABLE_get_integer_keyed_int(interp, obj, pos);
+        retval *= length;
+    }
+    return retval;
+}
+
+/*
+
+=back
+
+=head2 Methods
+
+=over 4
+
+=cut
+
+*/
+
+pmclass Parvik
+    extends ResizablePMCArray
+    provides array
+    group   parvik_group
+    auto_attrs
+    dynpmc
+    {
+/*
+
+=item C<void class_init()>
+
+initialize the pmc class. Store some constants, etc.
+
+=cut
+
+*/
+
+    void class_init() {
+    }
+
+
+/*
+
+=item C<PMC* init()>
+
+initialize the instance.
+
+=cut
+
+*/
+
+void init() {
+    SUPER();
+};
+
+=item C<PMC* get()>
+
+Returns a vector-like PMC.
+
+=cut
+
+*/
+
+    METHOD PMC* get() {
+        PMC* property;
+        INTVAL array_t;
+        STRING* property_name;
+
+        property_name = string_from_literal(INTERP, "property");
+        shape = VTABLE_getprop(INTERP, SELF, property_name);
+        if (PMC_IS_NULL(property)) {
+           /*
+            * No property has been set yet. This means that we are
+            * a simple vector
+            *
+            * we use our own type here. Perhaps a better way to
+            * specify it?
+            */
+            /*
+            array_t = Parrot_pmc_get_type_str(INTERP,
+                string_from_literal(INTERP, "Parvik"));
+            */
+            property = Parrot_pmc_new(INTERP, VTABLE_type(INTERP, SELF));
+
+            VTABLE_set_integer_native(INTERP, property, 1);
+            VTABLE_set_integer_keyed_int(INTERP, property, 0,
+                VTABLE_get_integer(INTERP, SELF));
+            VTABLE_setprop(INTERP, SELF, property_name, property);
+        }
+        RETURN(PMC* property);
+    }
+
+/*
+
+=item C<PMC* set()>
+
+Change the existing Parvik by passing in an existing vector.
+
+If the new property is larger than our old property, pad the end of the vector
+with elements from the beginning.
+
+If the new property is shorter than our old property, truncate elements from
+the end of the vector.
+
+=cut
+
+*/
+
+    METHOD set(PMC *new_property) {
+        STRING* property_name;
+        PMC*    old_property;
+        INTVAL  old_size, new_size, pos;
+
+        /* save the old property momentarily, set the new property */
+        property_name = string_from_literal(INTERP, "property");
+        old_property = VTABLE_getprop(INTERP, SELF, property_name);
+        VTABLE_setprop(INTERP, SELF, property_name, new_property);
+
+        /* how big are these property? */
+        old_size = size(INTERP, SELF, old_property);
+        new_size = size(INTERP, SELF, new_property);
+
+        if (old_size > new_size) {
+            for (; new_size != old_size; new_size++) {
+                VTABLE_pop_pmc(INTERP, SELF);
+            }
+        } else if (new_size > old_size) {
+            pos = 0;
+            for (; new_size != old_size; old_size++, pos++) {
+                VTABLE_push_pmc(INTERP, SELF,
+                    VTABLE_get_pmc_keyed_int(INTERP, SELF, pos));
+            }
+        }
+    }
+
+/*
+
+=back
+
+=cut
+
+*/
+
+}
+
+/*
+ * Local variables:
+ *   c-file-style: "parrot"
+ * End:
+ * vim: expandtab shiftwidth=4:
+ */
+# This just checks that the basic parsing and call to builtin say() works.
+say '1..4';
+say 'ok 1';
+say 'ok ', 2;
+say 'ok ', 2 + 1;
+say 'ok', ' ', 4;