Matt S Trout > Rakudo-Star-2012.08_001 > tools/dev/mk_language_shell.pl

Download:
Rakudo-Star-2012.08_001.tar.gz

Annotate this POD

Source  

NAME ^

tools/dev/mk_language_shell.pl -- create initial files for a new language implementation

SYNOPSIS ^

 % perl tools/dev/mk_language_shell.pl [options] Xyz [path]

option:

 --with-doc
 --with-ops
 --with-pmc

DESCRIPTION ^

This script populates a directory with files for building a new language translator in Parrot. The first argument is the name of the language to be built. The path argument says where to populate the directory, if no path is specified then it is taken to be a subdirectory of the current directory with the same name as the language (converted to lowercase).

For a language 'Xyz', this script will create the following files and directories (relative to path, which defaults to xyz if an explicit path isn't given):

    PARROT_REVISION
    README
    setup.pir
    xyz.pir
    doc/running.pod
    doc/Xyz.pod
    src/Xyz.pir
    src/Xyz/Grammar.pm
    src/Xyz/Actions.pm
    src/Xyz/Compiler.pm
    src/Xyz/Runtime.pm
    src/pmc/xyz.pmc
    src/ops/xyz.ops
    src/xyz.pir
    t/00-sanity.t
    xyz/.ignore

Any files that already exist are skipped, so this script can be used to repopulate a language directory with omitted files.

If all goes well, after creating the language shell one can simply change to the language directory and type

    $ parrot setup.pir
    $ parrot setup.pir test

to verify that the new language compiles and configures properly.

NAME ^

setup.pir - Python distutils style

DESCRIPTION ^

No Configure step, no Makefile generated.

USAGE ^

    $ parrot setup.pir build
    $ parrot setup.pir test
    $ sudo parrot setup.pir install

@lang@ ^

Design ^

SEE ALSO ^

Running ^

This document describes how to use the command line @lclang@ program, which ...

Usage

  parrot @lclang@.pbc [OPTIONS] <input>

or

  parrot-@lclang@@exe [OPTIONS] <input>

A number of additional options are available:

  -q  Quiet mode; suppress output of summary at the end.

TITLE ^

@lclang@.pir - A @lang@ compiler.

Description

This is the entry point for the @lang@ compiler.

Functions

main(args :slurpy) :main

Start compilation by passing any command line args to the @lang@ compiler.

TITLE ^

@lclang@.pir - A @lang@ compiler.

Description

This is the base file for the @lang@ 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 '@lang@'.

Functions

onload()

Creates the @lang@ compiler using a PCT::HLLCompiler object.

grammar @lang@::Grammar is HLL::Grammar;

token TOP { <statement_list> [ $ || <.panic: "Syntax error"> ] }

## Lexer items

# This <ws> rule treats # as "comment to eol". token ws { <!ww> [ '#' \N* \n? | \s+ ]* }

## Statements

rule statement_list { [ <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 { @lang@::Grammar.O(':prec<u>, :assoc<left>', '%multiplicative'); @lang@::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/@lang@/Actions.pm__ class @lang@::Actions is HLL::Actions;

method TOP($/) { make PAST::Block.new( $<statement_list>.ast , :hll<@lclang@>, :node($/) ); }

method statement_list($/) { 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/@lang@/Compiler.pm__ class @lang@::Compiler is HLL::Compiler;

INIT { @lang@::Compiler.language('@lang@'); @lang@::Compiler.parsegrammar(@lang@::Grammar); @lang@::Compiler.parseactions(@lang@::Actions); } __src/@lang@/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/pmc/@lclang@.pmc__ /* Copyright (C) 20xx, Parrot Foundation.

NAME ^

src/pmc/@lang@.pmc - @lang@

DESCRIPTION ^

These are the vtable functions for the @lang@ class.

Helper functions

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;
}

/*

Methods

void class_init()

initialize the pmc class. Store some constants, etc.

PMC* init()

initialize the instance.

PMC* get()

Returns a vector-like PMC.

PMC* set()

Change the existing @lang@ 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.

syntax highlighting: