本文整理汇总了Golang中go/ast.Expr.String方法的典型用法代码示例。如果您正苦于以下问题:Golang Expr.String方法的具体用法?Golang Expr.String怎么用?Golang Expr.String使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类go/ast.Expr
的用法示例。
在下文中一共展示了Expr.String方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: translateStmt
//.........这里部分代码省略.........
case *ast.AssignStmt:
expr = a.Rhs[0].(*ast.TypeAssertExpr).X
printCaseBodyPrefix = func(index int) {
value := refVar
caseClause := s.Body.List[index].(*ast.CaseClause)
if len(caseClause.List) == 1 {
t := c.p.Types[caseClause.List[0]].Type
if _, isInterface := t.Underlying().(*types.Interface); !isInterface && !types.Identical(t, types.Typ[types.UntypedNil]) {
value += ".$val"
}
}
c.Printf("%s = %s;", c.objectName(c.p.Implicits[caseClause]), value)
}
case *ast.ExprStmt:
expr = a.X.(*ast.TypeAssertExpr).X
}
c.Printf("%s = %s;", refVar, c.translateExpr(expr))
translateCond := func(cond ast.Expr) *expression {
if types.Identical(c.p.Types[cond].Type, types.Typ[types.UntypedNil]) {
return c.formatExpr("%s === $ifaceNil", refVar)
}
return c.formatExpr("$assertType(%s, %s, true)[1]", refVar, c.typeName(c.p.Types[cond].Type))
}
c.translateBranchingStmt(s.Body.List, true, translateCond, printCaseBodyPrefix, label, c.Flattened[s])
case *ast.ForStmt:
if s.Init != nil {
c.translateStmt(s.Init, nil)
}
cond := func() string {
if s.Cond == nil {
return "true"
}
return c.translateExpr(s.Cond).String()
}
c.translateLoopingStmt(cond, s.Body, nil, func() {
if s.Post != nil {
c.translateStmt(s.Post, nil)
}
}, label, c.Flattened[s])
case *ast.RangeStmt:
refVar := c.newVariable("_ref")
c.Printf("%s = %s;", refVar, c.translateExpr(s.X))
switch t := c.p.Types[s.X].Type.Underlying().(type) {
case *types.Basic:
iVar := c.newVariable("_i")
c.Printf("%s = 0;", iVar)
runeVar := c.newVariable("_rune")
c.translateLoopingStmt(func() string { return iVar + " < " + refVar + ".length" }, s.Body, func() {
c.Printf("%s = $decodeRune(%s, %s);", runeVar, refVar, iVar)
if !isBlank(s.Key) {
c.Printf("%s", c.translateAssign(s.Key, iVar, types.Typ[types.Int], s.Tok == token.DEFINE))
}
if !isBlank(s.Value) {
c.Printf("%s", c.translateAssign(s.Value, runeVar+"[0]", types.Typ[types.Rune], s.Tok == token.DEFINE))
}
}, func() {
c.Printf("%s += %s[1];", iVar, runeVar)
}, label, c.Flattened[s])
case *types.Map:
iVar := c.newVariable("_i")
c.Printf("%s = 0;", iVar)
keysVar := c.newVariable("_keys")
示例2: varDecl
func (cdd *CDD) varDecl(w *bytes.Buffer, typ types.Type, global bool, name string, val ast.Expr) (acds []*CDD) {
dim, acds := cdd.Type(w, typ)
w.WriteByte(' ')
w.WriteString(dimFuncPtr(name, dim))
constInit := true // true if C declaration can init value
if global {
cdd.copyDecl(w, ";\n") // Global variables may need declaration
if val != nil {
if i, ok := val.(*ast.Ident); !ok || i.Name != "nil" {
constInit = cdd.exprValue(val) != nil
}
}
}
if constInit {
w.WriteString(" = ")
if val != nil {
cdd.Expr(w, val, typ)
} else {
zeroVal(w, typ)
}
}
w.WriteString(";\n")
cdd.copyDef(w)
if !constInit {
w.Reset()
// Runtime initialisation
assign := false
switch t := underlying(typ).(type) {
case *types.Slice:
switch vt := val.(type) {
case *ast.CompositeLit:
aname := "array" + cdd.gtc.uniqueId()
var n int64
for _, elem := range vt.Elts {
switch e := elem.(type) {
case *ast.KeyValueExpr:
val := cdd.exprValue(e.Key)
if val == nil {
panic("slice: composite literal with non-constant key")
}
m, ok := exact.Int64Val(val)
if !ok {
panic("slice: can't convert " + val.String() + " to int64")
}
m++
if m > n {
n = m
}
default:
n++
}
}
at := types.NewArray(t.Elem(), n)
o := types.NewVar(vt.Lbrace, cdd.gtc.pkg, aname, at)
cdd.gtc.pkg.Scope().Insert(o)
acd := cdd.gtc.newCDD(o, VarDecl, cdd.il)
av := *vt
cdd.gtc.ti.Types[&av] = types.TypeAndValue{Type: at} // BUG: thread-unsafe
acd.varDecl(new(bytes.Buffer), o.Type(), cdd.gtc.isGlobal(o), aname, &av)
cdd.InitNext = acd
acds = append(acds, acd)
w.WriteByte('\t')
w.WriteString(name)
w.WriteString(" = ASLICE(")
w.WriteString(strconv.FormatInt(at.Len(), 10))
w.WriteString(", ")
w.WriteString(aname)
w.WriteString(");\n")
default:
assign = true
}
case *types.Array:
w.WriteByte('\t')
w.WriteString("ACPY(")
w.WriteString(name)
w.WriteString(", ")
switch val.(type) {
case *ast.CompositeLit:
w.WriteString("((")
dim, _ := cdd.Type(w, t.Elem())
dim = append([]string{"[]"}, dim...)
w.WriteString("(" + dimFuncPtr("", dim) + "))")
cdd.Expr(w, val, typ)
default:
cdd.Expr(w, val, typ)
}
//.........这里部分代码省略.........