Sha256: fd0fc2438c828f6d0a29183cb39b81a91887205bfd16b2251ae817b477e2715c

Contents?: true

Size: 1.98 KB

Versions: 23

Compression:

Stored size: 1.98 KB

Contents

# Author::    Eric Crane  (mailto:eric.crane@mac.com)
# Copyright:: Copyright (c) 2019 Eric Crane.  All rights reserved.
#
# If something is true, do something.
#

module GlooLang
  module Verbs
    class If < GlooLang::Core::Verb

      KEYWORD = 'if'.freeze
      KEYWORD_SHORT = 'if'.freeze
      THEN = 'then'.freeze
      MISSING_EXPR_ERR = 'Missing Expression!'.freeze

      #
      # Run the verb.
      #
      def run
        value = value_tokens
        return if value.nil?

        return unless evals_true( value )

        run_then
      end

      #
      # Get the Verb's keyword.
      #
      def self.keyword
        return KEYWORD
      end

      #
      # Get the Verb's keyword shortcut.
      #
      def self.keyword_shortcut
        return KEYWORD_SHORT
      end

      # ---------------------------------------------------------------------
      #    Private functions
      # ---------------------------------------------------------------------

      private

      #
      # Get the list of tokens that represent the parameters
      # of the if command.
      #
      def value_tokens
        value = @tokens.before_token( THEN )
        if value && value.count > 1
          # The first token is the verb, so we drop it.
          value = value[ 1..-1 ]
        else
          @engine.err MISSING_EXPR_ERR
        end

        return value
      end

      #
      # Does the given value evalute to true?
      #
      def evals_true( value )
        eval_result = false
        if value.count.positive?
          expr = GlooLang::Expr::Expression.new( @engine, value )
          result = expr.evaluate
          eval_result = true if result == true
          eval_result = true if result.is_a?( Numeric ) && result != 0
        end

        return eval_result
      end

      #
      # Run the 'then' command.
      #
      def run_then
        cmd = @tokens.expr_after( THEN )
        i = @engine.parser.parse_immediate cmd
        return unless i

        i.run
      end

    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
gloo-lang-1.4.3 lib/gloo_lang/verbs/if.rb
gloo-lang-1.4.2 lib/gloo_lang/verbs/if.rb
gloo-lang-1.4.1 lib/gloo_lang/verbs/if.rb
gloo-lang-1.4.0 lib/gloo_lang/verbs/if.rb
gloo-lang-1.3.2 lib/gloo_lang/verbs/if.rb
gloo-lang-1.3.1 lib/gloo_lang/verbs/if.rb
gloo-lang-1.3.0 lib/gloo_lang/verbs/if.rb
gloo-lang-1.2.8 lib/gloo_lang/verbs/if.rb
gloo-lang-1.2.7 lib/gloo_lang/verbs/if.rb
gloo-lang-1.2.6 lib/gloo_lang/verbs/if.rb
gloo-lang-1.2.5 lib/gloo_lang/verbs/if.rb
gloo-lang-1.2.4 lib/gloo_lang/verbs/if.rb
gloo-lang-1.2.3 lib/gloo_lang/verbs/if.rb
gloo-lang-1.2.2 lib/gloo_lang/verbs/if.rb
gloo-lang-1.2.1 lib/gloo_lang/verbs/if.rb
gloo-lang-1.2.0 lib/gloo_lang/verbs/if.rb
gloo-lang-1.1.0 lib/gloo_lang/verbs/if.rb
gloo-lang-1.0.2 lib/gloo_lang/verbs/if.rb
gloo-lang-1.0.1 lib/gloo_lang/verbs/if.rb
gloo-lang-1.0.0 lib/gloo_lang/verbs/if.rb