Documentation
¶
Index ¶
- Constants
- func AddConstants(chunk *Chunk, val Value) int
- func AsBool(value Value) bool
- func AsCString(value Value) string
- func AsNumber(value Value) float64
- func DisassembleChunk(chunk *Chunk, name string)
- func DisassembleInstruction(chunk *Chunk, offset int) int
- func Error(message string)
- func FreeArray(array any, cap int)
- func FreeChunk(chunk *Chunk)
- func FreeObjects(object *Obj)
- func FreeVM()
- func FreeValueArray(array *ValueArray)
- func GrowArrayChunks(code []uint8, oldcap, newcap int) []uint8
- func GrowArrayLines(lines []int, oldcap, newcap int) []int
- func GrowCapacity(oldcap int) int
- func InitChunk(chunk *Chunk)
- func InitCompiler(compiler *Compiler, _type FunctionType)
- func InitVM()
- func InitValueArray(array *ValueArray)
- func IsBool(value Value) bool
- func IsFunction(value Value) bool
- func IsNil(value Value) bool
- func IsNumber(value Value) bool
- func IsObjType(value Value, objType ObjType) bool
- func IsString(value Value) bool
- func IsValObj(value Value) bool
- func PrintValue(value Value)
- func Reallocate(pointer interface{}, oldSize, newSize int) interface{}
- func WriteChunk(chunk *Chunk, bytecode uint8, line int)
- func WriteValueArray(array *ValueArray, val Value)
- type CallFrame
- type Chunk
- type Compiler
- type Entry
- type FunctionType
- type InterpretResult
- type Local
- type Obj
- type ObjFunction
- type ObjType
- type ObjectString
- type ParseRule
- type Parsefn
- type Parser
- type Precedence
- type Scanner
- type Table
- type Token
- type VM
- type Value
- type ValueArray
- type ValueType
Constants ¶
const FrameMax = 64
FrameMax represents the maximum number of call frames.
const StackMax = 256
StackMax represents the maximum size of the stack.
const TableMaxLoad float32 = 0.75
TableMaxLoad is the maximum load factor for the table.
const Uint8Count = StackMax
Uint8Count represents the maximum number of local variables.
Variables ¶
This section is empty.
Functions ¶
func AddConstants ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 IsNil ¶
IsNil checks if the given value is nil.
It takes a parameter of type Value and returns a boolean value.
func IsNumber ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
ReadByteVM reads a single byte from the VM's instruction pointer.
No parameters. Returns a uint8 value.
func (*CallFrame) ReadConstant ¶
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.
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 ¶
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.
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 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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
Pop removes and returns the top element from the stack.
No parameters. Returns a Value.
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 GrowArrayValueArray ¶
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 ¶
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.