eval: Rewrite unwrappers to not use throw.

This commit is contained in:
Qi Xiao 2018-09-27 20:44:46 +01:00
parent a48ce453c0
commit 269e890a57
4 changed files with 71 additions and 40 deletions

View File

@ -521,7 +521,10 @@ func (op *forOp) Invoke(fm *Frame) error {
return fm.errorpf(op.varOp.Begin, op.varOp.End, "only one variable allowed")
}
variable := variables[0]
iterable := fm.ExecAndUnwrap("value being iterated", op.iterOp).One().Any()
iterable, err := fm.ExecAndUnwrap("value being iterated", op.iterOp).One().Any()
if err != nil {
return err
}
body := op.bodyOp.execlambdaOp(fm)
elseBody := op.elseOp.execlambdaOp(fm)

View File

@ -325,7 +325,10 @@ func (op *formOp) Invoke(fm *Frame) (errRet error) {
var args []interface{}
if op.headOp.Body != nil {
// head
headFn = fm.ExecAndUnwrap("head of command", op.headOp).One().Callable()
headFn, errRet = fm.ExecAndUnwrap("head of command", op.headOp).One().Callable()
if errRet != nil {
return errRet
}
// args
for _, argOp := range op.argOps {
@ -499,11 +502,15 @@ func (op *redirOp) Invoke(fm *Frame) error {
case parse.Write, parse.ReadWrite, parse.Append:
dst = 1
default:
panic("bad RedirMode; parser bug")
return fmt.Errorf("bad RedirMode; parser bug")
}
} else {
var err error
// dst must be a valid fd
dst = fm.ExecAndUnwrap("Fd", op.dstOp).One().NonNegativeInt()
dst, err = fm.ExecAndUnwrap("Fd", op.dstOp).One().NonNegativeInt()
if err != nil {
return err
}
}
fm.growPorts(dst + 1)
@ -512,7 +519,10 @@ func (op *redirOp) Invoke(fm *Frame) error {
srcUnwrap := fm.ExecAndUnwrap("redirection source", op.srcOp).One()
if op.srcIsFd {
src := srcUnwrap.FdOrClose()
src, err := srcUnwrap.FdOrClose()
if err != nil {
return err
}
if src == -1 {
// close
fm.ports[dst] = &Port{}
@ -520,7 +530,11 @@ func (op *redirOp) Invoke(fm *Frame) error {
fm.ports[dst] = fm.ports[src].Fork()
}
} else {
switch src := srcUnwrap.Any().(type) {
src, err := srcUnwrap.Any()
if err != nil {
return err
}
switch src := src.(type) {
case string:
f, err := os.OpenFile(src, op.flag, defaultFileRedirPerm)
if err != nil {

View File

@ -481,10 +481,12 @@ func (op *lambdaOp) Invoke(fm *Frame) ([]interface{}, error) {
}
optDefaults := make([]interface{}, len(op.optDefaultOps))
for i, op := range op.optDefaultOps {
defaultValue := fm.ExecAndUnwrap("option default value", op).One().Any()
defaultValue, err := fm.ExecAndUnwrap("option default value", op).One().Any()
if err != nil {
return nil, err
}
optDefaults[i] = defaultValue
}
// XXX(xiaq): Capture uses.
return []interface{}{&Closure{op.argNames, op.restArgName, op.optNames, optDefaults, op.subop, evCapture, op.srcMeta, op.defBegin, op.defEnd}}, nil
}

View File

@ -11,7 +11,7 @@ import (
// asserting certain properties of values and throwing exceptions when such
// properties are not satisfied.
type unwrapperInner struct {
type unwrapper struct {
// ctx is the evaluation context.
ctx *Frame
// description describes what is being unwrapped. It is used in error
@ -22,30 +22,27 @@ type unwrapperInner struct {
begin, end int
// values contain the Value's to unwrap.
values []interface{}
// Any errors during the unwrapping.
err error
}
func (u *unwrapperInner) error(want, gotfmt string, gotargs ...interface{}) {
func (u *unwrapper) error(want, gotfmt string, gotargs ...interface{}) {
if u.err != nil {
return
}
got := fmt.Sprintf(gotfmt, gotargs...)
throw(u.ctx.errorpf(u.begin, u.end, "%s must be %s; got %s", u.description,
want, got))
u.err = u.ctx.errorpf(
u.begin, u.end, "%s must be %s; got %s", u.description, want, got)
}
// ValuesUnwrapper unwraps []Value.
type ValuesUnwrapper struct{ *unwrapperInner }
// Unwrap creates an Unwrapper.
func (ctx *Frame) Unwrap(desc string, begin, end int, vs []interface{}) ValuesUnwrapper {
return ValuesUnwrapper{&unwrapperInner{ctx, desc, begin, end, vs}}
}
type ValuesUnwrapper struct{ *unwrapper }
// ExecAndUnwrap executes a ValuesOp and creates an Unwrapper for the obtained
// values.
func (ctx *Frame) ExecAndUnwrap(desc string, op ValuesOp) ValuesUnwrapper {
values, err := op.Exec(ctx)
if err != nil {
throw(err)
}
return ctx.Unwrap(desc, op.Begin, op.End, values)
return ValuesUnwrapper{&unwrapper{ctx, desc, op.Begin, op.End, values, err}}
}
// One unwraps the value to be exactly one value.
@ -53,53 +50,68 @@ func (u ValuesUnwrapper) One() ValueUnwrapper {
if len(u.values) != 1 {
u.error("a single value", "%d values", len(u.values))
}
return ValueUnwrapper{u.unwrapperInner}
return ValueUnwrapper{u.unwrapper}
}
// ValueUnwrapper unwraps one Value.
type ValueUnwrapper struct{ *unwrapperInner }
type ValueUnwrapper struct{ *unwrapper }
func (u ValueUnwrapper) Any() interface{} {
return u.values[0]
func (u ValueUnwrapper) Any() (interface{}, error) {
if u.err != nil {
return nil, u.err
}
return u.values[0], u.err
}
func (u ValueUnwrapper) String() string {
func (u ValueUnwrapper) String() (string, error) {
if u.err != nil {
return "", u.err
}
s, ok := u.values[0].(string)
if !ok {
u.error("string", "%s", vals.Kind(u.values[0]))
}
return s
return s, u.err
}
func (u ValueUnwrapper) Int() int {
s := u.String()
func (u ValueUnwrapper) Int() (int, error) {
s, err := u.String()
if err != nil {
return 0, u.err
}
i, err := strconv.Atoi(s)
if err != nil {
u.error("integer", "%s", s)
}
return i
return i, u.err
}
func (u ValueUnwrapper) NonNegativeInt() int {
i := u.Int()
func (u ValueUnwrapper) NonNegativeInt() (int, error) {
i, err := u.Int()
if err != nil {
return 0, err
}
if i < 0 {
u.error("non-negative int", "%d", i)
}
return i
return i, u.err
}
func (u ValueUnwrapper) FdOrClose() int {
s := u.String()
if s == "-" {
return -1
func (u ValueUnwrapper) FdOrClose() (int, error) {
s, err := u.String()
if err == nil && s == "-" {
return -1, nil
}
return u.NonNegativeInt()
}
func (u ValueUnwrapper) Callable() Callable {
func (u ValueUnwrapper) Callable() (Callable, error) {
if u.err != nil {
return nil, u.err
}
c, ok := u.values[0].(Callable)
if !ok {
u.error("callable", "%s", vals.Kind(u.values[0]))
}
return c
return c, u.err
}