Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Roll the Dice with Crapshoot

Roll the Dice with Crapshoot

Parsing and rolling mathematical expressions for pen & paper gaming.

Bryce "BonzoESC" Kerley

April 08, 2011
Tweet

More Decks by Bryce "BonzoESC" Kerley

Other Decks in Programming

Transcript

  1. Roll The Dice with Crapshoot Bryce Kerley Scottish Ruby Conference

    Lightning Talks April 8, 2011 Tuesday, October 2, 12
  2. Dice Code • 2d4 • Two four-sided dice • 1d100

    + 8 • One hundred-sided die plus eight • 4d6v • Four six-sided dice, minus the lowest Tuesday, October 2, 12
  3. Dice Language A programming language is […] designed to express

    computations, […] to express algorithms precisely, or as a mode of human communication. http://en.wikipedia.org/wiki/Programming_language Tuesday, October 2, 12
  4. Regular Language In computer science, a regular language is a

    formal language […] that satisfies the following equivalent properties: • it can be accepted by a deterministic finite state machine •it can be generated by a regular grammar http://en.wikipedia.org/wiki/Regular_language Tuesday, October 2, 12
  5. Dice Language Number = digit+; Constant = Number; Drop =

    ('^' | 'v'); Series = Number 'd' Number Drop?; Arithmetic = ('+' | '-'); UnaryExpression = Series | Constant; BinaryExpression = UnaryExpression (space* Arithmetic space* UnaryExpression)+; Expression = UnaryExpression | BinaryExpression; 100% CERTIFIED REGULAR LANGUAGE Tuesday, October 2, 12
  6. Ragel Ragel compiles executable finite state machines from regular languages.

    Ragel targets C, C++, Objective-C, D, Java and Ruby. http://www.complang.org/ragel/ Tuesday, October 2, 12
  7. scan.rl %%{ machine scanner; action _number { @mark_num = p

    } action number { @num_stack.push atos(data[@mark_num..p-1]) } action constant { @tokens << Tokens::Constant.new(@num_stack.pop) } action series { drop = @drop_current @drop_current = nil sides = @num_stack.pop count = @num_stack.pop @tokens << Tokens::Series.new(count, sides, drop) } action arithmetic { @tokens << Tokens::Arithmetic.new(data[p-1].chr) } action drop { @drop_current = data[p-1].chr } Number = digit+ >_number %number; Constant = Number %constant; Drop = ('^' | 'v') %drop; Series = Number 'd' Number Drop? %series; Arithmetic = ('+' | '-') %arithmetic; UnaryExpression = Series | Constant; BinaryExpression = UnaryExpression (space* Arithmetic space* UnaryExpression)+; Expression = UnaryExpression | BinaryExpression; main := Expression; }%% Actions Language Tuesday, October 2, 12
  8. Ragel         

          !   " #  $    % & '        !   #(    #(    ! #(   )     !  * ) +   *  )    ) scan.rl: 62 lines scan.rb: 330 lines scan.pdf: priceless Tuesday, October 2, 12
  9. scan.rl action constant { @tokens << Tokens::Constant.new(@num_stack.pop) } action series

    { drop = @drop_current @drop_current = nil sides = @num_stack.pop count = @num_stack.pop @tokens << Tokens::Series.new(count, sides, drop) } action arithmetic { @tokens << Tokens::Arithmetic.new(data[p-1].chr) } Transform constant, series, and arithmetic elements into Token objects Tuesday, October 2, 12
  10. Tokens Token#eval Turn token into result during evaluation Token#independent Determine

    if token needs the next token during postfixing Token#inspect <Crapshoot::Tokens::Series dice=4d6 drop=nothing> Tuesday, October 2, 12
  11. Parsed Dice 4d6v count: 4 sides: 6 drop: ‘v’ 3d10^

    count: 3 sides: 10 drop: ‘^’ 2d6 count: 2 sides: 6 drop: nil 300 value: 300 + - - Tuesday, October 2, 12
  12. Infix & Postfix Infix 1 + 2 + 3 +

    4 Used for centuries Hard for computers Tuesday, October 2, 12
  13. Infix & Postfix Infix 1 + 2 + 3 +

    4 Used for centuries Hard for computers Postfix 1 2 + 3 + 4 + Easy for computers Convertible from infix Tuesday, October 2, 12
  14. Parsed Dice 4d6v count: 4 sides: 6 drop: ‘v’ 3d10^

    count: 3 sides: 10 drop: ‘^’ 2d6 count: 2 sides: 6 drop: nil 300 value: 300 + - - Tuesday, October 2, 12
  15. Postfixed Dice 4d6v count: 4 sides: 6 drop: ‘v’ 3d10^

    count: 3 sides: 10 drop: ‘^’ 2d6 count: 2 sides: 6 drop: nil 300 value: 300 + - - Tuesday, October 2, 12
  16. Postfix Evaluation • Series • Roll dice • Do drop

    • Push result to stack Tuesday, October 2, 12
  17. Postfix Evaluation • Series • Roll dice • Do drop

    • Push result to stack • Constant Tuesday, October 2, 12
  18. Postfix Evaluation • Series • Roll dice • Do drop

    • Push result to stack • Constant • Push value to stack Tuesday, October 2, 12
  19. Postfix Evaluation • Series • Roll dice • Do drop

    • Push result to stack • Constant • Push value to stack • Arithmetic Tuesday, October 2, 12
  20. Postfix Evaluation • Series • Roll dice • Do drop

    • Push result to stack • Constant • Push value to stack • Arithmetic • Pop twice Tuesday, October 2, 12
  21. Postfix Evaluation • Series • Roll dice • Do drop

    • Push result to stack • Constant • Push value to stack • Arithmetic • Pop twice • Push result Tuesday, October 2, 12
  22. Postfix Evaluation 4d6v count: 4 sides: 6 drop: ‘v’ 3d10^

    count: 3 sides: 10 drop: ‘^’ 2d6 count: 2 sides: 6 drop: nil 300 value: 300 + - - Stack Tuesday, October 2, 12
  23. Postfix Evaluation 15 3d10^ count: 3 sides: 10 drop: ‘^’

    2d6 count: 2 sides: 6 drop: nil 300 value: 300 + - - Stack Tuesday, October 2, 12
  24. Postfix Evaluation 15 3d10^ count: 3 sides: 10 drop: ‘^’

    2d6 count: 2 sides: 6 drop: nil 300 value: 300 + - - Stack Tuesday, October 2, 12
  25. Postfix Evaluation 15 5 2d6 count: 2 sides: 6 drop:

    nil 300 value: 300 + - - Stack Tuesday, October 2, 12
  26. Postfix Evaluation 15 5 2d6 count: 2 sides: 6 drop:

    nil 300 value: 300 + - - Stack Tuesday, October 2, 12
  27. Postfix Evaluation 15 5 2d6 count: 2 sides: 6 drop:

    nil 300 value: 300 + - - Stack Tuesday, October 2, 12
  28. Postfix Evaluation 20 2d6 count: 2 sides: 6 drop: nil

    300 value: 300 - - Stack Tuesday, October 2, 12
  29. Operator Precedence Harder than no operator precedence Push lame operators

    like + and - into a stack Tuesday, October 2, 12