bytecode

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package bytecode provides binary serialization and deserialization of AST nodes for the English programming language. The binary format (.101 files) can be directly loaded and evaluated without parsing, similar to protobuf serialization.

Index

Constants

View Source
const (
	NodeProgram byte = iota + 1
	NodeVariableDecl
	NodeAssignment
	NodeFunctionDecl
	NodeFunctionCall
	NodeCallStatement
	NodeIfStatement
	NodeElseIfPart
	NodeWhileLoop
	NodeForLoop
	NodeForEachLoop
	NodeNumberLiteral
	NodeStringLiteral
	NodeBooleanLiteral
	NodeListLiteral
	NodeIdentifier
	NodeBinaryExpression
	NodeUnaryExpression
	NodeIndexExpression
	NodeIndexAssignment
	NodeLengthExpression
	NodeReturnStatement
	NodeOutputStatement
	NodeToggleStatement
	NodeBreakStatement
	NodeLocationExpression
	NodeImportStatement
	NodeTypedVariableDecl
	NodeErrorTypeDecl
	NodeErrorTypeCheckExpression
)

Node type identifiers

View Source
const CacheDir = "__engcache__"
View Source
const (
	// CacheHashBytes is the number of bytes from SipHash used for cache filenames.
	// SipHash produces an 8-byte (64-bit) hash, which provides good collision resistance
	// while being much faster than cryptographic hashes like SHA-256.
	// Using the full 8 bytes as per PEP 552 recommendations.
	CacheHashBytes = 8
)

Cache configuration

View Source
const FormatVersion uint8 = 1

Version of the bytecode format

Variables

View Source
var MagicBytes = []byte{0x10, 0x1E, 0x4E, 0x47}

Magic bytes to identify .101 files (binary identifier)

Functions

func GetCachePath

func GetCachePath(sourcePath string) string

GetCachePath returns the cache file path for a given source file. For example: "examples/math_library.abc" -> "__engcache__/39ccbccfa9db97df_math_library.abc.101" Uses SipHash for fast, non-cryptographic hashing as per PEP 552.

func IsCacheValid

func IsCacheValid(sourcePath, cachePath string) bool

IsCacheValid checks if the cached bytecode is up-to-date by comparing modification times. Returns true if the cache exists and is newer than or equal to the source file.

func LoadCachedOrParse

func LoadCachedOrParse(sourcePath string, parseFunc func(string) (*ast.Program, error)) (*ast.Program, bool, error)

LoadCachedOrParse attempts to load bytecode from cache, or parses the source file if cache is invalid. The parseFunc parameter receives the sourcePath and should parse it into an AST Program. Returns the parsed Program AST and a boolean indicating whether cache was used.

func ReadBytecodeCache

func ReadBytecodeCache(cachePath string) ([]byte, error)

ReadBytecodeCache reads bytecode from the cache.

func WriteBytecodeCache

func WriteBytecodeCache(cachePath string, data []byte) error

WriteBytecodeCache writes bytecode to the cache directory. Creates the cache directory if it doesn't exist.

Types

type Decoder

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

Decoder deserializes binary bytecode to AST

func NewDecoder

func NewDecoder(data []byte) *Decoder

NewDecoder creates a new bytecode decoder

func (*Decoder) Decode

func (d *Decoder) Decode() (*ast.Program, error)

Decode deserializes binary bytecode to a Program AST

type Encoder

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

Encoder serializes AST to binary format

func NewEncoder

func NewEncoder() *Encoder

NewEncoder creates a new bytecode encoder

func (*Encoder) Encode

func (e *Encoder) Encode(program *ast.Program) ([]byte, error)

Encode serializes a Program AST to binary bytecode

Directories

Path Synopsis
Package disasm – disasm.go
Package disasm – disasm.go

Jump to

Keyboard shortcuts

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