parent
5e21abfd74
commit
9c67444678
6 changed files with 16 additions and 9 deletions
|
@ -193,7 +193,7 @@ func compileFile(file *ast.File) (time.Duration, *compiler.Bytecode, error) {
|
||||||
|
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
|
|
||||||
c := compiler.NewCompiler(symTable, nil, nil)
|
c := compiler.NewCompiler(symTable, nil, nil, nil)
|
||||||
if err := c.Compile(file); err != nil {
|
if err := c.Compile(file); err != nil {
|
||||||
return time.Since(start), nil, err
|
return time.Since(start), nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -186,6 +186,8 @@ func runREPL(in io.Reader, out io.Writer) {
|
||||||
symbolTable.DefineBuiltin(idx, fn.Name)
|
symbolTable.DefineBuiltin(idx, fn.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var constants []objects.Object
|
||||||
|
|
||||||
for {
|
for {
|
||||||
_, _ = fmt.Fprintf(out, replPrompt)
|
_, _ = fmt.Fprintf(out, replPrompt)
|
||||||
|
|
||||||
|
@ -204,13 +206,15 @@ func runREPL(in io.Reader, out io.Writer) {
|
||||||
|
|
||||||
file = addPrints(file)
|
file = addPrints(file)
|
||||||
|
|
||||||
c := compiler.NewCompiler(symbolTable, nil, nil)
|
c := compiler.NewCompiler(symbolTable, constants, nil, nil)
|
||||||
if err := c.Compile(file); err != nil {
|
if err := c.Compile(file); err != nil {
|
||||||
_, _ = fmt.Fprintf(out, "Compilation error:\n %s\n", err.Error())
|
_, _ = fmt.Fprintf(out, "Compilation error:\n %s\n", err.Error())
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
machine := runtime.NewVM(c.Bytecode(), globals)
|
bytecode := c.Bytecode()
|
||||||
|
|
||||||
|
machine := runtime.NewVM(bytecode, globals)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
_, _ = fmt.Fprintf(out, "VM error:\n %s\n", err.Error())
|
_, _ = fmt.Fprintf(out, "VM error:\n %s\n", err.Error())
|
||||||
continue
|
continue
|
||||||
|
@ -219,6 +223,8 @@ func runREPL(in io.Reader, out io.Writer) {
|
||||||
_, _ = fmt.Fprintf(out, "Execution error:\n %s\n", err.Error())
|
_, _ = fmt.Fprintf(out, "Execution error:\n %s\n", err.Error())
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
constants = bytecode.Constants
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -231,7 +237,7 @@ func compileSrc(src []byte, filename string) (*compiler.Bytecode, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
c := compiler.NewCompiler(nil, nil, nil)
|
c := compiler.NewCompiler(nil, nil, nil, nil)
|
||||||
if err := c.Compile(file); err != nil {
|
if err := c.Compile(file); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,7 +34,7 @@ type Compiler struct {
|
||||||
// a new symbol table and use the default builtin functions. Likewise, standard
|
// 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.
|
// modules can be explicitly provided if user wants to add or remove some modules.
|
||||||
// By default, Compile will use all the standard modules otherwise.
|
// 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{
|
mainScope := CompilationScope{
|
||||||
instructions: make([]byte, 0),
|
instructions: make([]byte, 0),
|
||||||
}
|
}
|
||||||
|
@ -55,6 +55,7 @@ func NewCompiler(symbolTable *SymbolTable, stdModules map[string]*objects.Immuta
|
||||||
|
|
||||||
return &Compiler{
|
return &Compiler{
|
||||||
symbolTable: symbolTable,
|
symbolTable: symbolTable,
|
||||||
|
constants: constants,
|
||||||
scopes: []CompilationScope{mainScope},
|
scopes: []CompilationScope{mainScope},
|
||||||
scopeIndex: 0,
|
scopeIndex: 0,
|
||||||
loopIndex: -1,
|
loopIndex: -1,
|
||||||
|
@ -591,7 +592,7 @@ func (c *Compiler) SetModuleLoader(moduleLoader ModuleLoader) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Compiler) fork(moduleName string, symbolTable *SymbolTable) *Compiler {
|
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.moduleName = moduleName // name of the module to compile
|
||||||
child.parent = c // parent to set to current compiler
|
child.parent = c // parent to set to current compiler
|
||||||
child.moduleLoader = c.moduleLoader // share module loader
|
child.moduleLoader = c.moduleLoader // share module loader
|
||||||
|
|
|
@ -976,7 +976,7 @@ func traceCompile(input string, symbols map[string]objects.Object) (res *compile
|
||||||
}
|
}
|
||||||
|
|
||||||
tr := &tracer{}
|
tr := &tracer{}
|
||||||
c := compiler.NewCompiler(symTable, nil, tr)
|
c := compiler.NewCompiler(symTable, nil, nil, tr)
|
||||||
parsed, err := p.ParseFile()
|
parsed, err := p.ParseFile()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
|
|
|
@ -230,7 +230,7 @@ func traceCompileRun(file *ast.File, symbols map[string]objects.Object, userModu
|
||||||
}
|
}
|
||||||
|
|
||||||
tr := &tracer{}
|
tr := &tracer{}
|
||||||
c := compiler.NewCompiler(symTable, nil, tr)
|
c := compiler.NewCompiler(symTable, nil, nil, tr)
|
||||||
c.SetModuleLoader(func(moduleName string) ([]byte, error) {
|
c.SetModuleLoader(func(moduleName string) ([]byte, error) {
|
||||||
if src, ok := userModules[moduleName]; ok {
|
if src, ok := userModules[moduleName]; ok {
|
||||||
return []byte(src), nil
|
return []byte(src), nil
|
||||||
|
|
|
@ -94,7 +94,7 @@ func (s *Script) Compile() (*Compiled, error) {
|
||||||
return nil, fmt.Errorf("parse error: %s", err.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 {
|
if s.userModuleLoader != nil {
|
||||||
c.SetModuleLoader(s.userModuleLoader)
|
c.SetModuleLoader(s.userModuleLoader)
|
||||||
|
|
Loading…
Reference in a new issue