Fixes a REPL bug (Issue #91) (#92)

This commit is contained in:
Daniel 2019-02-10 00:32:37 -08:00 committed by GitHub
parent 5e21abfd74
commit 9c67444678
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 16 additions and 9 deletions

View file

@ -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
}

View file

@ -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
}

View file

@ -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

View file

@ -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

View file

@ -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

View file

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