Sha256: 1957482ecd07d23cc1451273f8f6ec8ebd25bf36197ebc5b87831f194ed98179

Contents?: true

Size: 1.31 KB

Versions: 19

Compression:

Stored size: 1.31 KB

Contents

module Finitio
  module Syntax
    module Expr
      include Node

      WORLD_VAR = "__world"

      def self.included(by)
        by.extend(Node::ClassHelpers)
      end

      def to_proc()
        src = to_proc_src
        ::Kernel.eval(src)
      end

      def to_proc_src
        initializer = free_variables
          .map{|v| "#{v} = __world.fetch(:#{v})" }
          .join("\n")
        <<-SRC.gsub(/^[ ]{10}/m,'')
          ->(__world){
            unless __world.is_a?(Hash) && __world.keys.all?{|k| k.is_a?(Symbol) }
              raise "Invalid world: " + __world.inspect
            end
            #{initializer}
            #{to_proc_source}
          }
        SRC
      end

      def to_proc_source
        raise NotImplementedError
      end

      def free_variables
        [].tap{|fvs| _free_variables(fvs) }.uniq
      end

      def _free_variables(fvs)
        raise NotImplementedError
      end

    end # module Expr
  end # module Syntax
end # module Finitio
require_relative 'expr/logic_dyadic'
require_relative 'expr/logic_not'
require_relative 'expr/comparison'
require_relative 'expr/arith_op'
require_relative 'expr/unary_minus_op'
require_relative 'expr/oo_call'
require_relative 'expr/fn_call'
require_relative 'expr/identifier'
require_relative 'expr/literal'
require_relative 'expr/parenthesized'

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
finitio-0.12.0 lib/finitio/syntax/expr.rb
finitio-0.11.4 lib/finitio/syntax/expr.rb
finitio-0.11.3 lib/finitio/syntax/expr.rb
finitio-0.11.2 lib/finitio/syntax/expr.rb
finitio-0.11.1 lib/finitio/syntax/expr.rb
finitio-0.10.0 lib/finitio/syntax/expr.rb
finitio-0.9.1 lib/finitio/syntax/expr.rb
finitio-0.9.0 lib/finitio/syntax/expr.rb
finitio-0.8.0 lib/finitio/syntax/expr.rb
finitio-0.7.0 lib/finitio/syntax/expr.rb
finitio-0.7.0.pre.rc4 lib/finitio/syntax/expr.rb
finitio-0.7.0.pre.rc3 lib/finitio/syntax/expr.rb
finitio-0.7.0.pre.rc2 lib/finitio/syntax/expr.rb
finitio-0.7.0.pre.rc1 lib/finitio/syntax/expr.rb
finitio-0.6.1 lib/finitio/syntax/expr.rb
finitio-0.6.0 lib/finitio/syntax/expr.rb
finitio-0.5.2 lib/finitio/syntax/expr.rb
finitio-0.5.1 lib/finitio/syntax/expr.rb
finitio-0.5.0 lib/finitio/syntax/expr.rb