diff --git a/README.md b/README.md index a20f376..c70c2c7 100644 --- a/README.md +++ b/README.md @@ -121,7 +121,6 @@ each([a, b, c, d], func(x) { mul := compiled.Get("mul") fmt.Println(sum, mul) // "22 288" } - ``` ## References diff --git a/bytecode.go b/bytecode.go index c465693..460d339 100644 --- a/bytecode.go +++ b/bytecode.go @@ -6,12 +6,12 @@ import ( "io" "reflect" - "github.com/d5/tengo/internal" + "github.com/d5/tengo/parser" ) // Bytecode is a compiled instructions and constants. type Bytecode struct { - FileSet *internal.SourceFileSet + FileSet *parser.SourceFileSet MainFunction *CompiledFunction Constants []Object } @@ -40,7 +40,7 @@ func (b *Bytecode) CountObjects() int { // FormatInstructions returns human readable string representations of // compiled instructions. func (b *Bytecode) FormatInstructions() []string { - return internal.FormatInstructions(b.MainFunction.Instructions, 0) + return FormatInstructions(b.MainFunction.Instructions, 0) } // FormatConstants returns human readable string representations of @@ -51,7 +51,7 @@ func (b *Bytecode) FormatConstants() (output []string) { case *CompiledFunction: output = append(output, fmt.Sprintf( "[% 3d] (Compiled Function|%p)", cidx, &cn)) - for _, l := range internal.FormatInstructions(cn.Instructions, 0) { + for _, l := range FormatInstructions(cn.Instructions, 0) { output = append(output, fmt.Sprintf(" %s", l)) } default: @@ -239,25 +239,25 @@ func updateConstIndexes(insts []byte, indexMap map[int]int) { i := 0 for i < len(insts) { op := insts[i] - numOperands := internal.OpcodeOperands[op] - _, read := internal.ReadOperands(numOperands, insts[i+1:]) + numOperands := parser.OpcodeOperands[op] + _, read := parser.ReadOperands(numOperands, insts[i+1:]) switch op { - case internal.OpConstant: + case parser.OpConstant: curIdx := int(insts[i+2]) | int(insts[i+1])<<8 newIdx, ok := indexMap[curIdx] if !ok { panic(fmt.Errorf("constant index not found: %d", curIdx)) } - copy(insts[i:], internal.MakeInstruction(op, newIdx)) - case internal.OpClosure: + copy(insts[i:], MakeInstruction(op, newIdx)) + case parser.OpClosure: curIdx := int(insts[i+2]) | int(insts[i+1])<<8 numFree := int(insts[i+3]) newIdx, ok := indexMap[curIdx] if !ok { panic(fmt.Errorf("constant index not found: %d", curIdx)) } - copy(insts[i:], internal.MakeInstruction(op, newIdx, numFree)) + copy(insts[i:], MakeInstruction(op, newIdx, numFree)) } i += 1 + read @@ -272,8 +272,8 @@ func inferModuleName(mod *ImmutableMap) string { } func init() { - gob.Register(&internal.SourceFileSet{}) - gob.Register(&internal.SourceFile{}) + gob.Register(&parser.SourceFileSet{}) + gob.Register(&parser.SourceFile{}) gob.Register(&Array{}) gob.Register(&Bool{}) gob.Register(&Bytes{}) diff --git a/bytecode_test.go b/bytecode_test.go index 5a221ee..1aaf35c 100644 --- a/bytecode_test.go +++ b/bytecode_test.go @@ -6,8 +6,8 @@ import ( "time" "github.com/d5/tengo" - "github.com/d5/tengo/internal" - "github.com/d5/tengo/internal/require" + "github.com/d5/tengo/parser" + "github.com/d5/tengo/require" ) type srcfile struct { @@ -23,20 +23,20 @@ func TestBytecode(t *testing.T) { &tengo.Char{Value: 'y'}, &tengo.Float{Value: 93.11}, compiledFunction(1, 0, - internal.MakeInstruction(internal.OpConstant, 3), - internal.MakeInstruction(internal.OpSetLocal, 0), - internal.MakeInstruction(internal.OpGetGlobal, 0), - internal.MakeInstruction(internal.OpGetFree, 0)), + tengo.MakeInstruction(parser.OpConstant, 3), + tengo.MakeInstruction(parser.OpSetLocal, 0), + tengo.MakeInstruction(parser.OpGetGlobal, 0), + tengo.MakeInstruction(parser.OpGetFree, 0)), &tengo.Float{Value: 39.2}, &tengo.Int{Value: 192}, &tengo.String{Value: "bar"}))) testBytecodeSerialization(t, bytecodeFileSet( concatInsts( - internal.MakeInstruction(internal.OpConstant, 0), - internal.MakeInstruction(internal.OpSetGlobal, 0), - internal.MakeInstruction(internal.OpConstant, 6), - internal.MakeInstruction(internal.OpPop)), + tengo.MakeInstruction(parser.OpConstant, 0), + tengo.MakeInstruction(parser.OpSetGlobal, 0), + tengo.MakeInstruction(parser.OpConstant, 6), + tengo.MakeInstruction(parser.OpPop)), objectsArray( &tengo.Int{Value: 55}, &tengo.Int{Value: 66}, @@ -99,29 +99,29 @@ func TestBytecode(t *testing.T) { }, }, compiledFunction(1, 0, - internal.MakeInstruction(internal.OpConstant, 3), - internal.MakeInstruction(internal.OpSetLocal, 0), - internal.MakeInstruction(internal.OpGetGlobal, 0), - internal.MakeInstruction(internal.OpGetFree, 0), - internal.MakeInstruction(internal.OpBinaryOp, 11), - internal.MakeInstruction(internal.OpGetFree, 1), - internal.MakeInstruction(internal.OpBinaryOp, 11), - internal.MakeInstruction(internal.OpGetLocal, 0), - internal.MakeInstruction(internal.OpBinaryOp, 11), - internal.MakeInstruction(internal.OpReturn, 1)), + tengo.MakeInstruction(parser.OpConstant, 3), + tengo.MakeInstruction(parser.OpSetLocal, 0), + tengo.MakeInstruction(parser.OpGetGlobal, 0), + tengo.MakeInstruction(parser.OpGetFree, 0), + tengo.MakeInstruction(parser.OpBinaryOp, 11), + tengo.MakeInstruction(parser.OpGetFree, 1), + tengo.MakeInstruction(parser.OpBinaryOp, 11), + tengo.MakeInstruction(parser.OpGetLocal, 0), + tengo.MakeInstruction(parser.OpBinaryOp, 11), + tengo.MakeInstruction(parser.OpReturn, 1)), compiledFunction(1, 0, - internal.MakeInstruction(internal.OpConstant, 2), - internal.MakeInstruction(internal.OpSetLocal, 0), - internal.MakeInstruction(internal.OpGetFree, 0), - internal.MakeInstruction(internal.OpGetLocal, 0), - internal.MakeInstruction(internal.OpClosure, 4, 2), - internal.MakeInstruction(internal.OpReturn, 1)), + tengo.MakeInstruction(parser.OpConstant, 2), + tengo.MakeInstruction(parser.OpSetLocal, 0), + tengo.MakeInstruction(parser.OpGetFree, 0), + tengo.MakeInstruction(parser.OpGetLocal, 0), + tengo.MakeInstruction(parser.OpClosure, 4, 2), + tengo.MakeInstruction(parser.OpReturn, 1)), compiledFunction(1, 0, - internal.MakeInstruction(internal.OpConstant, 1), - internal.MakeInstruction(internal.OpSetLocal, 0), - internal.MakeInstruction(internal.OpGetLocal, 0), - internal.MakeInstruction(internal.OpClosure, 5, 1), - internal.MakeInstruction(internal.OpReturn, 1))), + tengo.MakeInstruction(parser.OpConstant, 1), + tengo.MakeInstruction(parser.OpSetLocal, 0), + tengo.MakeInstruction(parser.OpGetLocal, 0), + tengo.MakeInstruction(parser.OpClosure, 5, 1), + tengo.MakeInstruction(parser.OpReturn, 1))), fileSet(srcfile{name: "file1", size: 100}, srcfile{name: "file2", size: 200}))) } @@ -133,10 +133,10 @@ func TestBytecode_RemoveDuplicates(t *testing.T) { &tengo.Char{Value: 'y'}, &tengo.Float{Value: 93.11}, compiledFunction(1, 0, - internal.MakeInstruction(internal.OpConstant, 3), - internal.MakeInstruction(internal.OpSetLocal, 0), - internal.MakeInstruction(internal.OpGetGlobal, 0), - internal.MakeInstruction(internal.OpGetFree, 0)), + tengo.MakeInstruction(parser.OpConstant, 3), + tengo.MakeInstruction(parser.OpSetLocal, 0), + tengo.MakeInstruction(parser.OpGetGlobal, 0), + tengo.MakeInstruction(parser.OpGetFree, 0)), &tengo.Float{Value: 39.2}, &tengo.Int{Value: 192}, &tengo.String{Value: "bar"})), @@ -145,10 +145,10 @@ func TestBytecode_RemoveDuplicates(t *testing.T) { &tengo.Char{Value: 'y'}, &tengo.Float{Value: 93.11}, compiledFunction(1, 0, - internal.MakeInstruction(internal.OpConstant, 3), - internal.MakeInstruction(internal.OpSetLocal, 0), - internal.MakeInstruction(internal.OpGetGlobal, 0), - internal.MakeInstruction(internal.OpGetFree, 0)), + tengo.MakeInstruction(parser.OpConstant, 3), + tengo.MakeInstruction(parser.OpSetLocal, 0), + tengo.MakeInstruction(parser.OpGetGlobal, 0), + tengo.MakeInstruction(parser.OpGetFree, 0)), &tengo.Float{Value: 39.2}, &tengo.Int{Value: 192}, &tengo.String{Value: "bar"}))) @@ -156,63 +156,63 @@ func TestBytecode_RemoveDuplicates(t *testing.T) { testBytecodeRemoveDuplicates(t, bytecode( concatInsts( - internal.MakeInstruction(internal.OpConstant, 0), - internal.MakeInstruction(internal.OpConstant, 1), - internal.MakeInstruction(internal.OpConstant, 2), - internal.MakeInstruction(internal.OpConstant, 3), - internal.MakeInstruction(internal.OpConstant, 4), - internal.MakeInstruction(internal.OpConstant, 5), - internal.MakeInstruction(internal.OpConstant, 6), - internal.MakeInstruction(internal.OpConstant, 7), - internal.MakeInstruction(internal.OpConstant, 8), - internal.MakeInstruction(internal.OpClosure, 4, 1)), + tengo.MakeInstruction(parser.OpConstant, 0), + tengo.MakeInstruction(parser.OpConstant, 1), + tengo.MakeInstruction(parser.OpConstant, 2), + tengo.MakeInstruction(parser.OpConstant, 3), + tengo.MakeInstruction(parser.OpConstant, 4), + tengo.MakeInstruction(parser.OpConstant, 5), + tengo.MakeInstruction(parser.OpConstant, 6), + tengo.MakeInstruction(parser.OpConstant, 7), + tengo.MakeInstruction(parser.OpConstant, 8), + tengo.MakeInstruction(parser.OpClosure, 4, 1)), objectsArray( &tengo.Int{Value: 1}, &tengo.Float{Value: 2.0}, &tengo.Char{Value: '3'}, &tengo.String{Value: "four"}, compiledFunction(1, 0, - internal.MakeInstruction(internal.OpConstant, 3), - internal.MakeInstruction(internal.OpConstant, 7), - internal.MakeInstruction(internal.OpSetLocal, 0), - internal.MakeInstruction(internal.OpGetGlobal, 0), - internal.MakeInstruction(internal.OpGetFree, 0)), + tengo.MakeInstruction(parser.OpConstant, 3), + tengo.MakeInstruction(parser.OpConstant, 7), + tengo.MakeInstruction(parser.OpSetLocal, 0), + tengo.MakeInstruction(parser.OpGetGlobal, 0), + tengo.MakeInstruction(parser.OpGetFree, 0)), &tengo.Int{Value: 1}, &tengo.Float{Value: 2.0}, &tengo.Char{Value: '3'}, &tengo.String{Value: "four"})), bytecode( concatInsts( - internal.MakeInstruction(internal.OpConstant, 0), - internal.MakeInstruction(internal.OpConstant, 1), - internal.MakeInstruction(internal.OpConstant, 2), - internal.MakeInstruction(internal.OpConstant, 3), - internal.MakeInstruction(internal.OpConstant, 4), - internal.MakeInstruction(internal.OpConstant, 0), - internal.MakeInstruction(internal.OpConstant, 1), - internal.MakeInstruction(internal.OpConstant, 2), - internal.MakeInstruction(internal.OpConstant, 3), - internal.MakeInstruction(internal.OpClosure, 4, 1)), + tengo.MakeInstruction(parser.OpConstant, 0), + tengo.MakeInstruction(parser.OpConstant, 1), + tengo.MakeInstruction(parser.OpConstant, 2), + tengo.MakeInstruction(parser.OpConstant, 3), + tengo.MakeInstruction(parser.OpConstant, 4), + tengo.MakeInstruction(parser.OpConstant, 0), + tengo.MakeInstruction(parser.OpConstant, 1), + tengo.MakeInstruction(parser.OpConstant, 2), + tengo.MakeInstruction(parser.OpConstant, 3), + tengo.MakeInstruction(parser.OpClosure, 4, 1)), objectsArray( &tengo.Int{Value: 1}, &tengo.Float{Value: 2.0}, &tengo.Char{Value: '3'}, &tengo.String{Value: "four"}, compiledFunction(1, 0, - internal.MakeInstruction(internal.OpConstant, 3), - internal.MakeInstruction(internal.OpConstant, 2), - internal.MakeInstruction(internal.OpSetLocal, 0), - internal.MakeInstruction(internal.OpGetGlobal, 0), - internal.MakeInstruction(internal.OpGetFree, 0))))) + tengo.MakeInstruction(parser.OpConstant, 3), + tengo.MakeInstruction(parser.OpConstant, 2), + tengo.MakeInstruction(parser.OpSetLocal, 0), + tengo.MakeInstruction(parser.OpGetGlobal, 0), + tengo.MakeInstruction(parser.OpGetFree, 0))))) testBytecodeRemoveDuplicates(t, bytecode( concatInsts( - internal.MakeInstruction(internal.OpConstant, 0), - internal.MakeInstruction(internal.OpConstant, 1), - internal.MakeInstruction(internal.OpConstant, 2), - internal.MakeInstruction(internal.OpConstant, 3), - internal.MakeInstruction(internal.OpConstant, 4)), + tengo.MakeInstruction(parser.OpConstant, 0), + tengo.MakeInstruction(parser.OpConstant, 1), + tengo.MakeInstruction(parser.OpConstant, 2), + tengo.MakeInstruction(parser.OpConstant, 3), + tengo.MakeInstruction(parser.OpConstant, 4)), objectsArray( &tengo.Int{Value: 1}, &tengo.Int{Value: 2}, @@ -221,11 +221,11 @@ func TestBytecode_RemoveDuplicates(t *testing.T) { &tengo.Int{Value: 3})), bytecode( concatInsts( - internal.MakeInstruction(internal.OpConstant, 0), - internal.MakeInstruction(internal.OpConstant, 1), - internal.MakeInstruction(internal.OpConstant, 2), - internal.MakeInstruction(internal.OpConstant, 0), - internal.MakeInstruction(internal.OpConstant, 2)), + tengo.MakeInstruction(parser.OpConstant, 0), + tengo.MakeInstruction(parser.OpConstant, 1), + tengo.MakeInstruction(parser.OpConstant, 2), + tengo.MakeInstruction(parser.OpConstant, 0), + tengo.MakeInstruction(parser.OpConstant, 2)), objectsArray( &tengo.Int{Value: 1}, &tengo.Int{Value: 2}, @@ -241,19 +241,19 @@ func TestBytecode_CountObjects(t *testing.T) { &tengo.Int{Value: 77}, &tengo.Int{Value: 88}, compiledFunction(1, 0, - internal.MakeInstruction(internal.OpConstant, 3), - internal.MakeInstruction(internal.OpReturn, 1)), + tengo.MakeInstruction(parser.OpConstant, 3), + tengo.MakeInstruction(parser.OpReturn, 1)), compiledFunction(1, 0, - internal.MakeInstruction(internal.OpConstant, 2), - internal.MakeInstruction(internal.OpReturn, 1)), + tengo.MakeInstruction(parser.OpConstant, 2), + tengo.MakeInstruction(parser.OpReturn, 1)), compiledFunction(1, 0, - internal.MakeInstruction(internal.OpConstant, 1), - internal.MakeInstruction(internal.OpReturn, 1)))) + tengo.MakeInstruction(parser.OpConstant, 1), + tengo.MakeInstruction(parser.OpReturn, 1)))) require.Equal(t, 7, b.CountObjects()) } -func fileSet(files ...srcfile) *internal.SourceFileSet { - fileSet := internal.NewFileSet() +func fileSet(files ...srcfile) *parser.SourceFileSet { + fileSet := parser.NewFileSet() for _, f := range files { fileSet.AddFile(f.name, -1, f.size) } @@ -263,7 +263,7 @@ func fileSet(files ...srcfile) *internal.SourceFileSet { func bytecodeFileSet( instructions []byte, constants []tengo.Object, - fileSet *internal.SourceFileSet, + fileSet *parser.SourceFileSet, ) *tengo.Bytecode { return &tengo.Bytecode{ FileSet: fileSet, diff --git a/cmd/bench/main.go b/cmd/bench/main.go index b36f12e..1e7ddd7 100644 --- a/cmd/bench/main.go +++ b/cmd/bench/main.go @@ -5,7 +5,7 @@ import ( "time" "github.com/d5/tengo" - "github.com/d5/tengo/internal" + "github.com/d5/tengo/parser" ) func main() { @@ -163,7 +163,7 @@ func runBench( result tengo.Object, err error, ) { - var astFile *internal.File + var astFile *parser.File parseTime, astFile, err = parse(input) if err != nil { return @@ -180,13 +180,13 @@ func runBench( return } -func parse(input []byte) (time.Duration, *internal.File, error) { - fileSet := internal.NewFileSet() +func parse(input []byte) (time.Duration, *parser.File, error) { + fileSet := parser.NewFileSet() inputFile := fileSet.AddFile("bench", -1, len(input)) start := time.Now() - p := internal.NewParser(inputFile, input, nil) + p := parser.NewParser(inputFile, input, nil) file, err := p.ParseFile() if err != nil { return time.Since(start), nil, err @@ -195,8 +195,8 @@ func parse(input []byte) (time.Duration, *internal.File, error) { return time.Since(start), file, nil } -func compileFile(file *internal.File) (time.Duration, *tengo.Bytecode, error) { - symTable := internal.NewSymbolTable() +func compileFile(file *parser.File) (time.Duration, *tengo.Bytecode, error) { + symTable := tengo.NewSymbolTable() symTable.Define("out") start := time.Now() diff --git a/cmd/tengo/main.go b/cmd/tengo/main.go index e6e514d..3eb97be 100644 --- a/cmd/tengo/main.go +++ b/cmd/tengo/main.go @@ -12,7 +12,7 @@ import ( "strings" "github.com/d5/tengo" - "github.com/d5/tengo/internal" + "github.com/d5/tengo/parser" "github.com/d5/tengo/stdlib" ) @@ -148,9 +148,9 @@ func RunCompiled(modules *tengo.ModuleMap, data []byte) (err error) { // RunREPL starts REPL. func RunREPL(modules *tengo.ModuleMap, in io.Reader, out io.Writer) { stdin := bufio.NewScanner(in) - fileSet := internal.NewFileSet() + fileSet := parser.NewFileSet() globals := make([]tengo.Object, tengo.GlobalsSize) - symbolTable := internal.NewSymbolTable() + symbolTable := tengo.NewSymbolTable() for idx, fn := range tengo.GetAllBuiltinFunctions() { symbolTable.DefineBuiltin(idx, fn.Name) } @@ -185,7 +185,7 @@ func RunREPL(modules *tengo.ModuleMap, in io.Reader, out io.Writer) { line := stdin.Text() srcFile := fileSet.AddFile("repl", -1, len(line)) - p := internal.NewParser(srcFile, []byte(line), nil) + p := parser.NewParser(srcFile, []byte(line), nil) file, err := p.ParseFile() if err != nil { _, _ = fmt.Fprintln(out, err.Error()) @@ -214,10 +214,10 @@ func compileSrc( src []byte, filename string, ) (*tengo.Bytecode, error) { - fileSet := internal.NewFileSet() + fileSet := parser.NewFileSet() srcFile := fileSet.AddFile(filename, -1, len(src)) - p := internal.NewParser(srcFile, src, nil) + p := parser.NewParser(srcFile, src, nil) file, err := p.ParseFile() if err != nil { return nil, err @@ -267,23 +267,23 @@ func doHelp() { fmt.Println() } -func addPrints(file *internal.File) *internal.File { - var stmts []internal.Stmt +func addPrints(file *parser.File) *parser.File { + var stmts []parser.Stmt for _, s := range file.Stmts { switch s := s.(type) { - case *internal.ExprStmt: - stmts = append(stmts, &internal.ExprStmt{ - Expr: &internal.CallExpr{ - Func: &internal.Ident{Name: "__repl_println__"}, - Args: []internal.Expr{s.Expr}, + case *parser.ExprStmt: + stmts = append(stmts, &parser.ExprStmt{ + Expr: &parser.CallExpr{ + Func: &parser.Ident{Name: "__repl_println__"}, + Args: []parser.Expr{s.Expr}, }, }) - case *internal.AssignStmt: + case *parser.AssignStmt: stmts = append(stmts, s) - stmts = append(stmts, &internal.ExprStmt{ - Expr: &internal.CallExpr{ - Func: &internal.Ident{ + stmts = append(stmts, &parser.ExprStmt{ + Expr: &parser.CallExpr{ + Func: &parser.Ident{ Name: "__repl_println__", }, Args: s.LHS, @@ -293,7 +293,7 @@ func addPrints(file *internal.File) *internal.File { stmts = append(stmts, s) } } - return &internal.File{ + return &parser.File{ InputFile: file.InputFile, Stmts: stmts, } diff --git a/compiler.go b/compiler.go index 158d5dc..3f8373d 100644 --- a/compiler.go +++ b/compiler.go @@ -8,23 +8,50 @@ import ( "reflect" "strings" - "github.com/d5/tengo/internal" - "github.com/d5/tengo/internal/token" + "github.com/d5/tengo/parser" + "github.com/d5/tengo/token" ) +// 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) +} + // Compiler compiles the AST into a bytecode. type Compiler struct { - file *internal.SourceFile + file *parser.SourceFile parent *Compiler modulePath string constants []Object - symbolTable *internal.SymbolTable - scopes []internal.CompilationScope + symbolTable *SymbolTable + scopes []compilationScope scopeIndex int modules *ModuleMap compiledModules map[string]*CompiledFunction allowFileImport bool - loops []*internal.Loop + loops []*loop loopIndex int trace io.Writer indent int @@ -32,20 +59,20 @@ type Compiler struct { // NewCompiler creates a Compiler. func NewCompiler( - file *internal.SourceFile, - symbolTable *internal.SymbolTable, + file *parser.SourceFile, + symbolTable *SymbolTable, constants []Object, modules *ModuleMap, trace io.Writer, ) *Compiler { - mainScope := internal.CompilationScope{ + mainScope := compilationScope{ SymbolInit: make(map[string]bool), - SourceMap: make(map[int]internal.Pos), + SourceMap: make(map[int]parser.Pos), } // symbol table if symbolTable == nil { - symbolTable = internal.NewSymbolTable() + symbolTable = NewSymbolTable() } // add builtin functions to the symbol table @@ -62,7 +89,7 @@ func NewCompiler( file: file, symbolTable: symbolTable, constants: constants, - scopes: []internal.CompilationScope{mainScope}, + scopes: []compilationScope{mainScope}, scopeIndex: 0, loopIndex: -1, trace: trace, @@ -72,7 +99,7 @@ func NewCompiler( } // Compile compiles the AST node. -func (c *Compiler) Compile(node internal.Node) error { +func (c *Compiler) Compile(node parser.Node) error { if c.trace != nil { if node != nil { defer untracec(tracec(c, fmt.Sprintf("%s (%s)", @@ -83,29 +110,29 @@ func (c *Compiler) Compile(node internal.Node) error { } switch node := node.(type) { - case *internal.File: + case *parser.File: for _, stmt := range node.Stmts { if err := c.Compile(stmt); err != nil { return err } } - case *internal.ExprStmt: + case *parser.ExprStmt: if err := c.Compile(node.Expr); err != nil { return err } - c.emit(node, internal.OpPop) - case *internal.IncDecStmt: + c.emit(node, parser.OpPop) + case *parser.IncDecStmt: op := token.AddAssign if node.Token == token.Dec { op = token.SubAssign } - return c.compileAssign(node, []internal.Expr{node.Expr}, - []internal.Expr{&internal.IntLit{Value: 1}}, op) - case *internal.ParenExpr: + return c.compileAssign(node, []parser.Expr{node.Expr}, + []parser.Expr{&parser.IntLit{Value: 1}}, op) + case *parser.ParenExpr: if err := c.Compile(node.Expr); err != nil { return err } - case *internal.BinaryExpr: + case *parser.BinaryExpr: if node.Token == token.LAnd || node.Token == token.LOr { return c.compileLogical(node) } @@ -116,7 +143,7 @@ func (c *Compiler) Compile(node internal.Node) error { if err := c.Compile(node.LHS); err != nil { return err } - c.emit(node, internal.OpBinaryOp, int(token.Greater)) + c.emit(node, parser.OpBinaryOp, int(token.Greater)) return nil } else if node.Token == token.LessEq { if err := c.Compile(node.RHS); err != nil { @@ -125,7 +152,7 @@ func (c *Compiler) Compile(node internal.Node) error { if err := c.Compile(node.LHS); err != nil { return err } - c.emit(node, internal.OpBinaryOp, int(token.GreaterEq)) + c.emit(node, parser.OpBinaryOp, int(token.GreaterEq)) return nil } if err := c.Compile(node.LHS); err != nil { @@ -137,81 +164,81 @@ func (c *Compiler) Compile(node internal.Node) error { switch node.Token { case token.Add: - c.emit(node, internal.OpBinaryOp, int(token.Add)) + c.emit(node, parser.OpBinaryOp, int(token.Add)) case token.Sub: - c.emit(node, internal.OpBinaryOp, int(token.Sub)) + c.emit(node, parser.OpBinaryOp, int(token.Sub)) case token.Mul: - c.emit(node, internal.OpBinaryOp, int(token.Mul)) + c.emit(node, parser.OpBinaryOp, int(token.Mul)) case token.Quo: - c.emit(node, internal.OpBinaryOp, int(token.Quo)) + c.emit(node, parser.OpBinaryOp, int(token.Quo)) case token.Rem: - c.emit(node, internal.OpBinaryOp, int(token.Rem)) + c.emit(node, parser.OpBinaryOp, int(token.Rem)) case token.Greater: - c.emit(node, internal.OpBinaryOp, int(token.Greater)) + c.emit(node, parser.OpBinaryOp, int(token.Greater)) case token.GreaterEq: - c.emit(node, internal.OpBinaryOp, int(token.GreaterEq)) + c.emit(node, parser.OpBinaryOp, int(token.GreaterEq)) case token.Equal: - c.emit(node, internal.OpEqual) + c.emit(node, parser.OpEqual) case token.NotEqual: - c.emit(node, internal.OpNotEqual) + c.emit(node, parser.OpNotEqual) case token.And: - c.emit(node, internal.OpBinaryOp, int(token.And)) + c.emit(node, parser.OpBinaryOp, int(token.And)) case token.Or: - c.emit(node, internal.OpBinaryOp, int(token.Or)) + c.emit(node, parser.OpBinaryOp, int(token.Or)) case token.Xor: - c.emit(node, internal.OpBinaryOp, int(token.Xor)) + c.emit(node, parser.OpBinaryOp, int(token.Xor)) case token.AndNot: - c.emit(node, internal.OpBinaryOp, int(token.AndNot)) + c.emit(node, parser.OpBinaryOp, int(token.AndNot)) case token.Shl: - c.emit(node, internal.OpBinaryOp, int(token.Shl)) + c.emit(node, parser.OpBinaryOp, int(token.Shl)) case token.Shr: - c.emit(node, internal.OpBinaryOp, int(token.Shr)) + c.emit(node, parser.OpBinaryOp, int(token.Shr)) default: return c.errorf(node, "invalid binary operator: %s", node.Token.String()) } - case *internal.IntLit: - c.emit(node, internal.OpConstant, + case *parser.IntLit: + c.emit(node, parser.OpConstant, c.addConstant(&Int{Value: node.Value})) - case *internal.FloatLit: - c.emit(node, internal.OpConstant, + case *parser.FloatLit: + c.emit(node, parser.OpConstant, c.addConstant(&Float{Value: node.Value})) - case *internal.BoolLit: + case *parser.BoolLit: if node.Value { - c.emit(node, internal.OpTrue) + c.emit(node, parser.OpTrue) } else { - c.emit(node, internal.OpFalse) + c.emit(node, parser.OpFalse) } - case *internal.StringLit: + case *parser.StringLit: if len(node.Value) > MaxStringLen { return c.error(node, ErrStringLimit) } - c.emit(node, internal.OpConstant, + c.emit(node, parser.OpConstant, c.addConstant(&String{Value: node.Value})) - case *internal.CharLit: - c.emit(node, internal.OpConstant, + case *parser.CharLit: + c.emit(node, parser.OpConstant, c.addConstant(&Char{Value: node.Value})) - case *internal.UndefinedLit: - c.emit(node, internal.OpNull) - case *internal.UnaryExpr: + case *parser.UndefinedLit: + c.emit(node, parser.OpNull) + case *parser.UnaryExpr: if err := c.Compile(node.Expr); err != nil { return err } switch node.Token { case token.Not: - c.emit(node, internal.OpLNot) + c.emit(node, parser.OpLNot) case token.Sub: - c.emit(node, internal.OpMinus) + c.emit(node, parser.OpMinus) case token.Xor: - c.emit(node, internal.OpBComplement) + c.emit(node, parser.OpBComplement) case token.Add: // do nothing? default: return c.errorf(node, "invalid unary operator: %s", node.Token.String()) } - case *internal.IfStmt: + case *parser.IfStmt: // open new symbol table for the statement c.symbolTable = c.symbolTable.Fork(true) defer func() { @@ -228,13 +255,13 @@ func (c *Compiler) Compile(node internal.Node) error { } // first jump placeholder - jumpPos1 := c.emit(node, internal.OpJumpFalsy, 0) + jumpPos1 := c.emit(node, parser.OpJumpFalsy, 0) if err := c.Compile(node.Body); err != nil { return err } if node.Else != nil { // second jump placeholder - jumpPos2 := c.emit(node, internal.OpJump, 0) + jumpPos2 := c.emit(node, parser.OpJump, 0) // update first jump offset curPos := len(c.currentInstructions()) @@ -251,30 +278,30 @@ func (c *Compiler) Compile(node internal.Node) error { curPos := len(c.currentInstructions()) c.changeOperand(jumpPos1, curPos) } - case *internal.ForStmt: + case *parser.ForStmt: return c.compileForStmt(node) - case *internal.ForInStmt: + case *parser.ForInStmt: return c.compileForInStmt(node) - case *internal.BranchStmt: + case *parser.BranchStmt: if node.Token == token.Break { curLoop := c.currentLoop() if curLoop == nil { return c.errorf(node, "break not allowed outside loop") } - pos := c.emit(node, internal.OpJump, 0) + pos := c.emit(node, parser.OpJump, 0) 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") } - pos := c.emit(node, internal.OpJump, 0) + pos := c.emit(node, parser.OpJump, 0) curLoop.Continues = append(curLoop.Continues, pos) } else { panic(fmt.Errorf("invalid branch statement: %s", node.Token.String())) } - case *internal.BlockStmt: + case *parser.BlockStmt: if len(node.Stmts) == 0 { return nil } @@ -289,41 +316,41 @@ func (c *Compiler) Compile(node internal.Node) error { return err } } - case *internal.AssignStmt: + case *parser.AssignStmt: err := c.compileAssign(node, node.LHS, node.RHS, node.Token) if err != nil { return err } - case *internal.Ident: + case *parser.Ident: symbol, _, ok := c.symbolTable.Resolve(node.Name) if !ok { return c.errorf(node, "unresolved reference '%s'", node.Name) } switch symbol.Scope { - case internal.ScopeGlobal: - c.emit(node, internal.OpGetGlobal, symbol.Index) - case internal.ScopeLocal: - c.emit(node, internal.OpGetLocal, symbol.Index) - case internal.ScopeBuiltin: - c.emit(node, internal.OpGetBuiltin, symbol.Index) - case internal.ScopeFree: - c.emit(node, internal.OpGetFree, symbol.Index) + 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 *internal.ArrayLit: + case *parser.ArrayLit: for _, elem := range node.Elements { if err := c.Compile(elem); err != nil { return err } } - c.emit(node, internal.OpArray, len(node.Elements)) - case *internal.MapLit: + c.emit(node, parser.OpArray, len(node.Elements)) + case *parser.MapLit: for _, elt := range node.Elements { // key if len(elt.Key) > MaxStringLen { return c.error(node, ErrStringLimit) } - c.emit(node, internal.OpConstant, + c.emit(node, parser.OpConstant, c.addConstant(&String{Value: elt.Key})) // value @@ -331,25 +358,25 @@ func (c *Compiler) Compile(node internal.Node) error { return err } } - c.emit(node, internal.OpMap, len(node.Elements)*2) + c.emit(node, parser.OpMap, len(node.Elements)*2) - case *internal.SelectorExpr: // selector on RHS side + case *parser.SelectorExpr: // selector on RHS side if err := c.Compile(node.Expr); err != nil { return err } if err := c.Compile(node.Sel); err != nil { return err } - c.emit(node, internal.OpIndex) - case *internal.IndexExpr: + c.emit(node, parser.OpIndex) + case *parser.IndexExpr: if err := c.Compile(node.Expr); err != nil { return err } if err := c.Compile(node.Index); err != nil { return err } - c.emit(node, internal.OpIndex) - case *internal.SliceExpr: + c.emit(node, parser.OpIndex) + case *parser.SliceExpr: if err := c.Compile(node.Expr); err != nil { return err } @@ -358,17 +385,17 @@ func (c *Compiler) Compile(node internal.Node) error { return err } } else { - c.emit(node, internal.OpNull) + c.emit(node, parser.OpNull) } if node.High != nil { if err := c.Compile(node.High); err != nil { return err } } else { - c.emit(node, internal.OpNull) + c.emit(node, parser.OpNull) } - c.emit(node, internal.OpSliceIndex) - case *internal.FuncLit: + c.emit(node, parser.OpSliceIndex) + case *parser.FuncLit: c.enterScope() for _, p := range node.Type.Params.List { @@ -391,7 +418,7 @@ func (c *Compiler) Compile(node internal.Node) error { for _, s := range freeSymbols { switch s.Scope { - case internal.ScopeLocal: + case ScopeLocal: if !s.LocalAssigned { // Here, the closure is capturing a local variable that's // not yet assigned its value. One example is a local @@ -432,13 +459,13 @@ func (c *Compiler) Compile(node internal.Node) error { // 0005 CLOSURE ? 1 // 0009 SETL 0 // - c.emit(node, internal.OpNull) - c.emit(node, internal.OpDefineLocal, s.Index) + c.emit(node, parser.OpNull) + c.emit(node, parser.OpDefineLocal, s.Index) s.LocalAssigned = true } - c.emit(node, internal.OpGetLocalPtr, s.Index) - case internal.ScopeFree: - c.emit(node, internal.OpGetFreePtr, s.Index) + c.emit(node, parser.OpGetLocalPtr, s.Index) + case ScopeFree: + c.emit(node, parser.OpGetFreePtr, s.Index) } } @@ -450,26 +477,26 @@ func (c *Compiler) Compile(node internal.Node) error { SourceMap: sourceMap, } if len(freeSymbols) > 0 { - c.emit(node, internal.OpClosure, + c.emit(node, parser.OpClosure, c.addConstant(compiledFunction), len(freeSymbols)) } else { - c.emit(node, internal.OpConstant, c.addConstant(compiledFunction)) + c.emit(node, parser.OpConstant, c.addConstant(compiledFunction)) } - case *internal.ReturnStmt: + case *parser.ReturnStmt: if c.symbolTable.Parent(true) == nil { // outside the function return c.errorf(node, "return not allowed outside function") } if node.Result == nil { - c.emit(node, internal.OpReturn, 0) + c.emit(node, parser.OpReturn, 0) } else { if err := c.Compile(node.Result); err != nil { return err } - c.emit(node, internal.OpReturn, 1) + c.emit(node, parser.OpReturn, 1) } - case *internal.CallExpr: + case *parser.CallExpr: if err := c.Compile(node.Func); err != nil { return err } @@ -478,8 +505,8 @@ func (c *Compiler) Compile(node internal.Node) error { return err } } - c.emit(node, internal.OpCall, len(node.Args)) - case *internal.ImportExpr: + c.emit(node, parser.OpCall, len(node.Args)) + case *parser.ImportExpr: if node.ModuleName == "" { return c.errorf(node, "empty module name") } @@ -497,10 +524,10 @@ func (c *Compiler) Compile(node internal.Node) error { if err != nil { return err } - c.emit(node, internal.OpConstant, c.addConstant(compiled)) - c.emit(node, internal.OpCall, 0) + c.emit(node, parser.OpConstant, c.addConstant(compiled)) + c.emit(node, parser.OpCall, 0) case Object: // builtin module - c.emit(node, internal.OpConstant, c.addConstant(v)) + c.emit(node, parser.OpConstant, c.addConstant(v)) default: panic(fmt.Errorf("invalid import value type: %T", v)) } @@ -531,12 +558,12 @@ func (c *Compiler) Compile(node internal.Node) error { if err != nil { return err } - c.emit(node, internal.OpConstant, c.addConstant(compiled)) - c.emit(node, internal.OpCall, 0) + c.emit(node, parser.OpConstant, c.addConstant(compiled)) + c.emit(node, parser.OpCall, 0) } else { return c.errorf(node, "module '%s' not found", node.ModuleName) } - case *internal.ExportStmt: + case *parser.ExportStmt: // export statement must be in top-level scope if c.scopeIndex != 0 { return c.errorf(node, "export not allowed inside function") @@ -549,31 +576,31 @@ func (c *Compiler) Compile(node internal.Node) error { if err := c.Compile(node.Result); err != nil { return err } - c.emit(node, internal.OpImmutable) - c.emit(node, internal.OpReturn, 1) - case *internal.ErrorExpr: + c.emit(node, parser.OpImmutable) + c.emit(node, parser.OpReturn, 1) + case *parser.ErrorExpr: if err := c.Compile(node.Expr); err != nil { return err } - c.emit(node, internal.OpError) - case *internal.ImmutableExpr: + c.emit(node, parser.OpError) + case *parser.ImmutableExpr: if err := c.Compile(node.Expr); err != nil { return err } - c.emit(node, internal.OpImmutable) - case *internal.CondExpr: + c.emit(node, parser.OpImmutable) + case *parser.CondExpr: if err := c.Compile(node.Cond); err != nil { return err } // first jump placeholder - jumpPos1 := c.emit(node, internal.OpJumpFalsy, 0) + jumpPos1 := c.emit(node, parser.OpJumpFalsy, 0) if err := c.Compile(node.True); err != nil { return err } // second jump placeholder - jumpPos2 := c.emit(node, internal.OpJump, 0) + jumpPos2 := c.emit(node, parser.OpJump, 0) // update first jump offset curPos := len(c.currentInstructions()) @@ -594,7 +621,7 @@ func (c *Compiler) Bytecode() *Bytecode { return &Bytecode{ FileSet: c.file.Set(), MainFunction: &CompiledFunction{ - Instructions: append(c.currentInstructions(), internal.OpSuspend), + Instructions: append(c.currentInstructions(), parser.OpSuspend), SourceMap: c.currentSourceMap(), }, Constants: c.constants, @@ -608,8 +635,8 @@ func (c *Compiler) EnableFileImport(enable bool) { } func (c *Compiler) compileAssign( - node internal.Node, - lhs, rhs []internal.Expr, + node parser.Node, + lhs, rhs []parser.Expr, op token.Token, ) error { numLHS, numRHS := len(lhs), len(rhs) @@ -654,27 +681,27 @@ func (c *Compiler) compileAssign( switch op { case token.AddAssign: - c.emit(node, internal.OpBinaryOp, int(token.Add)) + c.emit(node, parser.OpBinaryOp, int(token.Add)) case token.SubAssign: - c.emit(node, internal.OpBinaryOp, int(token.Sub)) + c.emit(node, parser.OpBinaryOp, int(token.Sub)) case token.MulAssign: - c.emit(node, internal.OpBinaryOp, int(token.Mul)) + c.emit(node, parser.OpBinaryOp, int(token.Mul)) case token.QuoAssign: - c.emit(node, internal.OpBinaryOp, int(token.Quo)) + c.emit(node, parser.OpBinaryOp, int(token.Quo)) case token.RemAssign: - c.emit(node, internal.OpBinaryOp, int(token.Rem)) + c.emit(node, parser.OpBinaryOp, int(token.Rem)) case token.AndAssign: - c.emit(node, internal.OpBinaryOp, int(token.And)) + c.emit(node, parser.OpBinaryOp, int(token.And)) case token.OrAssign: - c.emit(node, internal.OpBinaryOp, int(token.Or)) + c.emit(node, parser.OpBinaryOp, int(token.Or)) case token.AndNotAssign: - c.emit(node, internal.OpBinaryOp, int(token.AndNot)) + c.emit(node, parser.OpBinaryOp, int(token.AndNot)) case token.XorAssign: - c.emit(node, internal.OpBinaryOp, int(token.Xor)) + c.emit(node, parser.OpBinaryOp, int(token.Xor)) case token.ShlAssign: - c.emit(node, internal.OpBinaryOp, int(token.Shl)) + c.emit(node, parser.OpBinaryOp, int(token.Shl)) case token.ShrAssign: - c.emit(node, internal.OpBinaryOp, int(token.Shr)) + c.emit(node, parser.OpBinaryOp, int(token.Shr)) } // compile selector expressions (right to left) @@ -685,30 +712,30 @@ func (c *Compiler) compileAssign( } switch symbol.Scope { - case internal.ScopeGlobal: + case ScopeGlobal: if numSel > 0 { - c.emit(node, internal.OpSetSelGlobal, symbol.Index, numSel) + c.emit(node, parser.OpSetSelGlobal, symbol.Index, numSel) } else { - c.emit(node, internal.OpSetGlobal, symbol.Index) + c.emit(node, parser.OpSetGlobal, symbol.Index) } - case internal.ScopeLocal: + case ScopeLocal: if numSel > 0 { - c.emit(node, internal.OpSetSelLocal, symbol.Index, numSel) + c.emit(node, parser.OpSetSelLocal, symbol.Index, numSel) } else { if op == token.Define && !symbol.LocalAssigned { - c.emit(node, internal.OpDefineLocal, symbol.Index) + c.emit(node, parser.OpDefineLocal, symbol.Index) } else { - c.emit(node, internal.OpSetLocal, symbol.Index) + c.emit(node, parser.OpSetLocal, symbol.Index) } } // mark the symbol as local-assigned symbol.LocalAssigned = true - case internal.ScopeFree: + case ScopeFree: if numSel > 0 { - c.emit(node, internal.OpSetSelFree, symbol.Index, numSel) + c.emit(node, parser.OpSetSelFree, symbol.Index, numSel) } else { - c.emit(node, internal.OpSetFree, symbol.Index) + c.emit(node, parser.OpSetFree, symbol.Index) } default: panic(fmt.Errorf("invalid assignment variable scope: %s", @@ -717,7 +744,7 @@ func (c *Compiler) compileAssign( return nil } -func (c *Compiler) compileLogical(node *internal.BinaryExpr) error { +func (c *Compiler) compileLogical(node *parser.BinaryExpr) error { // left side term if err := c.Compile(node.LHS); err != nil { return err @@ -726,9 +753,9 @@ func (c *Compiler) compileLogical(node *internal.BinaryExpr) error { // jump position var jumpPos int if node.Token == token.LAnd { - jumpPos = c.emit(node, internal.OpAndJump, 0) + jumpPos = c.emit(node, parser.OpAndJump, 0) } else { - jumpPos = c.emit(node, internal.OpOrJump, 0) + jumpPos = c.emit(node, parser.OpOrJump, 0) } // right side term @@ -740,7 +767,7 @@ func (c *Compiler) compileLogical(node *internal.BinaryExpr) error { return nil } -func (c *Compiler) compileForStmt(stmt *internal.ForStmt) error { +func (c *Compiler) compileForStmt(stmt *parser.ForStmt) error { c.symbolTable = c.symbolTable.Fork(true) defer func() { c.symbolTable = c.symbolTable.Parent(false) @@ -763,7 +790,7 @@ func (c *Compiler) compileForStmt(stmt *internal.ForStmt) error { return err } // condition jump position - postCondPos = c.emit(stmt, internal.OpJumpFalsy, 0) + postCondPos = c.emit(stmt, parser.OpJumpFalsy, 0) } // enter loop @@ -788,7 +815,7 @@ func (c *Compiler) compileForStmt(stmt *internal.ForStmt) error { } // back to condition - c.emit(stmt, internal.OpJump, preCondPos) + c.emit(stmt, parser.OpJump, preCondPos) // post-statement position postStmtPos := len(c.currentInstructions()) @@ -806,7 +833,7 @@ func (c *Compiler) compileForStmt(stmt *internal.ForStmt) error { return nil } -func (c *Compiler) compileForInStmt(stmt *internal.ForInStmt) error { +func (c *Compiler) compileForInStmt(stmt *parser.ForInStmt) error { c.symbolTable = c.symbolTable.Fork(true) defer func() { c.symbolTable = c.symbolTable.Parent(false) @@ -829,11 +856,11 @@ func (c *Compiler) compileForInStmt(stmt *internal.ForInStmt) error { if err := c.Compile(stmt.Iterable); err != nil { return err } - c.emit(stmt, internal.OpIteratorInit) - if itSymbol.Scope == internal.ScopeGlobal { - c.emit(stmt, internal.OpSetGlobal, itSymbol.Index) + c.emit(stmt, parser.OpIteratorInit) + if itSymbol.Scope == ScopeGlobal { + c.emit(stmt, parser.OpSetGlobal, itSymbol.Index) } else { - c.emit(stmt, internal.OpDefineLocal, itSymbol.Index) + c.emit(stmt, parser.OpDefineLocal, itSymbol.Index) } // pre-condition position @@ -841,15 +868,15 @@ func (c *Compiler) compileForInStmt(stmt *internal.ForInStmt) error { // condition // :it.HasMore() - if itSymbol.Scope == internal.ScopeGlobal { - c.emit(stmt, internal.OpGetGlobal, itSymbol.Index) + if itSymbol.Scope == ScopeGlobal { + c.emit(stmt, parser.OpGetGlobal, itSymbol.Index) } else { - c.emit(stmt, internal.OpGetLocal, itSymbol.Index) + c.emit(stmt, parser.OpGetLocal, itSymbol.Index) } - c.emit(stmt, internal.OpIteratorNext) + c.emit(stmt, parser.OpIteratorNext) // condition jump position - postCondPos := c.emit(stmt, internal.OpJumpFalsy, 0) + postCondPos := c.emit(stmt, parser.OpJumpFalsy, 0) // enter loop loop := c.enterLoop() @@ -857,32 +884,32 @@ func (c *Compiler) compileForInStmt(stmt *internal.ForInStmt) error { // assign key variable if stmt.Key.Name != "_" { keySymbol := c.symbolTable.Define(stmt.Key.Name) - if itSymbol.Scope == internal.ScopeGlobal { - c.emit(stmt, internal.OpGetGlobal, itSymbol.Index) + if itSymbol.Scope == ScopeGlobal { + c.emit(stmt, parser.OpGetGlobal, itSymbol.Index) } else { - c.emit(stmt, internal.OpGetLocal, itSymbol.Index) + c.emit(stmt, parser.OpGetLocal, itSymbol.Index) } - c.emit(stmt, internal.OpIteratorKey) - if keySymbol.Scope == internal.ScopeGlobal { - c.emit(stmt, internal.OpSetGlobal, keySymbol.Index) + c.emit(stmt, parser.OpIteratorKey) + if keySymbol.Scope == ScopeGlobal { + c.emit(stmt, parser.OpSetGlobal, keySymbol.Index) } else { - c.emit(stmt, internal.OpDefineLocal, keySymbol.Index) + c.emit(stmt, parser.OpDefineLocal, keySymbol.Index) } } // assign value variable if stmt.Value.Name != "_" { valueSymbol := c.symbolTable.Define(stmt.Value.Name) - if itSymbol.Scope == internal.ScopeGlobal { - c.emit(stmt, internal.OpGetGlobal, itSymbol.Index) + if itSymbol.Scope == ScopeGlobal { + c.emit(stmt, parser.OpGetGlobal, itSymbol.Index) } else { - c.emit(stmt, internal.OpGetLocal, itSymbol.Index) + c.emit(stmt, parser.OpGetLocal, itSymbol.Index) } - c.emit(stmt, internal.OpIteratorValue) - if valueSymbol.Scope == internal.ScopeGlobal { - c.emit(stmt, internal.OpSetGlobal, valueSymbol.Index) + c.emit(stmt, parser.OpIteratorValue) + if valueSymbol.Scope == ScopeGlobal { + c.emit(stmt, parser.OpSetGlobal, valueSymbol.Index) } else { - c.emit(stmt, internal.OpDefineLocal, valueSymbol.Index) + c.emit(stmt, parser.OpDefineLocal, valueSymbol.Index) } } @@ -898,7 +925,7 @@ func (c *Compiler) compileForInStmt(stmt *internal.ForInStmt) error { postBodyPos := len(c.currentInstructions()) // back to condition - c.emit(stmt, internal.OpJump, preCondPos) + c.emit(stmt, parser.OpJump, preCondPos) // post-statement position postStmtPos := len(c.currentInstructions()) @@ -915,7 +942,7 @@ func (c *Compiler) compileForInStmt(stmt *internal.ForInStmt) error { } func (c *Compiler) checkCyclicImports( - node internal.Node, + node parser.Node, modulePath string, ) error { if c.modulePath == modulePath { @@ -927,7 +954,7 @@ func (c *Compiler) checkCyclicImports( } func (c *Compiler) compileModule( - node internal.Node, + node parser.Node, moduleName, modulePath string, src []byte, ) (*CompiledFunction, error) { @@ -941,14 +968,14 @@ func (c *Compiler) compileModule( } modFile := c.file.Set().AddFile(moduleName, -1, len(src)) - p := internal.NewParser(modFile, src, nil) + p := parser.NewParser(modFile, src, nil) file, err := p.ParseFile() if err != nil { return nil, err } // inherit builtin functions - symbolTable := internal.NewSymbolTable() + symbolTable := NewSymbolTable() for _, sym := range c.symbolTable.BuiltinSymbols() { symbolTable.DefineBuiltin(sym.Index, sym.Name) } @@ -990,8 +1017,8 @@ func (c *Compiler) storeCompiledModule( c.compiledModules[modulePath] = module } -func (c *Compiler) enterLoop() *internal.Loop { - loop := &internal.Loop{} +func (c *Compiler) enterLoop() *loop { + loop := &loop{} c.loops = append(c.loops, loop) c.loopIndex++ if c.trace != nil { @@ -1008,7 +1035,7 @@ func (c *Compiler) leaveLoop() { c.loopIndex-- } -func (c *Compiler) currentLoop() *internal.Loop { +func (c *Compiler) currentLoop() *loop { if c.loopIndex >= 0 { return c.loops[c.loopIndex] } @@ -1019,14 +1046,14 @@ func (c *Compiler) currentInstructions() []byte { return c.scopes[c.scopeIndex].Instructions } -func (c *Compiler) currentSourceMap() map[int]internal.Pos { +func (c *Compiler) currentSourceMap() map[int]parser.Pos { return c.scopes[c.scopeIndex].SourceMap } func (c *Compiler) enterScope() { - scope := internal.CompilationScope{ + scope := compilationScope{ SymbolInit: make(map[string]bool), - SourceMap: make(map[int]internal.Pos), + SourceMap: make(map[int]parser.Pos), } c.scopes = append(c.scopes, scope) c.scopeIndex++ @@ -1038,7 +1065,7 @@ func (c *Compiler) enterScope() { func (c *Compiler) leaveScope() ( instructions []byte, - sourceMap map[int]internal.Pos, + sourceMap map[int]parser.Pos, ) { instructions = c.currentInstructions() sourceMap = c.currentSourceMap() @@ -1052,9 +1079,9 @@ func (c *Compiler) leaveScope() ( } func (c *Compiler) fork( - file *internal.SourceFile, + file *parser.SourceFile, modulePath string, - symbolTable *internal.SymbolTable, + symbolTable *SymbolTable, ) *Compiler { child := NewCompiler(file, symbolTable, nil, c.modules, c.trace) child.modulePath = modulePath // module file path @@ -1062,8 +1089,8 @@ func (c *Compiler) fork( return child } -func (c *Compiler) error(node internal.Node, err error) error { - return &internal.CompilerError{ +func (c *Compiler) error(node parser.Node, err error) error { + return &CompilerError{ FileSet: c.file.Set(), Node: node, Err: err, @@ -1071,11 +1098,11 @@ func (c *Compiler) error(node internal.Node, err error) error { } func (c *Compiler) errorf( - node internal.Node, + node parser.Node, format string, args ...interface{}, ) error { - return &internal.CompilerError{ + return &CompilerError{ FileSet: c.file.Set(), Node: node, Err: fmt.Errorf(format, args...), @@ -1105,21 +1132,21 @@ func (c *Compiler) replaceInstruction(pos int, inst []byte) { copy(c.currentInstructions()[pos:], inst) if c.trace != nil { c.printTrace(fmt.Sprintf("REPLC %s", - internal.FormatInstructions( + FormatInstructions( c.scopes[c.scopeIndex].Instructions[pos:], pos)[0])) } } func (c *Compiler) changeOperand(opPos int, operand ...int) { op := c.currentInstructions()[opPos] - inst := internal.MakeInstruction(op, operand...) + inst := MakeInstruction(op, operand...) 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. -func (c *Compiler) optimizeFunc(node internal.Node) { +func (c *Compiler) optimizeFunc(node parser.Node) { // any instructions between RETURN and the function end // or instructions between RETURN and jump target position // are considered as unreachable. @@ -1127,10 +1154,10 @@ func (c *Compiler) optimizeFunc(node internal.Node) { // pass 1. identify all jump destinations dsts := make(map[int]bool) iterateInstructions(c.scopes[c.scopeIndex].Instructions, - func(pos int, opcode internal.Opcode, operands []int) bool { + func(pos int, opcode parser.Opcode, operands []int) bool { switch opcode { - case internal.OpJump, internal.OpJumpFalsy, - internal.OpAndJump, internal.OpOrJump: + case parser.OpJump, parser.OpJumpFalsy, + parser.OpAndJump, parser.OpOrJump: dsts[operands[0]] = true } return true @@ -1142,9 +1169,9 @@ func (c *Compiler) optimizeFunc(node internal.Node) { var dstIdx int var deadCode bool iterateInstructions(c.scopes[c.scopeIndex].Instructions, - func(pos int, opcode internal.Opcode, operands []int) bool { + func(pos int, opcode parser.Opcode, operands []int) bool { switch { - case opcode == internal.OpReturn: + case opcode == parser.OpReturn: if deadCode { return true } @@ -1157,23 +1184,23 @@ func (c *Compiler) optimizeFunc(node internal.Node) { } posMap[pos] = len(newInsts) newInsts = append(newInsts, - internal.MakeInstruction(opcode, operands...)...) + MakeInstruction(opcode, operands...)...) return true }) // pass 3. update jump positions - var lastOp internal.Opcode + var lastOp parser.Opcode var appendReturn bool endPos := len(c.scopes[c.scopeIndex].Instructions) iterateInstructions(newInsts, - func(pos int, opcode internal.Opcode, operands []int) bool { + func(pos int, opcode parser.Opcode, operands []int) bool { switch opcode { - case internal.OpJump, internal.OpJumpFalsy, internal.OpAndJump, - internal.OpOrJump: + case parser.OpJump, parser.OpJumpFalsy, parser.OpAndJump, + parser.OpOrJump: newDst, ok := posMap[operands[0]] if ok { copy(newInsts[pos:], - internal.MakeInstruction(opcode, newDst)) + MakeInstruction(opcode, newDst)) } else if endPos == operands[0] { // there's a jump instruction that jumps to the end of // function compiler should append "return". @@ -1185,12 +1212,12 @@ func (c *Compiler) optimizeFunc(node internal.Node) { lastOp = opcode return true }) - if lastOp != internal.OpReturn { + if lastOp != parser.OpReturn { appendReturn = true } // pass 4. update source map - newSourceMap := make(map[int]internal.Pos) + newSourceMap := make(map[int]parser.Pos) for pos, srcPos := range c.scopes[c.scopeIndex].SourceMap { newPos, ok := posMap[pos] if ok { @@ -1202,26 +1229,26 @@ func (c *Compiler) optimizeFunc(node internal.Node) { // append "return" if appendReturn { - c.emit(node, internal.OpReturn, 0) + c.emit(node, parser.OpReturn, 0) } } func (c *Compiler) emit( - node internal.Node, - opcode internal.Opcode, + node parser.Node, + opcode parser.Opcode, operands ...int, ) int { - filePos := internal.NoPos + filePos := parser.NoPos if node != nil { filePos = node.Pos() } - inst := internal.MakeInstruction(opcode, operands...) + inst := MakeInstruction(opcode, operands...) pos := c.addInstruction(inst) c.scopes[c.scopeIndex].SourceMap[pos] = filePos if c.trace != nil { c.printTrace(fmt.Sprintf("EMIT %s", - internal.FormatInstructions( + FormatInstructions( c.scopes[c.scopeIndex].Instructions[pos:], pos)[0])) } return pos @@ -1243,17 +1270,17 @@ func (c *Compiler) printTrace(a ...interface{}) { } func resolveAssignLHS( - expr internal.Expr, -) (name string, selectors []internal.Expr) { + expr parser.Expr, +) (name string, selectors []parser.Expr) { switch term := expr.(type) { - case *internal.SelectorExpr: + case *parser.SelectorExpr: name, selectors = resolveAssignLHS(term.Expr) selectors = append(selectors, term.Sel) return - case *internal.IndexExpr: + case *parser.IndexExpr: name, selectors = resolveAssignLHS(term.Expr) selectors = append(selectors, term.Index) - case *internal.Ident: + case *parser.Ident: name = term.Name } return @@ -1261,11 +1288,11 @@ func resolveAssignLHS( func iterateInstructions( b []byte, - fn func(pos int, opcode internal.Opcode, operands []int) bool, + fn func(pos int, opcode parser.Opcode, operands []int) bool, ) { for i := 0; i < len(b); i++ { - numOperands := internal.OpcodeOperands[b[i]] - operands, read := internal.ReadOperands(numOperands, b[i+1:]) + numOperands := parser.OpcodeOperands[b[i]] + operands, read := parser.ReadOperands(numOperands, b[i+1:]) if !fn(i, b[i], operands) { break } diff --git a/compiler_test.go b/compiler_test.go index 0a7e456..6f8619d 100644 --- a/compiler_test.go +++ b/compiler_test.go @@ -6,19 +6,19 @@ import ( "testing" "github.com/d5/tengo" - "github.com/d5/tengo/internal" - "github.com/d5/tengo/internal/require" + "github.com/d5/tengo/parser" + "github.com/d5/tengo/require" ) func TestCompiler_Compile(t *testing.T) { expectCompile(t, `1 + 2`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpConstant, 0), - internal.MakeInstruction(internal.OpConstant, 1), - internal.MakeInstruction(internal.OpBinaryOp, 11), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpConstant, 0), + tengo.MakeInstruction(parser.OpConstant, 1), + tengo.MakeInstruction(parser.OpBinaryOp, 11), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray( intObject(1), intObject(2)))) @@ -26,11 +26,11 @@ func TestCompiler_Compile(t *testing.T) { expectCompile(t, `1; 2`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpConstant, 0), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpConstant, 1), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpConstant, 0), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpConstant, 1), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray( intObject(1), intObject(2)))) @@ -38,11 +38,11 @@ func TestCompiler_Compile(t *testing.T) { expectCompile(t, `1 - 2`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpConstant, 0), - internal.MakeInstruction(internal.OpConstant, 1), - internal.MakeInstruction(internal.OpBinaryOp, 12), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpConstant, 0), + tengo.MakeInstruction(parser.OpConstant, 1), + tengo.MakeInstruction(parser.OpBinaryOp, 12), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray( intObject(1), intObject(2)))) @@ -50,11 +50,11 @@ func TestCompiler_Compile(t *testing.T) { expectCompile(t, `1 * 2`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpConstant, 0), - internal.MakeInstruction(internal.OpConstant, 1), - internal.MakeInstruction(internal.OpBinaryOp, 13), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpConstant, 0), + tengo.MakeInstruction(parser.OpConstant, 1), + tengo.MakeInstruction(parser.OpBinaryOp, 13), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray( intObject(1), intObject(2)))) @@ -62,11 +62,11 @@ func TestCompiler_Compile(t *testing.T) { expectCompile(t, `2 / 1`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpConstant, 0), - internal.MakeInstruction(internal.OpConstant, 1), - internal.MakeInstruction(internal.OpBinaryOp, 14), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpConstant, 0), + tengo.MakeInstruction(parser.OpConstant, 1), + tengo.MakeInstruction(parser.OpBinaryOp, 14), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray( intObject(2), intObject(1)))) @@ -74,27 +74,27 @@ func TestCompiler_Compile(t *testing.T) { expectCompile(t, `true`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpTrue), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpTrue), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray())) expectCompile(t, `false`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpFalse), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpFalse), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray())) expectCompile(t, `1 > 2`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpConstant, 0), - internal.MakeInstruction(internal.OpConstant, 1), - internal.MakeInstruction(internal.OpBinaryOp, 39), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpConstant, 0), + tengo.MakeInstruction(parser.OpConstant, 1), + tengo.MakeInstruction(parser.OpBinaryOp, 39), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray( intObject(1), intObject(2)))) @@ -102,11 +102,11 @@ func TestCompiler_Compile(t *testing.T) { expectCompile(t, `1 < 2`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpConstant, 0), - internal.MakeInstruction(internal.OpConstant, 1), - internal.MakeInstruction(internal.OpBinaryOp, 39), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpConstant, 0), + tengo.MakeInstruction(parser.OpConstant, 1), + tengo.MakeInstruction(parser.OpBinaryOp, 39), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray( intObject(2), intObject(1)))) @@ -114,11 +114,11 @@ func TestCompiler_Compile(t *testing.T) { expectCompile(t, `1 >= 2`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpConstant, 0), - internal.MakeInstruction(internal.OpConstant, 1), - internal.MakeInstruction(internal.OpBinaryOp, 44), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpConstant, 0), + tengo.MakeInstruction(parser.OpConstant, 1), + tengo.MakeInstruction(parser.OpBinaryOp, 44), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray( intObject(1), intObject(2)))) @@ -126,11 +126,11 @@ func TestCompiler_Compile(t *testing.T) { expectCompile(t, `1 <= 2`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpConstant, 0), - internal.MakeInstruction(internal.OpConstant, 1), - internal.MakeInstruction(internal.OpBinaryOp, 44), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpConstant, 0), + tengo.MakeInstruction(parser.OpConstant, 1), + tengo.MakeInstruction(parser.OpBinaryOp, 44), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray( intObject(2), intObject(1)))) @@ -138,11 +138,11 @@ func TestCompiler_Compile(t *testing.T) { expectCompile(t, `1 == 2`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpConstant, 0), - internal.MakeInstruction(internal.OpConstant, 1), - internal.MakeInstruction(internal.OpEqual), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpConstant, 0), + tengo.MakeInstruction(parser.OpConstant, 1), + tengo.MakeInstruction(parser.OpEqual), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray( intObject(1), intObject(2)))) @@ -150,11 +150,11 @@ func TestCompiler_Compile(t *testing.T) { expectCompile(t, `1 != 2`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpConstant, 0), - internal.MakeInstruction(internal.OpConstant, 1), - internal.MakeInstruction(internal.OpNotEqual), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpConstant, 0), + tengo.MakeInstruction(parser.OpConstant, 1), + tengo.MakeInstruction(parser.OpNotEqual), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray( intObject(1), intObject(2)))) @@ -162,52 +162,52 @@ func TestCompiler_Compile(t *testing.T) { expectCompile(t, `true == false`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpTrue), - internal.MakeInstruction(internal.OpFalse), - internal.MakeInstruction(internal.OpEqual), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpTrue), + tengo.MakeInstruction(parser.OpFalse), + tengo.MakeInstruction(parser.OpEqual), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray())) expectCompile(t, `true != false`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpTrue), - internal.MakeInstruction(internal.OpFalse), - internal.MakeInstruction(internal.OpNotEqual), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpTrue), + tengo.MakeInstruction(parser.OpFalse), + tengo.MakeInstruction(parser.OpNotEqual), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray())) expectCompile(t, `-1`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpConstant, 0), - internal.MakeInstruction(internal.OpMinus), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpConstant, 0), + tengo.MakeInstruction(parser.OpMinus), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray( intObject(1)))) expectCompile(t, `!true`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpTrue), - internal.MakeInstruction(internal.OpLNot), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpTrue), + tengo.MakeInstruction(parser.OpLNot), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray())) expectCompile(t, `if true { 10 }; 3333`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpTrue), // 0000 - internal.MakeInstruction(internal.OpJumpFalsy, 8), // 0001 - internal.MakeInstruction(internal.OpConstant, 0), // 0004 - internal.MakeInstruction(internal.OpPop), // 0007 - internal.MakeInstruction(internal.OpConstant, 1), // 0008 - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpSuspend)), // 0011 + tengo.MakeInstruction(parser.OpTrue), // 0000 + tengo.MakeInstruction(parser.OpJumpFalsy, 8), // 0001 + tengo.MakeInstruction(parser.OpConstant, 0), // 0004 + tengo.MakeInstruction(parser.OpPop), // 0007 + tengo.MakeInstruction(parser.OpConstant, 1), // 0008 + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpSuspend)), // 0011 objectsArray( intObject(10), intObject(3333)))) @@ -215,16 +215,16 @@ func TestCompiler_Compile(t *testing.T) { expectCompile(t, `if (true) { 10 } else { 20 }; 3333;`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpTrue), // 0000 - internal.MakeInstruction(internal.OpJumpFalsy, 11), // 0001 - internal.MakeInstruction(internal.OpConstant, 0), // 0004 - internal.MakeInstruction(internal.OpPop), // 0007 - internal.MakeInstruction(internal.OpJump, 15), // 0008 - internal.MakeInstruction(internal.OpConstant, 1), // 0011 - internal.MakeInstruction(internal.OpPop), // 0014 - internal.MakeInstruction(internal.OpConstant, 2), // 0015 - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpSuspend)), // 0018 + tengo.MakeInstruction(parser.OpTrue), // 0000 + tengo.MakeInstruction(parser.OpJumpFalsy, 11), // 0001 + tengo.MakeInstruction(parser.OpConstant, 0), // 0004 + tengo.MakeInstruction(parser.OpPop), // 0007 + tengo.MakeInstruction(parser.OpJump, 15), // 0008 + tengo.MakeInstruction(parser.OpConstant, 1), // 0011 + tengo.MakeInstruction(parser.OpPop), // 0014 + tengo.MakeInstruction(parser.OpConstant, 2), // 0015 + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpSuspend)), // 0018 objectsArray( intObject(10), intObject(20), @@ -233,20 +233,20 @@ func TestCompiler_Compile(t *testing.T) { expectCompile(t, `"kami"`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpConstant, 0), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpConstant, 0), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray( stringObject("kami")))) expectCompile(t, `"ka" + "mi"`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpConstant, 0), - internal.MakeInstruction(internal.OpConstant, 1), - internal.MakeInstruction(internal.OpBinaryOp, 11), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpConstant, 0), + tengo.MakeInstruction(parser.OpConstant, 1), + tengo.MakeInstruction(parser.OpBinaryOp, 11), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray( stringObject("ka"), stringObject("mi")))) @@ -254,15 +254,15 @@ func TestCompiler_Compile(t *testing.T) { expectCompile(t, `a := 1; b := 2; a += b`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpConstant, 0), - internal.MakeInstruction(internal.OpSetGlobal, 0), - internal.MakeInstruction(internal.OpConstant, 1), - internal.MakeInstruction(internal.OpSetGlobal, 1), - internal.MakeInstruction(internal.OpGetGlobal, 0), - internal.MakeInstruction(internal.OpGetGlobal, 1), - internal.MakeInstruction(internal.OpBinaryOp, 11), - internal.MakeInstruction(internal.OpSetGlobal, 0), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpConstant, 0), + tengo.MakeInstruction(parser.OpSetGlobal, 0), + tengo.MakeInstruction(parser.OpConstant, 1), + tengo.MakeInstruction(parser.OpSetGlobal, 1), + tengo.MakeInstruction(parser.OpGetGlobal, 0), + tengo.MakeInstruction(parser.OpGetGlobal, 1), + tengo.MakeInstruction(parser.OpBinaryOp, 11), + tengo.MakeInstruction(parser.OpSetGlobal, 0), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray( intObject(1), intObject(2)))) @@ -270,15 +270,15 @@ func TestCompiler_Compile(t *testing.T) { expectCompile(t, `a := 1; b := 2; a /= b`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpConstant, 0), - internal.MakeInstruction(internal.OpSetGlobal, 0), - internal.MakeInstruction(internal.OpConstant, 1), - internal.MakeInstruction(internal.OpSetGlobal, 1), - internal.MakeInstruction(internal.OpGetGlobal, 0), - internal.MakeInstruction(internal.OpGetGlobal, 1), - internal.MakeInstruction(internal.OpBinaryOp, 14), - internal.MakeInstruction(internal.OpSetGlobal, 0), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpConstant, 0), + tengo.MakeInstruction(parser.OpSetGlobal, 0), + tengo.MakeInstruction(parser.OpConstant, 1), + tengo.MakeInstruction(parser.OpSetGlobal, 1), + tengo.MakeInstruction(parser.OpGetGlobal, 0), + tengo.MakeInstruction(parser.OpGetGlobal, 1), + tengo.MakeInstruction(parser.OpBinaryOp, 14), + tengo.MakeInstruction(parser.OpSetGlobal, 0), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray( intObject(1), intObject(2)))) @@ -286,20 +286,20 @@ func TestCompiler_Compile(t *testing.T) { expectCompile(t, `[]`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpArray, 0), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpArray, 0), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray())) expectCompile(t, `[1, 2, 3]`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpConstant, 0), - internal.MakeInstruction(internal.OpConstant, 1), - internal.MakeInstruction(internal.OpConstant, 2), - internal.MakeInstruction(internal.OpArray, 3), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpConstant, 0), + tengo.MakeInstruction(parser.OpConstant, 1), + tengo.MakeInstruction(parser.OpConstant, 2), + tengo.MakeInstruction(parser.OpArray, 3), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray( intObject(1), intObject(2), @@ -308,18 +308,18 @@ func TestCompiler_Compile(t *testing.T) { expectCompile(t, `[1 + 2, 3 - 4, 5 * 6]`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpConstant, 0), - internal.MakeInstruction(internal.OpConstant, 1), - internal.MakeInstruction(internal.OpBinaryOp, 11), - internal.MakeInstruction(internal.OpConstant, 2), - internal.MakeInstruction(internal.OpConstant, 3), - internal.MakeInstruction(internal.OpBinaryOp, 12), - internal.MakeInstruction(internal.OpConstant, 4), - internal.MakeInstruction(internal.OpConstant, 5), - internal.MakeInstruction(internal.OpBinaryOp, 13), - internal.MakeInstruction(internal.OpArray, 3), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpConstant, 0), + tengo.MakeInstruction(parser.OpConstant, 1), + tengo.MakeInstruction(parser.OpBinaryOp, 11), + tengo.MakeInstruction(parser.OpConstant, 2), + tengo.MakeInstruction(parser.OpConstant, 3), + tengo.MakeInstruction(parser.OpBinaryOp, 12), + tengo.MakeInstruction(parser.OpConstant, 4), + tengo.MakeInstruction(parser.OpConstant, 5), + tengo.MakeInstruction(parser.OpBinaryOp, 13), + tengo.MakeInstruction(parser.OpArray, 3), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray( intObject(1), intObject(2), @@ -331,23 +331,23 @@ func TestCompiler_Compile(t *testing.T) { expectCompile(t, `{}`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpMap, 0), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpMap, 0), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray())) expectCompile(t, `{a: 2, b: 4, c: 6}`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpConstant, 0), - internal.MakeInstruction(internal.OpConstant, 1), - internal.MakeInstruction(internal.OpConstant, 2), - internal.MakeInstruction(internal.OpConstant, 3), - internal.MakeInstruction(internal.OpConstant, 4), - internal.MakeInstruction(internal.OpConstant, 5), - internal.MakeInstruction(internal.OpMap, 6), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpConstant, 0), + tengo.MakeInstruction(parser.OpConstant, 1), + tengo.MakeInstruction(parser.OpConstant, 2), + tengo.MakeInstruction(parser.OpConstant, 3), + tengo.MakeInstruction(parser.OpConstant, 4), + tengo.MakeInstruction(parser.OpConstant, 5), + tengo.MakeInstruction(parser.OpMap, 6), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray( stringObject("a"), intObject(2), @@ -359,17 +359,17 @@ func TestCompiler_Compile(t *testing.T) { expectCompile(t, `{a: 2 + 3, b: 5 * 6}`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpConstant, 0), - internal.MakeInstruction(internal.OpConstant, 1), - internal.MakeInstruction(internal.OpConstant, 2), - internal.MakeInstruction(internal.OpBinaryOp, 11), - internal.MakeInstruction(internal.OpConstant, 3), - internal.MakeInstruction(internal.OpConstant, 4), - internal.MakeInstruction(internal.OpConstant, 5), - internal.MakeInstruction(internal.OpBinaryOp, 13), - internal.MakeInstruction(internal.OpMap, 4), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpConstant, 0), + tengo.MakeInstruction(parser.OpConstant, 1), + tengo.MakeInstruction(parser.OpConstant, 2), + tengo.MakeInstruction(parser.OpBinaryOp, 11), + tengo.MakeInstruction(parser.OpConstant, 3), + tengo.MakeInstruction(parser.OpConstant, 4), + tengo.MakeInstruction(parser.OpConstant, 5), + tengo.MakeInstruction(parser.OpBinaryOp, 13), + tengo.MakeInstruction(parser.OpMap, 4), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray( stringObject("a"), intObject(2), @@ -381,16 +381,16 @@ func TestCompiler_Compile(t *testing.T) { expectCompile(t, `[1, 2, 3][1 + 1]`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpConstant, 0), - internal.MakeInstruction(internal.OpConstant, 1), - internal.MakeInstruction(internal.OpConstant, 2), - internal.MakeInstruction(internal.OpArray, 3), - internal.MakeInstruction(internal.OpConstant, 0), - internal.MakeInstruction(internal.OpConstant, 0), - internal.MakeInstruction(internal.OpBinaryOp, 11), - internal.MakeInstruction(internal.OpIndex), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpConstant, 0), + tengo.MakeInstruction(parser.OpConstant, 1), + tengo.MakeInstruction(parser.OpConstant, 2), + tengo.MakeInstruction(parser.OpArray, 3), + tengo.MakeInstruction(parser.OpConstant, 0), + tengo.MakeInstruction(parser.OpConstant, 0), + tengo.MakeInstruction(parser.OpBinaryOp, 11), + tengo.MakeInstruction(parser.OpIndex), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray( intObject(1), intObject(2), @@ -399,15 +399,15 @@ func TestCompiler_Compile(t *testing.T) { expectCompile(t, `{a: 2}[2 - 1]`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpConstant, 0), - internal.MakeInstruction(internal.OpConstant, 1), - internal.MakeInstruction(internal.OpMap, 2), - internal.MakeInstruction(internal.OpConstant, 1), - internal.MakeInstruction(internal.OpConstant, 2), - internal.MakeInstruction(internal.OpBinaryOp, 12), - internal.MakeInstruction(internal.OpIndex), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpConstant, 0), + tengo.MakeInstruction(parser.OpConstant, 1), + tengo.MakeInstruction(parser.OpMap, 2), + tengo.MakeInstruction(parser.OpConstant, 1), + tengo.MakeInstruction(parser.OpConstant, 2), + tengo.MakeInstruction(parser.OpBinaryOp, 12), + tengo.MakeInstruction(parser.OpIndex), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray( stringObject("a"), intObject(2), @@ -416,15 +416,15 @@ func TestCompiler_Compile(t *testing.T) { expectCompile(t, `[1, 2, 3][:]`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpConstant, 0), - internal.MakeInstruction(internal.OpConstant, 1), - internal.MakeInstruction(internal.OpConstant, 2), - internal.MakeInstruction(internal.OpArray, 3), - internal.MakeInstruction(internal.OpNull), - internal.MakeInstruction(internal.OpNull), - internal.MakeInstruction(internal.OpSliceIndex), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpConstant, 0), + tengo.MakeInstruction(parser.OpConstant, 1), + tengo.MakeInstruction(parser.OpConstant, 2), + tengo.MakeInstruction(parser.OpArray, 3), + tengo.MakeInstruction(parser.OpNull), + tengo.MakeInstruction(parser.OpNull), + tengo.MakeInstruction(parser.OpSliceIndex), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray( intObject(1), intObject(2), @@ -433,15 +433,15 @@ func TestCompiler_Compile(t *testing.T) { expectCompile(t, `[1, 2, 3][0 : 2]`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpConstant, 0), - internal.MakeInstruction(internal.OpConstant, 1), - internal.MakeInstruction(internal.OpConstant, 2), - internal.MakeInstruction(internal.OpArray, 3), - internal.MakeInstruction(internal.OpConstant, 3), - internal.MakeInstruction(internal.OpConstant, 1), - internal.MakeInstruction(internal.OpSliceIndex), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpConstant, 0), + tengo.MakeInstruction(parser.OpConstant, 1), + tengo.MakeInstruction(parser.OpConstant, 2), + tengo.MakeInstruction(parser.OpArray, 3), + tengo.MakeInstruction(parser.OpConstant, 3), + tengo.MakeInstruction(parser.OpConstant, 1), + tengo.MakeInstruction(parser.OpSliceIndex), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray( intObject(1), intObject(2), @@ -451,15 +451,15 @@ func TestCompiler_Compile(t *testing.T) { expectCompile(t, `[1, 2, 3][:2]`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpConstant, 0), - internal.MakeInstruction(internal.OpConstant, 1), - internal.MakeInstruction(internal.OpConstant, 2), - internal.MakeInstruction(internal.OpArray, 3), - internal.MakeInstruction(internal.OpNull), - internal.MakeInstruction(internal.OpConstant, 1), - internal.MakeInstruction(internal.OpSliceIndex), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpConstant, 0), + tengo.MakeInstruction(parser.OpConstant, 1), + tengo.MakeInstruction(parser.OpConstant, 2), + tengo.MakeInstruction(parser.OpArray, 3), + tengo.MakeInstruction(parser.OpNull), + tengo.MakeInstruction(parser.OpConstant, 1), + tengo.MakeInstruction(parser.OpSliceIndex), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray( intObject(1), intObject(2), @@ -468,15 +468,15 @@ func TestCompiler_Compile(t *testing.T) { expectCompile(t, `[1, 2, 3][0:]`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpConstant, 0), - internal.MakeInstruction(internal.OpConstant, 1), - internal.MakeInstruction(internal.OpConstant, 2), - internal.MakeInstruction(internal.OpArray, 3), - internal.MakeInstruction(internal.OpConstant, 3), - internal.MakeInstruction(internal.OpNull), - internal.MakeInstruction(internal.OpSliceIndex), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpConstant, 0), + tengo.MakeInstruction(parser.OpConstant, 1), + tengo.MakeInstruction(parser.OpConstant, 2), + tengo.MakeInstruction(parser.OpArray, 3), + tengo.MakeInstruction(parser.OpConstant, 3), + tengo.MakeInstruction(parser.OpNull), + tengo.MakeInstruction(parser.OpSliceIndex), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray( intObject(1), intObject(2), @@ -486,277 +486,277 @@ func TestCompiler_Compile(t *testing.T) { expectCompile(t, `func() { return 5 + 10 }`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpConstant, 2), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpConstant, 2), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray( intObject(5), intObject(10), compiledFunction(0, 0, - internal.MakeInstruction(internal.OpConstant, 0), - internal.MakeInstruction(internal.OpConstant, 1), - internal.MakeInstruction(internal.OpBinaryOp, 11), - internal.MakeInstruction(internal.OpReturn, 1))))) + tengo.MakeInstruction(parser.OpConstant, 0), + tengo.MakeInstruction(parser.OpConstant, 1), + tengo.MakeInstruction(parser.OpBinaryOp, 11), + tengo.MakeInstruction(parser.OpReturn, 1))))) expectCompile(t, `func() { 5 + 10 }`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpConstant, 2), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpConstant, 2), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray( intObject(5), intObject(10), compiledFunction(0, 0, - internal.MakeInstruction(internal.OpConstant, 0), - internal.MakeInstruction(internal.OpConstant, 1), - internal.MakeInstruction(internal.OpBinaryOp, 11), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpReturn, 0))))) + tengo.MakeInstruction(parser.OpConstant, 0), + tengo.MakeInstruction(parser.OpConstant, 1), + tengo.MakeInstruction(parser.OpBinaryOp, 11), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpReturn, 0))))) expectCompile(t, `func() { 1; 2 }`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpConstant, 2), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpConstant, 2), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray( intObject(1), intObject(2), compiledFunction(0, 0, - internal.MakeInstruction(internal.OpConstant, 0), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpConstant, 1), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpReturn, 0))))) + tengo.MakeInstruction(parser.OpConstant, 0), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpConstant, 1), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpReturn, 0))))) expectCompile(t, `func() { 1; return 2 }`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpConstant, 2), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpConstant, 2), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray( intObject(1), intObject(2), compiledFunction(0, 0, - internal.MakeInstruction(internal.OpConstant, 0), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpConstant, 1), - internal.MakeInstruction(internal.OpReturn, 1))))) + tengo.MakeInstruction(parser.OpConstant, 0), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpConstant, 1), + tengo.MakeInstruction(parser.OpReturn, 1))))) expectCompile(t, `func() { if(true) { return 1 } else { return 2 } }`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpConstant, 2), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpConstant, 2), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray( intObject(1), intObject(2), compiledFunction(0, 0, - internal.MakeInstruction(internal.OpTrue), // 0000 - internal.MakeInstruction(internal.OpJumpFalsy, 9), // 0001 - internal.MakeInstruction(internal.OpConstant, 0), // 0004 - internal.MakeInstruction(internal.OpReturn, 1), // 0007 - internal.MakeInstruction(internal.OpConstant, 1), // 0009 - internal.MakeInstruction(internal.OpReturn, 1))))) // 0012 + tengo.MakeInstruction(parser.OpTrue), // 0000 + tengo.MakeInstruction(parser.OpJumpFalsy, 9), // 0001 + tengo.MakeInstruction(parser.OpConstant, 0), // 0004 + tengo.MakeInstruction(parser.OpReturn, 1), // 0007 + tengo.MakeInstruction(parser.OpConstant, 1), // 0009 + tengo.MakeInstruction(parser.OpReturn, 1))))) // 0012 expectCompile(t, `func() { 1; if(true) { 2 } else { 3 }; 4 }`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpConstant, 4), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpConstant, 4), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray( intObject(1), intObject(2), intObject(3), intObject(4), compiledFunction(0, 0, - internal.MakeInstruction(internal.OpConstant, 0), // 0000 - internal.MakeInstruction(internal.OpPop), // 0003 - internal.MakeInstruction(internal.OpTrue), // 0004 - internal.MakeInstruction(internal.OpJumpFalsy, 15), // 0005 - internal.MakeInstruction(internal.OpConstant, 1), // 0008 - internal.MakeInstruction(internal.OpPop), // 0011 - internal.MakeInstruction(internal.OpJump, 19), // 0012 - internal.MakeInstruction(internal.OpConstant, 2), // 0015 - internal.MakeInstruction(internal.OpPop), // 0018 - internal.MakeInstruction(internal.OpConstant, 3), // 0019 - internal.MakeInstruction(internal.OpPop), // 0022 - internal.MakeInstruction(internal.OpReturn, 0))))) // 0023 + tengo.MakeInstruction(parser.OpConstant, 0), // 0000 + tengo.MakeInstruction(parser.OpPop), // 0003 + tengo.MakeInstruction(parser.OpTrue), // 0004 + tengo.MakeInstruction(parser.OpJumpFalsy, 15), // 0005 + tengo.MakeInstruction(parser.OpConstant, 1), // 0008 + tengo.MakeInstruction(parser.OpPop), // 0011 + tengo.MakeInstruction(parser.OpJump, 19), // 0012 + tengo.MakeInstruction(parser.OpConstant, 2), // 0015 + tengo.MakeInstruction(parser.OpPop), // 0018 + tengo.MakeInstruction(parser.OpConstant, 3), // 0019 + tengo.MakeInstruction(parser.OpPop), // 0022 + tengo.MakeInstruction(parser.OpReturn, 0))))) // 0023 expectCompile(t, `func() { }`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpConstant, 0), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpConstant, 0), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray( compiledFunction(0, 0, - internal.MakeInstruction(internal.OpReturn, 0))))) + tengo.MakeInstruction(parser.OpReturn, 0))))) expectCompile(t, `func() { 24 }()`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpConstant, 1), - internal.MakeInstruction(internal.OpCall, 0), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpConstant, 1), + tengo.MakeInstruction(parser.OpCall, 0), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray( intObject(24), compiledFunction(0, 0, - internal.MakeInstruction(internal.OpConstant, 0), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpReturn, 0))))) + tengo.MakeInstruction(parser.OpConstant, 0), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpReturn, 0))))) expectCompile(t, `func() { return 24 }()`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpConstant, 1), - internal.MakeInstruction(internal.OpCall, 0), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpConstant, 1), + tengo.MakeInstruction(parser.OpCall, 0), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray( intObject(24), compiledFunction(0, 0, - internal.MakeInstruction(internal.OpConstant, 0), - internal.MakeInstruction(internal.OpReturn, 1))))) + tengo.MakeInstruction(parser.OpConstant, 0), + tengo.MakeInstruction(parser.OpReturn, 1))))) expectCompile(t, `noArg := func() { 24 }; noArg();`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpConstant, 1), - internal.MakeInstruction(internal.OpSetGlobal, 0), - internal.MakeInstruction(internal.OpGetGlobal, 0), - internal.MakeInstruction(internal.OpCall, 0), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpConstant, 1), + tengo.MakeInstruction(parser.OpSetGlobal, 0), + tengo.MakeInstruction(parser.OpGetGlobal, 0), + tengo.MakeInstruction(parser.OpCall, 0), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray( intObject(24), compiledFunction(0, 0, - internal.MakeInstruction(internal.OpConstant, 0), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpReturn, 0))))) + tengo.MakeInstruction(parser.OpConstant, 0), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpReturn, 0))))) expectCompile(t, `noArg := func() { return 24 }; noArg();`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpConstant, 1), - internal.MakeInstruction(internal.OpSetGlobal, 0), - internal.MakeInstruction(internal.OpGetGlobal, 0), - internal.MakeInstruction(internal.OpCall, 0), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpConstant, 1), + tengo.MakeInstruction(parser.OpSetGlobal, 0), + tengo.MakeInstruction(parser.OpGetGlobal, 0), + tengo.MakeInstruction(parser.OpCall, 0), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray( intObject(24), compiledFunction(0, 0, - internal.MakeInstruction(internal.OpConstant, 0), - internal.MakeInstruction(internal.OpReturn, 1))))) + tengo.MakeInstruction(parser.OpConstant, 0), + tengo.MakeInstruction(parser.OpReturn, 1))))) expectCompile(t, `n := 55; func() { n };`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpConstant, 0), - internal.MakeInstruction(internal.OpSetGlobal, 0), - internal.MakeInstruction(internal.OpConstant, 1), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpConstant, 0), + tengo.MakeInstruction(parser.OpSetGlobal, 0), + tengo.MakeInstruction(parser.OpConstant, 1), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray( intObject(55), compiledFunction(0, 0, - internal.MakeInstruction(internal.OpGetGlobal, 0), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpReturn, 0))))) + tengo.MakeInstruction(parser.OpGetGlobal, 0), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpReturn, 0))))) expectCompile(t, `func() { n := 55; return n }`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpConstant, 1), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpConstant, 1), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray( intObject(55), compiledFunction(1, 0, - internal.MakeInstruction(internal.OpConstant, 0), - internal.MakeInstruction(internal.OpDefineLocal, 0), - internal.MakeInstruction(internal.OpGetLocal, 0), - internal.MakeInstruction(internal.OpReturn, 1))))) + tengo.MakeInstruction(parser.OpConstant, 0), + tengo.MakeInstruction(parser.OpDefineLocal, 0), + tengo.MakeInstruction(parser.OpGetLocal, 0), + tengo.MakeInstruction(parser.OpReturn, 1))))) expectCompile(t, `func() { a := 55; b := 77; return a + b }`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpConstant, 2), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpConstant, 2), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray( intObject(55), intObject(77), compiledFunction(2, 0, - internal.MakeInstruction(internal.OpConstant, 0), - internal.MakeInstruction(internal.OpDefineLocal, 0), - internal.MakeInstruction(internal.OpConstant, 1), - internal.MakeInstruction(internal.OpDefineLocal, 1), - internal.MakeInstruction(internal.OpGetLocal, 0), - internal.MakeInstruction(internal.OpGetLocal, 1), - internal.MakeInstruction(internal.OpBinaryOp, 11), - internal.MakeInstruction(internal.OpReturn, 1))))) + tengo.MakeInstruction(parser.OpConstant, 0), + tengo.MakeInstruction(parser.OpDefineLocal, 0), + tengo.MakeInstruction(parser.OpConstant, 1), + tengo.MakeInstruction(parser.OpDefineLocal, 1), + tengo.MakeInstruction(parser.OpGetLocal, 0), + tengo.MakeInstruction(parser.OpGetLocal, 1), + tengo.MakeInstruction(parser.OpBinaryOp, 11), + tengo.MakeInstruction(parser.OpReturn, 1))))) expectCompile(t, `f1 := func(a) { return a }; f1(24);`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpConstant, 0), - internal.MakeInstruction(internal.OpSetGlobal, 0), - internal.MakeInstruction(internal.OpGetGlobal, 0), - internal.MakeInstruction(internal.OpConstant, 1), - internal.MakeInstruction(internal.OpCall, 1), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpConstant, 0), + tengo.MakeInstruction(parser.OpSetGlobal, 0), + tengo.MakeInstruction(parser.OpGetGlobal, 0), + tengo.MakeInstruction(parser.OpConstant, 1), + tengo.MakeInstruction(parser.OpCall, 1), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray( compiledFunction(1, 1, - internal.MakeInstruction(internal.OpGetLocal, 0), - internal.MakeInstruction(internal.OpReturn, 1)), + tengo.MakeInstruction(parser.OpGetLocal, 0), + tengo.MakeInstruction(parser.OpReturn, 1)), intObject(24)))) expectCompile(t, `varTest := func(...a) { return a }; varTest(1,2,3);`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpConstant, 0), - internal.MakeInstruction(internal.OpSetGlobal, 0), - internal.MakeInstruction(internal.OpGetGlobal, 0), - internal.MakeInstruction(internal.OpConstant, 1), - internal.MakeInstruction(internal.OpConstant, 2), - internal.MakeInstruction(internal.OpConstant, 3), - internal.MakeInstruction(internal.OpCall, 3), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpConstant, 0), + tengo.MakeInstruction(parser.OpSetGlobal, 0), + tengo.MakeInstruction(parser.OpGetGlobal, 0), + tengo.MakeInstruction(parser.OpConstant, 1), + tengo.MakeInstruction(parser.OpConstant, 2), + tengo.MakeInstruction(parser.OpConstant, 3), + tengo.MakeInstruction(parser.OpCall, 3), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray( compiledFunction(1, 1, - internal.MakeInstruction(internal.OpGetLocal, 0), - internal.MakeInstruction(internal.OpReturn, 1)), + tengo.MakeInstruction(parser.OpGetLocal, 0), + tengo.MakeInstruction(parser.OpReturn, 1)), intObject(1), intObject(2), intObject(3)))) expectCompile(t, `f1 := func(a, b, c) { a; b; return c; }; f1(24, 25, 26);`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpConstant, 0), - internal.MakeInstruction(internal.OpSetGlobal, 0), - internal.MakeInstruction(internal.OpGetGlobal, 0), - internal.MakeInstruction(internal.OpConstant, 1), - internal.MakeInstruction(internal.OpConstant, 2), - internal.MakeInstruction(internal.OpConstant, 3), - internal.MakeInstruction(internal.OpCall, 3), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpConstant, 0), + tengo.MakeInstruction(parser.OpSetGlobal, 0), + tengo.MakeInstruction(parser.OpGetGlobal, 0), + tengo.MakeInstruction(parser.OpConstant, 1), + tengo.MakeInstruction(parser.OpConstant, 2), + tengo.MakeInstruction(parser.OpConstant, 3), + tengo.MakeInstruction(parser.OpCall, 3), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray( compiledFunction(3, 3, - internal.MakeInstruction(internal.OpGetLocal, 0), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpGetLocal, 1), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpGetLocal, 2), - internal.MakeInstruction(internal.OpReturn, 1)), + tengo.MakeInstruction(parser.OpGetLocal, 0), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpGetLocal, 1), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpGetLocal, 2), + tengo.MakeInstruction(parser.OpReturn, 1)), intObject(24), intObject(25), intObject(26)))) @@ -764,59 +764,59 @@ func TestCompiler_Compile(t *testing.T) { expectCompile(t, `func() { n := 55; n = 23; return n }`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpConstant, 2), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpConstant, 2), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray( intObject(55), intObject(23), compiledFunction(1, 0, - internal.MakeInstruction(internal.OpConstant, 0), - internal.MakeInstruction(internal.OpDefineLocal, 0), - internal.MakeInstruction(internal.OpConstant, 1), - internal.MakeInstruction(internal.OpSetLocal, 0), - internal.MakeInstruction(internal.OpGetLocal, 0), - internal.MakeInstruction(internal.OpReturn, 1))))) + tengo.MakeInstruction(parser.OpConstant, 0), + tengo.MakeInstruction(parser.OpDefineLocal, 0), + tengo.MakeInstruction(parser.OpConstant, 1), + tengo.MakeInstruction(parser.OpSetLocal, 0), + tengo.MakeInstruction(parser.OpGetLocal, 0), + tengo.MakeInstruction(parser.OpReturn, 1))))) expectCompile(t, `len([]);`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpGetBuiltin, 0), - internal.MakeInstruction(internal.OpArray, 0), - internal.MakeInstruction(internal.OpCall, 1), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpGetBuiltin, 0), + tengo.MakeInstruction(parser.OpArray, 0), + tengo.MakeInstruction(parser.OpCall, 1), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray())) expectCompile(t, `func() { return len([]) }`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpConstant, 0), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpConstant, 0), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray( compiledFunction(0, 0, - internal.MakeInstruction(internal.OpGetBuiltin, 0), - internal.MakeInstruction(internal.OpArray, 0), - internal.MakeInstruction(internal.OpCall, 1), - internal.MakeInstruction(internal.OpReturn, 1))))) + tengo.MakeInstruction(parser.OpGetBuiltin, 0), + tengo.MakeInstruction(parser.OpArray, 0), + tengo.MakeInstruction(parser.OpCall, 1), + tengo.MakeInstruction(parser.OpReturn, 1))))) expectCompile(t, `func(a) { func(b) { return a + b } }`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpConstant, 1), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpConstant, 1), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray( compiledFunction(1, 1, - internal.MakeInstruction(internal.OpGetFree, 0), - internal.MakeInstruction(internal.OpGetLocal, 0), - internal.MakeInstruction(internal.OpBinaryOp, 11), - internal.MakeInstruction(internal.OpReturn, 1)), + tengo.MakeInstruction(parser.OpGetFree, 0), + tengo.MakeInstruction(parser.OpGetLocal, 0), + tengo.MakeInstruction(parser.OpBinaryOp, 11), + tengo.MakeInstruction(parser.OpReturn, 1)), compiledFunction(1, 1, - internal.MakeInstruction(internal.OpGetLocalPtr, 0), - internal.MakeInstruction(internal.OpClosure, 0, 1), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpReturn, 0))))) + tengo.MakeInstruction(parser.OpGetLocalPtr, 0), + tengo.MakeInstruction(parser.OpClosure, 0, 1), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpReturn, 0))))) expectCompile(t, ` func(a) { @@ -828,26 +828,26 @@ func(a) { }`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpConstant, 2), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpConstant, 2), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray( compiledFunction(1, 1, - internal.MakeInstruction(internal.OpGetFree, 0), - internal.MakeInstruction(internal.OpGetFree, 1), - internal.MakeInstruction(internal.OpBinaryOp, 11), - internal.MakeInstruction(internal.OpGetLocal, 0), - internal.MakeInstruction(internal.OpBinaryOp, 11), - internal.MakeInstruction(internal.OpReturn, 1)), + tengo.MakeInstruction(parser.OpGetFree, 0), + tengo.MakeInstruction(parser.OpGetFree, 1), + tengo.MakeInstruction(parser.OpBinaryOp, 11), + tengo.MakeInstruction(parser.OpGetLocal, 0), + tengo.MakeInstruction(parser.OpBinaryOp, 11), + tengo.MakeInstruction(parser.OpReturn, 1)), compiledFunction(1, 1, - internal.MakeInstruction(internal.OpGetFreePtr, 0), - internal.MakeInstruction(internal.OpGetLocalPtr, 0), - internal.MakeInstruction(internal.OpClosure, 0, 2), - internal.MakeInstruction(internal.OpReturn, 1)), + tengo.MakeInstruction(parser.OpGetFreePtr, 0), + tengo.MakeInstruction(parser.OpGetLocalPtr, 0), + tengo.MakeInstruction(parser.OpClosure, 0, 2), + tengo.MakeInstruction(parser.OpReturn, 1)), compiledFunction(1, 1, - internal.MakeInstruction(internal.OpGetLocalPtr, 0), - internal.MakeInstruction(internal.OpClosure, 1, 1), - internal.MakeInstruction(internal.OpReturn, 1))))) + tengo.MakeInstruction(parser.OpGetLocalPtr, 0), + tengo.MakeInstruction(parser.OpClosure, 1, 1), + tengo.MakeInstruction(parser.OpReturn, 1))))) expectCompile(t, ` g := 55; @@ -867,56 +867,56 @@ func() { }`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpConstant, 0), - internal.MakeInstruction(internal.OpSetGlobal, 0), - internal.MakeInstruction(internal.OpConstant, 6), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpConstant, 0), + tengo.MakeInstruction(parser.OpSetGlobal, 0), + tengo.MakeInstruction(parser.OpConstant, 6), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray( intObject(55), intObject(66), intObject(77), intObject(88), compiledFunction(1, 0, - internal.MakeInstruction(internal.OpConstant, 3), - internal.MakeInstruction(internal.OpDefineLocal, 0), - internal.MakeInstruction(internal.OpGetGlobal, 0), - internal.MakeInstruction(internal.OpGetFree, 0), - internal.MakeInstruction(internal.OpBinaryOp, 11), - internal.MakeInstruction(internal.OpGetFree, 1), - internal.MakeInstruction(internal.OpBinaryOp, 11), - internal.MakeInstruction(internal.OpGetLocal, 0), - internal.MakeInstruction(internal.OpBinaryOp, 11), - internal.MakeInstruction(internal.OpReturn, 1)), + tengo.MakeInstruction(parser.OpConstant, 3), + tengo.MakeInstruction(parser.OpDefineLocal, 0), + tengo.MakeInstruction(parser.OpGetGlobal, 0), + tengo.MakeInstruction(parser.OpGetFree, 0), + tengo.MakeInstruction(parser.OpBinaryOp, 11), + tengo.MakeInstruction(parser.OpGetFree, 1), + tengo.MakeInstruction(parser.OpBinaryOp, 11), + tengo.MakeInstruction(parser.OpGetLocal, 0), + tengo.MakeInstruction(parser.OpBinaryOp, 11), + tengo.MakeInstruction(parser.OpReturn, 1)), compiledFunction(1, 0, - internal.MakeInstruction(internal.OpConstant, 2), - internal.MakeInstruction(internal.OpDefineLocal, 0), - internal.MakeInstruction(internal.OpGetFreePtr, 0), - internal.MakeInstruction(internal.OpGetLocalPtr, 0), - internal.MakeInstruction(internal.OpClosure, 4, 2), - internal.MakeInstruction(internal.OpReturn, 1)), + tengo.MakeInstruction(parser.OpConstant, 2), + tengo.MakeInstruction(parser.OpDefineLocal, 0), + tengo.MakeInstruction(parser.OpGetFreePtr, 0), + tengo.MakeInstruction(parser.OpGetLocalPtr, 0), + tengo.MakeInstruction(parser.OpClosure, 4, 2), + tengo.MakeInstruction(parser.OpReturn, 1)), compiledFunction(1, 0, - internal.MakeInstruction(internal.OpConstant, 1), - internal.MakeInstruction(internal.OpDefineLocal, 0), - internal.MakeInstruction(internal.OpGetLocalPtr, 0), - internal.MakeInstruction(internal.OpClosure, 5, 1), - internal.MakeInstruction(internal.OpReturn, 1))))) + tengo.MakeInstruction(parser.OpConstant, 1), + tengo.MakeInstruction(parser.OpDefineLocal, 0), + tengo.MakeInstruction(parser.OpGetLocalPtr, 0), + tengo.MakeInstruction(parser.OpClosure, 5, 1), + tengo.MakeInstruction(parser.OpReturn, 1))))) expectCompile(t, `for i:=0; i<10; i++ {}`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpConstant, 0), - internal.MakeInstruction(internal.OpSetGlobal, 0), - internal.MakeInstruction(internal.OpConstant, 1), - internal.MakeInstruction(internal.OpGetGlobal, 0), - internal.MakeInstruction(internal.OpBinaryOp, 39), - internal.MakeInstruction(internal.OpJumpFalsy, 31), - internal.MakeInstruction(internal.OpGetGlobal, 0), - internal.MakeInstruction(internal.OpConstant, 2), - internal.MakeInstruction(internal.OpBinaryOp, 11), - internal.MakeInstruction(internal.OpSetGlobal, 0), - internal.MakeInstruction(internal.OpJump, 6), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpConstant, 0), + tengo.MakeInstruction(parser.OpSetGlobal, 0), + tengo.MakeInstruction(parser.OpConstant, 1), + tengo.MakeInstruction(parser.OpGetGlobal, 0), + tengo.MakeInstruction(parser.OpBinaryOp, 39), + tengo.MakeInstruction(parser.OpJumpFalsy, 31), + tengo.MakeInstruction(parser.OpGetGlobal, 0), + tengo.MakeInstruction(parser.OpConstant, 2), + tengo.MakeInstruction(parser.OpBinaryOp, 11), + tengo.MakeInstruction(parser.OpSetGlobal, 0), + tengo.MakeInstruction(parser.OpJump, 6), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray( intObject(0), intObject(10), @@ -925,42 +925,42 @@ func() { expectCompile(t, `m := {}; for k, v in m {}`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpMap, 0), - internal.MakeInstruction(internal.OpSetGlobal, 0), - internal.MakeInstruction(internal.OpGetGlobal, 0), - internal.MakeInstruction(internal.OpIteratorInit), - internal.MakeInstruction(internal.OpSetGlobal, 1), - internal.MakeInstruction(internal.OpGetGlobal, 1), - internal.MakeInstruction(internal.OpIteratorNext), - internal.MakeInstruction(internal.OpJumpFalsy, 37), - internal.MakeInstruction(internal.OpGetGlobal, 1), - internal.MakeInstruction(internal.OpIteratorKey), - internal.MakeInstruction(internal.OpSetGlobal, 2), - internal.MakeInstruction(internal.OpGetGlobal, 1), - internal.MakeInstruction(internal.OpIteratorValue), - internal.MakeInstruction(internal.OpSetGlobal, 3), - internal.MakeInstruction(internal.OpJump, 13), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpMap, 0), + tengo.MakeInstruction(parser.OpSetGlobal, 0), + tengo.MakeInstruction(parser.OpGetGlobal, 0), + tengo.MakeInstruction(parser.OpIteratorInit), + tengo.MakeInstruction(parser.OpSetGlobal, 1), + tengo.MakeInstruction(parser.OpGetGlobal, 1), + tengo.MakeInstruction(parser.OpIteratorNext), + tengo.MakeInstruction(parser.OpJumpFalsy, 37), + tengo.MakeInstruction(parser.OpGetGlobal, 1), + tengo.MakeInstruction(parser.OpIteratorKey), + tengo.MakeInstruction(parser.OpSetGlobal, 2), + tengo.MakeInstruction(parser.OpGetGlobal, 1), + tengo.MakeInstruction(parser.OpIteratorValue), + tengo.MakeInstruction(parser.OpSetGlobal, 3), + tengo.MakeInstruction(parser.OpJump, 13), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray())) expectCompile(t, `a := 0; a == 0 && a != 1 || a < 1`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpConstant, 0), - internal.MakeInstruction(internal.OpSetGlobal, 0), - internal.MakeInstruction(internal.OpGetGlobal, 0), - internal.MakeInstruction(internal.OpConstant, 0), - internal.MakeInstruction(internal.OpEqual), - internal.MakeInstruction(internal.OpAndJump, 23), - internal.MakeInstruction(internal.OpGetGlobal, 0), - internal.MakeInstruction(internal.OpConstant, 1), - internal.MakeInstruction(internal.OpNotEqual), - internal.MakeInstruction(internal.OpOrJump, 34), - internal.MakeInstruction(internal.OpConstant, 1), - internal.MakeInstruction(internal.OpGetGlobal, 0), - internal.MakeInstruction(internal.OpBinaryOp, 39), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpConstant, 0), + tengo.MakeInstruction(parser.OpSetGlobal, 0), + tengo.MakeInstruction(parser.OpGetGlobal, 0), + tengo.MakeInstruction(parser.OpConstant, 0), + tengo.MakeInstruction(parser.OpEqual), + tengo.MakeInstruction(parser.OpAndJump, 23), + tengo.MakeInstruction(parser.OpGetGlobal, 0), + tengo.MakeInstruction(parser.OpConstant, 1), + tengo.MakeInstruction(parser.OpNotEqual), + tengo.MakeInstruction(parser.OpOrJump, 34), + tengo.MakeInstruction(parser.OpConstant, 1), + tengo.MakeInstruction(parser.OpGetGlobal, 0), + tengo.MakeInstruction(parser.OpBinaryOp, 39), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray( intObject(0), intObject(1)))) @@ -1023,17 +1023,17 @@ func() { }`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpConstant, 2), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpConstant, 2), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray( intObject(4), intObject(5), compiledFunction(0, 0, - internal.MakeInstruction(internal.OpConstant, 0), - internal.MakeInstruction(internal.OpDefineLocal, 0), - internal.MakeInstruction(internal.OpGetLocal, 0), - internal.MakeInstruction(internal.OpReturn, 1))))) + tengo.MakeInstruction(parser.OpConstant, 0), + tengo.MakeInstruction(parser.OpDefineLocal, 0), + tengo.MakeInstruction(parser.OpGetLocal, 0), + tengo.MakeInstruction(parser.OpReturn, 1))))) expectCompile(t, ` func() { @@ -1050,19 +1050,19 @@ func() { } }`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpConstant, 2), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpConstant, 2), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray( intObject(5), intObject(4), compiledFunction(0, 0, - internal.MakeInstruction(internal.OpTrue), - internal.MakeInstruction(internal.OpJumpFalsy, 9), - internal.MakeInstruction(internal.OpConstant, 0), - internal.MakeInstruction(internal.OpReturn, 1), - internal.MakeInstruction(internal.OpConstant, 1), - internal.MakeInstruction(internal.OpReturn, 1))))) + tengo.MakeInstruction(parser.OpTrue), + tengo.MakeInstruction(parser.OpJumpFalsy, 9), + tengo.MakeInstruction(parser.OpConstant, 0), + tengo.MakeInstruction(parser.OpReturn, 1), + tengo.MakeInstruction(parser.OpConstant, 1), + tengo.MakeInstruction(parser.OpReturn, 1))))) expectCompile(t, ` func() { @@ -1078,29 +1078,29 @@ func() { } }`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpConstant, 4), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpConstant, 4), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray( intObject(1), intObject(5), intObject(10), intObject(20), compiledFunction(0, 0, - internal.MakeInstruction(internal.OpConstant, 0), - internal.MakeInstruction(internal.OpDefineLocal, 0), - internal.MakeInstruction(internal.OpGetLocal, 0), - internal.MakeInstruction(internal.OpConstant, 1), - internal.MakeInstruction(internal.OpEqual), - internal.MakeInstruction(internal.OpJumpFalsy, 19), - internal.MakeInstruction(internal.OpConstant, 2), - internal.MakeInstruction(internal.OpReturn, 1), - internal.MakeInstruction(internal.OpConstant, 1), - internal.MakeInstruction(internal.OpConstant, 1), - internal.MakeInstruction(internal.OpBinaryOp, 11), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpConstant, 3), - internal.MakeInstruction(internal.OpReturn, 1))))) + tengo.MakeInstruction(parser.OpConstant, 0), + tengo.MakeInstruction(parser.OpDefineLocal, 0), + tengo.MakeInstruction(parser.OpGetLocal, 0), + tengo.MakeInstruction(parser.OpConstant, 1), + tengo.MakeInstruction(parser.OpEqual), + tengo.MakeInstruction(parser.OpJumpFalsy, 19), + tengo.MakeInstruction(parser.OpConstant, 2), + tengo.MakeInstruction(parser.OpReturn, 1), + tengo.MakeInstruction(parser.OpConstant, 1), + tengo.MakeInstruction(parser.OpConstant, 1), + tengo.MakeInstruction(parser.OpBinaryOp, 11), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpConstant, 3), + tengo.MakeInstruction(parser.OpReturn, 1))))) expectCompile(t, ` func() { @@ -1117,19 +1117,19 @@ func() { } }`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpConstant, 2), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpConstant, 2), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray( intObject(5), intObject(4), compiledFunction(0, 0, - internal.MakeInstruction(internal.OpTrue), - internal.MakeInstruction(internal.OpJumpFalsy, 9), - internal.MakeInstruction(internal.OpConstant, 0), - internal.MakeInstruction(internal.OpReturn, 1), - internal.MakeInstruction(internal.OpConstant, 1), - internal.MakeInstruction(internal.OpReturn, 1))))) + tengo.MakeInstruction(parser.OpTrue), + tengo.MakeInstruction(parser.OpJumpFalsy, 9), + tengo.MakeInstruction(parser.OpConstant, 0), + tengo.MakeInstruction(parser.OpReturn, 1), + tengo.MakeInstruction(parser.OpConstant, 1), + tengo.MakeInstruction(parser.OpReturn, 1))))) } func TestCompilerScopes(t *testing.T) { @@ -1142,20 +1142,20 @@ if a := 1; a { b := a }`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpConstant, 0), - internal.MakeInstruction(internal.OpSetGlobal, 0), - internal.MakeInstruction(internal.OpGetGlobal, 0), - internal.MakeInstruction(internal.OpJumpFalsy, 27), - internal.MakeInstruction(internal.OpConstant, 1), - internal.MakeInstruction(internal.OpSetGlobal, 0), - internal.MakeInstruction(internal.OpGetGlobal, 0), - internal.MakeInstruction(internal.OpSetGlobal, 1), - internal.MakeInstruction(internal.OpJump, 39), - internal.MakeInstruction(internal.OpConstant, 2), - internal.MakeInstruction(internal.OpSetGlobal, 0), - internal.MakeInstruction(internal.OpGetGlobal, 0), - internal.MakeInstruction(internal.OpSetGlobal, 1), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpConstant, 0), + tengo.MakeInstruction(parser.OpSetGlobal, 0), + tengo.MakeInstruction(parser.OpGetGlobal, 0), + tengo.MakeInstruction(parser.OpJumpFalsy, 27), + tengo.MakeInstruction(parser.OpConstant, 1), + tengo.MakeInstruction(parser.OpSetGlobal, 0), + tengo.MakeInstruction(parser.OpGetGlobal, 0), + tengo.MakeInstruction(parser.OpSetGlobal, 1), + tengo.MakeInstruction(parser.OpJump, 39), + tengo.MakeInstruction(parser.OpConstant, 2), + tengo.MakeInstruction(parser.OpSetGlobal, 0), + tengo.MakeInstruction(parser.OpGetGlobal, 0), + tengo.MakeInstruction(parser.OpSetGlobal, 1), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray( intObject(1), intObject(2), @@ -1172,28 +1172,28 @@ func() { } }`, bytecode( concatInsts( - internal.MakeInstruction(internal.OpConstant, 3), - internal.MakeInstruction(internal.OpPop), - internal.MakeInstruction(internal.OpSuspend)), + tengo.MakeInstruction(parser.OpConstant, 3), + tengo.MakeInstruction(parser.OpPop), + tengo.MakeInstruction(parser.OpSuspend)), objectsArray( intObject(1), intObject(2), intObject(3), compiledFunction(0, 0, - internal.MakeInstruction(internal.OpConstant, 0), - internal.MakeInstruction(internal.OpDefineLocal, 0), - internal.MakeInstruction(internal.OpGetLocal, 0), - internal.MakeInstruction(internal.OpJumpFalsy, 22), - internal.MakeInstruction(internal.OpConstant, 1), - internal.MakeInstruction(internal.OpSetLocal, 0), - internal.MakeInstruction(internal.OpGetLocal, 0), - internal.MakeInstruction(internal.OpDefineLocal, 1), - internal.MakeInstruction(internal.OpJump, 31), - internal.MakeInstruction(internal.OpConstant, 2), - internal.MakeInstruction(internal.OpSetLocal, 0), - internal.MakeInstruction(internal.OpGetLocal, 0), - internal.MakeInstruction(internal.OpDefineLocal, 1), - internal.MakeInstruction(internal.OpReturn, 0))))) + tengo.MakeInstruction(parser.OpConstant, 0), + tengo.MakeInstruction(parser.OpDefineLocal, 0), + tengo.MakeInstruction(parser.OpGetLocal, 0), + tengo.MakeInstruction(parser.OpJumpFalsy, 22), + tengo.MakeInstruction(parser.OpConstant, 1), + tengo.MakeInstruction(parser.OpSetLocal, 0), + tengo.MakeInstruction(parser.OpGetLocal, 0), + tengo.MakeInstruction(parser.OpDefineLocal, 1), + tengo.MakeInstruction(parser.OpJump, 31), + tengo.MakeInstruction(parser.OpConstant, 2), + tengo.MakeInstruction(parser.OpSetLocal, 0), + tengo.MakeInstruction(parser.OpGetLocal, 0), + tengo.MakeInstruction(parser.OpDefineLocal, 1), + tengo.MakeInstruction(parser.OpReturn, 0))))) } func concatInsts(instructions ...[]byte) []byte { @@ -1209,7 +1209,7 @@ func bytecode( constants []tengo.Object, ) *tengo.Bytecode { return &tengo.Bytecode{ - FileSet: internal.NewFileSet(), + FileSet: parser.NewFileSet(), MainFunction: &tengo.CompiledFunction{Instructions: instructions}, Constants: constants, } @@ -1279,12 +1279,12 @@ func traceCompile( input string, symbols map[string]tengo.Object, ) (res *tengo.Bytecode, trace []string, err error) { - fileSet := internal.NewFileSet() + fileSet := parser.NewFileSet() file := fileSet.AddFile("test", -1, len(input)) - p := internal.NewParser(file, []byte(input), nil) + p := parser.NewParser(file, []byte(input), nil) - symTable := internal.NewSymbolTable() + symTable := tengo.NewSymbolTable() for name := range symbols { symTable.Define(name) } diff --git a/doc.go b/doc.go new file mode 100644 index 0000000..05b47de --- /dev/null +++ b/doc.go @@ -0,0 +1,3 @@ +// tengo is a small, dynamic, fast, secure script language for Go. + +package tengo diff --git a/example_test.go b/example_test.go new file mode 100644 index 0000000..2ff3078 --- /dev/null +++ b/example_test.go @@ -0,0 +1,46 @@ +package tengo_test + +import ( + "context" + "fmt" + + "github.com/d5/tengo" +) + +func Example() { + // Tengo script code + src := ` +each := func(seq, fn) { + for x in seq { fn(x) } +} + +sum := 0 +mul := 1 +each([a, b, c, d], func(x) { + sum += x + mul *= x +})` + + // create a new Script instance + script := tengo.NewScript([]byte(src)) + + // set values + _ = script.Add("a", 1) + _ = script.Add("b", 9) + _ = script.Add("c", 8) + _ = script.Add("d", 4) + + // run the script + compiled, err := script.RunContext(context.Background()) + if err != nil { + panic(err) + } + + // retrieve values + sum := compiled.Get("sum") + mul := compiled.Get("mul") + fmt.Println(sum, mul) + + // Output: + // 22 288 +} diff --git a/internal/internal.go b/instructions.go similarity index 68% rename from internal/internal.go rename to instructions.go index 79f430b..9888e4b 100644 --- a/internal/internal.go +++ b/instructions.go @@ -1,10 +1,14 @@ -package internal +package tengo -import "fmt" +import ( + "fmt" + + "github.com/d5/tengo/parser" +) // MakeInstruction returns a bytecode for an opcode and the operands. -func MakeInstruction(opcode Opcode, operands ...int) []byte { - numOperands := OpcodeOperands[opcode] +func MakeInstruction(opcode parser.Opcode, operands ...int) []byte { + numOperands := parser.OpcodeOperands[opcode] totalLen := 1 for _, w := range numOperands { @@ -36,19 +40,19 @@ func FormatInstructions(b []byte, posOffset int) []string { i := 0 for i < len(b) { - numOperands := OpcodeOperands[b[i]] - operands, read := ReadOperands(numOperands, b[i+1:]) + numOperands := parser.OpcodeOperands[b[i]] + operands, read := parser.ReadOperands(numOperands, b[i+1:]) switch len(numOperands) { case 0: out = append(out, fmt.Sprintf("%04d %-7s", - posOffset+i, OpcodeNames[b[i]])) + posOffset+i, parser.OpcodeNames[b[i]])) case 1: out = append(out, fmt.Sprintf("%04d %-7s %-5d", - posOffset+i, OpcodeNames[b[i]], operands[0])) + posOffset+i, parser.OpcodeNames[b[i]], operands[0])) case 2: out = append(out, fmt.Sprintf("%04d %-7s %-5d %-5d", - posOffset+i, OpcodeNames[b[i]], + posOffset+i, parser.OpcodeNames[b[i]], operands[0], operands[1])) } i += 1 + read diff --git a/objects.go b/objects.go index 2b49439..dc346a3 100644 --- a/objects.go +++ b/objects.go @@ -8,8 +8,8 @@ import ( "strings" "time" - "github.com/d5/tengo/internal" - "github.com/d5/tengo/internal/token" + "github.com/d5/tengo/parser" + "github.com/d5/tengo/token" ) var ( @@ -574,7 +574,7 @@ type CompiledFunction struct { NumLocals int // number of local variables (including function parameters) NumParameters int VarArgs bool - SourceMap map[int]internal.Pos + SourceMap map[int]parser.Pos Free []*ObjectPtr } @@ -605,14 +605,14 @@ func (o *CompiledFunction) Equals(_ Object) bool { } // SourcePos returns the source position of the instruction at ip. -func (o *CompiledFunction) SourcePos(ip int) internal.Pos { +func (o *CompiledFunction) SourcePos(ip int) parser.Pos { for ip >= 0 { if p, ok := o.SourceMap[ip]; ok { return p } ip-- } - return internal.NoPos + return parser.NoPos } // CanCall returns whether the Object can be Called. diff --git a/objects_test.go b/objects_test.go index 3363902..807a509 100644 --- a/objects_test.go +++ b/objects_test.go @@ -4,8 +4,8 @@ import ( "testing" "github.com/d5/tengo" - "github.com/d5/tengo/internal/require" - "github.com/d5/tengo/internal/token" + "github.com/d5/tengo/require" + "github.com/d5/tengo/token" ) func TestObject_TypeName(t *testing.T) { diff --git a/internal/ast.go b/parser/ast.go similarity index 98% rename from internal/ast.go rename to parser/ast.go index ff52b32..8c2f7c0 100644 --- a/internal/ast.go +++ b/parser/ast.go @@ -1,4 +1,4 @@ -package internal +package parser import ( "strings" diff --git a/internal/ast_test.go b/parser/ast_test.go similarity index 75% rename from internal/ast_test.go rename to parser/ast_test.go index 26ae063..9b67138 100644 --- a/internal/ast_test.go +++ b/parser/ast_test.go @@ -1,14 +1,14 @@ -package internal_test +package parser_test import ( "testing" - "github.com/d5/tengo/internal" + "github.com/d5/tengo/parser" ) func TestIdentListString(t *testing.T) { - identListVar := &internal.IdentList{ - List: []*internal.Ident{ + identListVar := &parser.IdentList{ + List: []*parser.Ident{ {Name: "a"}, {Name: "b"}, {Name: "c"}, @@ -22,8 +22,8 @@ func TestIdentListString(t *testing.T) { identListVar, expectedVar, str) } - identList := &internal.IdentList{ - List: []*internal.Ident{ + identList := &parser.IdentList{ + List: []*parser.Ident{ {Name: "a"}, {Name: "b"}, {Name: "c"}, diff --git a/internal/expr.go b/parser/expr.go similarity index 99% rename from internal/expr.go rename to parser/expr.go index 059b646..3ea9b57 100644 --- a/internal/expr.go +++ b/parser/expr.go @@ -1,9 +1,9 @@ -package internal +package parser import ( "strings" - "github.com/d5/tengo/internal/token" + "github.com/d5/tengo/token" ) // Expr represents an expression node in the AST. diff --git a/internal/file.go b/parser/file.go similarity index 97% rename from internal/file.go rename to parser/file.go index 295153e..7cf50fe 100644 --- a/internal/file.go +++ b/parser/file.go @@ -1,4 +1,4 @@ -package internal +package parser import ( "strings" diff --git a/internal/opcodes.go b/parser/opcodes.go similarity index 99% rename from internal/opcodes.go rename to parser/opcodes.go index 2890ec6..a4fbfba 100644 --- a/internal/opcodes.go +++ b/parser/opcodes.go @@ -1,4 +1,4 @@ -package internal +package parser // Opcode represents a single byte operation code. type Opcode = byte diff --git a/internal/parser.go b/parser/parser.go similarity index 97% rename from internal/parser.go rename to parser/parser.go index a790973..051ca0b 100644 --- a/internal/parser.go +++ b/parser/parser.go @@ -1,4 +1,4 @@ -package internal +package parser import ( "fmt" @@ -6,7 +6,7 @@ import ( "sort" "strconv" - "github.com/d5/tengo/internal/token" + "github.com/d5/tengo/token" ) type bailout struct{} @@ -20,37 +20,37 @@ var stmtStart = map[token.Token]bool{ token.Export: true, } -// ParserError represents a parser error. -type ParserError struct { +// Error represents a parser error. +type Error struct { Pos SourceFilePos Msg string } -func (e ParserError) Error() string { +func (e Error) Error() string { if e.Pos.Filename != "" || e.Pos.IsValid() { return fmt.Sprintf("Parse Error: %s\n\tat %s", e.Msg, e.Pos) } return fmt.Sprintf("Parse Error: %s", e.Msg) } -// ParserErrorList is a collection of parser errors. -type ParserErrorList []*ParserError +// ErrorList is a collection of parser errors. +type ErrorList []*Error // Add adds a new parser error to the collection. -func (p *ParserErrorList) Add(pos SourceFilePos, msg string) { - *p = append(*p, &ParserError{pos, msg}) +func (p *ErrorList) Add(pos SourceFilePos, msg string) { + *p = append(*p, &Error{pos, msg}) } // Len returns the number of elements in the collection. -func (p ParserErrorList) Len() int { +func (p ErrorList) Len() int { return len(p) } -func (p ParserErrorList) Swap(i, j int) { +func (p ErrorList) Swap(i, j int) { p[i], p[j] = p[j], p[i] } -func (p ParserErrorList) Less(i, j int) bool { +func (p ErrorList) Less(i, j int) bool { e := &p[i].Pos f := &p[j].Pos @@ -67,11 +67,11 @@ func (p ParserErrorList) Less(i, j int) bool { } // Sort sorts the collection. -func (p ParserErrorList) Sort() { +func (p ErrorList) Sort() { sort.Sort(p) } -func (p ParserErrorList) Error() string { +func (p ErrorList) Error() string { switch len(p) { case 0: return "no errors" @@ -82,7 +82,7 @@ func (p ParserErrorList) Error() string { } // Err returns an error. -func (p ParserErrorList) Err() error { +func (p ErrorList) Err() error { if len(p) == 0 { return nil } @@ -93,7 +93,7 @@ func (p ParserErrorList) Err() error { // implementation. type Parser struct { file *SourceFile - errors ParserErrorList + errors ErrorList scanner *Scanner pos Pos token token.Token diff --git a/internal/parser_test.go b/parser/parser_test.go similarity index 99% rename from internal/parser_test.go rename to parser/parser_test.go index 5a9ac41..1ec5ec9 100644 --- a/internal/parser_test.go +++ b/parser/parser_test.go @@ -1,4 +1,4 @@ -package internal_test +package parser_test import ( "fmt" @@ -7,20 +7,20 @@ import ( "strings" "testing" - . "github.com/d5/tengo/internal" - "github.com/d5/tengo/internal/require" - "github.com/d5/tengo/internal/token" + . "github.com/d5/tengo/parser" + "github.com/d5/tengo/require" + "github.com/d5/tengo/token" ) func TestParserError(t *testing.T) { - err := &ParserError{Pos: SourceFilePos{ + err := &Error{Pos: SourceFilePos{ Offset: 10, Line: 1, Column: 10, }, Msg: "test"} require.Equal(t, "Parse Error: test\n\tat 1:10", err.Error()) } func TestParserErrorList(t *testing.T) { - var list ParserErrorList + var list ErrorList list.Add(SourceFilePos{Offset: 20, Line: 2, Column: 10}, "error 2") list.Add(SourceFilePos{Offset: 30, Line: 3, Column: 10}, "error 3") list.Add(SourceFilePos{Offset: 10, Line: 1, Column: 10}, "error 1") diff --git a/internal/pos.go b/parser/pos.go similarity index 92% rename from internal/pos.go rename to parser/pos.go index 280ee36..f8d3898 100644 --- a/internal/pos.go +++ b/parser/pos.go @@ -1,4 +1,4 @@ -package internal +package parser // Pos represents a position in the file set. type Pos int diff --git a/internal/scanner.go b/parser/scanner.go similarity index 99% rename from internal/scanner.go rename to parser/scanner.go index 555755f..a168cfb 100644 --- a/internal/scanner.go +++ b/parser/scanner.go @@ -1,11 +1,11 @@ -package internal +package parser import ( "fmt" "unicode" "unicode/utf8" - "github.com/d5/tengo/internal/token" + "github.com/d5/tengo/token" ) // byte order mark diff --git a/internal/scanner_test.go b/parser/scanner_test.go similarity index 90% rename from internal/scanner_test.go rename to parser/scanner_test.go index 88043ac..390851d 100644 --- a/internal/scanner_test.go +++ b/parser/scanner_test.go @@ -1,4 +1,4 @@ -package internal_test +package parser_test import ( "fmt" @@ -7,12 +7,12 @@ import ( "testing" "time" - "github.com/d5/tengo/internal" - "github.com/d5/tengo/internal/require" - "github.com/d5/tengo/internal/token" + "github.com/d5/tengo/parser" + "github.com/d5/tengo/require" + "github.com/d5/tengo/token" ) -var testFileSet = internal.NewFileSet() +var testFileSet = parser.NewFileSet() type scanResult struct { Token token.Token @@ -151,7 +151,7 @@ func TestScanner_Scan(t *testing.T) { switch tc.token { case token.Comment: // strip CRs in comments - expectedLiteral = string(internal.StripCR([]byte(tc.literal), + expectedLiteral = string(parser.StripCR([]byte(tc.literal), tc.literal[1] == '*')) //-style comment literal doesn't contain newline @@ -167,7 +167,7 @@ func TestScanner_Scan(t *testing.T) { // strip CRs in raw string expectedLiteral = tc.literal if expectedLiteral[0] == '`' { - expectedLiteral = string(internal.StripCR( + expectedLiteral = string(parser.StripCR( []byte(expectedLiteral), false)) } } else if tc.token.IsKeyword() { @@ -189,9 +189,9 @@ func TestScanner_Scan(t *testing.T) { } scanExpect(t, strings.Join(lines, "\n"), - internal.ScanComments|internal.DontInsertSemis, expected...) + parser.ScanComments|parser.DontInsertSemis, expected...) scanExpect(t, strings.Join(lines, "\n"), - internal.DontInsertSemis, expectedSkipComments...) + parser.DontInsertSemis, expectedSkipComments...) } func TestStripCR(t *testing.T) { @@ -210,7 +210,7 @@ func TestStripCR(t *testing.T) { {"/*\r/\r*\r/*/", "/*/*\r/*/"}, {"/*\r\r\r\r*/", "/**/"}, } { - actual := string(internal.StripCR([]byte(tc.input), + actual := string(parser.StripCR([]byte(tc.input), len(tc.input) >= 2 && tc.input[1] == '*')) require.Equal(t, tc.expect, actual) } @@ -219,15 +219,15 @@ func TestStripCR(t *testing.T) { func scanExpect( t *testing.T, input string, - mode internal.ScanMode, + mode parser.ScanMode, expected ...scanResult, ) { testFile := testFileSet.AddFile("test", -1, len(input)) - s := internal.NewScanner( + s := parser.NewScanner( testFile, []byte(input), - func(_ internal.SourceFilePos, msg string) { require.Fail(t, msg) }, + func(_ parser.SourceFilePos, msg string) { require.Fail(t, msg) }, mode) for idx, e := range expected { diff --git a/internal/source_file.go b/parser/source_file.go similarity index 99% rename from internal/source_file.go rename to parser/source_file.go index 6068b13..e9f4b0f 100644 --- a/internal/source_file.go +++ b/parser/source_file.go @@ -1,4 +1,4 @@ -package internal +package parser import ( "fmt" diff --git a/internal/stmt.go b/parser/stmt.go similarity index 99% rename from internal/stmt.go rename to parser/stmt.go index 70458a2..1b4de48 100644 --- a/internal/stmt.go +++ b/parser/stmt.go @@ -1,9 +1,9 @@ -package internal +package parser import ( "strings" - "github.com/d5/tengo/internal/token" + "github.com/d5/tengo/token" ) // Stmt represents a statement in the AST. diff --git a/internal/require/require.go b/require/require.go similarity index 90% rename from internal/require/require.go rename to require/require.go index f42c1af..918a268 100644 --- a/internal/require/require.go +++ b/require/require.go @@ -11,8 +11,8 @@ import ( "unicode/utf8" "github.com/d5/tengo" - "github.com/d5/tengo/internal" - "github.com/d5/tengo/internal/token" + "github.com/d5/tengo/parser" + "github.com/d5/tengo/token" ) // NoError asserts err is not an error. @@ -120,12 +120,12 @@ func Equal( if expected != actual.(rune) { failExpectedActual(t, expected, actual, msg...) } - case *internal.Symbol: - if !equalSymbol(expected, actual.(*internal.Symbol)) { + case *tengo.Symbol: + if !equalSymbol(expected, actual.(*tengo.Symbol)) { failExpectedActual(t, expected, actual, msg...) } - case internal.Pos: - if expected != actual.(internal.Pos) { + case parser.Pos: + if expected != actual.(parser.Pos) { failExpectedActual(t, expected, actual, msg...) } case token.Token: @@ -176,14 +176,14 @@ func Equal( if !expected.Equals(actual.(tengo.Object)) { failExpectedActual(t, expected, actual, msg...) } - case *internal.SourceFileSet: - equalFileSet(t, expected, actual.(*internal.SourceFileSet), msg...) - case *internal.SourceFile: - Equal(t, expected.Name, actual.(*internal.SourceFile).Name, msg...) - Equal(t, expected.Base, actual.(*internal.SourceFile).Base, msg...) - Equal(t, expected.Size, actual.(*internal.SourceFile).Size, msg...) + case *parser.SourceFileSet: + equalFileSet(t, expected, actual.(*parser.SourceFileSet), msg...) + case *parser.SourceFile: + Equal(t, expected.Name, actual.(*parser.SourceFile).Name, msg...) + Equal(t, expected.Base, actual.(*parser.SourceFile).Base, msg...) + Equal(t, expected.Size, actual.(*parser.SourceFile).Size, msg...) True(t, equalIntSlice(expected.Lines, - actual.(*internal.SourceFile).Lines), msg...) + actual.(*parser.SourceFile).Lines), msg...) case error: if expected != actual.(error) { failExpectedActual(t, expected, actual, msg...) @@ -253,7 +253,7 @@ func equalStringSlice(a, b []string) bool { return true } -func equalSymbol(a, b *internal.Symbol) bool { +func equalSymbol(a, b *tengo.Symbol) bool { return a.Name == b.Name && a.Index == b.Index && a.Scope == b.Scope @@ -272,7 +272,7 @@ func equalObjectSlice( func equalFileSet( t *testing.T, - expected, actual *internal.SourceFileSet, + expected, actual *parser.SourceFileSet, msg ...interface{}, ) { Equal(t, len(expected.Files), len(actual.Files), msg...) @@ -303,8 +303,8 @@ func equalCompiledFunction( expectedT := expected.(*tengo.CompiledFunction) actualT := actual.(*tengo.CompiledFunction) Equal(t, - internal.FormatInstructions(expectedT.Instructions, 0), - internal.FormatInstructions(actualT.Instructions, 0), msg...) + tengo.FormatInstructions(expectedT.Instructions, 0), + tengo.FormatInstructions(actualT.Instructions, 0), msg...) } func isNil(v interface{}) bool { diff --git a/script.go b/script.go index 4d1df70..adee2ae 100644 --- a/script.go +++ b/script.go @@ -5,7 +5,7 @@ import ( "fmt" "sync" - "github.com/d5/tengo/internal" + "github.com/d5/tengo/parser" ) // Script can simplify compilation and execution of embedded scripts. @@ -83,9 +83,9 @@ func (s *Script) Compile() (*Compiled, error) { return nil, err } - fileSet := internal.NewFileSet() + fileSet := parser.NewFileSet() srcFile := fileSet.AddFile("(main)", -1, len(s.input)) - p := internal.NewParser(srcFile, s.input, nil) + p := parser.NewParser(srcFile, s.input, nil) file, err := p.ParseFile() if err != nil { return nil, err @@ -104,7 +104,7 @@ func (s *Script) Compile() (*Compiled, error) { globalIndexes := make(map[string]int, len(globals)) for _, name := range symbolTable.Names() { symbol, _, _ := symbolTable.Resolve(name) - if symbol.Scope == internal.ScopeGlobal { + if symbol.Scope == ScopeGlobal { globalIndexes[name] = symbol.Index } } @@ -152,7 +152,7 @@ func (s *Script) RunContext( } func (s *Script) prepCompile() ( - symbolTable *internal.SymbolTable, + symbolTable *SymbolTable, globals []Object, err error, ) { @@ -161,7 +161,7 @@ func (s *Script) prepCompile() ( names = append(names, name) } - symbolTable = internal.NewSymbolTable() + symbolTable = NewSymbolTable() for idx, fn := range builtinFuncs { symbolTable.DefineBuiltin(idx, fn.Name) } diff --git a/script_test.go b/script_test.go index b1b5ba6..622cab7 100644 --- a/script_test.go +++ b/script_test.go @@ -11,9 +11,9 @@ import ( "time" "github.com/d5/tengo" - "github.com/d5/tengo/internal/require" - "github.com/d5/tengo/internal/token" + "github.com/d5/tengo/require" "github.com/d5/tengo/stdlib" + "github.com/d5/tengo/token" ) func TestScript_Add(t *testing.T) { diff --git a/stdlib/func_typedefs_test.go b/stdlib/func_typedefs_test.go index e0ba2da..8368fca 100644 --- a/stdlib/func_typedefs_test.go +++ b/stdlib/func_typedefs_test.go @@ -7,7 +7,7 @@ import ( "testing" "github.com/d5/tengo" - "github.com/d5/tengo/internal/require" + "github.com/d5/tengo/require" "github.com/d5/tengo/stdlib" ) diff --git a/stdlib/json/json_test.go b/stdlib/json/json_test.go index 1a035a0..e8e8fde 100644 --- a/stdlib/json/json_test.go +++ b/stdlib/json/json_test.go @@ -5,7 +5,7 @@ import ( "testing" "github.com/d5/tengo" - "github.com/d5/tengo/internal/require" + "github.com/d5/tengo/require" "github.com/d5/tengo/stdlib/json" ) diff --git a/stdlib/os_test.go b/stdlib/os_test.go index ea52d8b..d2cd804 100644 --- a/stdlib/os_test.go +++ b/stdlib/os_test.go @@ -6,7 +6,7 @@ import ( "testing" "github.com/d5/tengo" - "github.com/d5/tengo/internal/require" + "github.com/d5/tengo/require" ) func TestReadFile(t *testing.T) { diff --git a/stdlib/rand_test.go b/stdlib/rand_test.go index ff4b883..5428f54 100644 --- a/stdlib/rand_test.go +++ b/stdlib/rand_test.go @@ -5,7 +5,7 @@ import ( "testing" "github.com/d5/tengo" - "github.com/d5/tengo/internal/require" + "github.com/d5/tengo/require" ) func TestRand(t *testing.T) { diff --git a/stdlib/stdlib_test.go b/stdlib/stdlib_test.go index 03f6658..ec8756a 100644 --- a/stdlib/stdlib_test.go +++ b/stdlib/stdlib_test.go @@ -6,7 +6,7 @@ import ( "time" "github.com/d5/tengo" - "github.com/d5/tengo/internal/require" + "github.com/d5/tengo/require" "github.com/d5/tengo/stdlib" ) diff --git a/stdlib/times_test.go b/stdlib/times_test.go index 3565c85..efb85b6 100644 --- a/stdlib/times_test.go +++ b/stdlib/times_test.go @@ -5,7 +5,7 @@ import ( "time" "github.com/d5/tengo" - "github.com/d5/tengo/internal/require" + "github.com/d5/tengo/require" ) func TestTimes(t *testing.T) { diff --git a/internal/compile.go b/symbol_table.go similarity index 84% rename from internal/compile.go rename to symbol_table.go index cf61e2d..6ae5d7d 100644 --- a/internal/compile.go +++ b/symbol_table.go @@ -1,33 +1,4 @@ -package internal - -import "fmt" - -// 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]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 *SourceFileSet - Node 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) -} +package tengo // SymbolScope represents a symbol scope. type SymbolScope string diff --git a/internal/symbol_table_test.go b/symbol_table_test.go similarity index 81% rename from internal/symbol_table_test.go rename to symbol_table_test.go index c46e718..4376ffb 100644 --- a/internal/symbol_table_test.go +++ b/symbol_table_test.go @@ -1,10 +1,10 @@ -package internal_test +package tengo_test import ( "testing" - "github.com/d5/tengo/internal" - "github.com/d5/tengo/internal/require" + "github.com/d5/tengo" + "github.com/d5/tengo/require" ) func TestSymbolTable(t *testing.T) { @@ -93,37 +93,37 @@ func TestSymbolTable(t *testing.T) { func symbol( name string, - scope internal.SymbolScope, + scope tengo.SymbolScope, index int, -) *internal.Symbol { - return &internal.Symbol{ +) *tengo.Symbol { + return &tengo.Symbol{ Name: name, Scope: scope, Index: index, } } -func globalSymbol(name string, index int) *internal.Symbol { - return symbol(name, internal.ScopeGlobal, index) +func globalSymbol(name string, index int) *tengo.Symbol { + return symbol(name, tengo.ScopeGlobal, index) } -func localSymbol(name string, index int) *internal.Symbol { - return symbol(name, internal.ScopeLocal, index) +func localSymbol(name string, index int) *tengo.Symbol { + return symbol(name, tengo.ScopeLocal, index) } -func freeSymbol(name string, index int) *internal.Symbol { - return symbol(name, internal.ScopeFree, index) +func freeSymbol(name string, index int) *tengo.Symbol { + return symbol(name, tengo.ScopeFree, index) } -func symbolTable() *internal.SymbolTable { - return internal.NewSymbolTable() +func symbolTable() *tengo.SymbolTable { + return tengo.NewSymbolTable() } func resolveExpect( t *testing.T, - symbolTable *internal.SymbolTable, + symbolTable *tengo.SymbolTable, name string, - expectedSymbol *internal.Symbol, + expectedSymbol *tengo.Symbol, expectedDepth int, ) { actualSymbol, actualDepth, ok := symbolTable.Resolve(name) diff --git a/tengo_test.go b/tengo_test.go index 825d986..cae2237 100644 --- a/tengo_test.go +++ b/tengo_test.go @@ -6,16 +6,16 @@ import ( "time" "github.com/d5/tengo" - "github.com/d5/tengo/internal" - "github.com/d5/tengo/internal/require" + "github.com/d5/tengo/parser" + "github.com/d5/tengo/require" ) func TestInstructions_String(t *testing.T) { assertInstructionString(t, [][]byte{ - internal.MakeInstruction(internal.OpConstant, 1), - internal.MakeInstruction(internal.OpConstant, 2), - internal.MakeInstruction(internal.OpConstant, 65535), + tengo.MakeInstruction(parser.OpConstant, 1), + tengo.MakeInstruction(parser.OpConstant, 2), + tengo.MakeInstruction(parser.OpConstant, 65535), }, `0000 CONST 1 0003 CONST 2 @@ -23,9 +23,9 @@ func TestInstructions_String(t *testing.T) { assertInstructionString(t, [][]byte{ - internal.MakeInstruction(internal.OpBinaryOp, 11), - internal.MakeInstruction(internal.OpConstant, 2), - internal.MakeInstruction(internal.OpConstant, 65535), + tengo.MakeInstruction(parser.OpBinaryOp, 11), + tengo.MakeInstruction(parser.OpConstant, 2), + tengo.MakeInstruction(parser.OpConstant, 65535), }, `0000 BINARYOP 11 0002 CONST 2 @@ -33,10 +33,10 @@ func TestInstructions_String(t *testing.T) { assertInstructionString(t, [][]byte{ - internal.MakeInstruction(internal.OpBinaryOp, 11), - internal.MakeInstruction(internal.OpGetLocal, 1), - internal.MakeInstruction(internal.OpConstant, 2), - internal.MakeInstruction(internal.OpConstant, 65535), + tengo.MakeInstruction(parser.OpBinaryOp, 11), + tengo.MakeInstruction(parser.OpGetLocal, 1), + tengo.MakeInstruction(parser.OpConstant, 2), + tengo.MakeInstruction(parser.OpConstant, 65535), }, `0000 BINARYOP 11 0002 GETL 1 @@ -45,15 +45,15 @@ func TestInstructions_String(t *testing.T) { } func TestMakeInstruction(t *testing.T) { - makeInstruction(t, []byte{internal.OpConstant, 0, 0}, - internal.OpConstant, 0) - makeInstruction(t, []byte{internal.OpConstant, 0, 1}, - internal.OpConstant, 1) - makeInstruction(t, []byte{internal.OpConstant, 255, 254}, - internal.OpConstant, 65534) - makeInstruction(t, []byte{internal.OpPop}, internal.OpPop) - makeInstruction(t, []byte{internal.OpTrue}, internal.OpTrue) - makeInstruction(t, []byte{internal.OpFalse}, internal.OpFalse) + makeInstruction(t, []byte{parser.OpConstant, 0, 0}, + parser.OpConstant, 0) + makeInstruction(t, []byte{parser.OpConstant, 0, 1}, + parser.OpConstant, 1) + makeInstruction(t, []byte{parser.OpConstant, 255, 254}, + parser.OpConstant, 65534) + makeInstruction(t, []byte{parser.OpPop}, parser.OpPop) + makeInstruction(t, []byte{parser.OpTrue}, parser.OpTrue) + makeInstruction(t, []byte{parser.OpFalse}, parser.OpFalse) } func TestNumObjects(t *testing.T) { @@ -123,15 +123,15 @@ func assertInstructionString( concatted = append(concatted, e...) } require.Equal(t, expected, strings.Join( - internal.FormatInstructions(concatted, 0), "\n")) + tengo.FormatInstructions(concatted, 0), "\n")) } func makeInstruction( t *testing.T, expected []byte, - opcode internal.Opcode, + opcode parser.Opcode, operands ...int, ) { - inst := internal.MakeInstruction(opcode, operands...) + inst := tengo.MakeInstruction(opcode, operands...) require.Equal(t, expected, inst) } diff --git a/internal/token/tokens.go b/token/token.go similarity index 100% rename from internal/token/tokens.go rename to token/token.go diff --git a/variable_test.go b/variable_test.go index ff8f03d..0268035 100644 --- a/variable_test.go +++ b/variable_test.go @@ -4,7 +4,7 @@ import ( "testing" "github.com/d5/tengo" - "github.com/d5/tengo/internal/require" + "github.com/d5/tengo/require" ) type VariableTest struct { diff --git a/vm.go b/vm.go index 3332017..1f14771 100644 --- a/vm.go +++ b/vm.go @@ -4,8 +4,8 @@ import ( "fmt" "sync/atomic" - "github.com/d5/tengo/internal" - "github.com/d5/tengo/internal/token" + "github.com/d5/tengo/parser" + "github.com/d5/tengo/token" ) // frame represents a function call frame. @@ -22,7 +22,7 @@ type VM struct { stack [StackSize]Object sp int globals []Object - fileSet *internal.SourceFileSet + fileSet *parser.SourceFileSet frames [MaxFrames]frame framesIndex int curFrame *frame @@ -99,16 +99,16 @@ func (v *VM) run() { v.ip++ switch v.curInsts[v.ip] { - case internal.OpConstant: + case parser.OpConstant: v.ip += 2 cidx := int(v.curInsts[v.ip]) | int(v.curInsts[v.ip-1])<<8 v.stack[v.sp] = v.constants[cidx] v.sp++ - case internal.OpNull: + case parser.OpNull: v.stack[v.sp] = UndefinedValue v.sp++ - case internal.OpBinaryOp: + case parser.OpBinaryOp: v.ip++ right := v.stack[v.sp-1] left := v.stack[v.sp-2] @@ -133,7 +133,7 @@ func (v *VM) run() { v.stack[v.sp-2] = res v.sp-- - case internal.OpEqual: + case parser.OpEqual: right := v.stack[v.sp-1] left := v.stack[v.sp-2] v.sp -= 2 @@ -143,7 +143,7 @@ func (v *VM) run() { v.stack[v.sp] = FalseValue } v.sp++ - case internal.OpNotEqual: + case parser.OpNotEqual: right := v.stack[v.sp-1] left := v.stack[v.sp-2] v.sp -= 2 @@ -153,15 +153,15 @@ func (v *VM) run() { v.stack[v.sp] = TrueValue } v.sp++ - case internal.OpPop: + case parser.OpPop: v.sp-- - case internal.OpTrue: + case parser.OpTrue: v.stack[v.sp] = TrueValue v.sp++ - case internal.OpFalse: + case parser.OpFalse: v.stack[v.sp] = FalseValue v.sp++ - case internal.OpLNot: + case parser.OpLNot: operand := v.stack[v.sp-1] v.sp-- if operand.IsFalsy() { @@ -170,7 +170,7 @@ func (v *VM) run() { v.stack[v.sp] = FalseValue } v.sp++ - case internal.OpBComplement: + case parser.OpBComplement: operand := v.stack[v.sp-1] v.sp-- @@ -189,7 +189,7 @@ func (v *VM) run() { operand.TypeName()) return } - case internal.OpMinus: + case parser.OpMinus: operand := v.stack[v.sp-1] v.sp-- @@ -217,14 +217,14 @@ func (v *VM) run() { operand.TypeName()) return } - case internal.OpJumpFalsy: + case parser.OpJumpFalsy: v.ip += 2 v.sp-- if v.stack[v.sp].IsFalsy() { pos := int(v.curInsts[v.ip]) | int(v.curInsts[v.ip-1])<<8 v.ip = pos - 1 } - case internal.OpAndJump: + case parser.OpAndJump: v.ip += 2 if v.stack[v.sp-1].IsFalsy() { pos := int(v.curInsts[v.ip]) | int(v.curInsts[v.ip-1])<<8 @@ -232,7 +232,7 @@ func (v *VM) run() { } else { v.sp-- } - case internal.OpOrJump: + case parser.OpOrJump: v.ip += 2 if v.stack[v.sp-1].IsFalsy() { v.sp-- @@ -240,15 +240,15 @@ func (v *VM) run() { pos := int(v.curInsts[v.ip]) | int(v.curInsts[v.ip-1])<<8 v.ip = pos - 1 } - case internal.OpJump: + case parser.OpJump: pos := int(v.curInsts[v.ip+2]) | int(v.curInsts[v.ip+1])<<8 v.ip = pos - 1 - case internal.OpSetGlobal: + case parser.OpSetGlobal: v.ip += 2 v.sp-- globalIndex := int(v.curInsts[v.ip]) | int(v.curInsts[v.ip-1])<<8 v.globals[globalIndex] = v.stack[v.sp] - case internal.OpSetSelGlobal: + case parser.OpSetSelGlobal: v.ip += 3 globalIndex := int(v.curInsts[v.ip-1]) | int(v.curInsts[v.ip-2])<<8 numSelectors := int(v.curInsts[v.ip]) @@ -265,13 +265,13 @@ func (v *VM) run() { v.err = e return } - case internal.OpGetGlobal: + case parser.OpGetGlobal: v.ip += 2 globalIndex := int(v.curInsts[v.ip]) | int(v.curInsts[v.ip-1])<<8 val := v.globals[globalIndex] v.stack[v.sp] = val v.sp++ - case internal.OpArray: + case parser.OpArray: v.ip += 2 numElements := int(v.curInsts[v.ip]) | int(v.curInsts[v.ip-1])<<8 @@ -290,7 +290,7 @@ func (v *VM) run() { v.stack[v.sp] = arr v.sp++ - case internal.OpMap: + case parser.OpMap: v.ip += 2 numElements := int(v.curInsts[v.ip]) | int(v.curInsts[v.ip-1])<<8 kv := make(map[string]Object) @@ -309,7 +309,7 @@ func (v *VM) run() { } v.stack[v.sp] = m v.sp++ - case internal.OpError: + case parser.OpError: value := v.stack[v.sp-1] var e Object = &Error{ Value: value, @@ -320,7 +320,7 @@ func (v *VM) run() { return } v.stack[v.sp-1] = e - case internal.OpImmutable: + case parser.OpImmutable: value := v.stack[v.sp-1] switch value := value.(type) { case *Array: @@ -344,7 +344,7 @@ func (v *VM) run() { } v.stack[v.sp-1] = immutableMap } - case internal.OpIndex: + case parser.OpIndex: index := v.stack[v.sp-1] left := v.stack[v.sp-2] v.sp -= 2 @@ -368,7 +368,7 @@ func (v *VM) run() { } v.stack[v.sp] = val v.sp++ - case internal.OpSliceIndex: + case parser.OpSliceIndex: high := v.stack[v.sp-1] low := v.stack[v.sp-2] left := v.stack[v.sp-3] @@ -535,7 +535,7 @@ func (v *VM) run() { v.stack[v.sp] = val v.sp++ } - case internal.OpCall: + case parser.OpCall: numArgs := int(v.curInsts[v.ip+1]) v.ip++ value := v.stack[v.sp-1-numArgs] @@ -576,9 +576,9 @@ func (v *VM) run() { // test if it's tail-call if callee == v.curFrame.fn { // recursion nextOp := v.curInsts[v.ip+1] - if nextOp == internal.OpReturn || - (nextOp == internal.OpPop && - internal.OpReturn == v.curInsts[v.ip+2]) { + if nextOp == parser.OpReturn || + (nextOp == parser.OpPop && + parser.OpReturn == v.curInsts[v.ip+2]) { for p := 0; p < numArgs; p++ { v.stack[v.curFrame.basePointer+p] = v.stack[v.sp-numArgs+p] @@ -640,7 +640,7 @@ func (v *VM) run() { v.stack[v.sp] = ret v.sp++ } - case internal.OpReturn: + case parser.OpReturn: v.ip++ var retVal Object if int(v.curInsts[v.ip]) == 1 { @@ -658,7 +658,7 @@ func (v *VM) run() { // skip stack overflow check because (newSP) <= (oldSP) v.stack[v.sp-1] = retVal //v.sp++ - case internal.OpDefineLocal: + case parser.OpDefineLocal: v.ip++ localIndex := int(v.curInsts[v.ip]) sp := v.curFrame.basePointer + localIndex @@ -668,7 +668,7 @@ func (v *VM) run() { val := v.stack[v.sp-1] v.sp-- v.stack[sp] = val - case internal.OpSetLocal: + case parser.OpSetLocal: localIndex := int(v.curInsts[v.ip+1]) v.ip++ sp := v.curFrame.basePointer + localIndex @@ -683,7 +683,7 @@ func (v *VM) run() { val = obj } v.stack[sp] = val // also use a copy of popped value - case internal.OpSetSelLocal: + case parser.OpSetSelLocal: localIndex := int(v.curInsts[v.ip+1]) numSelectors := int(v.curInsts[v.ip+2]) v.ip += 2 @@ -703,7 +703,7 @@ func (v *VM) run() { v.err = e return } - case internal.OpGetLocal: + case parser.OpGetLocal: v.ip++ localIndex := int(v.curInsts[v.ip]) val := v.stack[v.curFrame.basePointer+localIndex] @@ -712,12 +712,12 @@ func (v *VM) run() { } v.stack[v.sp] = val v.sp++ - case internal.OpGetBuiltin: + case parser.OpGetBuiltin: v.ip++ builtinIndex := int(v.curInsts[v.ip]) v.stack[v.sp] = builtinFuncs[builtinIndex] v.sp++ - case internal.OpClosure: + case parser.OpClosure: v.ip += 3 constIndex := int(v.curInsts[v.ip-1]) | int(v.curInsts[v.ip-2])<<8 numFree := int(v.curInsts[v.ip]) @@ -752,24 +752,24 @@ func (v *VM) run() { } v.stack[v.sp] = cl v.sp++ - case internal.OpGetFreePtr: + case parser.OpGetFreePtr: v.ip++ freeIndex := int(v.curInsts[v.ip]) val := v.curFrame.freeVars[freeIndex] v.stack[v.sp] = val v.sp++ - case internal.OpGetFree: + case parser.OpGetFree: v.ip++ freeIndex := int(v.curInsts[v.ip]) val := *v.curFrame.freeVars[freeIndex].Value v.stack[v.sp] = val v.sp++ - case internal.OpSetFree: + case parser.OpSetFree: v.ip++ freeIndex := int(v.curInsts[v.ip]) *v.curFrame.freeVars[freeIndex].Value = v.stack[v.sp-1] v.sp-- - case internal.OpGetLocalPtr: + case parser.OpGetLocalPtr: v.ip++ localIndex := int(v.curInsts[v.ip]) sp := v.curFrame.basePointer + localIndex @@ -783,7 +783,7 @@ func (v *VM) run() { } v.stack[v.sp] = freeVar v.sp++ - case internal.OpSetSelFree: + case parser.OpSetSelFree: v.ip += 2 freeIndex := int(v.curInsts[v.ip-1]) numSelectors := int(v.curInsts[v.ip]) @@ -801,7 +801,7 @@ func (v *VM) run() { v.err = e return } - case internal.OpIteratorInit: + case parser.OpIteratorInit: var iterator Object dst := v.stack[v.sp-1] v.sp-- @@ -817,7 +817,7 @@ func (v *VM) run() { } v.stack[v.sp] = iterator v.sp++ - case internal.OpIteratorNext: + case parser.OpIteratorNext: iterator := v.stack[v.sp-1] v.sp-- hasMore := iterator.(Iterator).Next() @@ -827,19 +827,19 @@ func (v *VM) run() { v.stack[v.sp] = FalseValue } v.sp++ - case internal.OpIteratorKey: + case parser.OpIteratorKey: iterator := v.stack[v.sp-1] v.sp-- val := iterator.(Iterator).Key() v.stack[v.sp] = val v.sp++ - case internal.OpIteratorValue: + case parser.OpIteratorValue: iterator := v.stack[v.sp-1] v.sp-- val := iterator.(Iterator).Value() v.stack[v.sp] = val v.sp++ - case internal.OpSuspend: + case parser.OpSuspend: return default: v.err = fmt.Errorf("unknown opcode: %d", v.curInsts[v.ip]) diff --git a/vm_test.go b/vm_test.go index 2c99468..3dc3dab 100644 --- a/vm_test.go +++ b/vm_test.go @@ -11,10 +11,10 @@ import ( "testing" "github.com/d5/tengo" - "github.com/d5/tengo/internal" - "github.com/d5/tengo/internal/require" - "github.com/d5/tengo/internal/token" + "github.com/d5/tengo/parser" + "github.com/d5/tengo/require" "github.com/d5/tengo/stdlib" + "github.com/d5/tengo/token" ) const testOut = "out" @@ -3276,7 +3276,7 @@ func (o *vmTracer) Write(p []byte) (n int, err error) { } func traceCompileRun( - file *internal.File, + file *parser.File, symbols map[string]tengo.Object, modules *tengo.ModuleMap, maxAllocs int64, @@ -3306,7 +3306,7 @@ func traceCompileRun( globals := make([]tengo.Object, tengo.GlobalsSize) - symTable := internal.NewSymbolTable() + symTable := tengo.NewSymbolTable() for name, value := range symbols { sym := symTable.Define(name) @@ -3371,11 +3371,11 @@ func formatGlobals(globals []tengo.Object) (formatted []string) { return } -func parse(t *testing.T, input string) *internal.File { - testFileSet := internal.NewFileSet() +func parse(t *testing.T, input string) *parser.File { + testFileSet := parser.NewFileSet() testFile := testFileSet.AddFile("test", -1, len(input)) - p := internal.NewParser(testFile, []byte(input), nil) + p := parser.NewParser(testFile, []byte(input), nil) file, err := p.ParseFile() require.NoError(t, err) return file