This repository has been archived by the owner on Feb 3, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial version of NQP grammar and actions.
- Loading branch information
Showing
4 changed files
with
197 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
class NQP::Actions; | ||
|
||
method TOP($/, $key?) { | ||
make PAST::Val.new( :value($<integer>.ast) ); | ||
} | ||
|
||
method integer($/) { | ||
make | ||
$<hexint> | ||
?? $<hexint>.ast | ||
!! $<octint> | ||
?? $<octint>.ast | ||
!! string_to_int( $<digits>, $<base> eq 'b' ?? 2 !! 10 ); | ||
} | ||
|
||
method hexint($/) { | ||
make string_to_int( $/, 16 ); | ||
} | ||
|
||
method octint($/) { | ||
make string_to_int( $/, 8 ); | ||
} | ||
|
||
method quote_delimited($/) { | ||
my $str := ''; | ||
for $<quote_atom> { | ||
$str := $str ~ $_.ast; | ||
} | ||
make PAST::Val.new(:value($str), :node($/)); | ||
} | ||
|
||
method quote_atom($/) { | ||
make $<escape> ?? $<escape>.ast !! ~$/; | ||
} | ||
|
||
method escape:sym<nl>($/) { make "\n"; } | ||
method escape:sym<bs>($/) { make "\b"; } | ||
method escape:sym<tab>($/) { make "\t"; } | ||
|
||
sub string_to_int($src, $base) { | ||
Q:PIR { | ||
.local string src | ||
$P0 = find_lex '$src' | ||
src = $P0 | ||
.local int base, pos, eos, result | ||
$P0 = find_lex '$base' | ||
base = $P0 | ||
pos = 0 | ||
eos = length src | ||
result = 0 | ||
str_loop: | ||
unless pos < eos goto str_done | ||
.local string char | ||
char = substr src, pos, 1 | ||
if char == '_' goto str_next | ||
.local int digitval | ||
digitval = index "00112233445566778899AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz", char | ||
if digitval < 0 goto err_base | ||
digitval >>= 1 | ||
if digitval > base goto err_base | ||
result *= base | ||
result += digitval | ||
str_next: | ||
inc pos | ||
goto str_loop | ||
err_base: | ||
die "Invalid radix conversion" | ||
str_done: | ||
%r = box result | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# Copyright (C) 2009, Patrick R. Michaud | ||
|
||
=head1 NAME | ||
|
||
NQP::Compiler - NQP compiler | ||
|
||
=head1 DESCRIPTION | ||
|
||
=cut | ||
|
||
.sub '' :anon :load :init | ||
load_bytecode 'PCT.pbc' | ||
load_bytecode 'P6Regex.pbc' | ||
.end | ||
|
||
.include 'src/gen/nqp-grammar.pir' | ||
.include 'src/gen/nqp-actions.pir' | ||
|
||
.namespace ['NQP';'Compiler'] | ||
|
||
.sub '' :anon :load :init | ||
.local pmc p6meta, nqpproto | ||
p6meta = get_hll_global 'P6metaclass' | ||
nqpproto = p6meta.'new_class'('Regex::P6Grammar::Compiler', 'parent'=>'HLL::Compiler') | ||
nqpproto.'language'('NQP-rx') | ||
$P0 = get_hll_global ['NQP'], 'Grammar' | ||
nqpproto.'parsegrammar'($P0) | ||
$P0 = get_hll_global ['NQP'], 'Actions' | ||
nqpproto.'parseactions'($P0) | ||
.end | ||
|
||
.sub 'main' :main | ||
.param pmc args_str | ||
|
||
$P0 = compreg 'NQP-rx' | ||
$P1 = $P0.'command_line'(args_str, 'encoding'=>'utf8', 'transcode'=>'ascii iso-8859-1') | ||
exit 0 | ||
.end | ||
|
||
|
||
=cut | ||
|
||
# Local Variables: | ||
# mode: pir | ||
# fill-column: 100 | ||
# End: | ||
# vim: expandtab shiftwidth=4 ft=pir: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
grammar NQP::Grammar; | ||
|
||
token starter { \" } | ||
token stopper { \" } | ||
|
||
token TOP { ^ \s* <integer> \s* $ } | ||
|
||
token quote_delimited { | ||
<starter> <quote_atom>* <stopper> | ||
} | ||
|
||
token quote_atom { | ||
<!stopper> | ||
[ | ||
| <escape> | ||
| [ <-escape-stopper> ]+ | ||
] | ||
} | ||
|
||
token hexint { [<[ 0..9 a..f A..F ]>+] ** '_' } | ||
token hexints { [<.ws><hexint><.ws>] ** ',' } | ||
|
||
token octint { [<[ 0..7 ]>+] ** '_' } | ||
token octints { [<.ws><octint><.ws>] ** ',' } | ||
|
||
token integer { | ||
[ | ||
| 0 [ b $<digits>=[[<[01]>+] ** '_'] | ||
| o <octint> | ||
| x <hexint> | ||
| d $<digits>=[[\d+] ** '_'] | ||
] | ||
| $<digits>=[\d+ [_\d+]*] | ||
] | ||
} | ||
|
||
proto token escape { <...> } | ||
token escape:sym<backslash> { \\ \\ } | ||
token escape:sym<bs> { \\ b } | ||
token escape:sym<oct> { \\ o [ <octint> | '[' <octints> ']' ] } | ||
token escape:sym<hex> { \\ x [ <hexint> | '[' <hexints> ']' ] } | ||
token escape:sym<chr> { \\ c <charspec> } | ||
token escape:sym<nl> { \\ n } | ||
token escape:sym<cr> { \\ r } | ||
token escape:sym<tab> { \\ t } | ||
|
||
token charname { | ||
|| <integer> | ||
|| <[a..z A..Z]> <-[ \] , # ]>*? <[a..z A..Z ) ]> | ||
<?before \s* <[ \] , # ]> > | ||
} | ||
token charnames { [<.ws><charname><.ws>] ** ',' } | ||
|
||
token charspec { | ||
[ | ||
| '[' <charnames> ']' | ||
| \d+ [ _ \d+]* | ||
| <[ ?..Z ]> | ||
| <.panic: 'Unrecognized \\c character'> | ||
] | ||
} |