src

package
v0.0.0-...-c99806b Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const FrameMax = 64

FrameMax represents the maximum number of call frames.

View Source
const StackMax = 256

StackMax represents the maximum size of the stack.

View Source
const TableMaxLoad float32 = 0.75

TableMaxLoad is the maximum load factor for the table.

View Source
const Uint8Count = StackMax

Uint8Count represents the maximum number of local variables.

Variables

This section is empty.

Functions

func AddConstants

func AddConstants(chunk *Chunk, val Value) int

AddConstants adds a constant value to the chunk's list of constants.

Parameters: - chunk: a pointer to the Chunk struct representing the chunk of bytecode. - val: the Value to be added to the constants list.

Returns: - int: the index of the added constant in the constants list.

func AsBool

func AsBool(value Value) bool

AsBool returns the boolean value of the given Value.

It takes a single parameter: - value: the Value to convert to a boolean.

It returns a boolean value.

func AsCString

func AsCString(value Value) string

AsCString returns a string representation of a given Value as a C string.

It takes a Value as a parameter. It returns a string.

func AsNumber

func AsNumber(value Value) float64

AsNumber returns the value of the input parameter as a float64.

value: The value to be converted. Returns: The value as a float64.

func DisassembleChunk

func DisassembleChunk(chunk *Chunk, name string)

DisassembleChunk prints the disassembled instructions of a given chunk.

The function takes a pointer to a Chunk struct and a name string as parameters. It does not return any value.

func DisassembleInstruction

func DisassembleInstruction(chunk *Chunk, offset int) int

DisassembleInstruction disassembles an instruction in the given chunk at the specified offset.

Parameters: - chunk: A pointer to the Chunk struct representing the chunk of code. - offset: An integer representing the offset of the instruction in the chunk.

Return: - An integer representing the new offset after processing the instruction.

func Error

func Error(message string)

Error is a function that handles errors and logs them.

It takes a message string as a parameter and calls the errorAt function passing the address of the parser.Previous variable and the Error message.

func FreeArray

func FreeArray(array any, cap int)

FreeArray releases the memory occupied by the given array.

The function takes two parameters: - `array` of type `any`, which is the array to be freed. - `cap` of type `int`, which is the capacity of the array.

The function does not return anything.

func FreeChunk

func FreeChunk(chunk *Chunk)

FreeChunk frees the memory allocated for a given Chunk.

It takes a pointer to a Chunk as its parameter. The function does not return anything.

func FreeObjects

func FreeObjects(object *Obj)

FreeObjects frees all objects in the linked list starting from the given object.

object: a pointer to the first object in the linked list.

func FreeVM

func FreeVM()

FreeVM frees the virtual machine by calling the Freetable method on the vm.strings and vm.globals variables, and the FreeObjects function on the vm.objects variable.

No parameters. No return value.

func FreeValueArray

func FreeValueArray(array *ValueArray)

FreeValueArray frees the memory allocated for the values in the given ValueArray.

It takes a pointer to a ValueArray as a parameter. There is no return value.

func GrowArrayChunks

func GrowArrayChunks(code []uint8, oldcap, newcap int) []uint8

GrowArrayChunks is a Go function that takes in a code slice of uint8, along with the oldcap and newcap as integers.

It returns a new code slice of the same type ([]uint8).

func GrowArrayLines

func GrowArrayLines(lines []int, oldcap, newcap int) []int

GrowArrayLines returns a new slice with the same elements as the original slice, but with a larger capacity.

It takes in three parameters: - lines: a slice of integers representing the original array - oldcap: an integer representing the old capacity of the array - newcap: an integer representing the new capacity of the array

It returns a new slice of integers with the same elements as the original slice, but with a larger capacity.

func GrowCapacity

func GrowCapacity(oldcap int) int

GrowCapacity returns the new capacity after growing the old capacity.

oldcap - the old capacity (int) Returns the new capacity (int)

func InitChunk

func InitChunk(chunk *Chunk)

InitChunk initializes a Chunk.

The function takes a pointer to a Chunk struct as its parameter. It sets the Capacity and Count fields of the Chunk to 0. It sets the Lines and Code fields of the Chunk to nil. It initializes the Constants field of the Chunk using the InitValueArray function.

func InitCompiler

func InitCompiler(compiler *Compiler, _type FunctionType)

InitCompiler initializes the compiler.

It takes a pointer to a Compiler struct as a parameter.

func InitVM

func InitVM()

InitVM initializes the virtual machine.

It resets the stack, clears the objects, and initializes the strings and globals tables.

func InitValueArray

func InitValueArray(array *ValueArray)

InitValueArray initializes the given ValueArray.

Takes a pointer to a ValueArray as a parameter. Does not return anything.

func IsBool

func IsBool(value Value) bool

IsBool checks if the given value is a boolean.

value: the value to be checked. bool: true if the value is a boolean, false otherwise.

func IsFunction

func IsFunction(value Value) bool

IsFunction checks if the given value is a function.

value Value bool

func IsNil

func IsNil(value Value) bool

IsNil checks if the given value is nil.

It takes a parameter of type Value and returns a boolean value.

func IsNumber

func IsNumber(value Value) bool

IsNumber checks if the given value is of type number.

value: the value to be checked. bool: true if the value is of type number, false otherwise.

func IsObjType

func IsObjType(value Value, objType ObjType) bool

IsObjType checks if the value is of a specific object type.

Parameters: - value: The value to check. - objType: The object type to compare against.

Returns: - bool: True if the value is of the specified object type, false otherwise.

func IsString

func IsString(value Value) bool

IsString checks if the given value is a string.

value: The value to check. Returns: A boolean indicating if the value is a string.

func IsValObj

func IsValObj(value Value) bool

IsValObj checks if the given value is of type ValObj

value: the value to be checked. Returns: true if the value is of type ValObjStr, false otherwise.

func PrintValue

func PrintValue(value Value)

PrintValue prints the value of a given Value object.

It takes a Value object as a parameter and prints its value based on its type:

func Reallocate

func Reallocate(pointer interface{}, oldSize, newSize int) interface{}

Reallocate reallocates the memory of a pointer to a new size.

It takes in three parameters: - pointer: the pointer to reallocate the memory for. - oldSize: the current size of the memory block. - newSize: the new size of the memory block.

It returns an interface{} which is the reallocated pointer.

func WriteChunk

func WriteChunk(chunk *Chunk, bytecode uint8, line int)

WriteChunk writes a bytecode to the given chunk at the specified line.

Parameters: - chunk: A pointer to the Chunk struct that represents the chunk. - bytecode: The bytecode to be written to the chunk. - line: The line number where the bytecode is written.

func WriteValueArray

func WriteValueArray(array *ValueArray, val Value)

WriteValueArray writes a value to the given ValueArray.

It takes a pointer to a ValueArray and a Value as parameters. The ValueArray is dynamically resized if its capacity is not sufficient to accommodate the new value. After writing the value, the count of the ValueArray is incremented by 1.

Types

type CallFrame

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

func (*CallFrame) ReadByteVM

func (frame *CallFrame) ReadByteVM() uint8

ReadByteVM reads a single byte from the VM's instruction pointer.

No parameters. Returns a uint8 value.

func (*CallFrame) ReadConstant

func (frame *CallFrame) ReadConstant() Value

ReadConstant retrieves a constant value from the chunk's constant pool.

It reads a byte from the VM's instruction stream and uses it as an index into the constant pool. The constant value at that index is then returned. The constant pool is stored in the `Values` field of the `Constants` field of the `chunk` field of the `VM` struct.

Returns the constant value retrieved from the constant pool.

func (*CallFrame) ReadShort

func (frame *CallFrame) ReadShort() uint16

ReadShort reads a 16-bit value from the VM's instruction stream.

type Chunk

type Chunk struct {
	Code      []uint8    // The bytecode of the chunk.
	Constants ValueArray // The constants of the chunk.
	Lines     []int      // The line numbers of the chunk.
	Count     int        // The number of instructions in the chunk.
	Capacity  int        // The capacity of the chunk.
}

Chunk represents a chunk of bytecode.

type Compiler

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

Compiler represents a compiler object.

type Entry

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

Entry is a struct representing an entry in the table.

func GrowArrayEntries

func GrowArrayEntries(entries []Entry, oldcap, newcap int) []Entry

GrowArrayEntries returns a new slice of Entry with a larger capacity.

The function takes in the following parameters: - entries: a slice of Entry that contains the current entries. - oldcap: an integer that represents the old capacity of the slice. - newcap: an integer that represents the new capacity of the slice.

The function returns a new slice of Entry with the updated capacity.

type FunctionType

type FunctionType int

FunctionType represents the type of a function.

const (
	// FunctionTypeNative represents a native function.
	TypeFunction FunctionType = iota
	TypeScript
)

type InterpretResult

type InterpretResult int

InterpretResult represents the result of an interpretation.

const (
	// InterpretOk indicates successful interpretation
	InterpretOk InterpretResult = iota

	// InterpretCompileError indicates a compilation Error during interpretation
	InterpretCompileError

	// InterpretRuntimeError indicates a runtime Error during interpretation
	InterpretRuntimeError
)

func Interpret

func Interpret(source string) InterpretResult

Interpret interprets the given source code and returns the interpretation result.

Parameters: - source: The source code to be interpreted as a string.

Return type: - InterpretResult: The result of the interpretation.

type Local

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

Local represents a local variable in the compiler.

type Obj

type Obj struct {
	Type ObjType // The type of the object.
	Next *Obj    // The next object in the list.
}

Obj represents an object in the code.

func AsObj

func AsObj(value Value) *Obj

AsObj returns the *Obj value from the given Value.

It takes a single parameter: - value: the Value to extract the *Obj from.

It returns a *Obj.

type ObjFunction

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

ObjFunction represents a function object in the code.

func AsFunction

func AsFunction(value Value) *ObjFunction

AsFunction returns the ObjFunction from the given Value.

value Value *ObjFunction

func Compile

func Compile(source string, chunk *Chunk) *ObjFunction

Compile compiles the source code into an ObjFunction.

It takes a source string and a pointer to a Chunk as parameters and returns a pointer to an ObjFunction.

func NewFunction

func NewFunction() *ObjFunction

NewFunction initializes and returns a new ObjFunction.

No parameters. Returns a pointer to ObjFunction.

type ObjType

type ObjType int
const (
	ObjStringType ObjType = iota // The type of the string object.
	ObjFunctionType
)

func OBJType

func OBJType(value Value) ObjType

OBJStrType returns the ObjType of the given Value.

It takes a single parameter: - value: the Value to determine the ObjType for.

It returns the ObjType of the given Value.

type ObjectString

type ObjectString struct {
	Obj    Obj    // The object representing the string.
	Length int    // The length of the string.
	Chars  []byte // The byte array of characters composing the string.
	Hash   uint32 // The hash value of the string.
}

ObjectString represents a string object in the code.

func AsObjString

func AsObjString(value Value) *ObjectString

AsObjString returns the ObjectString representation of a Value.

value: The Value to convert to an ObjectString. Returns: A pointer to the ObjectString representation of the Value.

type ParseRule

type ParseRule struct {
	// Prefix is the parsing function for an expression with the token as a prefix.
	Prefix Parsefn

	// Infix is the parsing function for an expression with the token as an infix.
	Infix Parsefn

	// Precedence represents the precedence level of the token.
	Precedence Precedence
}

ParseRule represents the parsing rule for a specific token type.

type Parsefn

type Parsefn func(canAssign bool)

Parsefn represents the parsing function for a specific token type.

type Parser

type Parser struct {
	// Current represents the current token being processed.
	Current Token

	// Previous represents the previously processed token.
	Previous Token

	// HadError indicates whether an Error occurred during parsing.
	HadError bool

	// PanicMode indicates whether the parser is in panic mode.
	PanicMode bool
}

Parser is responsible for parsing the source code and generating the abstract syntax tree (AST).

type Precedence

type Precedence int
const (
	PrecNONE Precedence = iota
	PrecASSIGNMENT
	PrecOR
	PrecAND
	PrecEQUALITY
	PrecCOMPARISON
	PrecTERM
	PrecFACTOR
	PrecUNAR
	PrecCALL
	PrecPRIMARY
)

Precedence represents the precedence level of a token.

type Scanner

type Scanner struct {
	Start   int     // Start represents the start position of the scanner.
	Current int     // Current represents the current position of the scanner.
	Line    int     // Line represents the current line number.
	Source  *string // Source is a pointer to the source code being scanned.
}

Scanner represents a lexical scanner for the source code.

func (*Scanner) InitScanner

func (scanner *Scanner) InitScanner(source string)

InitScanner initializes the Scanner struct with the given source.

Parameters: - source: a string representing the source code to be scanned.

Return type: none.

func (*Scanner) ScanToken

func (scanner *Scanner) ScanToken(source *string) Token

ScanToken scans the source string and returns a Token.

It takes a pointer to a Scanner and a pointer to a string as parameters. The Scanner is used to keep track of the current position in the source string. The source string contains the code to be scanned.

It returns a Token based on the current character in the source string. The Token represents the type of the current character.

type Table

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

Table is a struct representing a table data structure.

func (*Table) Freetable

func (table *Table) Freetable()

Freetable frees the table by releasing its entries and reinitializing it.

No parameters. No return types.

func (*Table) InitTable

func (table *Table) InitTable()

InitTable initializes the Table struct.

No parameters. No return values.

func (*Table) TableAddAll

func (table *Table) TableAddAll(from *Table)

TableAddAll adds all elements from the given table to the current table.

It takes a pointer to the Table struct named "from" as a parameter. It does not return anything.

func (*Table) TableDelete

func (table *Table) TableDelete(key *ObjectString) bool

TableDelete deletes an entry from the Table.

It takes a key of type *ObjectString as a parameter and returns a boolean value indicating whether the deletion was successful.

func (Table) TableGet

func (table Table) TableGet(key *ObjectString, value *Value) bool

TableGet retrieves the value associated with the given key in the Table.

The function takes two parameters: key, a pointer to an ObjectString, and value, a pointer to a Value. It returns a boolean value indicating whether the key was found in the Table.

func (*Table) TableSet

func (table *Table) TableSet(key *ObjectString, value Value) bool

TableSet sets the value for a given key in the Table.

Parameters: - key: a pointer to an ObjectString representing the key. - value: the value to be set.

Returns: - bool: true if the key is a new key in the table, false otherwise.

type Token

type Token struct {
	TOKENType globals.TokenType // Represents the type of the token.
	Start     int               // Represents the starting position of the token.
	Length    int               // Represents the length of the token.
	Line      int               // Represents the line number where the token is found.
}

Token represents a lexical token in the code.

type VM

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

VM represents a virtual machine.

func (*VM) BinaryOp

func (vm *VM) BinaryOp(op func(Value, Value) Value, offset ...int) error

BinaryOp performs a binary operation on the top two values in the VM's stack using the provided operation function.

Parameters: - op: The operation function that takes two values and returns a value.

Return type: error

func (*VM) Peek

func (vm *VM) Peek(index ...int) Value

Peek returns the value at the top of the stack without removing it.

It does not take any parameters. It returns a Value.

func (*VM) Pop

func (vm *VM) Pop() Value

Pop removes and returns the top element from the stack.

No parameters. Returns a Value.

func (*VM) Push

func (vm *VM) Push(value Value)

Push pushes a value onto the stack.

value: the value to be pushed onto the stack.

func (*VM) ResetStack

func (vm *VM) ResetStack()

ResetStack resets the stack of the VM.

No parameters. No return type.

type Value

type Value struct {
	Type ValueType   // The type of the value
	As   interface{} // The value
}

Value represents a value in the language

func BoolValue

func BoolValue(value bool) Value

BoolValue returns a Value with Type ValBool and As value.

func GrowArrayValueArray

func GrowArrayValueArray(valarray []Value, oldcap, newcap int) []Value

GrowArrayValueArray returns a new array of Values with a larger capacity.

Parameters: - valarray: The original array of Values. - oldcap: The old capacity of the array. - newcap: The new capacity of the array.

Return type: - []Value: The new array of Values.

func NilValue

func NilValue() Value

NilValue returns a Value with Type ValNil and a nil As field.

NilValue does not take any parameters. It returns a Value.

func NumberValue

func NumberValue(value float64) Value

NumberValue creates a Value struct with the given float64 value.

Parameters: - value: The float64 value to be assigned to the Value struct.

Returns: The created Value struct.

func ObjFunctionValue

func ObjFunctionValue(value *ObjFunction) Value

ObjFunctionValue returns the value of the ObjFunction.

value *ObjFunction - the ObjFunction parameter Value - the return type

func ObjStrValue

func ObjStrValue(value *ObjectString) Value

ObjStrValue returns a Value with Type ValObjStr and As value.

value: a pointer to an ObjectString. Returns: a Value.

func ObjVal

func ObjVal(object *ObjFunction) Value

OBJ_VAL description of the Go function.

It takes a parameter object of type *Obj and returns a Value type.

type ValueArray

type ValueArray struct {
	Capacity int     // The maximum number of elements that can be stored in the array.
	Count    int     // The current number of elements in the array.
	Values   []Value // The array of values.
}

ValueArray represents an array of values.

type ValueType

type ValueType int
const (
	ValBool ValueType = iota
	ValNil
	ValObjStr
	ValObj
	ValNumber
)

Value types

Jump to

Keyboard shortcuts

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