2014-03-03 13:41:44 +08:00
|
|
|
package eval
|
|
|
|
|
2016-02-22 03:15:43 +08:00
|
|
|
//go:generate ./builtin_modules.bash
|
2016-02-17 06:14:53 +08:00
|
|
|
|
2014-04-02 10:51:09 +08:00
|
|
|
// Builtin special forms.
|
2014-03-03 13:41:44 +08:00
|
|
|
|
2015-01-25 06:45:34 +08:00
|
|
|
import (
|
2016-02-17 06:14:53 +08:00
|
|
|
"fmt"
|
2015-01-25 07:31:52 +08:00
|
|
|
"os"
|
2016-10-11 00:07:57 +08:00
|
|
|
"strings"
|
2015-01-25 06:45:34 +08:00
|
|
|
|
2016-01-25 01:10:54 +08:00
|
|
|
"github.com/elves/elvish/parse"
|
2016-02-17 06:14:53 +08:00
|
|
|
"github.com/elves/elvish/store"
|
2015-01-25 06:45:34 +08:00
|
|
|
)
|
2014-03-03 13:41:44 +08:00
|
|
|
|
2016-02-20 07:48:13 +08:00
|
|
|
type compileBuiltin func(*compiler, *parse.Form) OpFunc
|
2014-04-02 10:51:09 +08:00
|
|
|
|
2016-01-23 01:05:15 +08:00
|
|
|
var builtinSpecials map[string]compileBuiltin
|
2016-02-08 06:23:16 +08:00
|
|
|
|
|
|
|
// BuiltinSpecialNames contains all names of builtin special forms. It is
|
|
|
|
// useful for the syntax highlighter.
|
2015-07-06 23:58:51 +08:00
|
|
|
var BuiltinSpecialNames []string
|
2014-04-02 10:51:09 +08:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
// Needed to avoid initialization loop
|
2016-01-23 01:05:15 +08:00
|
|
|
builtinSpecials = map[string]compileBuiltin{
|
|
|
|
"del": compileDel,
|
2016-01-23 08:24:47 +08:00
|
|
|
"fn": compileFn,
|
2016-02-17 06:14:53 +08:00
|
|
|
"use": compileUse,
|
2014-04-02 10:51:09 +08:00
|
|
|
}
|
2016-02-08 06:23:16 +08:00
|
|
|
for k := range builtinSpecials {
|
2015-07-06 23:58:51 +08:00
|
|
|
BuiltinSpecialNames = append(BuiltinSpecialNames, k)
|
|
|
|
}
|
2014-04-02 10:51:09 +08:00
|
|
|
}
|
|
|
|
|
2015-01-22 00:22:55 +08:00
|
|
|
// DelForm = 'del' { VariablePrimary }
|
2016-02-20 07:48:13 +08:00
|
|
|
func compileDel(cp *compiler, fn *parse.Form) OpFunc {
|
2014-09-16 23:57:33 +08:00
|
|
|
// Do conventional compiling of all compound expressions, including
|
|
|
|
// ensuring that variables can be resolved
|
2015-01-25 07:05:47 +08:00
|
|
|
var names, envNames []string
|
2016-01-23 07:56:09 +08:00
|
|
|
for _, cn := range fn.Args {
|
2016-02-20 04:16:23 +08:00
|
|
|
cp.compiling(cn)
|
2016-01-23 07:56:09 +08:00
|
|
|
qname := mustString(cp, cn, "should be a literal variable name")
|
2016-09-15 06:16:57 +08:00
|
|
|
splice, ns, name := ParseAndFixVariable(qname)
|
2016-02-07 10:12:54 +08:00
|
|
|
if splice {
|
2016-02-20 04:16:23 +08:00
|
|
|
cp.errorf("removing spliced variable makes no sense")
|
2016-02-07 10:12:54 +08:00
|
|
|
}
|
2015-01-25 07:05:47 +08:00
|
|
|
switch ns {
|
|
|
|
case "", "local":
|
2016-01-23 07:56:09 +08:00
|
|
|
if !cp.thisScope()[name] {
|
2016-02-20 04:16:23 +08:00
|
|
|
cp.errorf("variable $%s not found on current local scope", name)
|
2015-01-25 07:05:47 +08:00
|
|
|
}
|
2016-01-23 07:56:09 +08:00
|
|
|
delete(cp.thisScope(), name)
|
2015-01-25 07:05:47 +08:00
|
|
|
names = append(names, name)
|
2016-09-10 06:54:18 +08:00
|
|
|
case "e", "E":
|
2015-01-25 07:05:47 +08:00
|
|
|
envNames = append(envNames, name)
|
|
|
|
default:
|
2016-09-10 06:54:18 +08:00
|
|
|
cp.errorf("can only delete a variable in local: or E:")
|
2014-04-02 11:13:43 +08:00
|
|
|
}
|
2015-01-22 00:22:55 +08:00
|
|
|
|
2014-04-02 11:13:43 +08:00
|
|
|
}
|
2016-02-12 07:30:36 +08:00
|
|
|
return func(ec *EvalCtx) {
|
2014-09-26 05:26:56 +08:00
|
|
|
for _, name := range names {
|
2015-02-25 19:21:06 +08:00
|
|
|
delete(ec.local, name)
|
2014-04-30 10:45:54 +08:00
|
|
|
}
|
2015-01-25 07:05:47 +08:00
|
|
|
for _, name := range envNames {
|
2015-02-26 22:17:05 +08:00
|
|
|
// BUG(xiaq): We rely on the fact that os.Unsetenv always returns
|
|
|
|
// nil.
|
2015-01-25 07:31:52 +08:00
|
|
|
os.Unsetenv(name)
|
2015-01-25 07:05:47 +08:00
|
|
|
}
|
2014-04-02 11:13:43 +08:00
|
|
|
}
|
|
|
|
}
|
2015-01-20 05:11:44 +08:00
|
|
|
|
2016-01-31 09:00:31 +08:00
|
|
|
// makeFnOp wraps an op such that a return is converted to an ok.
|
2016-02-08 06:23:16 +08:00
|
|
|
func makeFnOp(op Op) Op {
|
2016-02-20 07:48:13 +08:00
|
|
|
return Op{func(ec *EvalCtx) {
|
|
|
|
err := ec.PEval(op)
|
|
|
|
if err != nil && err != Return {
|
2016-01-31 09:11:10 +08:00
|
|
|
// rethrow
|
2016-02-20 07:48:13 +08:00
|
|
|
throw(err)
|
2015-07-08 18:31:40 +08:00
|
|
|
}
|
2016-02-20 07:48:13 +08:00
|
|
|
}, op.Begin, op.End}
|
2015-07-08 18:31:40 +08:00
|
|
|
}
|
|
|
|
|
2016-01-27 00:42:37 +08:00
|
|
|
// FnForm = 'fn' StringPrimary LambdaPrimary
|
2015-01-22 01:06:14 +08:00
|
|
|
//
|
2016-01-28 05:24:17 +08:00
|
|
|
// fn f []{foobar} is a shorthand for set '&'f = []{foobar}.
|
2016-02-20 07:48:13 +08:00
|
|
|
func compileFn(cp *compiler, fn *parse.Form) OpFunc {
|
2016-01-23 08:24:47 +08:00
|
|
|
if len(fn.Args) == 0 {
|
2016-02-20 04:16:23 +08:00
|
|
|
end := fn.End()
|
|
|
|
cp.errorpf(end, end, "should be followed by function name")
|
2015-01-22 01:06:14 +08:00
|
|
|
}
|
2016-01-23 08:24:47 +08:00
|
|
|
fnName := mustString(cp, fn.Args[0], "must be a literal string")
|
2016-01-27 00:42:37 +08:00
|
|
|
varName := FnPrefix + fnName
|
2015-01-22 01:06:14 +08:00
|
|
|
|
2016-01-23 08:24:47 +08:00
|
|
|
if len(fn.Args) == 1 {
|
2016-02-20 04:16:23 +08:00
|
|
|
end := fn.Args[0].End()
|
|
|
|
cp.errorpf(end, end, "should be followed by a lambda")
|
2015-01-22 01:06:14 +08:00
|
|
|
}
|
2016-01-23 08:24:47 +08:00
|
|
|
pn := mustPrimary(cp, fn.Args[1], "should be a lambda")
|
|
|
|
if pn.Type != parse.Lambda {
|
2016-02-20 04:16:23 +08:00
|
|
|
cp.compiling(pn)
|
|
|
|
cp.errorf("should be a lambda")
|
2016-01-23 08:24:47 +08:00
|
|
|
}
|
|
|
|
if len(fn.Args) > 2 {
|
2016-02-20 04:16:23 +08:00
|
|
|
cp.errorpf(fn.Args[2].Begin(), fn.Args[len(fn.Args)-1].End(), "superfluous argument(s)")
|
2015-01-22 01:06:14 +08:00
|
|
|
}
|
|
|
|
|
2016-01-26 06:45:51 +08:00
|
|
|
cp.registerVariableSet(":" + varName)
|
2016-01-23 08:24:47 +08:00
|
|
|
op := cp.lambda(pn)
|
2015-01-22 01:06:14 +08:00
|
|
|
|
2016-02-12 07:30:36 +08:00
|
|
|
return func(ec *EvalCtx) {
|
2016-02-16 21:49:22 +08:00
|
|
|
// Initialize the function variable with the builtin nop
|
|
|
|
// function. This step allows the definition of recursive
|
|
|
|
// functions; the actual function will never be called.
|
|
|
|
ec.local[varName] = NewPtrVariable(&BuiltinFn{"<shouldn't be called>", nop})
|
2016-01-29 20:55:14 +08:00
|
|
|
closure := op(ec)[0].(*Closure)
|
2015-07-08 18:31:40 +08:00
|
|
|
closure.Op = makeFnOp(closure.Op)
|
2016-02-16 21:49:22 +08:00
|
|
|
ec.local[varName].Set(closure)
|
2015-01-22 01:06:14 +08:00
|
|
|
}
|
|
|
|
}
|
2016-02-17 06:14:53 +08:00
|
|
|
|
|
|
|
// UseForm = 'use' StringPrimary [ Compound ]
|
2016-02-20 07:48:13 +08:00
|
|
|
func compileUse(cp *compiler, fn *parse.Form) OpFunc {
|
2016-02-17 06:14:53 +08:00
|
|
|
var modname string
|
|
|
|
var filenameOp ValuesOp
|
2016-02-20 07:48:13 +08:00
|
|
|
var filenameBegin, filenameEnd int
|
2016-02-17 06:14:53 +08:00
|
|
|
|
|
|
|
switch len(fn.Args) {
|
|
|
|
case 0:
|
2016-02-20 04:16:23 +08:00
|
|
|
end := fn.Head.End()
|
|
|
|
cp.errorpf(end, end, "lack module name")
|
2016-02-17 06:14:53 +08:00
|
|
|
case 2:
|
2016-02-20 07:48:13 +08:00
|
|
|
filenameOp = cp.compoundOp(fn.Args[1])
|
2016-02-17 06:14:53 +08:00
|
|
|
filenameBegin = fn.Args[1].Begin()
|
2016-02-20 07:48:13 +08:00
|
|
|
filenameEnd = fn.Args[1].End()
|
2016-02-17 06:14:53 +08:00
|
|
|
fallthrough
|
|
|
|
case 1:
|
|
|
|
modname = mustString(cp, fn.Args[0], "should be a literal module name")
|
|
|
|
default:
|
2016-02-20 04:16:23 +08:00
|
|
|
cp.errorpf(fn.Args[2].Begin(), fn.Args[len(fn.Args)-1].End(), "superfluous argument(s)")
|
2016-02-17 06:14:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return func(ec *EvalCtx) {
|
2016-02-20 07:48:13 +08:00
|
|
|
if filenameOp.Func != nil {
|
|
|
|
values := filenameOp.Exec(ec)
|
|
|
|
valuesMust := &muster{ec, "module filename", filenameBegin, filenameEnd, values}
|
2016-02-22 03:10:33 +08:00
|
|
|
filename := string(valuesMust.mustOneStr())
|
|
|
|
use(ec, modname, &filename)
|
2016-02-17 06:14:53 +08:00
|
|
|
} else {
|
2016-02-22 03:10:33 +08:00
|
|
|
use(ec, modname, nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func use(ec *EvalCtx, modname string, pfilename *string) {
|
2016-03-21 07:55:34 +08:00
|
|
|
if _, ok := ec.Evaler.Modules[modname]; ok {
|
2016-02-22 03:10:33 +08:00
|
|
|
// Module already loaded.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load the source.
|
|
|
|
var filename, source string
|
|
|
|
|
|
|
|
if pfilename != nil {
|
|
|
|
filename = *pfilename
|
|
|
|
var err error
|
|
|
|
source, err = readFileUTF8(filename)
|
|
|
|
maybeThrow(err)
|
|
|
|
} else {
|
|
|
|
// No filename; defaulting to $datadir/$modname.elv.
|
|
|
|
dataDir, err := store.DataDir()
|
|
|
|
maybeThrow(err)
|
2016-10-11 00:07:57 +08:00
|
|
|
filename = dataDir + "/" + strings.Replace(modname, ":", "/", -1) + ".elv"
|
2016-02-22 03:10:33 +08:00
|
|
|
if _, err := os.Stat(filename); os.IsNotExist(err) {
|
|
|
|
// File does not exist. Try loading from the table of builtin
|
|
|
|
// modules.
|
|
|
|
var ok bool
|
|
|
|
if source, ok = builtinModules[modname]; ok {
|
|
|
|
// Source is loaded. Do nothing more.
|
|
|
|
filename = "<builtin module>"
|
2016-02-17 06:14:53 +08:00
|
|
|
} else {
|
2016-02-22 03:10:33 +08:00
|
|
|
throw(fmt.Errorf("cannot load %s: %s does not exist", modname, filename))
|
2016-02-17 06:14:53 +08:00
|
|
|
}
|
2016-02-22 03:10:33 +08:00
|
|
|
} else {
|
|
|
|
// File exists. Load it.
|
|
|
|
source, err = readFileUTF8(filename)
|
|
|
|
maybeThrow(err)
|
2016-02-17 06:14:53 +08:00
|
|
|
}
|
2016-02-22 03:10:33 +08:00
|
|
|
}
|
2016-02-17 06:14:53 +08:00
|
|
|
|
2016-10-09 22:52:02 +08:00
|
|
|
n, err := parse.Parse(source)
|
|
|
|
maybeThrow(err)
|
|
|
|
|
|
|
|
// Make an empty namespace.
|
|
|
|
local := Namespace{}
|
|
|
|
|
2016-02-22 03:10:33 +08:00
|
|
|
// TODO(xiaq): Should handle failures when evaluting the module
|
|
|
|
newEc := &EvalCtx{
|
|
|
|
ec.Evaler,
|
|
|
|
filename, source, "module " + modname,
|
2016-10-09 22:52:02 +08:00
|
|
|
local, Namespace{},
|
2016-07-08 07:03:34 +08:00
|
|
|
ec.ports, nil, true,
|
2016-10-11 22:47:55 +08:00
|
|
|
0, len(source), ec.addTraceback(),
|
2016-02-22 03:10:33 +08:00
|
|
|
}
|
2016-02-17 06:14:53 +08:00
|
|
|
|
2016-10-11 21:37:27 +08:00
|
|
|
op, err := newEc.Compile(n, filename, source)
|
2016-02-22 03:10:33 +08:00
|
|
|
// TODO the err originates in another source, should add appropriate information.
|
|
|
|
maybeThrow(err)
|
2016-02-17 06:14:53 +08:00
|
|
|
|
2016-10-09 22:52:02 +08:00
|
|
|
// Load the namespace before executing. This avoids mutual and self use's to
|
|
|
|
// result in an infinite recursion.
|
|
|
|
ec.Evaler.Modules[modname] = local
|
|
|
|
err = newEc.PEval(op)
|
|
|
|
if err != nil {
|
|
|
|
// Unload the namespace.
|
|
|
|
delete(ec.Modules, modname)
|
|
|
|
throw(err)
|
|
|
|
}
|
2016-02-17 06:14:53 +08:00
|
|
|
}
|