Sha256: 84c97b08e4c79a8a774e1fd9895d0a6a63846c8e4d2cb80403aed3ea71c55295

Contents?: true

Size: 1.08 KB

Versions: 11

Compression:

Stored size: 1.08 KB

Contents

module Unitwise
  module Expression
    # Transformer is responsible for turning a Unitwise::Expression::Parser
    # hash result into a collection of Unitwise::Terms.
    class Transformer < Parslet::Transform

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

      rule(:prefix_code => simple(:c)) { |x| Prefix.find(x[:c], x[:mode]) }
      rule(:atom_code => simple(:c))   { |x| Atom.find(x[:c], x[: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) }) do
        n ** e
      end

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

Version data entries

11 entries across 11 versions & 2 rubygems

Version Path
unitwise-2.3.0 lib/unitwise/expression/transformer.rb
unitwise-2.2.0 lib/unitwise/expression/transformer.rb
unitwise-2.1.0 lib/unitwise/expression/transformer.rb
unitwise-2.0.0 lib/unitwise/expression/transformer.rb
unitwise-1.1.0 lib/unitwise/expression/transformer.rb
unitwise-193-1.0.4 lib/unitwise/expression/transformer.rb
unitwise-1.0.4 lib/unitwise/expression/transformer.rb
unitwise-1.0.3 lib/unitwise/expression/transformer.rb
unitwise-1.0.2 lib/unitwise/expression/transformer.rb
unitwise-1.0.1 lib/unitwise/expression/transformer.rb
unitwise-1.0.0 lib/unitwise/expression/transformer.rb