diff --git a/cmd/bench/main.go b/cmd/bench/main.go index 17a8093..730ce6b 100644 --- a/cmd/bench/main.go +++ b/cmd/bench/main.go @@ -193,7 +193,7 @@ func compileFile(file *ast.File) (time.Duration, *compiler.Bytecode, error) { start := time.Now() - c := compiler.NewCompiler(symTable, nil, nil) + c := compiler.NewCompiler(symTable, nil, nil, nil) if err := c.Compile(file); err != nil { return time.Since(start), nil, err } diff --git a/cmd/tengo/main.go b/cmd/tengo/main.go index f9d19b5..81e3faf 100644 --- a/cmd/tengo/main.go +++ b/cmd/tengo/main.go @@ -186,6 +186,8 @@ func runREPL(in io.Reader, out io.Writer) { symbolTable.DefineBuiltin(idx, fn.Name) } + var constants []objects.Object + for { _, _ = fmt.Fprintf(out, replPrompt) @@ -204,13 +206,15 @@ func runREPL(in io.Reader, out io.Writer) { file = addPrints(file) - c := compiler.NewCompiler(symbolTable, nil, nil) + c := compiler.NewCompiler(symbolTable, constants, nil, nil) if err := c.Compile(file); err != nil { _, _ = fmt.Fprintf(out, "Compilation error:\n %s\n", err.Error()) continue } - machine := runtime.NewVM(c.Bytecode(), globals) + bytecode := c.Bytecode() + + machine := runtime.NewVM(bytecode, globals) if err != nil { _, _ = fmt.Fprintf(out, "VM error:\n %s\n", err.Error()) continue @@ -219,6 +223,8 @@ func runREPL(in io.Reader, out io.Writer) { _, _ = fmt.Fprintf(out, "Execution error:\n %s\n", err.Error()) continue } + + constants = bytecode.Constants } } @@ -231,7 +237,7 @@ func compileSrc(src []byte, filename string) (*compiler.Bytecode, error) { return nil, err } - c := compiler.NewCompiler(nil, nil, nil) + c := compiler.NewCompiler(nil, nil, nil, nil) if err := c.Compile(file); err != nil { return nil, err } diff --git a/compiler/compiler.go b/compiler/compiler.go index 17a6d3a..13967e7 100644 --- a/compiler/compiler.go +++ b/compiler/compiler.go @@ -34,7 +34,7 @@ type Compiler struct { // a new symbol table and use the default builtin functions. Likewise, standard // modules can be explicitly provided if user wants to add or remove some modules. // By default, Compile will use all the standard modules otherwise. -func NewCompiler(symbolTable *SymbolTable, stdModules map[string]*objects.ImmutableMap, trace io.Writer) *Compiler { +func NewCompiler(symbolTable *SymbolTable, constants []objects.Object, stdModules map[string]*objects.ImmutableMap, trace io.Writer) *Compiler { mainScope := CompilationScope{ instructions: make([]byte, 0), } @@ -55,6 +55,7 @@ func NewCompiler(symbolTable *SymbolTable, stdModules map[string]*objects.Immuta return &Compiler{ symbolTable: symbolTable, + constants: constants, scopes: []CompilationScope{mainScope}, scopeIndex: 0, loopIndex: -1, @@ -591,7 +592,7 @@ func (c *Compiler) SetModuleLoader(moduleLoader ModuleLoader) { } func (c *Compiler) fork(moduleName string, symbolTable *SymbolTable) *Compiler { - child := NewCompiler(symbolTable, c.stdModules, c.trace) + child := NewCompiler(symbolTable, nil, c.stdModules, c.trace) child.moduleName = moduleName // name of the module to compile child.parent = c // parent to set to current compiler child.moduleLoader = c.moduleLoader // share module loader diff --git a/compiler/compiler_test.go b/compiler/compiler_test.go index 6e16db9..a139143 100644 --- a/compiler/compiler_test.go +++ b/compiler/compiler_test.go @@ -976,7 +976,7 @@ func traceCompile(input string, symbols map[string]objects.Object) (res *compile } tr := &tracer{} - c := compiler.NewCompiler(symTable, nil, tr) + c := compiler.NewCompiler(symTable, nil, nil, tr) parsed, err := p.ParseFile() if err != nil { return diff --git a/runtime/vm_test.go b/runtime/vm_test.go index 103e785..b963330 100644 --- a/runtime/vm_test.go +++ b/runtime/vm_test.go @@ -230,7 +230,7 @@ func traceCompileRun(file *ast.File, symbols map[string]objects.Object, userModu } tr := &tracer{} - c := compiler.NewCompiler(symTable, nil, tr) + c := compiler.NewCompiler(symTable, nil, nil, tr) c.SetModuleLoader(func(moduleName string) ([]byte, error) { if src, ok := userModules[moduleName]; ok { return []byte(src), nil diff --git a/script/script.go b/script/script.go index 4b9ade9..6a56f45 100644 --- a/script/script.go +++ b/script/script.go @@ -94,7 +94,7 @@ func (s *Script) Compile() (*Compiled, error) { return nil, fmt.Errorf("parse error: %s", err.Error()) } - c := compiler.NewCompiler(symbolTable, stdModules, nil) + c := compiler.NewCompiler(symbolTable, nil, stdModules, nil) if s.userModuleLoader != nil { c.SetModuleLoader(s.userModuleLoader)