compiler

package
v1.27.0 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package compiler compiles an AST to virtual machine instructions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddRegexFlags added in v1.19.0

func AddRegexFlags(regex string) string

AddRegexFlags add the necessary flags to regex to make it work like other AWKs (exported so we can also use this in the interpreter).

Types

type Action

type Action struct {
	Pattern [][]Opcode
	Body    []Opcode
}

Action holds a compiled pattern-action block.

type AugOp

type AugOp Opcode

AugOp represents an augmented assignment operation.

const (
	AugOpAdd AugOp = iota
	AugOpSub
	AugOpMul
	AugOpDiv
	AugOpPow
	AugOpMod
)

func (AugOp) String

func (i AugOp) String() string

type BuiltinOp

type BuiltinOp Opcode

BuiltinOp represents a builtin function call.

const (
	BuiltinAtan2 BuiltinOp = iota
	BuiltinClose
	BuiltinCos
	BuiltinExp
	BuiltinFflush
	BuiltinFflushAll
	BuiltinGsub
	BuiltinIndex
	BuiltinInt
	BuiltinLength
	BuiltinLengthArg
	BuiltinLog
	BuiltinMatch
	BuiltinRand
	BuiltinSin
	BuiltinSqrt
	BuiltinSrand
	BuiltinSrandSeed
	BuiltinSub
	BuiltinSubstr
	BuiltinSubstrLength
	BuiltinSystem
	BuiltinTolower
	BuiltinToupper
)

func (BuiltinOp) String

func (i BuiltinOp) String() string

type Function

type Function struct {
	Name       string
	Params     []string
	Arrays     []bool
	NumScalars int
	NumArrays  int
	Body       []Opcode
}

Function holds a compiled function.

type Opcode

type Opcode int32

Opcode represents a single virtual machine instruction (or argument). The comments beside each opcode show any arguments that instruction consumes.

Normally this is called "bytecode", but I've avoided that term here as each opcode is a 32-bit word, not an 8-bit byte.

I tested various bit widths, and I believe 32 bit was the fastest, but also means we don't have to worry about jump offsets overflowing. That's tested in the compiler, but who's going to have an AWK program bigger than 2GB?

const (
	Nop Opcode = iota

	// Stack operations
	Num // numIndex
	Str // strIndex
	Dupe
	Drop
	Swap
	Rote

	// Fetch a field, variable, or array item
	Field
	FieldInt // index
	FieldByName
	FieldByNameStr // strIndex
	Global         // index
	Local          // index
	Special        // index
	ArrayGlobal    // arrayIndex
	ArrayLocal     // arrayIndex
	InGlobal       // arrayIndex
	InLocal        // arrayIndex

	// Assign a field, variable, or array item
	AssignField
	AssignGlobal      // index
	AssignLocal       // index
	AssignSpecial     // index
	AssignArrayGlobal // arrayIndex
	AssignArrayLocal  // arrayIndex

	// Delete statement
	Delete    // arrayScope arrayIndex
	DeleteAll // arrayScope arrayIndex

	// Post-increment and post-decrement
	IncrField       // amount
	IncrGlobal      // amount index
	IncrLocal       // amount index
	IncrSpecial     // amount index
	IncrArrayGlobal // amount arrayIndex
	IncrArrayLocal  // amount arrayIndex

	// Augmented assignment (also used for pre-increment and pre-decrement)
	AugAssignField       // augOp
	AugAssignGlobal      // augOp index
	AugAssignLocal       // augOp index
	AugAssignSpecial     // augOp index
	AugAssignArrayGlobal // augOp arrayIndex
	AugAssignArrayLocal  // augOp arrayIndex

	// Stand-alone regex expression /foo/
	Regex // regexIndex

	// Multi-index concatenation
	IndexMulti // num

	// Multi-value concatenation
	ConcatMulti // num

	// Binary operators
	Add
	Subtract
	Multiply
	Divide
	Power
	Modulo
	Equals
	NotEquals
	Less
	Greater
	LessOrEqual
	GreaterOrEqual
	Concat
	Match
	NotMatch

	// Unary operators
	Not
	UnaryMinus
	UnaryPlus
	Boolean

	// Control flow
	Jump               // offset
	JumpFalse          // offset
	JumpTrue           // offset
	JumpEquals         // offset
	JumpNotEquals      // offset
	JumpLess           // offset
	JumpGreater        // offset
	JumpLessOrEqual    // offset
	JumpGreaterOrEqual // offset
	Next
	Nextfile
	Exit
	ExitStatus
	ForIn // varScope varIndex arrayScope arrayIndex offset
	BreakForIn

	// Builtin functions
	CallBuiltin     // builtinOp
	CallLengthArray // arrayScope arrayIndex
	CallSplit       // arrayScope arrayIndex
	CallSplitSep    // arrayScope arrayIndex
	CallSprintf     // numArgs

	// User and native functions
	CallUser   // funcIndex numArrayArgs [arrayScope1 arrayIndex1 ...]
	CallNative // funcIndex numArgs
	Return
	ReturnNull
	Nulls // numNulls

	// Print, printf, and getline
	Print          // numArgs redirect
	Printf         // numArgs redirect
	Getline        // redirect
	GetlineField   // redirect
	GetlineGlobal  // redirect index
	GetlineLocal   // redirect index
	GetlineSpecial // redirect index
	GetlineArray   // redirect arrayScope arrayIndex

	EndOpcode
)

func (Opcode) String

func (i Opcode) String() string

type Program

type Program struct {
	Begin     []Opcode
	Actions   []Action
	End       []Opcode
	Functions []Function
	Nums      []float64
	Strs      []string
	Regexes   []*regexp.Regexp
	// contains filtered or unexported fields
}

Program holds an entire compiled program.

func Compile

func Compile(resolved *resolver.ResolvedProgram) (compiledProg *Program, err error)

Compile compiles an AST (parsed program) into virtual machine instructions.

func (*Program) Disassemble

func (p *Program) Disassemble(writer io.Writer) error

Disassemble writes a human-readable form of the program's virtual machine instructions to writer.

Jump to

Keyboard shortcuts

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