Sha256: ecc36814ea1799f40556620159c280a2f528b800b69ab55a722dac55205a93b2

Contents?: true

Size: 1.7 KB

Versions: 23

Compression:

Stored size: 1.7 KB

Contents

# Author::    Eric Crane  (mailto:eric.crane@mac.com)
# Copyright:: Copyright (c) 2020 Eric Crane.  All rights reserved.
#
# An object with a decimal value.
#

module GlooLang
  module Objs
    class Decimal < GlooLang::Core::Obj

      KEYWORD = 'decimal'.freeze
      KEYWORD_SHORT = 'num'.freeze

      #
      # The name of the object type.
      #
      def self.typename
        return KEYWORD
      end

      #
      # The short name of the object type.
      #
      def self.short_typename
        return KEYWORD_SHORT
      end

      #
      # Set the value with any necessary type conversions.
      #
      def set_value( new_value )
        if new_value.nil?
          self.value = 0.0
          return
        end

        unless new_value.is_a? Numeric
          self.value = @engine.converter.convert( new_value, 'Decimal', 0.0 )
          return
        end

        self.value = new_value.to_f
      end

      # ---------------------------------------------------------------------
      #    Messages
      # ---------------------------------------------------------------------

      #
      # Get a list of message names that this object receives.
      #
      def self.messages
        return super + %w[round]
      end

      #
      # Round the value to a whole value.
      # If a parameter is included in the message,
      # round to the precision given.
      #
      def msg_round
        data = 0
        if @params&.token_count&.positive?
          expr = GlooLang::Expr::Expression.new( @engine, @params.tokens )
          data = expr.evaluate.to_i
        end

        i = self.value.round( data )
        set_value i
        @engine.heap.it.set_to i
        return i
      end

    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

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