Sha256: 811178d84636ee61dc09b7827894147788915caf2356e9cc1d4f86f5e7f071b7

Contents?: true

Size: 1.58 KB

Versions: 6

Compression:

Stored size: 1.58 KB

Contents

# Author::    Eric Crane  (mailto:eric.crane@mac.com)
# Copyright:: Copyright (c) 2019 Eric Crane.  All rights reserved.
#
# Create an object, optionally of a type.
#

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

      KEYWORD = 'create'.freeze
      KEYWORD_SHORT = '`'.freeze
      AS = 'as'.freeze
      VAL = ':'.freeze
      NO_NAME_ERR = 'Object name is missing!'.freeze

      #
      # Run the verb.
      #
      def run
        name = @tokens.second
        type = @tokens.after_token( AS )
        value = @tokens.after_token( VAL )

        unless name
          $engine.err NO_NAME_ERR
          return
        end
        create name, type, value
      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

      #
      # Create an object with given name of given type with
      # the given initial value.
      #
      def create( name, type, value )
        if GlooLang::Expr::LString.string?( value )
          value = GlooLang::Expr::LString.strip_quotes( value )
        end
        obj = $engine.factory.create( { name: name, type: type, value: value } )

        obj.add_default_children if obj&.add_children_on_create?
        $engine.heap.it.set_to value
      end

    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
gloo-lang-0.9.8 lib/gloo_lang/verbs/create.rb
gloo-lang-0.9.7 lib/gloo_lang/verbs/create.rb
gloo-lang-0.9.6 lib/gloo_lang/verbs/create.rb
gloo-lang-0.9.5 lib/gloo_lang/verbs/create.rb
gloo-lang-0.9.4 lib/gloo_lang/verbs/create.rb
gloo-lang-0.9.3 lib/gloo_lang/verbs/create.rb