diff --git a/build/Makefile.in b/build/Makefile.in index c1952b2..379d26f 100644 --- a/build/Makefile.in +++ b/build/Makefile.in @@ -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 @@ -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 \ @@ -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 \ diff --git a/src/NQP/Actions.pm b/src/NQP/Actions.pm index 1ac5a2a..fbf44c9 100644 --- a/src/NQP/Actions.pm +++ b/src/NQP/Actions.pm @@ -1,9 +1,6 @@ -class NQP::Actions; +class NQP::Actions is HLL::Actions; -NQP::Grammar.O(':prec, :assoc', '%multiplicative'); -NQP::Grammar.O(':prec, :assoc', '%additive'); - -method TOPx($/) { +method TOP($/) { make $.ast; } @@ -14,105 +11,3 @@ method value($/) { make $past; } -method integer($/) { - make $ - ?? string_to_int( $, 10) - !! ( $ - ?? $.ast - !! ( $ - ?? $.ast - !! string_to_int( $, 2) - ) - ); -} - -method hexint($/) { - make string_to_int( $/, 16 ); -} - -method octint($/) { - make string_to_int( $/, 8 ); -} - -method quote_delimited($/) { - my $str := ''; - for $ { - $str := $str ~ $_.ast; - } - make PAST::Val.new(:value($str), :node($/)); -} - -method quote_atom($/) { - make $ ?? $.ast !! ~$/; -} - -method escape:sym($/) { make "\n"; } -method escape:sym($/) { make "\b"; } -method escape:sym($/) { make "\t"; } - -method escape:sym($/) { - make ints_to_string( $ ?? $ !! $ ); -} - -method escape:sym($/) { - make ints_to_string( $ ?? $ !! $ ); -} - - -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 - }; -} diff --git a/src/NQP/Compiler.pir b/src/NQP/Compiler.pir index 59f8a5a..0f2ae84 100644 --- a/src/NQP/Compiler.pir +++ b/src/NQP/Compiler.pir @@ -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' @@ -37,9 +37,6 @@ NQP::Compiler - NQP compiler exit 0 .end - -.include 'src/cheats/nqp-grammar.pir' - =cut # Local Variables: diff --git a/src/NQP/Grammar.pm b/src/NQP/Grammar.pm index 72ae065..0649534 100644 --- a/src/NQP/Grammar.pm +++ b/src/NQP/Grammar.pm @@ -1,73 +1,9 @@ -grammar NQP::Grammar; +grammar NQP::Grammar is HLL::Grammar; -token starter { \" } -token stopper { \" } - -token TOP { * } +token TOP { } token value { | | } -token quote_delimited { - * -} - -token quote_atom { - - [ - | - | [ <-escape-stopper> ]+ - ] -} - -token hexint { [<[ 0..9 a..f A..F ]>+] ** '_' } -token hexints { [<.ws><.ws>] ** ',' } - -token octint { [<[ 0..7 ]>+] ** '_' } -token octints { [<.ws><.ws>] ** ',' } - -token integer { - [ - | 0 [ b $=[[<[01]>+] ** '_'] - | o - | x - | d $=[[\d+] ** '_'] - ] - | $=[\d+ [_\d+]*] - ] -} - -proto token escape { <...> } -token escape:sym { \\ \\ } -token escape:sym { \\ b } -token escape:sym { \\ o [ | '[' ']' ] } -token escape:sym { \\ x [ | '[' ']' ] } -token escape:sym { \\ c } -token escape:sym { \\ n } -token escape:sym { \\ r } -token escape:sym { \\ t } - -token charname { - || - || <[a..z A..Z]> <-[ \] , # ]>*? <[a..z A..Z ) ]> - > -} -token charnames { [<.ws><.ws>] ** ',' } - -token charspec { - [ - | '[' ']' - | \d+ [ _ \d+]* - | <[ ?..Z ]> - | <.panic: 'Unrecognized \\c character'> - ] -} - -proto token infix { <...> } -token infix:sym<+> { '+' ')> } -token infix:sym<-> { '-' ')> } -token infix:sym<*> { '*' ')> } -token infix:sym { '/' ')> } - diff --git a/src/cheats/hll-grammar.pir b/src/cheats/hll-grammar.pir index 2ab7e07..e98c08a 100644 --- a/src/cheats/hll-grammar.pir +++ b/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 @@ -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