Sha256: 4f1878dc332bf14999fbc5ec9d617b72d6288e81bcfa4f605b557851c1e0d272

Contents?: true

Size: 947 Bytes

Versions: 3

Compression:

Stored size: 947 Bytes

Contents

module Unitwise
  module Expression
    class Transformer < Parslet::Transform

      rule(:integer => simple(:i)) { i.to_i }
      rule(:fixnum => simple(:f)) { f.to_f }

      rule(:prefix_code => simple(:c)) { |ctx| Prefix.find(ctx[:c], ctx[:mode]) }
      rule(:atom_code => simple(:c))   { |ctx| Atom.find(ctx[:c], ctx[:mode]) }
      rule(:term => subtree(:h))       { Term.new(h) }

      rule(:operator => simple(:o), :right => simple(:r)) do
        o == '/' ? r ** -1 : r
      end

      rule(:left => simple(:l), :operator => simple(:o), :right => simple(:r)) do
        o == '/' ? l / r : l * r
      end

      rule(:left => simple(:l)) { l }

      rule(:group => { :factor => simple(:f) , :nested => simple(:n), :exponent => simple(:e) }) do
        ( n ** e ) * f
      end
      rule(:group => { :nested => simple(:n) , :exponent => simple(:e)}) { n ** e }

      rule(:group => { :nested => simple(:n) }) { n }
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
unitwise-0.10.0 lib/unitwise/expression/transformer.rb
unitwise-0.9.1 lib/unitwise/expression/transformer.rb
unitwise-0.9.0 lib/unitwise/expression/transformer.rb