parse

package
v0.0.0-...-eadb41f Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Overview

Package parse contains the code that parses code text and transforms it into bytecode and constants that are able to be run in a virtual machine or translated into another format.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	StringCoers bool // disallow string coersion in arith
	RequireOnly bool // require std libs instead of available by default
	EnvReadonly bool // not allowed to change _ENV
	LocalOnly   bool // not allowed to define globals only locals
	Strict      bool // type checking and throw parsing errors if types are bad
}

Config not fully implemented yet. It is a configuration for how the Parser behaves on each file that it parses. It may allow for the program to be more strict.

type FnProto

type FnProto struct {
	Name     string
	Filename string
	Comment  string
	Locals   []*Local // name mapped to stack index of where the local was loaded

	Constants []any      // constant values to be loaded into the stack
	UpIndexes []Upindex  // name mapped to upindex
	ByteCodes []uint32   // bytecode for this function
	FnTable   []*FnProto // indexes of functions in constants
	LineTrace []LineInfo

	LineInfo
	Arity int64 // parameter count

	Varargs bool // if the function call has varargs
	// contains filtered or unexported fields
}

FnProto is a construct that captures a function scope that can be called. it is not always a function, even the main scope of a file outside of a function is a FnProto.

func File

func File(path string, mode LoadMode) (*FnProto, error)

File is a helper function around Parse to open and close a file automatically.

func NewEmptyFnProto

func NewEmptyFnProto(name string, rootFn *FnProto) *FnProto

NewEmptyFnProto creates a new fnproto without any parsing. This is mainly used by the runtime package for running a repl.

func NewFnProto

func NewFnProto(
	filename, name string, prev *FnProto, params []*Local, vararg bool, defn *types.Function, linfo LineInfo,
) *FnProto

NewFnProto creates a new FnProto for parsing. It is the result from parsing that contains the bytecode and debugging information for if an error happens.

func NewFnProtoFrom

func NewFnProtoFrom(fn *FnProto) *FnProto

NewFnProtoFrom creates a new FnProto from another, used for repl.

func Parse

func Parse(filename string, src io.ReadSeeker, mode LoadMode) (*FnProto, error)

Parse will, depending on the LoadMode, parse a text file and return bytecode or if the load mode is binary, it will undump an already parsed fnproto. If both modes are passed, it will try to figure out which kind of file it is parsing.

func TryStat

func TryStat(src string, parentFn *FnProto) (*FnProto, error)

TryStat allows for trying a single statement. This is primarily for repl.

func UndumpFnProto

func UndumpFnProto(buf io.Reader) (*FnProto, error)

UndumpFnProto will deserialize fnproto data into a new fnproto ready for interpreting.

func (*FnProto) Dump

func (fn *FnProto) Dump(_ bool) ([]byte, error)

Dump will serialize fnproto data into a byte array for writing out to a file.

func (*FnProto) GetConst

func (fn *FnProto) GetConst(idx int64) any

GetConst gets a constant from predefined constants in the fn.

func (*FnProto) String

func (fn *FnProto) String() string

type LineInfo

type LineInfo struct {
	Line   int64
	Column int64
}

LineInfo is a shared struct that is used for tracking where the behviour originated from in the sourcecode.

type LoadMode

type LoadMode uint

LoadMode are flags to indicate how to load/parse a chunk of data.

const (
	// ModeText implies that the chunk of text being loaded is plain text.
	ModeText LoadMode = 0b01
	// ModeBinary implies that the chunk of data being loaded is pre parsed binary.
	ModeBinary LoadMode = 0b10
)

type Local

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

Local is a local variable refence.

type MetaMethod

type MetaMethod string

MetaMethod is the enum of valid meta methods.

const (

	// MetaAdd is the __add metamethod.
	MetaAdd MetaMethod = "__add"
	// MetaSub is the __sub metamethod.
	MetaSub MetaMethod = "__sub"
	// MetaMul is the __mul metamethod.
	MetaMul MetaMethod = "__mul"
	// MetaDiv is the __div metamethod.
	MetaDiv MetaMethod = "__div"
	// MetaMod is the __mod methamethod.
	MetaMod MetaMethod = "__mod"
	// MetaPow is the __pow methamethod.
	MetaPow MetaMethod = "__pow"
	// MetaUNM is the __unm methamethod.
	MetaUNM MetaMethod = "__unm"
	// MetaIDiv is the __idiv methamethod.
	MetaIDiv MetaMethod = "__idiv"
	// MetaBAnd is the __band methamethod.
	MetaBAnd MetaMethod = "__band"
	// MetaBOr is the __bor methamethod.
	MetaBOr MetaMethod = "__bor"
	// MetaBXOr is the __bxor methamethod.
	MetaBXOr MetaMethod = "__bxor"
	// MetaBNot is the __bnot methamethod.
	MetaBNot MetaMethod = "__bnot"
	// MetaShl is the __shl methamethod.
	MetaShl MetaMethod = "__shl"
	// MetaShr is the __shr methamethod.
	MetaShr MetaMethod = "__shr"
	// MetaConcat is the __concat methamethod.
	MetaConcat MetaMethod = "__concat"
	// MetaLen is the __len methamethod.
	MetaLen MetaMethod = "__len"
	// MetaEq is the __eq methamethod.
	MetaEq MetaMethod = "__eq"
	// MetaLt is the __lt methamethod.
	MetaLt MetaMethod = "__lt"
	// MetaLe is the __le methamethod.
	MetaLe MetaMethod = "__le"
	// MetaIndex is the __index methamethod.
	MetaIndex MetaMethod = "__index"
	// MetaNewIndex is the __newindex methamethod.
	MetaNewIndex MetaMethod = "__newindex"
	// MetaCall is the __call methamethod.
	MetaCall MetaMethod = "__call"
	// MetaClose is the __close methamethod.
	MetaClose MetaMethod = "__close"
	// MetaToString is the __tostring methamethod.
	MetaToString MetaMethod = "__tostring"
	// MetaName is the __name methamethod.
	MetaName MetaMethod = "__name"
	// MetaPairs is the __pairs methamethod.
	MetaPairs MetaMethod = "__pairs"
	// MetaMeta is the __metatable methamethod.
	MetaMeta MetaMethod = "__metatable"
	// MetaGC is the __gc methamethod.
	MetaGC MetaMethod = "__gc"
)

type Parser

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

Parser is the object that will parse a file an be able to return bytecode ready for the VM.

func New

func New() *Parser

New creates a new parser that can parse one file at a time.

func (*Parser) Parse

func (p *Parser) Parse(filename string, src io.Reader) (*FnProto, error)

Parse will reset the parser but parse the source within the context of this function. This allows parsing in repl and still be able to have visibility of locals.

type Upindex

type Upindex struct {
	Name      string
	FromStack bool

	Index uint8
	// contains filtered or unexported fields
}

Upindex captures an upvalue position for fetching them during runtime.

Jump to

Keyboard shortcuts

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