本文整理汇总了Golang中go/ast.SwitchStmt.Pos方法的典型用法代码示例。如果您正苦于以下问题:Golang SwitchStmt.Pos方法的具体用法?Golang SwitchStmt.Pos怎么用?Golang SwitchStmt.Pos使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类go/ast.SwitchStmt
的用法示例。
在下文中一共展示了SwitchStmt.Pos方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: wrapSwitch
func (v *visitor) wrapSwitch(_switch *ast.SwitchStmt, identList []*ast.Ident) (block *ast.BlockStmt) {
block = astPrintf(`
{
godebug.Line(ctx, %s, %s)
%s
scope := %s.EnteringNewChildScope()
_ = scope // placeholder
_ = scope // placeholder
}`, v.scopeVar, pos2lineString(_switch.Pos()), _switch.Init, v.scopeVar)[0].(*ast.BlockStmt)
block.List[3] = newDeclareCall(idents.scope, identList)
_switch.Init = nil
block.List[4] = _switch
return block
}
示例2: compileSwitchStmt
func (a *stmtCompiler) compileSwitchStmt(s *ast.SwitchStmt) {
// Create implicit scope around switch
bc := a.enterChild()
defer bc.exit()
// Compile init statement, if any
if s.Init != nil {
bc.compileStmt(s.Init)
}
// Compile condition, if any, and extract its effects
var cond *expr
condbc := bc.enterChild()
if s.Tag != nil {
e := condbc.compileExpr(condbc.block, false, s.Tag)
if e != nil {
var effect func(*Thread)
effect, cond = e.extractEffect(condbc.block, "switch")
a.push(effect)
}
}
// Count cases
ncases := 0
hasDefault := false
for _, c := range s.Body.List {
clause, ok := c.(*ast.CaseClause)
if !ok {
a.diagAt(clause.Pos(), "switch statement must contain case clauses")
continue
}
if clause.List == nil {
if hasDefault {
a.diagAt(clause.Pos(), "switch statement contains more than one default case")
}
hasDefault = true
} else {
ncases += len(clause.List)
}
}
// Compile case expressions
cases := make([]func(*Thread) bool, ncases)
i := 0
for _, c := range s.Body.List {
clause, ok := c.(*ast.CaseClause)
if !ok {
continue
}
for _, v := range clause.List {
e := condbc.compileExpr(condbc.block, false, v)
switch {
case e == nil:
// Error reported by compileExpr
case cond == nil && !e.t.isBoolean():
a.diagAt(v.Pos(), "'case' condition must be boolean")
case cond == nil:
cases[i] = e.asBool()
case cond != nil:
// Create comparison
// TOOD(austin) This produces bad error messages
compare := e.compileBinaryExpr(token.EQL, cond, e)
if compare != nil {
cases[i] = compare.asBool()
}
}
i++
}
}
// Emit condition
casePCs := make([]*uint, ncases+1)
endPC := badPC
a.flow.put(false, false, casePCs)
a.push(func(t *Thread) {
for i, c := range cases {
if c(t) {
t.pc = *casePCs[i]
return
}
}
t.pc = *casePCs[ncases]
})
condbc.exit()
// Compile cases
i = 0
for _, c := range s.Body.List {
clause, ok := c.(*ast.CaseClause)
if !ok {
continue
}
// Save jump PC's
pc := a.nextPC()
if clause.List != nil {
for _ = range clause.List {
casePCs[i] = &pc
i++
//.........这里部分代码省略.........