本文整理匯總了Golang中github.com/elves/elvish/parse.Form.Begin方法的典型用法代碼示例。如果您正苦於以下問題:Golang Form.Begin方法的具體用法?Golang Form.Begin怎麽用?Golang Form.Begin使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/elves/elvish/parse.Form
的用法示例。
在下文中一共展示了Form.Begin方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: form
func (cp *compiler) form(n *parse.Form) Op {
if len(n.Assignments) > 0 {
if n.Head != nil {
cp.errorf(n.Begin(), "temporary assignments not yet supported")
}
ops := cp.assignments(n.Assignments)
return func(ec *EvalCtx) {
for _, op := range ops {
op(ec)
}
}
}
headStr, ok := oneString(n.Head)
if ok {
compileForm, ok := builtinSpecials[headStr]
if ok {
// special form
return compileForm(cp, n)
}
// Ignore the output. If a matching function exists it will be
// captured and eventually the Evaler executes it. If not, nothing
// happens here and the Evaler executes an external command.
cp.registerVariableGet(FnPrefix + headStr)
// XXX Dynamic head names should always refer to external commands
}
headOp := cp.compound(n.Head)
argOps := cp.compounds(n.Args)
// TODO: n.NamedArgs
redirOps := cp.redirs(n.Redirs)
// TODO: n.ErrorRedir
p := n.Begin()
// ec here is always a subevaler created in compiler.pipeline, so it can
// be safely modified.
return func(ec *EvalCtx) {
// head
headValues := headOp(ec)
headMust := ec.must(headValues, "the head of command", p)
headMust.mustLen(1)
switch headValues[0].(type) {
case String, Caller, Indexer:
default:
headMust.error("a string or callable", headValues[0].Kind())
}
// args
var args []Value
for _, argOp := range argOps {
args = append(args, argOp(ec)...)
}
// redirs
for _, redirOp := range redirOps {
redirOp(ec)
}
ec.resolveCaller(headValues[0]).Call(ec, args)
}
}
示例2: formOp
func (cp *compiler) formOp(n *parse.Form) Op {
return Op{cp.form(n), n.Begin(), n.End()}
}
示例3: form
func (cp *compiler) form(n *parse.Form) OpFunc {
var saveVarsOps []LValuesOp
var assignmentOps []Op
if len(n.Assignments) > 0 {
assignmentOps = cp.assignmentOps(n.Assignments)
if n.Head == nil {
// Permanent assignment.
return func(ec *EvalCtx) {
for _, op := range assignmentOps {
op.Exec(ec)
}
}
} else {
for _, a := range n.Assignments {
v, r := cp.lvaluesOp(a.Dst)
saveVarsOps = append(saveVarsOps, v, r)
}
}
}
if n.Control != nil {
if len(n.Args) > 0 {
cp.errorpf(n.Args[0].Begin(), n.Args[len(n.Args)-1].End(), "control structure takes no arguments")
}
redirOps := cp.redirOps(n.Redirs)
controlOp := cp.controlOp(n.Control)
return func(ec *EvalCtx) {
for _, redirOp := range redirOps {
redirOp.Exec(ec)
}
controlOp.Exec(ec)
}
}
headStr, ok := oneString(n.Head)
if ok {
compileForm, ok := builtinSpecials[headStr]
if ok {
// special form
return compileForm(cp, n)
}
// Ignore the output. If a matching function exists it will be
// captured and eventually the Evaler executes it. If not, nothing
// happens here and the Evaler executes an external command.
cp.registerVariableGet(FnPrefix + headStr)
// XXX Dynamic head names should always refer to external commands
}
headOp := cp.compoundOp(n.Head)
argOps := cp.compoundOps(n.Args)
optsOp := cp.mapPairs(n.Opts)
redirOps := cp.redirOps(n.Redirs)
// TODO: n.ErrorRedir
begin, end := n.Begin(), n.End()
// ec here is always a subevaler created in compiler.pipeline, so it can
// be safely modified.
return func(ec *EvalCtx) {
// Temporary assignment.
if len(saveVarsOps) > 0 {
// There is a temporary assignment.
// Save variables.
var saveVars []Variable
var saveVals []Value
for _, op := range saveVarsOps {
saveVars = append(saveVars, op.Exec(ec)...)
}
for _, v := range saveVars {
val := v.Get()
saveVals = append(saveVals, val)
Logger.Printf("saved %s = %s", v, val)
}
// Do assignment.
for _, op := range assignmentOps {
op.Exec(ec)
}
// Defer variable restoration. Will be executed even if an error
// occurs when evaling other part of the form.
defer func() {
for i, v := range saveVars {
val := saveVals[i]
if val == nil {
// XXX Old value is nonexistent. We should delete the
// variable. However, since the compiler now doesn't delete
// it, we don't delete it in the evaler either.
val = String("")
}
v.Set(val)
Logger.Printf("restored %s = %s", v, val)
}
}()
}
// head
headValues := headOp.Exec(ec)
ec.must(headValues, "head of command", headOp.Begin, headOp.End).mustLen(1)
headFn := mustFn(headValues[0])
// args
var args []Value
for _, argOp := range argOps {
//.........這裏部分代碼省略.........