Permalink
Browse files

Add examples from pmichaud++ about using nqp-rx to define Kea's gramm…

…ar, and fiddle with the current grammar
  • Loading branch information...
1 parent 4af0208 commit 353eab3799e318c363df53cde464166dfee8d274 @leto committed Nov 4, 2009
Showing with 85 additions and 7 deletions.
  1. +75 −0 NQP
  2. +1 −0 build/Makefile.in
  3. +7 −5 src/builtins/dup.pir
  4. +1 −1 src/parser/actions.pm
  5. +1 −1 src/parser/grammar.pg
View
75 NQP
@@ -0,0 +1,75 @@
+Some examples from pmichaud++ about implementing basic prefix math ops:
+
+$ cat kea
+#! nqp
+
+grammar Kea {
+
+ token TOP { <expr> }
+
+ proto token expr { <...> }
+ rule expr:sym<+> { '+' <expr> <expr> }
+ rule expr:sym<-> { '-' <expr> <expr> }
+ rule expr:sym<*> { '*' <expr> <expr> }
+ rule expr:sym</> { '/' <expr> <expr> }
+
+ token expr:sym<value> { \d+ }
+
+}
+
+class KeaActions {
+ method TOP($/) { make $<expr>.ast }
+
+ method expr:sym<+>($/) { make $<expr>[0].ast + $<expr>[1].ast }
+ method expr:sym<->($/) { make $<expr>[0].ast - $<expr>[1].ast }
+ method expr:sym<*>($/) { make $<expr>[0].ast * $<expr>[1].ast }
+ method expr:sym</>($/) { make $<expr>[0].ast / $<expr>[1].ast }
+
+ method expr:sym<value>($/) { make +$/ }
+}
+
+my $match := Kea.parse('+ 3 * 2 4', :actions(KeaActions));
+
+say($match.ast);
+
+$ ./nqp kea
+11
+$
+
+
+$ cat kea
+#! nqp
+
+grammar Kea {
+
+ token TOP { <expr> }
+
+ proto token expr { <...> }
+ rule expr:sym<+> { '+' <expr> <expr> }
+ rule expr:sym<-> { '-' <expr> <expr> }
+ rule expr:sym<*> { '*' <expr> <expr> }
+ rule expr:sym</> { '/' <expr> <expr> }
+
+ token expr:sym<value> { \d+ }
+
+}
+
+my $match := Kea.parse('+ 3 * 2 4');
+_dumper($match);
+
+$ ./nqp kea
+"VAR1" => PMC 'Regex;Match' => "+ 3 * 2 4" @ 0 {
+ <expr> => PMC 'Regex;Match' => "+ 3 * 2 4" @ 0 {
+ <expr> => ResizablePMCArray (size:2) [
+ PMC 'Regex;Match' => "3" @ 2,
+ PMC 'Regex;Match' => "* 2 4" @ 4 {
+ <expr> => ResizablePMCArray (size:2) [
+ PMC 'Regex;Match' => "2" @ 6,
+ PMC 'Regex;Match' => "4" @ 8
+ ]
+ }
+ ]
+ }
+}
+$
+
View
@@ -50,6 +50,7 @@ SOURCES := \
kea.pir
BUILTINS_PIR := \
+ src/builtins/dup.pir \
src/builtins/say.pir
DOCS := README
View
@@ -8,14 +8,16 @@ dup.pir -- simple implementation of a dup function
.namespace []
.sub main
- .global pmc stack
- stack = new 'ResizablePMCArray'
+ #.global pmc stack
+ #stack = new 'ResizablePMCArray'
.end
.sub 'dup'
- $P0 = pop stack
- push stack, $P0
- push stack, $P0
+ $P0 = new 'Integer'
+ $P0 = 42
+ say 'Dupe!'
+ #push stack, $P0
+ #push stack, $P0
.return ()
.end
View
@@ -26,7 +26,7 @@ method TOP($/) {
method statement($/) {
- my $past := PAST::Op.new( :name('say'), :pasttype('call'), :node( $/ ) );
+ my $past := PAST::Op.new( :name('dup'), :pasttype('call'), :node( $/ ) );
for $<expression> {
$past.push( $( $_ ) );
}
View
@@ -22,7 +22,7 @@ token ws {
}
rule statement {
- 'say' <expression> [ ',' <expression> ]* ';'
+ 'dup' <expression> [ ',' <expression> ]* ';'
{*}
}

0 comments on commit 353eab3

Please sign in to comment.