Sha256: 3128f668410b16d3bb6628c375a5534441925728025a6538c43b8ea17d19ee77

Contents?: true

Size: 1.33 KB

Versions: 1

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true

require_relative '../datatype/all_datatypes'

module Loxxy
  module BackEnd
    # rubocop: disable Style/AccessorGrouping
    # Representation of a Lox function.
    # It is a named slot that can be associated with a value at the time.
    class Function
      # @return [String]
      attr_reader :name

      # @return [Array<>] the parameters
      attr_reader :parameters
      attr_reader :body
      attr_reader :stack

      # Create a variable with given name and initial value
      # @param aName [String] The name of the variable
      # @param aValue [Datatype::BuiltinDatatype] the initial assigned value
      def initialize(aName, parameterList, aBody, aStack)
        @name = aName.dup
        @parameters = parameterList
        @body = aBody
        @stack = aStack
      end

      def accept(_visitor)
        stack.push self
      end

      def call(aVisitor)
        body.empty? ? Datatype::Nil.instance : body.accept(aVisitor)
      end

      # Logical negation.
      # As a function is a truthy thing, its negation is thus false.
      # @return [Datatype::False]
      def !
        Datatype::False.instance
      end

      # Text representation of a Lox function
      def to_str
        "<fn #{name}>"
      end
    end # class
    # rubocop: enable Style/AccessorGrouping
  end # module
end # module

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
loxxy-0.1.04 lib/loxxy/back_end/function.rb