secret
Last active

slangs, what are they?

  • Download Gist
COBOL.pm
Perl
1 2 3 4 5 6 7 8
class COBOL does Slangy {
has $.grammar = grammar {
token TOP { .* }
};
has $.actions = class {
method TOP($/) { say "method TOP($/)" }
}
}
in-short.md
Markdown

I'd imagine that the following could be the nicest way to create your own slang:

# The `slang` keyword implicitly applies "does Slangy" which stubs `has $.grammar;`
# and `has $.actions;`. It also makes sure the slang will be available as `$~COBOL`
# and the grammar and actions will be put in `%*LANG`.
slang COBOL {
    has $.grammar = COBOL::Grammar.new;
    has $.actions = COBOL::Actions.new;

    # Optionally register our own module loader to support `use Foo:from<COBOL`
    # from within Perl 6.
    has $.module-loader = COBOL::ModuleLoader;
}

# Or register the ML later, by:
$~COBOL.module-loader = COBOL::ModuleLoader.new;
rakudo.diff
Diff
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
diff --git a/src/Perl6/Grammar.nqp b/src/Perl6/Grammar.nqp
index a37b6ec..69e0d3e 100644
--- a/src/Perl6/Grammar.nqp
+++ b/src/Perl6/Grammar.nqp
@@ -854,6 +854,7 @@ grammar Perl6::Grammar is HLL::Grammar does STD {
# Quasis and unquotes
:my $*IN_QUASI := 0; # whether we're currently in a quasi block
+ :my $*MAIN := 'MAIN';
# Setting loading and symbol setup.
{
@@ -922,6 +923,9 @@ grammar Perl6::Grammar is HLL::Grammar does STD {
%*LANG{$_.key} := $_.value;
}
}
+ if $have_outer && $*UNIT_OUTER.symbol('$*MAIN') {
+ $*MAIN := $*UNIT_OUTER.symbol('$*MAIN')<value>;
+ }
# Install unless we've no setting, in which case we've likely no
# static lexpad class yet either. Also, UNIT needs a code object.
@@ -944,7 +948,7 @@ grammar Perl6::Grammar is HLL::Grammar does STD {
<.finishpad>
<.bom>?
- <statementlist>
+ <statementlist=.LANG($*MAIN, 'statementlist')>
<.install_doc_phaser>
@@ -1257,13 +1261,23 @@ grammar Perl6::Grammar is HLL::Grammar does STD {
token statement_control:sym<use> {
:my $longname;
+ :my $module;
:my $*IN_DECL := 'use';
:my $*HAS_SELF := '';
:my $*SCOPE := 'use';
+ :my $OLD_MAIN := ~$*MAIN;
$<doc>=[ 'DOC' \h+ ]**0..1
<sym> <.ws>
[
- | <version>
+ | <version> [ <?{ ~$<version><vnum>[0] eq '5' }> {
+ $module := $*W.load_module($/, 'Perl5', {}, $*GLOBALish);
+ do_import($/, $module, 'Perl5');
+ $longname := 'Perl5';
+ $/.CURSOR.import_EXPORTHOW($module);
+ } ]?
+ [ <?{ ~$<version><vnum>[0] eq '6' }> {
+ $*MAIN := 'MAIN';
+ } ]?
| <module_name>
{
$longname := $<module_name><longname>;
@@ -1296,7 +1310,7 @@ grammar Perl6::Grammar is HLL::Grammar does STD {
$<arglist><EXPR>.ast);
$arglist := nqp::getattr($arglist.list.eager,
$*W.find_symbol(['List']), '$!items');
- my $module := $*W.load_module($/, $name, %cp, $*GLOBALish);
+ $module := $*W.load_module($/, $name, %cp, $*GLOBALish);
do_import($/, $module, $name, $arglist);
$/.CURSOR.import_EXPORTHOW($module);
}
@@ -1306,7 +1320,7 @@ grammar Perl6::Grammar is HLL::Grammar does STD {
my $lnd := $*W.dissect_longname($longname);
my $name := $lnd.name;
my %cp := $lnd.colonpairs_hash('use');
- my $module := $*W.load_module($/, $name, %cp, $*GLOBALish);
+ $module := $*W.load_module($/, $name, %cp, $*GLOBALish);
do_import($/, $module, $name);
$/.CURSOR.import_EXPORTHOW($module);
}
@@ -1314,6 +1328,17 @@ grammar Perl6::Grammar is HLL::Grammar does STD {
}
]
]
+ [
+ || <?{ nqp::defined($module) && nqp::existskey($module, ~$longname)
+ && nqp::istype($module{~$longname}, $*W.find_symbol(['Slangy'])) }>
+ {
+ $*MAIN := ~$longname;
+ %*LANG<COBOL> := $module{~$longname}.new.grammar;
+ %*LANG<COBOL-actions> := $module{~$longname}.new.actions;
+ }
+ <statementlist=.LANG($*MAIN, 'TOP')>
+ || <?>
+ ]
<.ws>
}
diff --git a/src/core/Positional.pm b/src/core/Positional.pm
index ac73982..bd764fd 100644
--- a/src/core/Positional.pm
+++ b/src/core/Positional.pm
@@ -1,3 +1,7 @@
my role Positional[::T = Mu] {
method of() { T }
}
+
+my role Slangy[::T = Mu] {
+ method of() { T }
+}
diff --git a/src/core/core_prologue.pm b/src/core/core_prologue.pm
index e41cc40..3c24c95 100644
--- a/src/core/core_prologue.pm
+++ b/src/core/core_prologue.pm
@@ -4,6 +4,7 @@ use Perl6::BOOTSTRAP;
my class Pair { ... }
my class Whatever { ... }
my class WhateverCode { ... }
+my role Slangy { ... }
# Stub these or we can't use any sigil other than $.
my role Positional { ... }
use-cobol.sh
Shell
1 2 3 4 5 6 7 8 9 10 11 12
$ perl6-p -I. -e 'use COBOL'
method TOP()
===SORRY!===
Can not get attribute '$!pos' declared in class 'NQPCursor' with this object
 
# Problem is that the Perl6 grammar is a NQP class, while our COBOL grammar is from Perl 6 land.
# We need to ross that boundary somehow...
 
 
$ perl6-p -I. -e 'use COBOL; hurz'
===SORRY!===
No such method 'substr' for invocant of type 'String'

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.