2014-03-03 13:41:44 +08:00
|
|
|
package eval
|
|
|
|
|
2017-06-19 18:50:52 +08:00
|
|
|
// Builtin special forms. Special forms behave mostly like ordinary commands -
|
|
|
|
// they are valid commands syntactically, and can take part in pipelines - but
|
|
|
|
// they have special rules for the evaluation of their arguments and can affect
|
|
|
|
// the compilation phase (whereas ordinary commands can only affect the
|
|
|
|
// evaluation phase).
|
|
|
|
//
|
|
|
|
// For instance, the "and" special form evaluates its arguments from left to
|
|
|
|
// right, and stops as soon as one booleanly false value is obtained: the
|
|
|
|
// command "and $false (fail haha)" does not produce an exception.
|
|
|
|
//
|
|
|
|
// As another instance, the "del" special form removes a variable, affecting the
|
|
|
|
// compiler.
|
|
|
|
//
|
|
|
|
// Flow control structures are also implemented as special forms in elvish, with
|
|
|
|
// closures functioning as code blocks.
|
2014-03-03 13:41:44 +08:00
|
|
|
|
2015-01-25 06:45:34 +08:00
|
|
|
import (
|
2017-01-28 05:25:32 +08:00
|
|
|
"errors"
|
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"
|
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
|
|
|
|
2017-06-11 07:04:53 +08:00
|
|
|
// ErrNoDataDir is thrown by the "use" special form when there is no data
|
|
|
|
// directory.
|
2017-01-28 05:25:32 +08:00
|
|
|
var ErrNoDataDir = errors.New("There is no data directory")
|
|
|
|
|
2016-01-23 01:05:15 +08:00
|
|
|
var builtinSpecials map[string]compileBuiltin
|
2016-02-08 06:23:16 +08:00
|
|
|
|
2017-05-30 06:39:40 +08:00
|
|
|
// IsBuiltinSpecial is the set of all names of builtin special forms. It is
|
|
|
|
// intended for external consumption, e.g. the syntax highlighter.
|
|
|
|
var IsBuiltinSpecial = map[string]bool{}
|
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{
|
2017-02-17 08:28:58 +08:00
|
|
|
"del": compileDel,
|
|
|
|
"fn": compileFn,
|
|
|
|
"use": compileUse,
|
2017-03-03 08:21:59 +08:00
|
|
|
"and": compileAnd,
|
|
|
|
"or": compileOr,
|
2017-02-18 12:34:11 +08:00
|
|
|
"if": compileIf,
|
2017-02-17 08:28:58 +08:00
|
|
|
"while": compileWhile,
|
2017-02-18 12:34:11 +08:00
|
|
|
"for": compileFor,
|
2017-02-18 12:16:21 +08:00
|
|
|
"try": compileTry,
|
2014-04-02 10:51:09 +08:00
|
|
|
}
|
2017-05-30 06:39:40 +08:00
|
|
|
for name := range builtinSpecials {
|
|
|
|
IsBuiltinSpecial[name] = true
|
2015-07-06 23:58:51 +08:00
|
|
|
}
|
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-10-13 14:10:10 +08:00
|
|
|
explode, ns, name := ParseAndFixVariable(qname)
|
|
|
|
if explode {
|
|
|
|
cp.errorf("removing exploded 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-10-18 22:17:15 +08:00
|
|
|
case "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)
|
2017-01-29 10:29:03 +08:00
|
|
|
if err != nil && err.(*Exception).Cause != 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 {
|
2017-02-18 13:30:37 +08:00
|
|
|
args := cp.walkArgs(fn)
|
|
|
|
nameNode := args.next()
|
|
|
|
varName := FnPrefix + mustString(cp, nameNode, "must be a literal string")
|
|
|
|
bodyNode := args.nextMustLambda()
|
|
|
|
args.mustEnd()
|
2015-01-22 01:06:14 +08:00
|
|
|
|
2016-01-26 06:45:51 +08:00
|
|
|
cp.registerVariableSet(":" + varName)
|
2017-02-18 13:30:37 +08:00
|
|
|
op := cp.lambda(bodyNode)
|
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
|
|
|
|
2017-09-20 18:46:35 +08:00
|
|
|
// UseForm = 'use' StringPrimary
|
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
|
|
|
|
|
|
|
|
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 1:
|
|
|
|
modname = mustString(cp, fn.Args[0], "should be a literal module name")
|
|
|
|
default:
|
2017-09-20 18:46:35 +08:00
|
|
|
cp.errorpf(fn.Args[1].Begin(), fn.Args[len(fn.Args)-1].End(), "superfluous argument(s)")
|
2016-02-17 06:14:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return func(ec *EvalCtx) {
|
2017-09-20 18:46:35 +08:00
|
|
|
use(ec, modname)
|
2016-02-22 03:10:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-20 18:46:35 +08:00
|
|
|
func use(ec *EvalCtx, spec string) {
|
|
|
|
// When modspec = "a/b/c:d", modname is c:d, and modpath is a/b/c/d
|
|
|
|
modname := spec[strings.LastIndexByte(spec, '/')+1:]
|
|
|
|
modpath := strings.Replace(spec, ":", "/", -1)
|
|
|
|
|
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
|
|
|
|
|
2017-09-20 18:46:35 +08:00
|
|
|
// No filename; defaulting to $datadir/lib/$modpath.elv.
|
|
|
|
if ec.DataDir == "" {
|
|
|
|
throw(ErrNoDataDir)
|
|
|
|
}
|
|
|
|
filename = ec.DataDir + "/lib/" + modpath + ".elv"
|
|
|
|
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 = embeddedModules[modpath]; ok {
|
|
|
|
// Source is loaded. Do nothing more.
|
|
|
|
filename = "<builtin module>"
|
2016-02-22 03:10:33 +08:00
|
|
|
} else {
|
2017-09-20 18:46:35 +08:00
|
|
|
throw(fmt.Errorf("cannot load %s: %s does not exist", modpath, filename))
|
2016-02-17 06:14:53 +08:00
|
|
|
}
|
2017-09-20 18:46:35 +08:00
|
|
|
} else {
|
|
|
|
// File exists. Load it.
|
|
|
|
source, err = readFileUTF8(filename)
|
|
|
|
maybeThrow(err)
|
2016-02-22 03:10:33 +08:00
|
|
|
}
|
2016-02-17 06:14:53 +08:00
|
|
|
|
2016-10-13 23:03:17 +08:00
|
|
|
n, err := parse.Parse(filename, source)
|
2016-10-09 22:52:02 +08:00
|
|
|
maybeThrow(err)
|
|
|
|
|
2017-09-20 18:46:35 +08:00
|
|
|
// Make an empty namespace to evaluate the module in.
|
2016-10-09 22:52:02 +08:00
|
|
|
local := Namespace{}
|
|
|
|
|
2016-02-22 03:10:33 +08:00
|
|
|
newEc := &EvalCtx{
|
2017-09-20 18:46:35 +08:00
|
|
|
ec.Evaler, "module " + modpath,
|
2016-10-13 21:51:51 +08:00
|
|
|
filename, source,
|
2016-10-09 22:52:02 +08:00
|
|
|
local, Namespace{},
|
2017-08-06 01:54:16 +08:00
|
|
|
ec.ports,
|
2017-01-13 22:50:14 +08:00
|
|
|
0, len(source), ec.addTraceback(), false,
|
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
|
|
|
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
|
|
|
}
|
2017-02-17 08:20:02 +08:00
|
|
|
|
2017-03-03 08:21:59 +08:00
|
|
|
// compileAnd compiles the "and" special form.
|
|
|
|
// The and special form evaluates arguments until a false-ish values is found
|
|
|
|
// and outputs it; the remaining arguments are not evaluated. If there are no
|
|
|
|
// false-ish values, the last value is output. If there are no arguments, it
|
|
|
|
// outputs $true, as if there is a hidden $true before actual arguments.
|
|
|
|
func compileAnd(cp *compiler, fn *parse.Form) OpFunc {
|
|
|
|
return compileAndOr(cp, fn, true, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// compileOr compiles the "or" special form.
|
|
|
|
// The or special form evaluates arguments until a true-ish values is found and
|
|
|
|
// outputs it; the remaining arguments are not evaluated. If there are no
|
|
|
|
// true-ish values, the last value is output. If there are no arguments, it
|
|
|
|
// outputs $false, as if there is a hidden $false before actual arguments.
|
|
|
|
func compileOr(cp *compiler, fn *parse.Form) OpFunc {
|
|
|
|
return compileAndOr(cp, fn, false, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func compileAndOr(cp *compiler, fn *parse.Form, init, stopAt bool) OpFunc {
|
|
|
|
argOps := cp.compoundOps(fn.Args)
|
|
|
|
return func(ec *EvalCtx) {
|
|
|
|
var lastValue Value = Bool(init)
|
|
|
|
for _, op := range argOps {
|
|
|
|
values := op.Exec(ec)
|
|
|
|
for _, value := range values {
|
|
|
|
if ToBool(value) == stopAt {
|
|
|
|
ec.OutputChan() <- value
|
|
|
|
return
|
|
|
|
}
|
|
|
|
lastValue = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ec.OutputChan() <- lastValue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-18 12:34:11 +08:00
|
|
|
func compileIf(cp *compiler, fn *parse.Form) OpFunc {
|
|
|
|
args := cp.walkArgs(fn)
|
2017-02-18 13:25:42 +08:00
|
|
|
var condNodes []*parse.Compound
|
|
|
|
var bodyNodes []*parse.Primary
|
2017-02-18 12:34:11 +08:00
|
|
|
for {
|
|
|
|
condNodes = append(condNodes, args.next())
|
2017-02-18 13:25:42 +08:00
|
|
|
bodyNodes = append(bodyNodes, args.nextMustLambda())
|
2017-02-18 12:34:11 +08:00
|
|
|
if !args.nextIs("elif") {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2017-02-18 13:25:42 +08:00
|
|
|
elseNode := args.nextMustLambdaIfAfter("else")
|
2017-02-18 12:34:11 +08:00
|
|
|
args.mustEnd()
|
|
|
|
|
|
|
|
condOps := cp.compoundOps(condNodes)
|
2017-02-18 13:25:42 +08:00
|
|
|
bodyOps := cp.primaryOps(bodyNodes)
|
2017-02-18 12:34:11 +08:00
|
|
|
var elseOp ValuesOp
|
|
|
|
if elseNode != nil {
|
2017-02-18 13:25:42 +08:00
|
|
|
elseOp = cp.primaryOp(elseNode)
|
2017-02-18 12:34:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return func(ec *EvalCtx) {
|
2017-02-18 12:45:40 +08:00
|
|
|
bodies := make([]Callable, len(bodyOps))
|
2017-02-18 12:34:11 +08:00
|
|
|
for i, bodyOp := range bodyOps {
|
2017-02-18 13:25:42 +08:00
|
|
|
bodies[i] = bodyOp.execlambdaOp(ec)
|
2017-02-18 12:34:11 +08:00
|
|
|
}
|
2017-02-18 13:25:42 +08:00
|
|
|
else_ := elseOp.execlambdaOp(ec)
|
2017-02-18 12:34:11 +08:00
|
|
|
for i, condOp := range condOps {
|
2017-03-03 09:13:04 +08:00
|
|
|
if allTrue(condOp.Exec(ec.fork("if cond"))) {
|
|
|
|
bodies[i].Call(ec.fork("if body"), NoArgs, NoOpts)
|
2017-02-18 12:34:11 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if elseOp.Func != nil {
|
2017-03-03 09:13:04 +08:00
|
|
|
else_.Call(ec.fork("if else"), NoArgs, NoOpts)
|
2017-02-18 12:34:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-17 08:28:58 +08:00
|
|
|
func compileWhile(cp *compiler, fn *parse.Form) OpFunc {
|
|
|
|
args := cp.walkArgs(fn)
|
|
|
|
condNode := args.next()
|
2017-02-18 13:25:42 +08:00
|
|
|
bodyNode := args.nextMustLambda()
|
2017-02-17 08:28:58 +08:00
|
|
|
args.mustEnd()
|
|
|
|
|
|
|
|
condOp := cp.compoundOp(condNode)
|
2017-02-18 13:25:42 +08:00
|
|
|
bodyOp := cp.primaryOp(bodyNode)
|
2017-02-17 08:28:58 +08:00
|
|
|
|
|
|
|
return func(ec *EvalCtx) {
|
2017-02-18 13:25:42 +08:00
|
|
|
body := bodyOp.execlambdaOp(ec)
|
2017-02-17 08:28:58 +08:00
|
|
|
|
|
|
|
for {
|
2017-03-03 09:13:04 +08:00
|
|
|
cond := condOp.Exec(ec.fork("while cond"))
|
2017-02-17 08:28:58 +08:00
|
|
|
if !allTrue(cond) {
|
|
|
|
break
|
|
|
|
}
|
2017-03-03 09:13:04 +08:00
|
|
|
err := ec.fork("while").PCall(body, NoArgs, NoOpts)
|
2017-02-17 08:28:58 +08:00
|
|
|
if err != nil {
|
|
|
|
exc := err.(*Exception)
|
|
|
|
if exc.Cause == Continue {
|
|
|
|
// do nothing
|
|
|
|
} else if exc.Cause == Break {
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
throw(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-17 08:20:02 +08:00
|
|
|
func compileFor(cp *compiler, fn *parse.Form) OpFunc {
|
|
|
|
args := cp.walkArgs(fn)
|
|
|
|
varNode := args.next()
|
|
|
|
iterNode := args.next()
|
2017-02-18 13:25:42 +08:00
|
|
|
bodyNode := args.nextMustLambda()
|
|
|
|
elseNode := args.nextMustLambdaIfAfter("else")
|
2017-02-17 08:20:02 +08:00
|
|
|
args.mustEnd()
|
|
|
|
|
|
|
|
varOp, restOp := cp.lvaluesOp(varNode.Indexings[0])
|
|
|
|
if restOp.Func != nil {
|
|
|
|
cp.errorpf(restOp.Begin, restOp.End, "rest not allowed")
|
|
|
|
}
|
|
|
|
|
|
|
|
iterOp := cp.compoundOp(iterNode)
|
2017-02-18 13:25:42 +08:00
|
|
|
bodyOp := cp.primaryOp(bodyNode)
|
2017-02-17 08:20:02 +08:00
|
|
|
var elseOp ValuesOp
|
|
|
|
if elseNode != nil {
|
2017-02-18 13:25:42 +08:00
|
|
|
elseOp = cp.primaryOp(elseNode)
|
2017-02-17 08:20:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return func(ec *EvalCtx) {
|
|
|
|
variables := varOp.Exec(ec)
|
|
|
|
if len(variables) != 1 {
|
|
|
|
ec.errorpf(varOp.Begin, varOp.End, "only one variable allowed")
|
|
|
|
}
|
|
|
|
variable := variables[0]
|
|
|
|
|
2017-09-05 07:15:06 +08:00
|
|
|
iterable := ec.ExecAndUnwrap("value being iterated", iterOp).One().Iterable()
|
2017-02-17 08:20:02 +08:00
|
|
|
|
2017-02-18 13:25:42 +08:00
|
|
|
body := bodyOp.execlambdaOp(ec)
|
|
|
|
elseBody := elseOp.execlambdaOp(ec)
|
2017-02-17 08:20:02 +08:00
|
|
|
|
|
|
|
iterated := false
|
|
|
|
iterable.Iterate(func(v Value) bool {
|
|
|
|
iterated = true
|
|
|
|
variable.Set(v)
|
2017-03-03 09:13:04 +08:00
|
|
|
err := ec.fork("for").PCall(body, NoArgs, NoOpts)
|
2017-02-17 08:20:02 +08:00
|
|
|
if err != nil {
|
|
|
|
exc := err.(*Exception)
|
|
|
|
if exc.Cause == Continue {
|
|
|
|
// do nothing
|
|
|
|
} else if exc.Cause == Break {
|
|
|
|
return false
|
|
|
|
} else {
|
|
|
|
throw(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
if !iterated && elseBody != nil {
|
2017-03-03 09:13:04 +08:00
|
|
|
elseBody.Call(ec.fork("for else"), NoArgs, NoOpts)
|
2017-02-17 08:20:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-02-18 08:26:44 +08:00
|
|
|
|
2017-02-18 12:16:21 +08:00
|
|
|
func compileTry(cp *compiler, fn *parse.Form) OpFunc {
|
2017-05-22 06:54:04 +08:00
|
|
|
logger.Println("compiling try")
|
2017-02-18 12:16:21 +08:00
|
|
|
args := cp.walkArgs(fn)
|
2017-02-18 13:25:42 +08:00
|
|
|
bodyNode := args.nextMustLambda()
|
2017-05-22 06:54:04 +08:00
|
|
|
logger.Printf("body is %q", bodyNode.SourceText())
|
2017-02-18 12:16:21 +08:00
|
|
|
var exceptVarNode *parse.Indexing
|
2017-02-18 13:25:42 +08:00
|
|
|
var exceptNode *parse.Primary
|
2017-02-18 12:16:21 +08:00
|
|
|
if args.nextIs("except") {
|
2017-05-22 06:54:04 +08:00
|
|
|
logger.Println("except-ing")
|
2017-02-18 13:25:42 +08:00
|
|
|
n := args.peek()
|
|
|
|
// Is this a variable?
|
|
|
|
if len(n.Indexings) == 1 && n.Indexings[0].Head.Type == parse.Bareword {
|
|
|
|
exceptVarNode = n.Indexings[0]
|
|
|
|
args.next()
|
2017-02-18 12:16:21 +08:00
|
|
|
}
|
2017-02-18 13:25:42 +08:00
|
|
|
exceptNode = args.nextMustLambda()
|
2017-02-18 12:16:21 +08:00
|
|
|
}
|
2017-02-18 13:25:42 +08:00
|
|
|
elseNode := args.nextMustLambdaIfAfter("else")
|
|
|
|
finallyNode := args.nextMustLambdaIfAfter("finally")
|
2017-02-18 12:16:21 +08:00
|
|
|
args.mustEnd()
|
|
|
|
|
|
|
|
var exceptVarOp LValuesOp
|
|
|
|
var bodyOp, exceptOp, elseOp, finallyOp ValuesOp
|
2017-02-18 13:25:42 +08:00
|
|
|
bodyOp = cp.primaryOp(bodyNode)
|
2017-02-18 12:16:21 +08:00
|
|
|
if exceptVarNode != nil {
|
|
|
|
var restOp LValuesOp
|
|
|
|
exceptVarOp, restOp = cp.lvaluesOp(exceptVarNode)
|
|
|
|
if restOp.Func != nil {
|
|
|
|
cp.errorpf(restOp.Begin, restOp.End, "may not use @rest in except variable")
|
|
|
|
}
|
2017-03-03 09:05:46 +08:00
|
|
|
}
|
|
|
|
if exceptNode != nil {
|
2017-02-18 13:25:42 +08:00
|
|
|
exceptOp = cp.primaryOp(exceptNode)
|
2017-02-18 12:16:21 +08:00
|
|
|
}
|
|
|
|
if elseNode != nil {
|
2017-02-18 13:25:42 +08:00
|
|
|
elseOp = cp.primaryOp(elseNode)
|
2017-02-18 12:16:21 +08:00
|
|
|
}
|
|
|
|
if finallyNode != nil {
|
2017-02-18 13:25:42 +08:00
|
|
|
finallyOp = cp.primaryOp(finallyNode)
|
2017-02-18 12:16:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return func(ec *EvalCtx) {
|
2017-02-18 13:25:42 +08:00
|
|
|
body := bodyOp.execlambdaOp(ec)
|
2017-02-18 12:16:21 +08:00
|
|
|
exceptVar := exceptVarOp.execMustOne(ec)
|
2017-02-18 13:25:42 +08:00
|
|
|
except := exceptOp.execlambdaOp(ec)
|
|
|
|
else_ := elseOp.execlambdaOp(ec)
|
|
|
|
finally := finallyOp.execlambdaOp(ec)
|
2017-02-18 12:16:21 +08:00
|
|
|
|
2017-03-03 09:13:04 +08:00
|
|
|
err := ec.fork("try body").PCall(body, NoArgs, NoOpts)
|
2017-02-18 12:16:21 +08:00
|
|
|
if err != nil {
|
|
|
|
if except != nil {
|
2017-03-03 09:05:46 +08:00
|
|
|
if exceptVar != nil {
|
|
|
|
exceptVar.Set(err.(*Exception))
|
|
|
|
}
|
2017-03-03 09:13:04 +08:00
|
|
|
err = ec.fork("try except").PCall(except, NoArgs, NoOpts)
|
2017-02-18 12:16:21 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if else_ != nil {
|
2017-03-03 09:13:04 +08:00
|
|
|
err = ec.fork("try else").PCall(else_, NoArgs, NoOpts)
|
2017-02-18 12:16:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if finally != nil {
|
2017-03-03 09:13:04 +08:00
|
|
|
finally.Call(ec.fork("try finally"), NoArgs, NoOpts)
|
2017-02-18 12:16:21 +08:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
throw(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-18 13:25:42 +08:00
|
|
|
// execLambdaOp executes a ValuesOp that is known to yield a lambda and returns
|
|
|
|
// the lambda. If the ValuesOp is empty, it returns a nil.
|
|
|
|
func (op ValuesOp) execlambdaOp(ec *EvalCtx) Callable {
|
2017-02-18 08:26:44 +08:00
|
|
|
if op.Func == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-18 13:25:42 +08:00
|
|
|
return op.Exec(ec)[0].(Callable)
|
2017-02-18 08:26:44 +08:00
|
|
|
}
|
2017-02-18 12:16:21 +08:00
|
|
|
|
|
|
|
// execMustOne executes the LValuesOp and raises an exception if it does not
|
|
|
|
// evaluate to exactly one Variable. If the given LValuesOp is empty, it returns
|
|
|
|
// nil.
|
|
|
|
func (op LValuesOp) execMustOne(ec *EvalCtx) Variable {
|
|
|
|
if op.Func == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
variables := op.Exec(ec)
|
|
|
|
if len(variables) != 1 {
|
|
|
|
ec.errorpf(op.Begin, op.End, "should be one variable")
|
|
|
|
}
|
|
|
|
return variables[0]
|
|
|
|
}
|