2016-02-20 00:21:51 +08:00
|
|
|
package eval
|
|
|
|
|
|
|
|
import (
|
2016-02-20 00:33:55 +08:00
|
|
|
"fmt"
|
2020-04-26 20:14:51 +08:00
|
|
|
"io"
|
2016-02-20 00:33:55 +08:00
|
|
|
|
2019-12-24 04:00:59 +08:00
|
|
|
"github.com/elves/elvish/pkg/diag"
|
2021-01-09 22:59:00 +08:00
|
|
|
"github.com/elves/elvish/pkg/eval/vars"
|
2019-12-24 04:00:59 +08:00
|
|
|
"github.com/elves/elvish/pkg/parse"
|
2021-01-20 07:17:56 +08:00
|
|
|
"github.com/elves/elvish/pkg/prog"
|
2016-02-20 00:21:51 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// compiler maintains the set of states needed when compiling a single source
|
|
|
|
// file.
|
|
|
|
type compiler struct {
|
2017-12-24 07:51:32 +08:00
|
|
|
// Builtin namespace.
|
2020-12-25 01:39:51 +08:00
|
|
|
builtin *staticNs
|
2017-12-24 07:51:32 +08:00
|
|
|
// Lexical namespaces.
|
2020-12-25 01:39:51 +08:00
|
|
|
scopes []*staticNs
|
|
|
|
// Sources of captured variables.
|
|
|
|
captures []*staticUpNs
|
2020-04-26 20:14:51 +08:00
|
|
|
// Destination of warning messages. This is currently only used for
|
|
|
|
// deprecation messages.
|
|
|
|
warn io.Writer
|
|
|
|
// Deprecation registry.
|
|
|
|
deprecations deprecationRegistry
|
2016-10-11 21:37:27 +08:00
|
|
|
// Information about the source.
|
2020-04-26 02:22:38 +08:00
|
|
|
srcMeta parse.Source
|
2016-02-20 00:21:51 +08:00
|
|
|
}
|
|
|
|
|
2020-12-25 01:39:51 +08:00
|
|
|
type capture struct {
|
|
|
|
name string
|
|
|
|
// If true, the captured variable is from the immediate outer level scope,
|
|
|
|
// i.e. the local scope the lambda is evaluated in. Otherwise the captured
|
|
|
|
// variable is from a more outer level, i.e. the upvalue scope the lambda is
|
|
|
|
// evaluated in.
|
|
|
|
local bool
|
|
|
|
// Index to the captured variable.
|
|
|
|
index int
|
|
|
|
}
|
|
|
|
|
2021-01-09 22:59:00 +08:00
|
|
|
func compile(b, g *staticNs, tree parse.Tree, w io.Writer) (op nsOp, err error) {
|
2020-12-25 01:39:51 +08:00
|
|
|
g = g.clone()
|
2020-04-26 20:14:51 +08:00
|
|
|
cp := &compiler{
|
2020-12-25 01:39:51 +08:00
|
|
|
b, []*staticNs{g}, []*staticUpNs{new(staticUpNs)},
|
2020-04-26 20:14:51 +08:00
|
|
|
w, newDeprecationRegistry(), tree.Source}
|
2020-01-13 06:55:17 +08:00
|
|
|
defer func() {
|
|
|
|
r := recover()
|
|
|
|
if r == nil {
|
|
|
|
return
|
2021-01-02 07:26:24 +08:00
|
|
|
} else if e := GetCompilationError(r); e != nil {
|
2020-01-13 06:55:17 +08:00
|
|
|
// Save the compilation error and stop the panic.
|
|
|
|
err = e
|
|
|
|
} else {
|
|
|
|
// Resume the panic; it is not supposed to be handled here.
|
|
|
|
panic(r)
|
|
|
|
}
|
|
|
|
}()
|
2020-04-27 02:04:49 +08:00
|
|
|
chunkOp := cp.chunkOp(tree.Root)
|
2021-01-10 08:54:02 +08:00
|
|
|
return nsOp{chunkOp, g}, nil
|
2021-01-09 22:59:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type nsOp struct {
|
2021-01-10 08:54:02 +08:00
|
|
|
inner effectOp
|
|
|
|
template *staticNs
|
2021-01-09 22:59:00 +08:00
|
|
|
}
|
|
|
|
|
2021-01-24 22:10:45 +08:00
|
|
|
// Prepares the local namespace, and returns the namespace and a function for
|
|
|
|
// executing the inner effectOp. Mutates fm.local.
|
|
|
|
func (op nsOp) prepare(fm *Frame) (*Ns, func() Exception) {
|
2021-01-10 08:54:02 +08:00
|
|
|
if len(op.template.names) > len(fm.local.names) {
|
|
|
|
n := len(op.template.names)
|
|
|
|
newLocal := &Ns{make([]vars.Var, n), op.template.names, op.template.deleted}
|
2021-01-09 22:59:00 +08:00
|
|
|
copy(newLocal.slots, fm.local.slots)
|
2021-01-10 08:54:02 +08:00
|
|
|
for i := len(fm.local.names); i < n; i++ {
|
2021-01-17 09:33:25 +08:00
|
|
|
newLocal.slots[i] = MakeVarFromName(newLocal.names[i])
|
2021-01-09 22:59:00 +08:00
|
|
|
}
|
|
|
|
fm.local = newLocal
|
2021-01-10 08:54:02 +08:00
|
|
|
} else {
|
|
|
|
// If no new has been created, there might still be some existing
|
|
|
|
// variables deleted.
|
|
|
|
fm.local = &Ns{fm.local.slots, fm.local.names, op.template.deleted}
|
2021-01-09 22:59:00 +08:00
|
|
|
}
|
2021-01-24 22:10:45 +08:00
|
|
|
return fm.local, func() Exception { return op.inner.exec(fm) }
|
2020-04-27 02:04:49 +08:00
|
|
|
}
|
|
|
|
|
2021-01-04 00:25:52 +08:00
|
|
|
const compilationErrorType = "compilation error"
|
|
|
|
|
2020-03-29 04:34:52 +08:00
|
|
|
func (cp *compiler) errorpf(r diag.Ranger, format string, args ...interface{}) {
|
2020-01-13 06:55:17 +08:00
|
|
|
// The panic is caught by the recover in compile above.
|
2021-01-04 00:25:52 +08:00
|
|
|
panic(&diag.Error{
|
|
|
|
Type: compilationErrorType,
|
|
|
|
Message: fmt.Sprintf(format, args...),
|
|
|
|
Context: *diag.NewContext(cp.srcMeta.Name, cp.srcMeta.Code, r)})
|
2016-02-20 04:16:23 +08:00
|
|
|
}
|
|
|
|
|
2021-01-04 00:25:52 +08:00
|
|
|
// GetCompilationError returns a *diag.Error if the given value is a compilation
|
|
|
|
// error. Otherwise it returns nil.
|
|
|
|
func GetCompilationError(e interface{}) *diag.Error {
|
|
|
|
if e, ok := e.(*diag.Error); ok && e.Type == compilationErrorType {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2020-12-25 01:39:51 +08:00
|
|
|
func (cp *compiler) thisScope() *staticNs {
|
2016-02-20 00:21:51 +08:00
|
|
|
return cp.scopes[len(cp.scopes)-1]
|
|
|
|
}
|
|
|
|
|
2020-12-25 01:39:51 +08:00
|
|
|
func (cp *compiler) pushScope() (*staticNs, *staticUpNs) {
|
|
|
|
sc := new(staticNs)
|
|
|
|
up := new(staticUpNs)
|
2016-02-20 00:21:51 +08:00
|
|
|
cp.scopes = append(cp.scopes, sc)
|
2020-12-25 01:39:51 +08:00
|
|
|
cp.captures = append(cp.captures, up)
|
|
|
|
return sc, up
|
2016-02-20 00:21:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cp *compiler) popScope() {
|
2020-12-25 01:39:51 +08:00
|
|
|
cp.scopes[len(cp.scopes)-1] = nil
|
2016-02-20 00:21:51 +08:00
|
|
|
cp.scopes = cp.scopes[:len(cp.scopes)-1]
|
2020-12-25 01:39:51 +08:00
|
|
|
cp.captures[len(cp.captures)-1] = nil
|
|
|
|
cp.captures = cp.captures[:len(cp.captures)-1]
|
2020-04-26 20:14:51 +08:00
|
|
|
}
|
2021-01-20 07:17:56 +08:00
|
|
|
|
|
|
|
func (cp *compiler) checkDeprecatedBuiltin(name string, r diag.Ranger) {
|
|
|
|
msg := ""
|
|
|
|
minLevel := 15
|
|
|
|
switch name {
|
|
|
|
case "-source~":
|
|
|
|
msg = `the "source" command is deprecated; use "eval" instead`
|
|
|
|
case "ord~":
|
|
|
|
msg = `the "ord" command is deprecated; use "str:to-codepoints" instead`
|
|
|
|
case "chr~":
|
|
|
|
msg = `the "chr" command is deprecated; use "str:from-codepoints" instead`
|
|
|
|
case "has-prefix~":
|
|
|
|
msg = `the "has-prefix" command is deprecated; use "str:has-prefix" instead`
|
|
|
|
case "has-suffix~":
|
|
|
|
msg = `the "has-suffix" command is deprecated; use "str:has-suffix" instead`
|
|
|
|
case "esleep~":
|
|
|
|
msg = `the "esleep" command is deprecated; use "sleep" instead`
|
|
|
|
case "eval-symlinks~":
|
|
|
|
msg = `the "eval-symlinks" command is deprecated; use "path:eval-symlinks" instead`
|
|
|
|
case "path-abs~":
|
|
|
|
msg = `the "path-abs" command is deprecated; use "path:abs" instead`
|
|
|
|
case "path-base~":
|
|
|
|
msg = `the "path-base" command is deprecated; use "path:base" instead`
|
|
|
|
case "path-clean~":
|
|
|
|
msg = `the "path-clean" command is deprecated; use "path:clean" instead`
|
|
|
|
case "path-dir~":
|
|
|
|
msg = `the "path-dir" command is deprecated; use "path:dir" instead`
|
|
|
|
case "path-ext~":
|
|
|
|
msg = `the "path-ext" command is deprecated; use "path:ext" instead`
|
|
|
|
case "-is-dir~":
|
|
|
|
msg = `the "-is-dir" command is deprecated; use "path:is-dir" instead`
|
|
|
|
default:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
cp.deprecate(r, msg, minLevel)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cp *compiler) deprecate(r diag.Ranger, msg string, minLevel int) {
|
|
|
|
if cp.warn == nil || r == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
dep := deprecation{cp.srcMeta.Name, r.Range(), msg}
|
|
|
|
if prog.DeprecationLevel >= minLevel && cp.deprecations.register(dep) {
|
|
|
|
err := diag.Error{
|
|
|
|
Type: "deprecation", Message: msg,
|
|
|
|
Context: diag.Context{
|
|
|
|
Name: cp.srcMeta.Name, Source: cp.srcMeta.Code, Ranging: r.Range()}}
|
|
|
|
fmt.Fprintln(cp.warn, err.Show(""))
|
|
|
|
}
|
|
|
|
}
|