vm

package
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 22, 2017 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var BuiltinClassMethods = []*BuiltInMethod{
	{
		Name: "include",
		Fn: func(receiver Object) builtinMethodBody {
			return func(vm *VM, args []Object, blockFrame *callFrame) Object {
				module := args[0].(*RClass)
				class := receiver.(*RClass)
				module.superClass = class.superClass
				class.superClass = module

				return class
			}
		},
	},
	{
		Name: "new",
		Fn: func(receiver Object) builtinMethodBody {
			return func(vm *VM, args []Object, blockFrame *callFrame) Object {

				class := receiver.(*RClass)
				instance := class.initializeInstance()
				initMethod := class.lookupInstanceMethod("initialize")

				if initMethod != nil {
					instance.InitializeMethod = initMethod.(*Method)
				}

				return instance
			}
		},
	},
	{
		Name: "name",
		Fn: func(receiver Object) builtinMethodBody {
			return func(vm *VM, args []Object, blockFrame *callFrame) Object {

				name := receiver.(Class).ReturnName()
				nameString := initializeString(name)
				return nameString
			}
		},
	},
	{
		Name: "superclass",
		Fn: func(receiver Object) builtinMethodBody {
			return func(vm *VM, args []Object, blockFrame *callFrame) Object {

				return receiver.(Class).returnSuperClass()
			}
		},
	},
}

BuiltinClassMethods is a collection of class methods used by Class

Functions

This section is empty.

Types

type ArrayObject

type ArrayObject struct {
	Class    *RArray
	Elements []Object
}

ArrayObject represents instance from Array class. An array is a collection of different objects that are ordered and indexed. Elements in an array can belong to any class.

func (*ArrayObject) Inspect

func (a *ArrayObject) Inspect() string

Inspect returns detailed info of a array include elements it contains

type BaseClass

type BaseClass struct {
	// Name is the class's name
	Name string
	// Methods contains its instances' methods
	Methods *environment
	// ClassMethods contains this class's methods
	ClassMethods *environment

	// Class points to this class's class, which should be ClassClass
	Class *RClass
	// Singleton is a flag marks if this class a singleton class
	Singleton bool
	// contains filtered or unexported fields
}

BaseClass is a embedded struct that contains all the essential fields for a class

func (*BaseClass) Inspect

func (c *BaseClass) Inspect() string

Inspect returns the basic inspected result (which is class name) of current class TODO: Singleton class's inspect() should also mark if it's a singleton class explicitly.

func (*BaseClass) ReturnName

func (c *BaseClass) ReturnName() string

ReturnName returns the name of the class

type BaseObject

type BaseObject interface {
	Object
	// contains filtered or unexported methods
}

BaseObject is an interface that implements basic functions any object requires.

type BooleanObject

type BooleanObject struct {
	Class *RBool
	Value bool
}

BooleanObject represents boolean object in goby. It includes `true` and `FALSE` which represents logically true and false value.

var (

	// TRUE is shared boolean object that represents true
	TRUE *BooleanObject
	// FALSE is shared boolean object that represents false
	FALSE *BooleanObject
)

func (*BooleanObject) Inspect

func (b *BooleanObject) Inspect() string

Inspect returns boolean object's value, which is either true or false.

type BuiltInMethod

type BuiltInMethod struct {
	Name string
	Fn   func(receiver Object) builtinMethodBody
}

BuiltInMethod represents methods defined in go.

func (*BuiltInMethod) Inspect

func (bim *BuiltInMethod) Inspect() string

type Class

type Class interface {
	ReturnName() string

	BaseObject
	// contains filtered or unexported methods
}

Class is an interface that implements a class's basic functions. - lookupClassMethod: search for current class's class method with given name. - lookupInstanceMethod: search for current class's instance method with given name. - ReturnName returns class's name

type Error

type Error struct {
	Class   *ErrorClass
	Message string
}

func (*Error) Inspect

func (e *Error) Inspect() string

type ErrorClass

type ErrorClass struct {
	*BaseClass
}

type HashObject

type HashObject struct {
	Class *RHash
	Pairs map[string]Object
}

HashObject represents hash instances

func (*HashObject) Inspect

func (h *HashObject) Inspect() string

func (*HashObject) Length

func (h *HashObject) Length() int

type IntegerObject

type IntegerObject struct {
	Class *RInteger
	Value int
}

IntegerObject represents number objects which can bring into mathematical calculations.

```ruby 1 + 1 # => 2 2 * 2 # => 4 ```

func (*IntegerObject) Inspect

func (i *IntegerObject) Inspect() string

type Method

type Method struct {
	Name string
	// contains filtered or unexported fields
}

Method represents methods defined using goby.

func (*Method) Inspect

func (m *Method) Inspect() string

type Null

type Null struct {
	Class *RNull
}
var (
	NULL *Null
)

func (*Null) Inspect

func (n *Null) Inspect() string

type Object

type Object interface {
	Inspect() string
	// contains filtered or unexported methods
}

type Pointer

type Pointer struct {
	Target Object
}

type RArray

type RArray struct {
	*BaseClass
}

RArray is the built in array class

type RBool

type RBool struct {
	*BaseClass
}

RBool is the built in class of goby's boolean objects.

type RClass

type RClass struct {
	*BaseClass
}

RClass represents normal (not built in) class object

type RHash

type RHash struct {
	*BaseClass
}

RHash is the class of hash objects

type RInteger

type RInteger struct {
	*BaseClass
}

RInteger is integer class

type RNull

type RNull struct {
	*BaseClass
}

type RObject

type RObject struct {
	Class             *RClass
	InstanceVariables *environment
	InitializeMethod  *Method
}

RObject represents any non built-in class's instance.

func (*RObject) Inspect

func (ro *RObject) Inspect() string

type RString

type RString struct {
	*BaseClass
}

RString is the built in string class

type StringObject

type StringObject struct {
	Class *RString
	Value string
}

StringObject represents string instances

func (*StringObject) Inspect

func (s *StringObject) Inspect() string

type VM

type VM struct {
	// contains filtered or unexported fields
}

VM represents a stack based virtual machine.

func New

func New(fileDir string, args []string) *VM

New initializes a vm to initialize state and returns it.

func (*VM) ExecBytecodes

func (vm *VM) ExecBytecodes(bytecodes, fn string)

ExecBytecodes accepts a sequence of bytecodes and use vm to evaluate them.

func (*VM) GetExecResult

func (vm *VM) GetExecResult() Object

GetExecResult returns stack's top most value. Normally it's used in tests.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL