Sha256: 628ab39f431aae2701ab1793e75e4e3fe3b4b270e51b40bf0929453ed9aaf2ee
Contents?: true
Size: 1.38 KB
Versions: 6
Compression:
Stored size: 1.38 KB
Contents
# frozen_string_literal: true require_relative '../datatype/all_datatypes' module Loxxy module BackEnd # Runtime representation of a Lox object (instance). class LoxInstance # @return BackEnd::LoxClass] the class that this object is an instance of attr_reader :klass attr_reader :engine # @return [Hash{String => BuiltinDatatype | LoxFunction | LoxInstance }] attr_reader :fields # Create an instance from given class # @param aClass [BackEnd::LoxClass] the class this this object belong def initialize(aClass, anEngine) @klass = aClass @engine = anEngine @fields = {} end def accept(_visitor) engine.stack.push self end # Text representation of a Lox instance def to_str "#{klass.to_str} instance" end # Look up the value of property with given name # aName [String] name of object property def get(aName) return fields[aName] if fields.include? aName method = klass.find_method(aName) unless method raise Loxxy::RuntimeError, "Undefined property '#{aName}'." end method.bind(self) end # Set the value of property with given name # aName [String] name of object property def set(aName, aValue) fields[aName] = aValue end end # class end # module end # module
Version data entries
6 entries across 6 versions & 1 rubygems