Sha256: d19fdc43f19ace8a2392fd4e5169d3abc9e7f2df98b83fc3aa4a43bbeb152939

Contents?: true

Size: 1.39 KB

Versions: 1

Compression:

Stored size: 1.39 KB

Contents

module Bindy
  module Language

    class << self

      def parse(expression)
        parsed_expression = parser.parse expression
        raise parser.failure_reason unless parsed_expression
        parsed_expression
      end

      private

      def parser
        @parser ||= LanguageParser.new
      end

    end

    class Function < Treetop::Runtime::SyntaxNode
      def evaluate(context)
        context.public_send name, *arguments.map { |a| a.evaluate context }
      end

      def name
        identifier.text_value
      end

      def arguments
        arg_list.respond_to?(:to_a) ? arg_list.to_a : [arg_list]
      end
    end

    class ArgList < Treetop::Runtime::SyntaxNode
      def to_a
        [expression] | (arg_list.respond_to?(:to_a) ? arg_list.to_a : [arg_list])
      end
    end

    class LiteralString < Treetop::Runtime::SyntaxNode
      def evaluate(context)
        text_value
      end
    end

    class LiteralInteger < Treetop::Runtime::SyntaxNode
      def evaluate(context)
        text_value.to_i
      end
    end

    class LiteralFloat < Treetop::Runtime::SyntaxNode
      def evaluate(context)
        text_value.to_f
      end
    end

    class LiteralTrue < Treetop::Runtime::SyntaxNode
      def evaluate(context)
        true
      end
    end

    class LiteralFalse < Treetop::Runtime::SyntaxNode
      def evaluate(context)
        false
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bindy-0.0.1 lib/bindy/language_interpreters.rb