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.
Split NQP into shared (HLL) and non-shared (NQP) components.
- Loading branch information
Showing
3 changed files
with
163 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
class HLL::Actions; | ||
|
||
method integer($/) { | ||
make $<decint> | ||
?? string_to_int( $<decint>, 10) | ||
!! ( $<hexint> | ||
?? $<hexint>.ast | ||
!! ( $<octint> | ||
?? $<octint>.ast | ||
!! string_to_int( $<binint>, 2) | ||
) | ||
); | ||
} | ||
|
||
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"; } | ||
|
||
method escape:sym<hex>($/) { | ||
make ints_to_string( $<hexint> ?? $<hexint> !! $<hexints><hexint> ); | ||
} | ||
|
||
method escape:sym<oct>($/) { | ||
make ints_to_string( $<octint> ?? $<octint> !! $<octints><octint> ); | ||
} | ||
|
||
|
||
sub string_to_int($src, $base) { | ||
Q:PIR { | ||
.local pmc src | ||
.local string src_s | ||
src = find_lex '$src' | ||
src_s = src | ||
.local int base, pos, eos, result | ||
$P0 = find_lex '$base' | ||
base = $P0 | ||
pos = 0 | ||
eos = length src_s | ||
result = 0 | ||
str_loop: | ||
unless pos < eos goto str_done | ||
.local string char | ||
char = substr src_s, 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: | ||
src.'panic'('Invalid radix conversion of "', char, '"') | ||
str_done: | ||
%r = box result | ||
}; | ||
} | ||
|
||
sub ints_to_string($ints) { | ||
Q:PIR { | ||
.local string result | ||
result = '' | ||
.local pmc ints, ints_it | ||
ints = find_lex '$ints' | ||
$I0 = does ints, 'array' | ||
unless $I0 goto ints_1 | ||
ints_it = iter ints | ||
ints_loop: | ||
unless ints_it goto ints_done | ||
$P0 = shift ints_it | ||
$I0 = $P0.'ast'() | ||
$S0 = chr $I0 | ||
concat result, $S0 | ||
goto ints_loop | ||
ints_1: | ||
$I0 = ints.'ast'() | ||
result = chr $I0 | ||
ints_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,59 @@ | ||
grammar HLL::Grammar; | ||
|
||
token starter { \" } | ||
token stopper { \" } | ||
|
||
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 $<binint>=[[<[01]>+] ** '_'] | ||
| o <octint> | ||
| x <hexint> | ||
| d $<decint>=[[\d+] ** '_'] | ||
] | ||
| $<decint>=[\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'> | ||
] | ||
} | ||
|
File renamed without changes.