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
- Variables
- func GetCachePath(sourcePath string) string
- func IsCacheValid(sourcePath, cachePath string) bool
- func LoadCachedOrParse(sourcePath string, parseFunc func(string) (*ast.Program, error)) (*ast.Program, bool, error)
- func ReadBytecodeCache(cachePath string) ([]byte, error)
- func WriteBytecodeCache(cachePath string, data []byte) error
- type Decoder
- type Encoder
Constants ¶
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
const CacheDir = "__engcache__"
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
const FormatVersion uint8 = 1
Version of the bytecode format
Variables ¶
var MagicBytes = []byte{0x10, 0x1E, 0x4E, 0x47}
Magic bytes to identify .101 files (binary identifier)
Functions ¶
func GetCachePath ¶
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 ¶
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 ¶
ReadBytecodeCache reads bytecode from the cache.
func WriteBytecodeCache ¶
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