Skip to content
This repository has been archived by the owner on Feb 3, 2021. It is now read-only.

Commit

Permalink
Split NQP into shared (HLL) and non-shared (NQP) components.
Browse files Browse the repository at this point in the history
  • Loading branch information
pmichaud committed Oct 19, 2009
1 parent a5131aa commit b25240b
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 184 deletions.
22 changes: 18 additions & 4 deletions build/Makefile.in
Expand Up @@ -56,11 +56,15 @@ P6GRAMMAR_SOURCES = \
src/Regex/P6Grammar/Grammar.pm \
src/Regex/P6Grammar/Actions.pm \

HLLGRAMMAR_SOURCES = \
src/HLL/Grammar.pm \
src/HLL/Actions.pm \
src/cheats/hll-grammar.pir \

NQP_SOURCES = \
src/NQP/Grammar.pm \
src/NQP/Actions.pm \
src/NQP/Compiler.pir \
src/cheats/nqp-grammar.pir \

STAGE0 = src/stage0
STAGE1 = src/stage1
Expand All @@ -79,11 +83,14 @@ P6GRAMMAR_PBC_0 = $(STAGE0)/$(P6GRAMMAR_PBC)
P6GRAMMAR_PBC_1 = $(STAGE1)/$(P6GRAMMAR_PBC)
P6GRAMMAR_G_1 = $(STAGE1)/$(P6GRAMMAR_G)

HLLGRAMMAR_PBC = HLLGrammar.pbc

CLEANUPS = \
*.manifest \
*.pdb \
P6Regex.pbc \
P6Grammar.pbc \
$(P6REGEX_PBC) \
$(P6GRAMMAR_PBC) \
$(HLLGRAMMAR_PBC) \
nqp.pbc \
nqp$(EXE) \
src/stage0/*.pbc \
Expand Down Expand Up @@ -145,7 +152,14 @@ P6Regex$(EXE): $(P6REGEX_PBC) $(PBC_TO_EXE)
P6Grammar$(EXE): $(P6GRAMMAR_PBC) $(PBC_TO_EXE)
$(PBC_TO_EXE) $(P6GRAMMAR_PBC)

nqp$(EXE): $(P6GRAMMAR_PBC) $(PARROT_NQP) $(PBC_TO_EXE) $(NQP_SOURCES)
$(HLLGRAMMAR_PBC): $(P6GRAMMAR_PBC) $(P6REGEX_PBC) $(HLLGRAMMAR_SOURCES)
$(PARROT) $(P6GRAMMAR_PBC) --target=pir \
src/HLL/Grammar.pm >src/gen/hll-grammar.pir
$(PARROT_NQP) --target=pir \
src/HLL/Actions.pm >src/gen/hll-actions.pir
$(PARROT) -o HLLGrammar.pbc src/cheats/hll-grammar.pir

nqp$(EXE): $(HLLGRAMMAR_PBC) $(P6GRAMMAR_PBC) $(PARROT_NQP) $(PBC_TO_EXE) $(NQP_SOURCES)
$(PARROT) $(P6GRAMMAR_PBC) --target=pir \
src/NQP/Grammar.pm >src/gen/nqp-grammar.pir
$(PARROT_NQP) --target=pir \
Expand Down
109 changes: 2 additions & 107 deletions src/NQP/Actions.pm
@@ -1,9 +1,6 @@
class NQP::Actions;
class NQP::Actions is HLL::Actions;

NQP::Grammar.O(':prec<u=>, :assoc<left>', '%multiplicative');
NQP::Grammar.O(':prec<t=>, :assoc<left>', '%additive');

method TOPx($/) {
method TOP($/) {
make $<value>.ast;
}

Expand All @@ -14,105 +11,3 @@ method value($/) {
make $past;
}

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
};
}
5 changes: 1 addition & 4 deletions src/NQP/Compiler.pir
Expand Up @@ -10,7 +10,7 @@ NQP::Compiler - NQP compiler

.sub '' :anon :load :init
load_bytecode 'PCT.pbc'
load_bytecode 'P6Regex.pbc'
load_bytecode 'HLLGrammar.pbc'
.end

.include 'src/gen/nqp-grammar.pir'
Expand All @@ -37,9 +37,6 @@ NQP::Compiler - NQP compiler
exit 0
.end


.include 'src/cheats/nqp-grammar.pir'

=cut

# Local Variables:
Expand Down
68 changes: 2 additions & 66 deletions src/NQP/Grammar.pm
@@ -1,73 +1,9 @@
grammar NQP::Grammar;
grammar NQP::Grammar is HLL::Grammar;

token starter { \" }
token stopper { \" }

token TOP { <infix>* }
token TOP { <value> }

token value {
| <integer>
| <quote_delimited>
}

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'>
]
}

proto token infix { <...> }
token infix:sym<+> { '+' <O('%additive , :pirop<add>')> }
token infix:sym<-> { '-' <O('%additive , :pirop<sub>')> }
token infix:sym<*> { '*' <O('%multiplicative , :pirop<mul>')> }
token infix:sym</> { '/' <O('%multiplicative , :pirop<div>')> }

11 changes: 8 additions & 3 deletions src/cheats/hll-grammar.pir
@@ -1,6 +1,13 @@
.include 'cclass.pasm'

.namespace ['NQP';'Grammar']
.sub '' :load :init
load_bytecode 'P6Regex.pbc'
.end

.include 'src/gen/hll-grammar.pir'
.include 'src/gen/hll-actions.pir'

.namespace ['HLL';'Grammar']

.sub 'O' :method
.param string spec
Expand All @@ -19,11 +26,9 @@
unless null hash goto hash_done

hash = new ['Hash']

.local int pos, eos
pos = 0
eos = length spec

spec_loop:
pos = find_not_cclass .CCLASS_WHITESPACE, spec, pos, eos
if pos >= eos goto spec_done
Expand Down

0 comments on commit b25240b

Please sign in to comment.