Sha256: 527d73ddc777d3b02f61e939d99f1733211dbc034a5754023ac296610ad73de1

Contents?: true

Size: 1.49 KB

Versions: 2

Compression:

Stored size: 1.49 KB

Contents

# A grammar for mathematical formulas that apply the basic four operations to
# non-negative numbers (integers and floats), respecting operator precedence and
# ignoring whitespace.
grammar Calc
  rule term
    additive | factor
  end

  rule additive
    (factor operator:additive_op term) {
      def value
        operator.apply(factor.value, term.value)
      end
    }
  end

  rule factor
    multiplicative | primary
  end

  rule multiplicative
    (primary operator:multiplicative_op factor) {
      def value
        operator.apply(primary.value, factor.value)
      end
    }
  end

  rule primary
    term_paren | number
  end

  rule term_paren
    (lparen term rparen) {
      def value
        term.value
      end
    }
  end

  rule additive_op
    (plus | minus) {
      def apply(factor, term)
        text.strip == '+' ? factor + term : factor - term
      end
    }
  end

  rule multiplicative_op
    (star | slash) {
      def apply(primary, factor)
        text.strip == '*' ? primary * factor : primary / factor
      end
    }
  end

  rule number
    float | integer
  end

  rule float
    ([0-9]+ '.' [0-9]+ space) {
      def value
        text.strip.to_f
      end
    }
  end

  rule integer
    ([0-9]+ space) {
      def value
        text.strip.to_i
      end
    }
  end

  rule lparen  '(' space  end
  rule rparen  ')' space  end
  rule plus    '+' space  end
  rule minus   '-' space  end
  rule star    '*' space  end
  rule slash   '/' space  end

  rule space
    [ \t\n\r]*
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
citrus-1.4.0 examples/calc.citrus
citrus-1.3.0 examples/calc.citrus