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") return fm.errorpf(op.varOp.Begin, op.varOp.End, "only one variable allowed")
} }
variable := variables[0] 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) body := op.bodyOp.execlambdaOp(fm)
elseBody := op.elseOp.execlambdaOp(fm) elseBody := op.elseOp.execlambdaOp(fm)

View File

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

View File

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