2019-12-20 22:40:38 +03:00
|
|
|
package tengo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"path/filepath"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
|
2019-12-30 00:38:51 +03:00
|
|
|
"github.com/d5/tengo/v2/parser"
|
|
|
|
"github.com/d5/tengo/v2/token"
|
2019-12-20 22:40:38 +03:00
|
|
|
)
|
|
|
|
|
2019-12-24 18:42:30 +03:00
|
|
|
// compilationScope represents a compiled instructions and the last two
|
|
|
|
// instructions that were emitted.
|
|
|
|
type compilationScope struct {
|
|
|
|
Instructions []byte
|
|
|
|
SymbolInit map[string]bool
|
|
|
|
SourceMap map[int]parser.Pos
|
|
|
|
}
|
|
|
|
|
|
|
|
// loop represents a loop construct that the compiler uses to track the current
|
|
|
|
// loop.
|
|
|
|
type loop struct {
|
|
|
|
Continues []int
|
|
|
|
Breaks []int
|
|
|
|
}
|
|
|
|
|
|
|
|
// CompilerError represents a compiler error.
|
|
|
|
type CompilerError struct {
|
|
|
|
FileSet *parser.SourceFileSet
|
|
|
|
Node parser.Node
|
|
|
|
Err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *CompilerError) Error() string {
|
|
|
|
filePos := e.FileSet.Position(e.Node.Pos())
|
|
|
|
return fmt.Sprintf("Compile Error: %s\n\tat %s", e.Err.Error(), filePos)
|
|
|
|
}
|
|
|
|
|
2019-12-20 22:40:38 +03:00
|
|
|
// Compiler compiles the AST into a bytecode.
|
|
|
|
type Compiler struct {
|
2019-12-24 18:42:30 +03:00
|
|
|
file *parser.SourceFile
|
2019-12-20 22:40:38 +03:00
|
|
|
parent *Compiler
|
|
|
|
modulePath string
|
2020-05-22 23:57:38 +03:00
|
|
|
importDir string
|
2019-12-20 22:40:38 +03:00
|
|
|
constants []Object
|
2019-12-24 18:42:30 +03:00
|
|
|
symbolTable *SymbolTable
|
|
|
|
scopes []compilationScope
|
2019-12-20 22:40:38 +03:00
|
|
|
scopeIndex int
|
|
|
|
modules *ModuleMap
|
|
|
|
compiledModules map[string]*CompiledFunction
|
|
|
|
allowFileImport bool
|
2019-12-24 18:42:30 +03:00
|
|
|
loops []*loop
|
2019-12-20 22:40:38 +03:00
|
|
|
loopIndex int
|
|
|
|
trace io.Writer
|
|
|
|
indent int
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewCompiler creates a Compiler.
|
|
|
|
func NewCompiler(
|
2019-12-24 18:42:30 +03:00
|
|
|
file *parser.SourceFile,
|
|
|
|
symbolTable *SymbolTable,
|
2019-12-20 22:40:38 +03:00
|
|
|
constants []Object,
|
|
|
|
modules *ModuleMap,
|
|
|
|
trace io.Writer,
|
|
|
|
) *Compiler {
|
2019-12-24 18:42:30 +03:00
|
|
|
mainScope := compilationScope{
|
2019-12-20 22:40:38 +03:00
|
|
|
SymbolInit: make(map[string]bool),
|
2019-12-24 18:42:30 +03:00
|
|
|
SourceMap: make(map[int]parser.Pos),
|
2019-12-20 22:40:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// symbol table
|
|
|
|
if symbolTable == nil {
|
2019-12-24 18:42:30 +03:00
|
|
|
symbolTable = NewSymbolTable()
|
2019-12-20 22:40:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// add builtin functions to the symbol table
|
|
|
|
for idx, fn := range builtinFuncs {
|
|
|
|
symbolTable.DefineBuiltin(idx, fn.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// builtin modules
|
|
|
|
if modules == nil {
|
|
|
|
modules = NewModuleMap()
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Compiler{
|
|
|
|
file: file,
|
|
|
|
symbolTable: symbolTable,
|
|
|
|
constants: constants,
|
2019-12-24 18:42:30 +03:00
|
|
|
scopes: []compilationScope{mainScope},
|
2019-12-20 22:40:38 +03:00
|
|
|
scopeIndex: 0,
|
|
|
|
loopIndex: -1,
|
|
|
|
trace: trace,
|
|
|
|
modules: modules,
|
|
|
|
compiledModules: make(map[string]*CompiledFunction),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compile compiles the AST node.
|
2019-12-24 18:42:30 +03:00
|
|
|
func (c *Compiler) Compile(node parser.Node) error {
|
2019-12-20 22:40:38 +03:00
|
|
|
if c.trace != nil {
|
|
|
|
if node != nil {
|
|
|
|
defer untracec(tracec(c, fmt.Sprintf("%s (%s)",
|
|
|
|
node.String(), reflect.TypeOf(node).Elem().Name())))
|
|
|
|
} else {
|
|
|
|
defer untracec(tracec(c, "<nil>"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch node := node.(type) {
|
2019-12-24 18:42:30 +03:00
|
|
|
case *parser.File:
|
2019-12-20 22:40:38 +03:00
|
|
|
for _, stmt := range node.Stmts {
|
|
|
|
if err := c.Compile(stmt); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2019-12-24 18:42:30 +03:00
|
|
|
case *parser.ExprStmt:
|
2019-12-20 22:40:38 +03:00
|
|
|
if err := c.Compile(node.Expr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(node, parser.OpPop)
|
|
|
|
case *parser.IncDecStmt:
|
2019-12-20 22:40:38 +03:00
|
|
|
op := token.AddAssign
|
|
|
|
if node.Token == token.Dec {
|
|
|
|
op = token.SubAssign
|
|
|
|
}
|
2019-12-24 18:42:30 +03:00
|
|
|
return c.compileAssign(node, []parser.Expr{node.Expr},
|
|
|
|
[]parser.Expr{&parser.IntLit{Value: 1}}, op)
|
|
|
|
case *parser.ParenExpr:
|
2019-12-20 22:40:38 +03:00
|
|
|
if err := c.Compile(node.Expr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-24 18:42:30 +03:00
|
|
|
case *parser.BinaryExpr:
|
2019-12-20 22:40:38 +03:00
|
|
|
if node.Token == token.LAnd || node.Token == token.LOr {
|
|
|
|
return c.compileLogical(node)
|
|
|
|
}
|
|
|
|
if node.Token == token.Less {
|
|
|
|
if err := c.Compile(node.RHS); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := c.Compile(node.LHS); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(node, parser.OpBinaryOp, int(token.Greater))
|
2019-12-20 22:40:38 +03:00
|
|
|
return nil
|
|
|
|
} else if node.Token == token.LessEq {
|
|
|
|
if err := c.Compile(node.RHS); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := c.Compile(node.LHS); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(node, parser.OpBinaryOp, int(token.GreaterEq))
|
2019-12-20 22:40:38 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err := c.Compile(node.LHS); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := c.Compile(node.RHS); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch node.Token {
|
|
|
|
case token.Add:
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(node, parser.OpBinaryOp, int(token.Add))
|
2019-12-20 22:40:38 +03:00
|
|
|
case token.Sub:
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(node, parser.OpBinaryOp, int(token.Sub))
|
2019-12-20 22:40:38 +03:00
|
|
|
case token.Mul:
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(node, parser.OpBinaryOp, int(token.Mul))
|
2019-12-20 22:40:38 +03:00
|
|
|
case token.Quo:
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(node, parser.OpBinaryOp, int(token.Quo))
|
2019-12-20 22:40:38 +03:00
|
|
|
case token.Rem:
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(node, parser.OpBinaryOp, int(token.Rem))
|
2019-12-20 22:40:38 +03:00
|
|
|
case token.Greater:
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(node, parser.OpBinaryOp, int(token.Greater))
|
2019-12-20 22:40:38 +03:00
|
|
|
case token.GreaterEq:
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(node, parser.OpBinaryOp, int(token.GreaterEq))
|
2019-12-20 22:40:38 +03:00
|
|
|
case token.Equal:
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(node, parser.OpEqual)
|
2019-12-20 22:40:38 +03:00
|
|
|
case token.NotEqual:
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(node, parser.OpNotEqual)
|
2019-12-20 22:40:38 +03:00
|
|
|
case token.And:
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(node, parser.OpBinaryOp, int(token.And))
|
2019-12-20 22:40:38 +03:00
|
|
|
case token.Or:
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(node, parser.OpBinaryOp, int(token.Or))
|
2019-12-20 22:40:38 +03:00
|
|
|
case token.Xor:
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(node, parser.OpBinaryOp, int(token.Xor))
|
2019-12-20 22:40:38 +03:00
|
|
|
case token.AndNot:
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(node, parser.OpBinaryOp, int(token.AndNot))
|
2019-12-20 22:40:38 +03:00
|
|
|
case token.Shl:
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(node, parser.OpBinaryOp, int(token.Shl))
|
2019-12-20 22:40:38 +03:00
|
|
|
case token.Shr:
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(node, parser.OpBinaryOp, int(token.Shr))
|
2019-12-20 22:40:38 +03:00
|
|
|
default:
|
|
|
|
return c.errorf(node, "invalid binary operator: %s",
|
|
|
|
node.Token.String())
|
|
|
|
}
|
2019-12-24 18:42:30 +03:00
|
|
|
case *parser.IntLit:
|
|
|
|
c.emit(node, parser.OpConstant,
|
2019-12-20 22:40:38 +03:00
|
|
|
c.addConstant(&Int{Value: node.Value}))
|
2019-12-24 18:42:30 +03:00
|
|
|
case *parser.FloatLit:
|
|
|
|
c.emit(node, parser.OpConstant,
|
2019-12-20 22:40:38 +03:00
|
|
|
c.addConstant(&Float{Value: node.Value}))
|
2019-12-24 18:42:30 +03:00
|
|
|
case *parser.BoolLit:
|
2019-12-20 22:40:38 +03:00
|
|
|
if node.Value {
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(node, parser.OpTrue)
|
2019-12-20 22:40:38 +03:00
|
|
|
} else {
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(node, parser.OpFalse)
|
2019-12-20 22:40:38 +03:00
|
|
|
}
|
2019-12-24 18:42:30 +03:00
|
|
|
case *parser.StringLit:
|
2019-12-20 22:40:38 +03:00
|
|
|
if len(node.Value) > MaxStringLen {
|
|
|
|
return c.error(node, ErrStringLimit)
|
|
|
|
}
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(node, parser.OpConstant,
|
2019-12-20 22:40:38 +03:00
|
|
|
c.addConstant(&String{Value: node.Value}))
|
2019-12-24 18:42:30 +03:00
|
|
|
case *parser.CharLit:
|
|
|
|
c.emit(node, parser.OpConstant,
|
2019-12-20 22:40:38 +03:00
|
|
|
c.addConstant(&Char{Value: node.Value}))
|
2019-12-24 18:42:30 +03:00
|
|
|
case *parser.UndefinedLit:
|
|
|
|
c.emit(node, parser.OpNull)
|
|
|
|
case *parser.UnaryExpr:
|
2019-12-20 22:40:38 +03:00
|
|
|
if err := c.Compile(node.Expr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch node.Token {
|
|
|
|
case token.Not:
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(node, parser.OpLNot)
|
2019-12-20 22:40:38 +03:00
|
|
|
case token.Sub:
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(node, parser.OpMinus)
|
2019-12-20 22:40:38 +03:00
|
|
|
case token.Xor:
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(node, parser.OpBComplement)
|
2019-12-20 22:40:38 +03:00
|
|
|
case token.Add:
|
|
|
|
// do nothing?
|
|
|
|
default:
|
|
|
|
return c.errorf(node,
|
|
|
|
"invalid unary operator: %s", node.Token.String())
|
|
|
|
}
|
2019-12-24 18:42:30 +03:00
|
|
|
case *parser.IfStmt:
|
2019-12-20 22:40:38 +03:00
|
|
|
// open new symbol table for the statement
|
|
|
|
c.symbolTable = c.symbolTable.Fork(true)
|
|
|
|
defer func() {
|
|
|
|
c.symbolTable = c.symbolTable.Parent(false)
|
|
|
|
}()
|
|
|
|
|
|
|
|
if node.Init != nil {
|
|
|
|
if err := c.Compile(node.Init); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := c.Compile(node.Cond); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// first jump placeholder
|
2019-12-24 18:42:30 +03:00
|
|
|
jumpPos1 := c.emit(node, parser.OpJumpFalsy, 0)
|
2019-12-20 22:40:38 +03:00
|
|
|
if err := c.Compile(node.Body); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if node.Else != nil {
|
|
|
|
// second jump placeholder
|
2019-12-24 18:42:30 +03:00
|
|
|
jumpPos2 := c.emit(node, parser.OpJump, 0)
|
2019-12-20 22:40:38 +03:00
|
|
|
|
|
|
|
// update first jump offset
|
|
|
|
curPos := len(c.currentInstructions())
|
|
|
|
c.changeOperand(jumpPos1, curPos)
|
|
|
|
if err := c.Compile(node.Else); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// update second jump offset
|
|
|
|
curPos = len(c.currentInstructions())
|
|
|
|
c.changeOperand(jumpPos2, curPos)
|
|
|
|
} else {
|
|
|
|
// update first jump offset
|
|
|
|
curPos := len(c.currentInstructions())
|
|
|
|
c.changeOperand(jumpPos1, curPos)
|
|
|
|
}
|
2019-12-24 18:42:30 +03:00
|
|
|
case *parser.ForStmt:
|
2019-12-20 22:40:38 +03:00
|
|
|
return c.compileForStmt(node)
|
2019-12-24 18:42:30 +03:00
|
|
|
case *parser.ForInStmt:
|
2019-12-20 22:40:38 +03:00
|
|
|
return c.compileForInStmt(node)
|
2019-12-24 18:42:30 +03:00
|
|
|
case *parser.BranchStmt:
|
2019-12-20 22:40:38 +03:00
|
|
|
if node.Token == token.Break {
|
|
|
|
curLoop := c.currentLoop()
|
|
|
|
if curLoop == nil {
|
|
|
|
return c.errorf(node, "break not allowed outside loop")
|
|
|
|
}
|
2019-12-24 18:42:30 +03:00
|
|
|
pos := c.emit(node, parser.OpJump, 0)
|
2019-12-20 22:40:38 +03:00
|
|
|
curLoop.Breaks = append(curLoop.Breaks, pos)
|
|
|
|
} else if node.Token == token.Continue {
|
|
|
|
curLoop := c.currentLoop()
|
|
|
|
if curLoop == nil {
|
|
|
|
return c.errorf(node, "continue not allowed outside loop")
|
|
|
|
}
|
2019-12-24 18:42:30 +03:00
|
|
|
pos := c.emit(node, parser.OpJump, 0)
|
2019-12-20 22:40:38 +03:00
|
|
|
curLoop.Continues = append(curLoop.Continues, pos)
|
|
|
|
} else {
|
|
|
|
panic(fmt.Errorf("invalid branch statement: %s",
|
|
|
|
node.Token.String()))
|
|
|
|
}
|
2019-12-24 18:42:30 +03:00
|
|
|
case *parser.BlockStmt:
|
2019-12-20 22:40:38 +03:00
|
|
|
if len(node.Stmts) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
c.symbolTable = c.symbolTable.Fork(true)
|
|
|
|
defer func() {
|
|
|
|
c.symbolTable = c.symbolTable.Parent(false)
|
|
|
|
}()
|
|
|
|
|
|
|
|
for _, stmt := range node.Stmts {
|
|
|
|
if err := c.Compile(stmt); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2019-12-24 18:42:30 +03:00
|
|
|
case *parser.AssignStmt:
|
2019-12-20 22:40:38 +03:00
|
|
|
err := c.compileAssign(node, node.LHS, node.RHS, node.Token)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-24 18:42:30 +03:00
|
|
|
case *parser.Ident:
|
2019-12-20 22:40:38 +03:00
|
|
|
symbol, _, ok := c.symbolTable.Resolve(node.Name)
|
|
|
|
if !ok {
|
|
|
|
return c.errorf(node, "unresolved reference '%s'", node.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch symbol.Scope {
|
2019-12-24 18:42:30 +03:00
|
|
|
case ScopeGlobal:
|
|
|
|
c.emit(node, parser.OpGetGlobal, symbol.Index)
|
|
|
|
case ScopeLocal:
|
|
|
|
c.emit(node, parser.OpGetLocal, symbol.Index)
|
|
|
|
case ScopeBuiltin:
|
|
|
|
c.emit(node, parser.OpGetBuiltin, symbol.Index)
|
|
|
|
case ScopeFree:
|
|
|
|
c.emit(node, parser.OpGetFree, symbol.Index)
|
|
|
|
}
|
|
|
|
case *parser.ArrayLit:
|
2019-12-20 22:40:38 +03:00
|
|
|
for _, elem := range node.Elements {
|
|
|
|
if err := c.Compile(elem); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(node, parser.OpArray, len(node.Elements))
|
|
|
|
case *parser.MapLit:
|
2019-12-20 22:40:38 +03:00
|
|
|
for _, elt := range node.Elements {
|
|
|
|
// key
|
|
|
|
if len(elt.Key) > MaxStringLen {
|
|
|
|
return c.error(node, ErrStringLimit)
|
|
|
|
}
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(node, parser.OpConstant,
|
2019-12-20 22:40:38 +03:00
|
|
|
c.addConstant(&String{Value: elt.Key}))
|
|
|
|
|
|
|
|
// value
|
|
|
|
if err := c.Compile(elt.Value); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(node, parser.OpMap, len(node.Elements)*2)
|
2019-12-20 22:40:38 +03:00
|
|
|
|
2019-12-24 18:42:30 +03:00
|
|
|
case *parser.SelectorExpr: // selector on RHS side
|
2019-12-20 22:40:38 +03:00
|
|
|
if err := c.Compile(node.Expr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := c.Compile(node.Sel); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(node, parser.OpIndex)
|
|
|
|
case *parser.IndexExpr:
|
2019-12-20 22:40:38 +03:00
|
|
|
if err := c.Compile(node.Expr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := c.Compile(node.Index); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(node, parser.OpIndex)
|
|
|
|
case *parser.SliceExpr:
|
2019-12-20 22:40:38 +03:00
|
|
|
if err := c.Compile(node.Expr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if node.Low != nil {
|
|
|
|
if err := c.Compile(node.Low); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(node, parser.OpNull)
|
2019-12-20 22:40:38 +03:00
|
|
|
}
|
|
|
|
if node.High != nil {
|
|
|
|
if err := c.Compile(node.High); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(node, parser.OpNull)
|
2019-12-20 22:40:38 +03:00
|
|
|
}
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(node, parser.OpSliceIndex)
|
|
|
|
case *parser.FuncLit:
|
2019-12-20 22:40:38 +03:00
|
|
|
c.enterScope()
|
|
|
|
|
|
|
|
for _, p := range node.Type.Params.List {
|
|
|
|
s := c.symbolTable.Define(p.Name)
|
|
|
|
|
|
|
|
// function arguments is not assigned directly.
|
|
|
|
s.LocalAssigned = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.Compile(node.Body); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// code optimization
|
|
|
|
c.optimizeFunc(node)
|
|
|
|
|
|
|
|
freeSymbols := c.symbolTable.FreeSymbols()
|
|
|
|
numLocals := c.symbolTable.MaxSymbols()
|
|
|
|
instructions, sourceMap := c.leaveScope()
|
|
|
|
|
|
|
|
for _, s := range freeSymbols {
|
|
|
|
switch s.Scope {
|
2019-12-24 18:42:30 +03:00
|
|
|
case ScopeLocal:
|
2019-12-20 22:40:38 +03:00
|
|
|
if !s.LocalAssigned {
|
|
|
|
// Here, the closure is capturing a local variable that's
|
|
|
|
// not yet assigned its value. One example is a local
|
|
|
|
// recursive function:
|
|
|
|
//
|
|
|
|
// func() {
|
|
|
|
// foo := func(x) {
|
|
|
|
// // ..
|
|
|
|
// return foo(x-1)
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// which translate into
|
|
|
|
//
|
|
|
|
// 0000 GETL 0
|
|
|
|
// 0002 CLOSURE ? 1
|
|
|
|
// 0006 DEFL 0
|
|
|
|
//
|
|
|
|
// . So the local variable (0) is being captured before
|
|
|
|
// it's assigned the value.
|
|
|
|
//
|
|
|
|
// Solution is to transform the code into something like
|
|
|
|
// this:
|
|
|
|
//
|
|
|
|
// func() {
|
|
|
|
// foo := undefined
|
|
|
|
// foo = func(x) {
|
|
|
|
// // ..
|
|
|
|
// return foo(x-1)
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// that is equivalent to
|
|
|
|
//
|
|
|
|
// 0000 NULL
|
|
|
|
// 0001 DEFL 0
|
|
|
|
// 0003 GETL 0
|
|
|
|
// 0005 CLOSURE ? 1
|
|
|
|
// 0009 SETL 0
|
|
|
|
//
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(node, parser.OpNull)
|
|
|
|
c.emit(node, parser.OpDefineLocal, s.Index)
|
2019-12-20 22:40:38 +03:00
|
|
|
s.LocalAssigned = true
|
|
|
|
}
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(node, parser.OpGetLocalPtr, s.Index)
|
|
|
|
case ScopeFree:
|
|
|
|
c.emit(node, parser.OpGetFreePtr, s.Index)
|
2019-12-20 22:40:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
compiledFunction := &CompiledFunction{
|
|
|
|
Instructions: instructions,
|
|
|
|
NumLocals: numLocals,
|
|
|
|
NumParameters: len(node.Type.Params.List),
|
|
|
|
VarArgs: node.Type.Params.VarArgs,
|
|
|
|
SourceMap: sourceMap,
|
|
|
|
}
|
|
|
|
if len(freeSymbols) > 0 {
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(node, parser.OpClosure,
|
2019-12-20 22:40:38 +03:00
|
|
|
c.addConstant(compiledFunction), len(freeSymbols))
|
|
|
|
} else {
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(node, parser.OpConstant, c.addConstant(compiledFunction))
|
2019-12-20 22:40:38 +03:00
|
|
|
}
|
2019-12-24 18:42:30 +03:00
|
|
|
case *parser.ReturnStmt:
|
2019-12-20 22:40:38 +03:00
|
|
|
if c.symbolTable.Parent(true) == nil {
|
|
|
|
// outside the function
|
|
|
|
return c.errorf(node, "return not allowed outside function")
|
|
|
|
}
|
|
|
|
|
|
|
|
if node.Result == nil {
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(node, parser.OpReturn, 0)
|
2019-12-20 22:40:38 +03:00
|
|
|
} else {
|
|
|
|
if err := c.Compile(node.Result); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(node, parser.OpReturn, 1)
|
2019-12-20 22:40:38 +03:00
|
|
|
}
|
2019-12-24 18:42:30 +03:00
|
|
|
case *parser.CallExpr:
|
2019-12-20 22:40:38 +03:00
|
|
|
if err := c.Compile(node.Func); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, arg := range node.Args {
|
|
|
|
if err := c.Compile(arg); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-06-08 19:54:24 +03:00
|
|
|
ellipsis := 0
|
|
|
|
if node.Ellipsis.IsValid() {
|
|
|
|
ellipsis = 1
|
|
|
|
}
|
|
|
|
c.emit(node, parser.OpCall, len(node.Args), ellipsis)
|
2019-12-24 18:42:30 +03:00
|
|
|
case *parser.ImportExpr:
|
2019-12-20 22:40:38 +03:00
|
|
|
if node.ModuleName == "" {
|
|
|
|
return c.errorf(node, "empty module name")
|
|
|
|
}
|
|
|
|
|
|
|
|
if mod := c.modules.Get(node.ModuleName); mod != nil {
|
|
|
|
v, err := mod.Import(node.ModuleName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch v := v.(type) {
|
|
|
|
case []byte: // module written in Tengo
|
|
|
|
compiled, err := c.compileModule(node,
|
2020-05-22 23:57:38 +03:00
|
|
|
node.ModuleName, v, false)
|
2019-12-20 22:40:38 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(node, parser.OpConstant, c.addConstant(compiled))
|
2020-06-08 19:54:24 +03:00
|
|
|
c.emit(node, parser.OpCall, 0, 0)
|
2019-12-20 22:40:38 +03:00
|
|
|
case Object: // builtin module
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(node, parser.OpConstant, c.addConstant(v))
|
2019-12-20 22:40:38 +03:00
|
|
|
default:
|
|
|
|
panic(fmt.Errorf("invalid import value type: %T", v))
|
|
|
|
}
|
|
|
|
} else if c.allowFileImport {
|
|
|
|
moduleName := node.ModuleName
|
|
|
|
if !strings.HasSuffix(moduleName, ".tengo") {
|
|
|
|
moduleName += ".tengo"
|
|
|
|
}
|
|
|
|
|
2020-05-22 23:57:38 +03:00
|
|
|
modulePath, err := filepath.Abs(
|
|
|
|
filepath.Join(c.importDir, moduleName))
|
2019-12-20 22:40:38 +03:00
|
|
|
if err != nil {
|
|
|
|
return c.errorf(node, "module file path error: %s",
|
|
|
|
err.Error())
|
|
|
|
}
|
|
|
|
|
2020-05-22 23:57:38 +03:00
|
|
|
moduleSrc, err := ioutil.ReadFile(modulePath)
|
2019-12-20 22:40:38 +03:00
|
|
|
if err != nil {
|
|
|
|
return c.errorf(node, "module file read error: %s",
|
|
|
|
err.Error())
|
|
|
|
}
|
|
|
|
|
2020-05-22 23:57:38 +03:00
|
|
|
compiled, err := c.compileModule(node, modulePath, moduleSrc, true)
|
2019-12-20 22:40:38 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(node, parser.OpConstant, c.addConstant(compiled))
|
2020-06-08 19:54:24 +03:00
|
|
|
c.emit(node, parser.OpCall, 0, 0)
|
2019-12-20 22:40:38 +03:00
|
|
|
} else {
|
|
|
|
return c.errorf(node, "module '%s' not found", node.ModuleName)
|
|
|
|
}
|
2019-12-24 18:42:30 +03:00
|
|
|
case *parser.ExportStmt:
|
2019-12-20 22:40:38 +03:00
|
|
|
// export statement must be in top-level scope
|
|
|
|
if c.scopeIndex != 0 {
|
|
|
|
return c.errorf(node, "export not allowed inside function")
|
|
|
|
}
|
|
|
|
|
|
|
|
// export statement is simply ignore when compiling non-module code
|
|
|
|
if c.parent == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err := c.Compile(node.Result); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(node, parser.OpImmutable)
|
|
|
|
c.emit(node, parser.OpReturn, 1)
|
|
|
|
case *parser.ErrorExpr:
|
2019-12-20 22:40:38 +03:00
|
|
|
if err := c.Compile(node.Expr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(node, parser.OpError)
|
|
|
|
case *parser.ImmutableExpr:
|
2019-12-20 22:40:38 +03:00
|
|
|
if err := c.Compile(node.Expr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(node, parser.OpImmutable)
|
|
|
|
case *parser.CondExpr:
|
2019-12-20 22:40:38 +03:00
|
|
|
if err := c.Compile(node.Cond); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// first jump placeholder
|
2019-12-24 18:42:30 +03:00
|
|
|
jumpPos1 := c.emit(node, parser.OpJumpFalsy, 0)
|
2019-12-20 22:40:38 +03:00
|
|
|
if err := c.Compile(node.True); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// second jump placeholder
|
2019-12-24 18:42:30 +03:00
|
|
|
jumpPos2 := c.emit(node, parser.OpJump, 0)
|
2019-12-20 22:40:38 +03:00
|
|
|
|
|
|
|
// update first jump offset
|
|
|
|
curPos := len(c.currentInstructions())
|
|
|
|
c.changeOperand(jumpPos1, curPos)
|
|
|
|
if err := c.Compile(node.False); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// update second jump offset
|
|
|
|
curPos = len(c.currentInstructions())
|
|
|
|
c.changeOperand(jumpPos2, curPos)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bytecode returns a compiled bytecode.
|
|
|
|
func (c *Compiler) Bytecode() *Bytecode {
|
|
|
|
return &Bytecode{
|
|
|
|
FileSet: c.file.Set(),
|
|
|
|
MainFunction: &CompiledFunction{
|
2019-12-24 18:42:30 +03:00
|
|
|
Instructions: append(c.currentInstructions(), parser.OpSuspend),
|
2019-12-20 22:40:38 +03:00
|
|
|
SourceMap: c.currentSourceMap(),
|
|
|
|
},
|
|
|
|
Constants: c.constants,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// EnableFileImport enables or disables module loading from local files.
|
|
|
|
// Local file modules are disabled by default.
|
|
|
|
func (c *Compiler) EnableFileImport(enable bool) {
|
|
|
|
c.allowFileImport = enable
|
|
|
|
}
|
|
|
|
|
2020-05-22 23:57:38 +03:00
|
|
|
// SetImportDir sets the initial import directory path for file imports.
|
|
|
|
func (c *Compiler) SetImportDir(dir string) {
|
|
|
|
c.importDir = dir
|
|
|
|
}
|
|
|
|
|
2019-12-20 22:40:38 +03:00
|
|
|
func (c *Compiler) compileAssign(
|
2019-12-24 18:42:30 +03:00
|
|
|
node parser.Node,
|
|
|
|
lhs, rhs []parser.Expr,
|
2019-12-20 22:40:38 +03:00
|
|
|
op token.Token,
|
|
|
|
) error {
|
|
|
|
numLHS, numRHS := len(lhs), len(rhs)
|
|
|
|
if numLHS > 1 || numRHS > 1 {
|
|
|
|
return c.errorf(node, "tuple assignment not allowed")
|
|
|
|
}
|
|
|
|
|
|
|
|
// resolve and compile left-hand side
|
|
|
|
ident, selectors := resolveAssignLHS(lhs[0])
|
|
|
|
numSel := len(selectors)
|
|
|
|
|
|
|
|
if op == token.Define && numSel > 0 {
|
|
|
|
// using selector on new variable does not make sense
|
|
|
|
return c.errorf(node, "operator ':=' not allowed with selector")
|
|
|
|
}
|
|
|
|
|
|
|
|
symbol, depth, exists := c.symbolTable.Resolve(ident)
|
|
|
|
if op == token.Define {
|
|
|
|
if depth == 0 && exists {
|
|
|
|
return c.errorf(node, "'%s' redeclared in this block", ident)
|
|
|
|
}
|
|
|
|
symbol = c.symbolTable.Define(ident)
|
|
|
|
} else {
|
|
|
|
if !exists {
|
|
|
|
return c.errorf(node, "unresolved reference '%s'", ident)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// +=, -=, *=, /=
|
|
|
|
if op != token.Assign && op != token.Define {
|
|
|
|
if err := c.Compile(lhs[0]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// compile RHSs
|
|
|
|
for _, expr := range rhs {
|
|
|
|
if err := c.Compile(expr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch op {
|
|
|
|
case token.AddAssign:
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(node, parser.OpBinaryOp, int(token.Add))
|
2019-12-20 22:40:38 +03:00
|
|
|
case token.SubAssign:
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(node, parser.OpBinaryOp, int(token.Sub))
|
2019-12-20 22:40:38 +03:00
|
|
|
case token.MulAssign:
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(node, parser.OpBinaryOp, int(token.Mul))
|
2019-12-20 22:40:38 +03:00
|
|
|
case token.QuoAssign:
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(node, parser.OpBinaryOp, int(token.Quo))
|
2019-12-20 22:40:38 +03:00
|
|
|
case token.RemAssign:
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(node, parser.OpBinaryOp, int(token.Rem))
|
2019-12-20 22:40:38 +03:00
|
|
|
case token.AndAssign:
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(node, parser.OpBinaryOp, int(token.And))
|
2019-12-20 22:40:38 +03:00
|
|
|
case token.OrAssign:
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(node, parser.OpBinaryOp, int(token.Or))
|
2019-12-20 22:40:38 +03:00
|
|
|
case token.AndNotAssign:
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(node, parser.OpBinaryOp, int(token.AndNot))
|
2019-12-20 22:40:38 +03:00
|
|
|
case token.XorAssign:
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(node, parser.OpBinaryOp, int(token.Xor))
|
2019-12-20 22:40:38 +03:00
|
|
|
case token.ShlAssign:
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(node, parser.OpBinaryOp, int(token.Shl))
|
2019-12-20 22:40:38 +03:00
|
|
|
case token.ShrAssign:
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(node, parser.OpBinaryOp, int(token.Shr))
|
2019-12-20 22:40:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// compile selector expressions (right to left)
|
|
|
|
for i := numSel - 1; i >= 0; i-- {
|
|
|
|
if err := c.Compile(selectors[i]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch symbol.Scope {
|
2019-12-24 18:42:30 +03:00
|
|
|
case ScopeGlobal:
|
2019-12-20 22:40:38 +03:00
|
|
|
if numSel > 0 {
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(node, parser.OpSetSelGlobal, symbol.Index, numSel)
|
2019-12-20 22:40:38 +03:00
|
|
|
} else {
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(node, parser.OpSetGlobal, symbol.Index)
|
2019-12-20 22:40:38 +03:00
|
|
|
}
|
2019-12-24 18:42:30 +03:00
|
|
|
case ScopeLocal:
|
2019-12-20 22:40:38 +03:00
|
|
|
if numSel > 0 {
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(node, parser.OpSetSelLocal, symbol.Index, numSel)
|
2019-12-20 22:40:38 +03:00
|
|
|
} else {
|
|
|
|
if op == token.Define && !symbol.LocalAssigned {
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(node, parser.OpDefineLocal, symbol.Index)
|
2019-12-20 22:40:38 +03:00
|
|
|
} else {
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(node, parser.OpSetLocal, symbol.Index)
|
2019-12-20 22:40:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// mark the symbol as local-assigned
|
|
|
|
symbol.LocalAssigned = true
|
2019-12-24 18:42:30 +03:00
|
|
|
case ScopeFree:
|
2019-12-20 22:40:38 +03:00
|
|
|
if numSel > 0 {
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(node, parser.OpSetSelFree, symbol.Index, numSel)
|
2019-12-20 22:40:38 +03:00
|
|
|
} else {
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(node, parser.OpSetFree, symbol.Index)
|
2019-12-20 22:40:38 +03:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
panic(fmt.Errorf("invalid assignment variable scope: %s",
|
|
|
|
symbol.Scope))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-24 18:42:30 +03:00
|
|
|
func (c *Compiler) compileLogical(node *parser.BinaryExpr) error {
|
2019-12-20 22:40:38 +03:00
|
|
|
// left side term
|
|
|
|
if err := c.Compile(node.LHS); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// jump position
|
|
|
|
var jumpPos int
|
|
|
|
if node.Token == token.LAnd {
|
2019-12-24 18:42:30 +03:00
|
|
|
jumpPos = c.emit(node, parser.OpAndJump, 0)
|
2019-12-20 22:40:38 +03:00
|
|
|
} else {
|
2019-12-24 18:42:30 +03:00
|
|
|
jumpPos = c.emit(node, parser.OpOrJump, 0)
|
2019-12-20 22:40:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// right side term
|
|
|
|
if err := c.Compile(node.RHS); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.changeOperand(jumpPos, len(c.currentInstructions()))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-24 18:42:30 +03:00
|
|
|
func (c *Compiler) compileForStmt(stmt *parser.ForStmt) error {
|
2019-12-20 22:40:38 +03:00
|
|
|
c.symbolTable = c.symbolTable.Fork(true)
|
|
|
|
defer func() {
|
|
|
|
c.symbolTable = c.symbolTable.Parent(false)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// init statement
|
|
|
|
if stmt.Init != nil {
|
|
|
|
if err := c.Compile(stmt.Init); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// pre-condition position
|
|
|
|
preCondPos := len(c.currentInstructions())
|
|
|
|
|
|
|
|
// condition expression
|
|
|
|
postCondPos := -1
|
|
|
|
if stmt.Cond != nil {
|
|
|
|
if err := c.Compile(stmt.Cond); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// condition jump position
|
2019-12-24 18:42:30 +03:00
|
|
|
postCondPos = c.emit(stmt, parser.OpJumpFalsy, 0)
|
2019-12-20 22:40:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// enter loop
|
|
|
|
loop := c.enterLoop()
|
|
|
|
|
|
|
|
// body statement
|
|
|
|
if err := c.Compile(stmt.Body); err != nil {
|
|
|
|
c.leaveLoop()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.leaveLoop()
|
|
|
|
|
|
|
|
// post-body position
|
|
|
|
postBodyPos := len(c.currentInstructions())
|
|
|
|
|
|
|
|
// post statement
|
|
|
|
if stmt.Post != nil {
|
|
|
|
if err := c.Compile(stmt.Post); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// back to condition
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(stmt, parser.OpJump, preCondPos)
|
2019-12-20 22:40:38 +03:00
|
|
|
|
|
|
|
// post-statement position
|
|
|
|
postStmtPos := len(c.currentInstructions())
|
|
|
|
if postCondPos >= 0 {
|
|
|
|
c.changeOperand(postCondPos, postStmtPos)
|
|
|
|
}
|
|
|
|
|
|
|
|
// update all break/continue jump positions
|
|
|
|
for _, pos := range loop.Breaks {
|
|
|
|
c.changeOperand(pos, postStmtPos)
|
|
|
|
}
|
|
|
|
for _, pos := range loop.Continues {
|
|
|
|
c.changeOperand(pos, postBodyPos)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-24 18:42:30 +03:00
|
|
|
func (c *Compiler) compileForInStmt(stmt *parser.ForInStmt) error {
|
2019-12-20 22:40:38 +03:00
|
|
|
c.symbolTable = c.symbolTable.Fork(true)
|
|
|
|
defer func() {
|
|
|
|
c.symbolTable = c.symbolTable.Parent(false)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// for-in statement is compiled like following:
|
|
|
|
//
|
|
|
|
// for :it := iterator(iterable); :it.next(); {
|
|
|
|
// k, v := :it.get() // DEFINE operator
|
|
|
|
//
|
|
|
|
// ... body ...
|
|
|
|
// }
|
|
|
|
//
|
2020-05-22 21:38:29 +03:00
|
|
|
// ":it" is a local variable but it will not conflict with other user variables
|
|
|
|
// because character ":" is not allowed in the variable names.
|
2019-12-20 22:40:38 +03:00
|
|
|
|
|
|
|
// init
|
|
|
|
// :it = iterator(iterable)
|
|
|
|
itSymbol := c.symbolTable.Define(":it")
|
|
|
|
if err := c.Compile(stmt.Iterable); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(stmt, parser.OpIteratorInit)
|
|
|
|
if itSymbol.Scope == ScopeGlobal {
|
|
|
|
c.emit(stmt, parser.OpSetGlobal, itSymbol.Index)
|
2019-12-20 22:40:38 +03:00
|
|
|
} else {
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(stmt, parser.OpDefineLocal, itSymbol.Index)
|
2019-12-20 22:40:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// pre-condition position
|
|
|
|
preCondPos := len(c.currentInstructions())
|
|
|
|
|
|
|
|
// condition
|
|
|
|
// :it.HasMore()
|
2019-12-24 18:42:30 +03:00
|
|
|
if itSymbol.Scope == ScopeGlobal {
|
|
|
|
c.emit(stmt, parser.OpGetGlobal, itSymbol.Index)
|
2019-12-20 22:40:38 +03:00
|
|
|
} else {
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(stmt, parser.OpGetLocal, itSymbol.Index)
|
2019-12-20 22:40:38 +03:00
|
|
|
}
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(stmt, parser.OpIteratorNext)
|
2019-12-20 22:40:38 +03:00
|
|
|
|
|
|
|
// condition jump position
|
2019-12-24 18:42:30 +03:00
|
|
|
postCondPos := c.emit(stmt, parser.OpJumpFalsy, 0)
|
2019-12-20 22:40:38 +03:00
|
|
|
|
|
|
|
// enter loop
|
|
|
|
loop := c.enterLoop()
|
|
|
|
|
|
|
|
// assign key variable
|
|
|
|
if stmt.Key.Name != "_" {
|
|
|
|
keySymbol := c.symbolTable.Define(stmt.Key.Name)
|
2019-12-24 18:42:30 +03:00
|
|
|
if itSymbol.Scope == ScopeGlobal {
|
|
|
|
c.emit(stmt, parser.OpGetGlobal, itSymbol.Index)
|
2019-12-20 22:40:38 +03:00
|
|
|
} else {
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(stmt, parser.OpGetLocal, itSymbol.Index)
|
2019-12-20 22:40:38 +03:00
|
|
|
}
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(stmt, parser.OpIteratorKey)
|
|
|
|
if keySymbol.Scope == ScopeGlobal {
|
|
|
|
c.emit(stmt, parser.OpSetGlobal, keySymbol.Index)
|
2019-12-20 22:40:38 +03:00
|
|
|
} else {
|
2020-05-22 21:38:29 +03:00
|
|
|
keySymbol.LocalAssigned = true
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(stmt, parser.OpDefineLocal, keySymbol.Index)
|
2019-12-20 22:40:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// assign value variable
|
|
|
|
if stmt.Value.Name != "_" {
|
|
|
|
valueSymbol := c.symbolTable.Define(stmt.Value.Name)
|
2019-12-24 18:42:30 +03:00
|
|
|
if itSymbol.Scope == ScopeGlobal {
|
|
|
|
c.emit(stmt, parser.OpGetGlobal, itSymbol.Index)
|
2019-12-20 22:40:38 +03:00
|
|
|
} else {
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(stmt, parser.OpGetLocal, itSymbol.Index)
|
2019-12-20 22:40:38 +03:00
|
|
|
}
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(stmt, parser.OpIteratorValue)
|
|
|
|
if valueSymbol.Scope == ScopeGlobal {
|
|
|
|
c.emit(stmt, parser.OpSetGlobal, valueSymbol.Index)
|
2019-12-20 22:40:38 +03:00
|
|
|
} else {
|
2020-05-22 21:38:29 +03:00
|
|
|
valueSymbol.LocalAssigned = true
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(stmt, parser.OpDefineLocal, valueSymbol.Index)
|
2019-12-20 22:40:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// body statement
|
|
|
|
if err := c.Compile(stmt.Body); err != nil {
|
|
|
|
c.leaveLoop()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.leaveLoop()
|
|
|
|
|
|
|
|
// post-body position
|
|
|
|
postBodyPos := len(c.currentInstructions())
|
|
|
|
|
|
|
|
// back to condition
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(stmt, parser.OpJump, preCondPos)
|
2019-12-20 22:40:38 +03:00
|
|
|
|
|
|
|
// post-statement position
|
|
|
|
postStmtPos := len(c.currentInstructions())
|
|
|
|
c.changeOperand(postCondPos, postStmtPos)
|
|
|
|
|
|
|
|
// update all break/continue jump positions
|
|
|
|
for _, pos := range loop.Breaks {
|
|
|
|
c.changeOperand(pos, postStmtPos)
|
|
|
|
}
|
|
|
|
for _, pos := range loop.Continues {
|
|
|
|
c.changeOperand(pos, postBodyPos)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Compiler) checkCyclicImports(
|
2019-12-24 18:42:30 +03:00
|
|
|
node parser.Node,
|
2019-12-20 22:40:38 +03:00
|
|
|
modulePath string,
|
|
|
|
) error {
|
|
|
|
if c.modulePath == modulePath {
|
|
|
|
return c.errorf(node, "cyclic module import: %s", modulePath)
|
|
|
|
} else if c.parent != nil {
|
|
|
|
return c.parent.checkCyclicImports(node, modulePath)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Compiler) compileModule(
|
2019-12-24 18:42:30 +03:00
|
|
|
node parser.Node,
|
2020-05-22 23:57:38 +03:00
|
|
|
modulePath string,
|
2019-12-20 22:40:38 +03:00
|
|
|
src []byte,
|
2020-05-22 23:57:38 +03:00
|
|
|
isFile bool,
|
2019-12-20 22:40:38 +03:00
|
|
|
) (*CompiledFunction, error) {
|
|
|
|
if err := c.checkCyclicImports(node, modulePath); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
compiledModule, exists := c.loadCompiledModule(modulePath)
|
|
|
|
if exists {
|
|
|
|
return compiledModule, nil
|
|
|
|
}
|
|
|
|
|
2020-05-22 23:57:38 +03:00
|
|
|
modFile := c.file.Set().AddFile(modulePath, -1, len(src))
|
2019-12-24 18:42:30 +03:00
|
|
|
p := parser.NewParser(modFile, src, nil)
|
2019-12-20 22:40:38 +03:00
|
|
|
file, err := p.ParseFile()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// inherit builtin functions
|
2019-12-24 18:42:30 +03:00
|
|
|
symbolTable := NewSymbolTable()
|
2019-12-20 22:40:38 +03:00
|
|
|
for _, sym := range c.symbolTable.BuiltinSymbols() {
|
|
|
|
symbolTable.DefineBuiltin(sym.Index, sym.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// no global scope for the module
|
|
|
|
symbolTable = symbolTable.Fork(false)
|
|
|
|
|
|
|
|
// compile module
|
2020-05-22 23:57:38 +03:00
|
|
|
moduleCompiler := c.fork(modFile, modulePath, symbolTable, isFile)
|
2019-12-20 22:40:38 +03:00
|
|
|
if err := moduleCompiler.Compile(file); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// code optimization
|
|
|
|
moduleCompiler.optimizeFunc(node)
|
|
|
|
compiledFunc := moduleCompiler.Bytecode().MainFunction
|
|
|
|
compiledFunc.NumLocals = symbolTable.MaxSymbols()
|
|
|
|
c.storeCompiledModule(modulePath, compiledFunc)
|
|
|
|
return compiledFunc, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Compiler) loadCompiledModule(
|
|
|
|
modulePath string,
|
|
|
|
) (mod *CompiledFunction, ok bool) {
|
|
|
|
if c.parent != nil {
|
|
|
|
return c.parent.loadCompiledModule(modulePath)
|
|
|
|
}
|
|
|
|
mod, ok = c.compiledModules[modulePath]
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Compiler) storeCompiledModule(
|
|
|
|
modulePath string,
|
|
|
|
module *CompiledFunction,
|
|
|
|
) {
|
|
|
|
if c.parent != nil {
|
|
|
|
c.parent.storeCompiledModule(modulePath, module)
|
|
|
|
}
|
|
|
|
c.compiledModules[modulePath] = module
|
|
|
|
}
|
|
|
|
|
2019-12-24 18:42:30 +03:00
|
|
|
func (c *Compiler) enterLoop() *loop {
|
|
|
|
loop := &loop{}
|
2019-12-20 22:40:38 +03:00
|
|
|
c.loops = append(c.loops, loop)
|
|
|
|
c.loopIndex++
|
|
|
|
if c.trace != nil {
|
|
|
|
c.printTrace("LOOPE", c.loopIndex)
|
|
|
|
}
|
|
|
|
return loop
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Compiler) leaveLoop() {
|
|
|
|
if c.trace != nil {
|
|
|
|
c.printTrace("LOOPL", c.loopIndex)
|
|
|
|
}
|
|
|
|
c.loops = c.loops[:len(c.loops)-1]
|
|
|
|
c.loopIndex--
|
|
|
|
}
|
|
|
|
|
2019-12-24 18:42:30 +03:00
|
|
|
func (c *Compiler) currentLoop() *loop {
|
2019-12-20 22:40:38 +03:00
|
|
|
if c.loopIndex >= 0 {
|
|
|
|
return c.loops[c.loopIndex]
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Compiler) currentInstructions() []byte {
|
|
|
|
return c.scopes[c.scopeIndex].Instructions
|
|
|
|
}
|
|
|
|
|
2019-12-24 18:42:30 +03:00
|
|
|
func (c *Compiler) currentSourceMap() map[int]parser.Pos {
|
2019-12-20 22:40:38 +03:00
|
|
|
return c.scopes[c.scopeIndex].SourceMap
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Compiler) enterScope() {
|
2019-12-24 18:42:30 +03:00
|
|
|
scope := compilationScope{
|
2019-12-20 22:40:38 +03:00
|
|
|
SymbolInit: make(map[string]bool),
|
2019-12-24 18:42:30 +03:00
|
|
|
SourceMap: make(map[int]parser.Pos),
|
2019-12-20 22:40:38 +03:00
|
|
|
}
|
|
|
|
c.scopes = append(c.scopes, scope)
|
|
|
|
c.scopeIndex++
|
|
|
|
c.symbolTable = c.symbolTable.Fork(false)
|
|
|
|
if c.trace != nil {
|
|
|
|
c.printTrace("SCOPE", c.scopeIndex)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Compiler) leaveScope() (
|
|
|
|
instructions []byte,
|
2019-12-24 18:42:30 +03:00
|
|
|
sourceMap map[int]parser.Pos,
|
2019-12-20 22:40:38 +03:00
|
|
|
) {
|
|
|
|
instructions = c.currentInstructions()
|
|
|
|
sourceMap = c.currentSourceMap()
|
|
|
|
c.scopes = c.scopes[:len(c.scopes)-1]
|
|
|
|
c.scopeIndex--
|
|
|
|
c.symbolTable = c.symbolTable.Parent(true)
|
|
|
|
if c.trace != nil {
|
|
|
|
c.printTrace("SCOPL", c.scopeIndex)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Compiler) fork(
|
2019-12-24 18:42:30 +03:00
|
|
|
file *parser.SourceFile,
|
2019-12-20 22:40:38 +03:00
|
|
|
modulePath string,
|
2019-12-24 18:42:30 +03:00
|
|
|
symbolTable *SymbolTable,
|
2020-05-22 23:57:38 +03:00
|
|
|
isFile bool,
|
2019-12-20 22:40:38 +03:00
|
|
|
) *Compiler {
|
|
|
|
child := NewCompiler(file, symbolTable, nil, c.modules, c.trace)
|
|
|
|
child.modulePath = modulePath // module file path
|
|
|
|
child.parent = c // parent to set to current compiler
|
2020-05-19 17:24:03 +03:00
|
|
|
child.allowFileImport = c.allowFileImport
|
2020-05-22 23:57:38 +03:00
|
|
|
child.importDir = c.importDir
|
|
|
|
if isFile && c.importDir != "" {
|
|
|
|
child.importDir = filepath.Dir(modulePath)
|
|
|
|
}
|
2019-12-20 22:40:38 +03:00
|
|
|
return child
|
|
|
|
}
|
|
|
|
|
2019-12-24 18:42:30 +03:00
|
|
|
func (c *Compiler) error(node parser.Node, err error) error {
|
|
|
|
return &CompilerError{
|
2019-12-20 22:40:38 +03:00
|
|
|
FileSet: c.file.Set(),
|
|
|
|
Node: node,
|
|
|
|
Err: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Compiler) errorf(
|
2019-12-24 18:42:30 +03:00
|
|
|
node parser.Node,
|
2019-12-20 22:40:38 +03:00
|
|
|
format string,
|
|
|
|
args ...interface{},
|
|
|
|
) error {
|
2019-12-24 18:42:30 +03:00
|
|
|
return &CompilerError{
|
2019-12-20 22:40:38 +03:00
|
|
|
FileSet: c.file.Set(),
|
|
|
|
Node: node,
|
|
|
|
Err: fmt.Errorf(format, args...),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Compiler) addConstant(o Object) int {
|
|
|
|
if c.parent != nil {
|
|
|
|
// module compilers will use their parent's constants array
|
|
|
|
return c.parent.addConstant(o)
|
|
|
|
}
|
|
|
|
c.constants = append(c.constants, o)
|
|
|
|
if c.trace != nil {
|
|
|
|
c.printTrace(fmt.Sprintf("CONST %04d %s", len(c.constants)-1, o))
|
|
|
|
}
|
|
|
|
return len(c.constants) - 1
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Compiler) addInstruction(b []byte) int {
|
|
|
|
posNewIns := len(c.currentInstructions())
|
|
|
|
c.scopes[c.scopeIndex].Instructions = append(
|
|
|
|
c.currentInstructions(), b...)
|
|
|
|
return posNewIns
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Compiler) replaceInstruction(pos int, inst []byte) {
|
|
|
|
copy(c.currentInstructions()[pos:], inst)
|
|
|
|
if c.trace != nil {
|
|
|
|
c.printTrace(fmt.Sprintf("REPLC %s",
|
2019-12-24 18:42:30 +03:00
|
|
|
FormatInstructions(
|
2019-12-20 22:40:38 +03:00
|
|
|
c.scopes[c.scopeIndex].Instructions[pos:], pos)[0]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Compiler) changeOperand(opPos int, operand ...int) {
|
|
|
|
op := c.currentInstructions()[opPos]
|
2019-12-24 18:42:30 +03:00
|
|
|
inst := MakeInstruction(op, operand...)
|
2019-12-20 22:40:38 +03:00
|
|
|
c.replaceInstruction(opPos, inst)
|
|
|
|
}
|
|
|
|
|
|
|
|
// optimizeFunc performs some code-level optimization for the current function
|
|
|
|
// instructions. It also removes unreachable (dead code) instructions and adds
|
|
|
|
// "returns" instruction if needed.
|
2019-12-24 18:42:30 +03:00
|
|
|
func (c *Compiler) optimizeFunc(node parser.Node) {
|
2019-12-20 22:40:38 +03:00
|
|
|
// any instructions between RETURN and the function end
|
|
|
|
// or instructions between RETURN and jump target position
|
|
|
|
// are considered as unreachable.
|
|
|
|
|
|
|
|
// pass 1. identify all jump destinations
|
|
|
|
dsts := make(map[int]bool)
|
|
|
|
iterateInstructions(c.scopes[c.scopeIndex].Instructions,
|
2019-12-24 18:42:30 +03:00
|
|
|
func(pos int, opcode parser.Opcode, operands []int) bool {
|
2019-12-20 22:40:38 +03:00
|
|
|
switch opcode {
|
2019-12-24 18:42:30 +03:00
|
|
|
case parser.OpJump, parser.OpJumpFalsy,
|
|
|
|
parser.OpAndJump, parser.OpOrJump:
|
2019-12-20 22:40:38 +03:00
|
|
|
dsts[operands[0]] = true
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
// pass 2. eliminate dead code
|
|
|
|
var newInsts []byte
|
|
|
|
posMap := make(map[int]int) // old position to new position
|
|
|
|
var dstIdx int
|
|
|
|
var deadCode bool
|
|
|
|
iterateInstructions(c.scopes[c.scopeIndex].Instructions,
|
2019-12-24 18:42:30 +03:00
|
|
|
func(pos int, opcode parser.Opcode, operands []int) bool {
|
2019-12-20 22:40:38 +03:00
|
|
|
switch {
|
2019-12-24 18:42:30 +03:00
|
|
|
case opcode == parser.OpReturn:
|
2019-12-20 22:40:38 +03:00
|
|
|
if deadCode {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
deadCode = true
|
|
|
|
case dsts[pos]:
|
|
|
|
dstIdx++
|
|
|
|
deadCode = false
|
|
|
|
case deadCode:
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
posMap[pos] = len(newInsts)
|
|
|
|
newInsts = append(newInsts,
|
2019-12-24 18:42:30 +03:00
|
|
|
MakeInstruction(opcode, operands...)...)
|
2019-12-20 22:40:38 +03:00
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
// pass 3. update jump positions
|
2019-12-24 18:42:30 +03:00
|
|
|
var lastOp parser.Opcode
|
2019-12-20 22:40:38 +03:00
|
|
|
var appendReturn bool
|
|
|
|
endPos := len(c.scopes[c.scopeIndex].Instructions)
|
2020-05-19 17:00:53 +03:00
|
|
|
newEndPost := len(newInsts)
|
2019-12-20 22:40:38 +03:00
|
|
|
iterateInstructions(newInsts,
|
2019-12-24 18:42:30 +03:00
|
|
|
func(pos int, opcode parser.Opcode, operands []int) bool {
|
2019-12-20 22:40:38 +03:00
|
|
|
switch opcode {
|
2019-12-24 18:42:30 +03:00
|
|
|
case parser.OpJump, parser.OpJumpFalsy, parser.OpAndJump,
|
|
|
|
parser.OpOrJump:
|
2019-12-20 22:40:38 +03:00
|
|
|
newDst, ok := posMap[operands[0]]
|
|
|
|
if ok {
|
|
|
|
copy(newInsts[pos:],
|
2019-12-24 18:42:30 +03:00
|
|
|
MakeInstruction(opcode, newDst))
|
2019-12-20 22:40:38 +03:00
|
|
|
} else if endPos == operands[0] {
|
|
|
|
// there's a jump instruction that jumps to the end of
|
|
|
|
// function compiler should append "return".
|
2020-05-19 17:00:53 +03:00
|
|
|
copy(newInsts[pos:],
|
|
|
|
MakeInstruction(opcode, newEndPost))
|
2019-12-20 22:40:38 +03:00
|
|
|
appendReturn = true
|
|
|
|
} else {
|
|
|
|
panic(fmt.Errorf("invalid jump position: %d", newDst))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lastOp = opcode
|
|
|
|
return true
|
|
|
|
})
|
2019-12-24 18:42:30 +03:00
|
|
|
if lastOp != parser.OpReturn {
|
2019-12-20 22:40:38 +03:00
|
|
|
appendReturn = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// pass 4. update source map
|
2019-12-24 18:42:30 +03:00
|
|
|
newSourceMap := make(map[int]parser.Pos)
|
2019-12-20 22:40:38 +03:00
|
|
|
for pos, srcPos := range c.scopes[c.scopeIndex].SourceMap {
|
|
|
|
newPos, ok := posMap[pos]
|
|
|
|
if ok {
|
|
|
|
newSourceMap[newPos] = srcPos
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.scopes[c.scopeIndex].Instructions = newInsts
|
|
|
|
c.scopes[c.scopeIndex].SourceMap = newSourceMap
|
|
|
|
|
|
|
|
// append "return"
|
|
|
|
if appendReturn {
|
2019-12-24 18:42:30 +03:00
|
|
|
c.emit(node, parser.OpReturn, 0)
|
2019-12-20 22:40:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Compiler) emit(
|
2019-12-24 18:42:30 +03:00
|
|
|
node parser.Node,
|
|
|
|
opcode parser.Opcode,
|
2019-12-20 22:40:38 +03:00
|
|
|
operands ...int,
|
|
|
|
) int {
|
2019-12-24 18:42:30 +03:00
|
|
|
filePos := parser.NoPos
|
2019-12-20 22:40:38 +03:00
|
|
|
if node != nil {
|
|
|
|
filePos = node.Pos()
|
|
|
|
}
|
|
|
|
|
2019-12-24 18:42:30 +03:00
|
|
|
inst := MakeInstruction(opcode, operands...)
|
2019-12-20 22:40:38 +03:00
|
|
|
pos := c.addInstruction(inst)
|
|
|
|
c.scopes[c.scopeIndex].SourceMap[pos] = filePos
|
|
|
|
if c.trace != nil {
|
|
|
|
c.printTrace(fmt.Sprintf("EMIT %s",
|
2019-12-24 18:42:30 +03:00
|
|
|
FormatInstructions(
|
2019-12-20 22:40:38 +03:00
|
|
|
c.scopes[c.scopeIndex].Instructions[pos:], pos)[0]))
|
|
|
|
}
|
|
|
|
return pos
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Compiler) printTrace(a ...interface{}) {
|
|
|
|
const (
|
|
|
|
dots = ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . "
|
|
|
|
n = len(dots)
|
|
|
|
)
|
|
|
|
|
|
|
|
i := 2 * c.indent
|
|
|
|
for i > n {
|
|
|
|
_, _ = fmt.Fprint(c.trace, dots)
|
|
|
|
i -= n
|
|
|
|
}
|
|
|
|
_, _ = fmt.Fprint(c.trace, dots[0:i])
|
|
|
|
_, _ = fmt.Fprintln(c.trace, a...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resolveAssignLHS(
|
2019-12-24 18:42:30 +03:00
|
|
|
expr parser.Expr,
|
|
|
|
) (name string, selectors []parser.Expr) {
|
2019-12-20 22:40:38 +03:00
|
|
|
switch term := expr.(type) {
|
2019-12-24 18:42:30 +03:00
|
|
|
case *parser.SelectorExpr:
|
2019-12-20 22:40:38 +03:00
|
|
|
name, selectors = resolveAssignLHS(term.Expr)
|
|
|
|
selectors = append(selectors, term.Sel)
|
|
|
|
return
|
2019-12-24 18:42:30 +03:00
|
|
|
case *parser.IndexExpr:
|
2019-12-20 22:40:38 +03:00
|
|
|
name, selectors = resolveAssignLHS(term.Expr)
|
|
|
|
selectors = append(selectors, term.Index)
|
2019-12-24 18:42:30 +03:00
|
|
|
case *parser.Ident:
|
2019-12-20 22:40:38 +03:00
|
|
|
name = term.Name
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func iterateInstructions(
|
|
|
|
b []byte,
|
2019-12-24 18:42:30 +03:00
|
|
|
fn func(pos int, opcode parser.Opcode, operands []int) bool,
|
2019-12-20 22:40:38 +03:00
|
|
|
) {
|
|
|
|
for i := 0; i < len(b); i++ {
|
2019-12-24 18:42:30 +03:00
|
|
|
numOperands := parser.OpcodeOperands[b[i]]
|
|
|
|
operands, read := parser.ReadOperands(numOperands, b[i+1:])
|
2019-12-20 22:40:38 +03:00
|
|
|
if !fn(i, b[i], operands) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
i += read
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func tracec(c *Compiler, msg string) *Compiler {
|
|
|
|
c.printTrace(msg, "{")
|
|
|
|
c.indent++
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func untracec(c *Compiler) {
|
|
|
|
c.indent--
|
|
|
|
c.printTrace("}")
|
|
|
|
}
|