Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 46 lines (39 sloc) 1.189 kb
eb5b2d0 @masak [examples/algebra] imported example showing off OPTable
authored
1 use v6;
2 use GGE::Exp;
3 use GGE::OPTable;
4
5 class Algebra::Literal is GGE::Exp does ShowContents {
6 method evaluate() {
7 +$.ast;
8 }
9 }
10
11 class Algebra::BinOp is GGE::Exp does ShowContents {
12 }
13
14 class Algebra::Addition is Algebra::BinOp {
15 method evaluate() {
16 $.llist[0].evaluate() + $.llist[1].evaluate();
17 }
18 }
19
20 class Algebra::Multiplication is Algebra::BinOp {
21 method evaluate() {
22 $.llist[0].evaluate() * $.llist[1].evaluate();
23 }
24 }
25
26 given GGE::OPTable.new() -> $optable {
3c2f61b @masak [GGE::OPTable] can now use PGE regexes in :parsed
authored
27 $optable.newtok('term:', :precedence<=>,
28 :parsed(&Perl6::Grammar::number),
29 :match(Algebra::Literal));
30 $optable.newtok('infix:+', :looser<term:>,
31 :match(Algebra::Addition));
eb5b2d0 @masak [examples/algebra] imported example showing off OPTable
authored
32 $optable.newtok('infix:*', :tighter<infix:+>,
33 :match(Algebra::Multiplication));
34
35 while prompt('> ') -> $input {
36 my $match = $optable.parse($input);
37 if $match.to < $input.chars {
38 say 'Could not parse the arithmetic expression';
39 next;
40 }
41 my $expr = $match<expr>;
42 say $expr.evaluate;
43 }
44 say '';
45 }
Something went wrong with that request. Please try again.