Sha256: ea569bc0fbd2955a3cedd60628200aa585da5264ff3d8e007d28e76588a3263b

Contents?: true

Size: 891 Bytes

Versions: 2

Compression:

Stored size: 891 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_by(ctx[:key], ctx[:c]) }
      rule(atom_code: simple(:c))   { |ctx| Atom.find_by(ctx[:key], ctx[:c]) }
      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

2 entries across 2 versions & 1 rubygems

Version Path
unitwise-0.3.2 lib/unitwise/expression/transformer.rb
unitwise-0.3.1 lib/unitwise/expression/transformer.rb