The compiler no longer takes name and source.

This commit is contained in:
Qi Xiao 2016-02-19 17:33:55 +01:00
parent 9a2c30dd3e
commit be3b938bcf
4 changed files with 11 additions and 13 deletions

View File

@ -119,7 +119,6 @@ func (ed *Editor) notify(format string, args ...interface{}) {
func (ed *Editor) refresh(fullRefresh bool, tips bool) error {
// Re-lex the line, unless we are in modeCompletion
name := "[interacitve]"
src := ed.line
if ed.mode != modeCompletion {
n, err := parse.Parse(src)
@ -134,7 +133,7 @@ func (ed *Editor) refresh(fullRefresh bool, tips bool) error {
ed.tokens = []Token{{ParserError, src, nil, ""}}
} else {
ed.tokens = tokenize(src, n)
_, err := ed.evaler.Compile(name, src, n)
_, err := ed.evaler.Compile(n)
if err != nil {
if tips && !atEnd(err, len(src)) {
ed.addTip("compiler error: %s", err)

View File

@ -197,7 +197,8 @@ func compileUse(cp *compiler, fn *parse.Form) Op {
n, err := parse.Parse(source)
maybeThrow(err)
op, err := newEc.Compile(filename, source, n)
op, err := newEc.Compile(n)
// TODO the err originates in another source, should add appropriate information.
maybeThrow(err)
op(newEc)

View File

@ -3,6 +3,8 @@ package eval
//go:generate ./boilerplate.py
import (
"fmt"
"github.com/elves/elvish/parse"
"github.com/elves/elvish/util"
)
@ -20,24 +22,20 @@ type (
// compiler maintains the set of states needed when compiling a single source
// file.
type compiler struct {
// Used in error messages.
name, source string
// Lexical scopes.
scopes []scope
// Variables captured from outer scopes.
capture scope
// Stored error.
error error
}
func compile(name, source string, sc scope, n *parse.Chunk) (op Op, err error) {
cp := &compiler{name, source, []scope{sc}, scope{}, nil}
func compile(sc scope, n *parse.Chunk) (op Op, err error) {
cp := &compiler{[]scope{sc}, scope{}}
defer util.Catch(&err)
return cp.chunk(n), nil
}
func (cp *compiler) errorf(p int, format string, args ...interface{}) {
throw(util.NewContextualError(cp.name, "syntax error", cp.source, p, format, args...))
throw(&util.PosError{p, p, fmt.Errorf(format, args...)})
}
func (cp *compiler) thisScope() scope {

View File

@ -130,7 +130,7 @@ func makeScope(s Namespace) scope {
// Eval evaluates a chunk node n. The supplied name and text are used in
// diagnostic messages.
func (ev *Evaler) Eval(name, text string, n *parse.Chunk, ports []*Port) error {
op, err := ev.Compile(name, text, n)
op, err := ev.Compile(n)
if err != nil {
return err
}
@ -164,8 +164,8 @@ func (ev *Evaler) EvalInteractive(text string, n *parse.Chunk) error {
}
// Compile compiles elvish code in the global scope.
func (ev *Evaler) Compile(name, text string, n *parse.Chunk) (Op, error) {
return compile(name, text, makeScope(ev.global), n)
func (ev *Evaler) Compile(n *parse.Chunk) (Op, error) {
return compile(makeScope(ev.global), n)
}
// PEval evaluates an op in a protected environment so that calls to errorf are