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
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 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 )
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 )
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.