3d6 # simplest one 3d6+3 # with a result modifier 3d8r1 # reroll and discard any 1 3d8rlt3 # reroll and discard any lesser than 3 3d8rgt6 # reroll and discard any greater than 6 3d8rgt6+2 # reroll and discard any greater than 6 and add +2 to the final result #### # 3d8r1 modifier_r received: ({}, { die_type => "1d8", rolls => [8, 1, 7] }, "r", 1) # 3d8rgt1 modifier_r received: ({}, { die_type => "1d8", rolls => [4, 8, 1] }, "r", "gt", 1) #### use Marpa::R2; use Data::Dump; # resurces (along with ones on cpan): # http://marpa-guide.github.io/ # http://savage.net.au/Perl-modules/html/marpa.faq/faq.html # http://savage.net.au/Perl-modules/html/marpa.papers/ # https://github.com/choroba/marpa-enhanced-calculator # https://perlmaven.com/marpa-for-building-parsers # https://perlmaven.com/marpa-debugging my $dsl = <<'END_OF_DSL'; #:default ::= action => [values] :default ::= action => [name,values] lexeme default = latm => 1 Dice_Expression ::= Simple_Dice |Dice_with_modifier_x |Dice_with_modifier_r Dice_with_modifier_x ::= Simple_Dice 'x' Die_Modifier_Val action => modifier_x Dice_with_modifier_r ::= Simple_Dice 'r' Die_Modifier_Val action => modifier_r |Simple_Dice 'r' Die_Modifier_Val action => modifier_r Simple_Dice ::= Rolls 'd' Sides action => do_simple_roll Die_Modifier_Val ~ digits Die_Modifier_Comp ~ 'gt' | 'lt' Rolls ~ digits Sides ~ digits digits ~ [\d]+ :discard ~ whitespace whitespace ~ [\s]+ END_OF_DSL my $grammar = Marpa::R2::Scanless::G->new( { source => \$dsl } ); my $input = $ARGV[0] // '6d4x1'; my $value_ref = $grammar->parse( \$input, 'My_Actions' ); print "\n\nFinal result: ";dd $value_ref; sub My_Actions::modifier_r{ print "modifier_r received: ";dd @_; } sub My_Actions::do_simple_roll { my ( undef, $rolls, undef, $sides ) = @_; print "do_simple_roll received: "; dd @_; my $res = []; map{ $die = 1+int(rand($sides)); print "\tRolled : $die\n"; push @$res, $die} 1..$rolls; my $return = { die_type => "1d$sides", rolls => $res}; print "do_simple_roll returning: "; dd $return; return $return; } sub My_Actions::modifier_x { my ( undef, $rolls_ref, $modifier, $modifier_val ) = @_; print "modifier_x received: "; dd @_; #dd ($rolls_ref,$modifier, $modifier_val ); my @descr = @{$rolls_ref->{rolls}}; # some roll need to be exploded while ( 0 < grep{ $_ =~ /^$modifier_val$/ }@descr ){ foreach my $roll( @descr ){ print "\tanalyzing: $roll\n"; if ( $roll == $modifier_val ){ $roll = $roll."x"; print "\t\texploding a die..\n"; my $new = $grammar->parse( \$rolls_ref->{die_type}, 'My_Actions' ); print "\tdo_simple_roll returned: ";dd $new; push @descr, $$$new[1]->{rolls}->[0]; } } } my @numbers = map{ $_=~/(\d+)/; $1 }@descr; my $sum = 0; $sum += $_ for @numbers; my $return = { result => $sum, description => join ' ',@descr}; print "do_roll_with_die_modifier_x returning: "; dd $return; return $return; }