本文整理匯總了Golang中code/google/com/p/go/tools/go/exact.BinaryOp函數的典型用法代碼示例。如果您正苦於以下問題:Golang BinaryOp函數的具體用法?Golang BinaryOp怎麽用?Golang BinaryOp使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了BinaryOp函數的13個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: parseNumber
// number = int_lit [ "p" int_lit ] .
//
func (p *gcParser) parseNumber() (x operand) {
x.mode = constant
// mantissa
mant := exact.MakeFromLiteral(p.parseInt(), token.INT)
assert(mant != nil)
if p.lit == "p" {
// exponent (base 2)
p.next()
exp, err := strconv.ParseInt(p.parseInt(), 10, 0)
if err != nil {
p.error(err)
}
if exp < 0 {
denom := exact.MakeInt64(1)
denom = exact.Shift(denom, token.SHL, uint(-exp))
x.typ = Typ[UntypedFloat]
x.val = exact.BinaryOp(mant, token.QUO, denom)
return
}
if exp > 0 {
mant = exact.Shift(mant, token.SHL, uint(exp))
}
x.typ = Typ[UntypedFloat]
x.val = mant
return
}
x.typ = Typ[UntypedInt]
x.val = mant
return
}
示例2: parseNumber
// number = int_lit [ "p" int_lit ] .
//
func (p *parser) parseNumber() (typ *types.Basic, val exact.Value) {
// mantissa
mant := exact.MakeFromLiteral(p.parseInt(), token.INT)
if mant == nil {
panic("invalid mantissa")
}
if p.lit == "p" {
// exponent (base 2)
p.next()
exp, err := strconv.ParseInt(p.parseInt(), 10, 0)
if err != nil {
p.error(err)
}
if exp < 0 {
denom := exact.MakeInt64(1)
denom = exact.Shift(denom, token.SHL, uint(-exp))
typ = types.Typ[types.UntypedFloat]
val = exact.BinaryOp(mant, token.QUO, denom)
return
}
if exp > 0 {
mant = exact.Shift(mant, token.SHL, uint(exp))
}
typ = types.Typ[types.UntypedFloat]
val = mant
return
}
typ = types.Typ[types.UntypedInt]
val = mant
return
}
示例3: ufloat
func (p *importer) ufloat() exact.Value {
exp := p.int()
x := exact.MakeFromBytes(p.bytes())
switch {
case exp < 0:
d := exact.Shift(exact.MakeInt64(1), token.SHL, uint(-exp))
x = exact.BinaryOp(x, token.QUO, d)
case exp > 0:
x = exact.Shift(x, token.SHL, uint(exp))
}
return x
}
示例4: fraction
func (p *importer) fraction() exact.Value {
sign := p.int()
if sign == 0 {
return exact.MakeInt64(0)
}
x := exact.BinaryOp(p.ufloat(), token.QUO, p.ufloat())
if sign < 0 {
x = exact.UnaryOp(token.SUB, x, 0)
}
return x
}
示例5: doOp
func doOp(x exact.Value, op token.Token, y exact.Value) (z exact.Value) {
defer panicHandler(&z)
if x == nil {
return exact.UnaryOp(op, y, -1)
}
switch op {
case token.EQL, token.NEQ, token.LSS, token.LEQ, token.GTR, token.GEQ:
return exact.MakeBool(exact.Compare(x, op, y))
case token.SHL, token.SHR:
s, _ := exact.Int64Val(y)
return exact.Shift(x, op, uint(s))
default:
return exact.BinaryOp(x, op, y)
}
}
示例6: value
func (p *importer) value() exact.Value {
switch kind := exact.Kind(p.int()); kind {
case falseTag:
return exact.MakeBool(false)
case trueTag:
return exact.MakeBool(true)
case int64Tag:
return exact.MakeInt64(p.int64())
case floatTag:
return p.float()
case fractionTag:
return p.fraction()
case complexTag:
re := p.fraction()
im := p.fraction()
return exact.BinaryOp(re, token.ADD, exact.MakeImag(im))
case stringTag:
return exact.MakeString(p.string())
default:
panic(fmt.Sprintf("unexpected value kind %d", kind))
}
}
示例7: evalAction
func evalAction(n ast.Node) exact.Value {
switch e := n.(type) {
case *ast.BasicLit:
return val(e.Value)
case *ast.BinaryExpr:
x := evalAction(e.X)
if x == nil {
return nil
}
y := evalAction(e.Y)
if y == nil {
return nil
}
switch e.Op {
case token.EQL, token.NEQ, token.LSS, token.LEQ, token.GTR, token.GEQ:
return exact.MakeBool(exact.Compare(x, e.Op, y))
case token.SHL, token.SHR:
s, _ := exact.Int64Val(y)
return exact.Shift(x, e.Op, uint(s))
default:
return exact.BinaryOp(x, e.Op, y)
}
case *ast.UnaryExpr:
return exact.UnaryOp(e.Op, evalAction(e.X), -1)
case *ast.CallExpr:
fmt.Printf("Can't handle call (%s) yet at pos %d\n", e.Fun, e.Pos())
return nil
case *ast.Ident:
fmt.Printf("Can't handle Ident %s here at pos %d\n", e.Name, e.Pos())
return nil
case *ast.ParenExpr:
return evalAction(e.X)
default:
fmt.Println("Can't handle")
fmt.Printf("n: %s, e: %s\n", n, e)
return nil
}
}
示例8: binary
func (check *Checker) binary(x *operand, lhs, rhs ast.Expr, op token.Token) {
var y operand
check.expr(x, lhs)
check.expr(&y, rhs)
if x.mode == invalid {
return
}
if y.mode == invalid {
x.mode = invalid
x.expr = y.expr
return
}
if isShift(op) {
check.shift(x, &y, op)
return
}
check.convertUntyped(x, y.typ)
if x.mode == invalid {
return
}
check.convertUntyped(&y, x.typ)
if y.mode == invalid {
x.mode = invalid
return
}
if isComparison(op) {
check.comparison(x, &y, op)
return
}
if !Identical(x.typ, y.typ) {
// only report an error if we have valid types
// (otherwise we had an error reported elsewhere already)
if x.typ != Typ[Invalid] && y.typ != Typ[Invalid] {
check.invalidOp(x.pos(), "mismatched types %s and %s", x.typ, y.typ)
}
x.mode = invalid
return
}
if !check.op(binaryOpPredicates, x, op) {
x.mode = invalid
return
}
if (op == token.QUO || op == token.REM) && (x.mode == constant || isInteger(x.typ)) && y.mode == constant && exact.Sign(y.val) == 0 {
check.invalidOp(y.pos(), "division by zero")
x.mode = invalid
return
}
if x.mode == constant && y.mode == constant {
typ := x.typ.Underlying().(*Basic)
// force integer division of integer operands
if op == token.QUO && isInteger(typ) {
op = token.QUO_ASSIGN
}
x.val = exact.BinaryOp(x.val, op, y.val)
// Typed constants must be representable in
// their type after each constant operation.
if isTyped(typ) {
check.representable(x, typ)
}
return
}
x.mode = value
// x.typ is unchanged
}
示例9: representableConst
// representableConst reports whether x can be represented as
// value of the given basic type kind and for the configuration
// provided (only needed for int/uint sizes).
//
// If rounded != nil, *rounded is set to the rounded value of x for
// representable floating-point values; it is left alone otherwise.
// It is ok to provide the addressof the first argument for rounded.
func representableConst(x exact.Value, conf *Config, as BasicKind, rounded *exact.Value) bool {
switch x.Kind() {
case exact.Unknown:
return true
case exact.Bool:
return as == Bool || as == UntypedBool
case exact.Int:
if x, ok := exact.Int64Val(x); ok {
switch as {
case Int:
var s = uint(conf.sizeof(Typ[as])) * 8
return int64(-1)<<(s-1) <= x && x <= int64(1)<<(s-1)-1
case Int8:
const s = 8
return -1<<(s-1) <= x && x <= 1<<(s-1)-1
case Int16:
const s = 16
return -1<<(s-1) <= x && x <= 1<<(s-1)-1
case Int32:
const s = 32
return -1<<(s-1) <= x && x <= 1<<(s-1)-1
case Int64:
return true
case Uint, Uintptr:
if s := uint(conf.sizeof(Typ[as])) * 8; s < 64 {
return 0 <= x && x <= int64(1)<<s-1
}
return 0 <= x
case Uint8:
const s = 8
return 0 <= x && x <= 1<<s-1
case Uint16:
const s = 16
return 0 <= x && x <= 1<<s-1
case Uint32:
const s = 32
return 0 <= x && x <= 1<<s-1
case Uint64:
return 0 <= x
case Float32, Float64, Complex64, Complex128,
UntypedInt, UntypedFloat, UntypedComplex:
return true
}
}
n := exact.BitLen(x)
switch as {
case Uint, Uintptr:
var s = uint(conf.sizeof(Typ[as])) * 8
return exact.Sign(x) >= 0 && n <= int(s)
case Uint64:
return exact.Sign(x) >= 0 && n <= 64
case Float32, Complex64:
if rounded == nil {
return fitsFloat32(x)
}
r := roundFloat32(x)
if r != nil {
*rounded = r
return true
}
case Float64, Complex128:
if rounded == nil {
return fitsFloat64(x)
}
r := roundFloat64(x)
if r != nil {
*rounded = r
return true
}
case UntypedInt, UntypedFloat, UntypedComplex:
return true
}
case exact.Float:
switch as {
case Float32, Complex64:
if rounded == nil {
return fitsFloat32(x)
}
r := roundFloat32(x)
if r != nil {
*rounded = r
return true
}
case Float64, Complex128:
if rounded == nil {
return fitsFloat64(x)
}
r := roundFloat64(x)
if r != nil {
//.........這裏部分代碼省略.........
示例10: builtin
//.........這裏部分代碼省略.........
check.recordBuiltinType(call.Fun, makeSig(nil, c))
}
case _Complex:
// complex(x, y realT) complexT
if !check.complexArg(x) {
return
}
var y operand
arg(&y, 1)
if y.mode == invalid {
return
}
if !check.complexArg(&y) {
return
}
check.convertUntyped(x, y.typ)
if x.mode == invalid {
return
}
check.convertUntyped(&y, x.typ)
if y.mode == invalid {
return
}
if !Identical(x.typ, y.typ) {
check.invalidArg(x.pos(), "mismatched types %s and %s", x.typ, y.typ)
return
}
if x.mode == constant && y.mode == constant {
x.val = exact.BinaryOp(x.val, token.ADD, exact.MakeImag(y.val))
} else {
x.mode = value
}
realT := x.typ
complexT := Typ[Invalid]
switch realT.Underlying().(*Basic).kind {
case Float32:
complexT = Typ[Complex64]
case Float64:
complexT = Typ[Complex128]
case UntypedInt, UntypedRune, UntypedFloat:
if x.mode == constant {
realT = defaultType(realT).(*Basic)
complexT = Typ[UntypedComplex]
} else {
// untyped but not constant; probably because one
// operand is a non-constant shift of untyped lhs
realT = Typ[Float64]
complexT = Typ[Complex128]
}
default:
check.invalidArg(x.pos(), "float32 or float64 arguments expected")
return
}
x.typ = complexT
if check.Types != nil && x.mode != constant {
check.recordBuiltinType(call.Fun, makeSig(complexT, realT, realT))
}
if x.mode != constant {
示例11: parseConstDecl
// ConstDecl = "const" ExportedName [ Type ] "=" Literal .
// Literal = bool_lit | int_lit | float_lit | complex_lit | rune_lit | string_lit .
// bool_lit = "true" | "false" .
// complex_lit = "(" float_lit "+" float_lit "i" ")" .
// rune_lit = "(" int_lit "+" int_lit ")" .
// string_lit = `"` { unicode_char } `"` .
//
func (p *parser) parseConstDecl() {
p.expectKeyword("const")
pkg, name := p.parseExportedName()
var typ0 types.Type
if p.tok != '=' {
typ0 = p.parseType()
}
p.expect('=')
var typ types.Type
var val exact.Value
switch p.tok {
case scanner.Ident:
// bool_lit
if p.lit != "true" && p.lit != "false" {
p.error("expected true or false")
}
typ = types.Typ[types.UntypedBool]
val = exact.MakeBool(p.lit == "true")
p.next()
case '-', scanner.Int:
// int_lit
typ, val = p.parseNumber()
case '(':
// complex_lit or rune_lit
p.next()
if p.tok == scanner.Char {
p.next()
p.expect('+')
typ = types.Typ[types.UntypedRune]
_, val = p.parseNumber()
p.expect(')')
break
}
_, re := p.parseNumber()
p.expect('+')
_, im := p.parseNumber()
p.expectKeyword("i")
p.expect(')')
typ = types.Typ[types.UntypedComplex]
val = exact.BinaryOp(re, token.ADD, exact.MakeImag(im))
case scanner.Char:
// rune_lit
typ = types.Typ[types.UntypedRune]
val = exact.MakeFromLiteral(p.lit, token.CHAR)
p.next()
case scanner.String:
// string_lit
typ = types.Typ[types.UntypedString]
val = exact.MakeFromLiteral(p.lit, token.STRING)
p.next()
default:
p.errorf("expected literal got %s", scanner.TokenString(p.tok))
}
if typ0 == nil {
typ0 = typ
}
pkg.Scope().Insert(types.NewConst(token.NoPos, pkg, name, typ0, val))
}
示例12: parseConstValue
// ConstValue = string | "false" | "true" | ["-"] (int ["'"] | FloatOrComplex) .
// FloatOrComplex = float ["i" | ("+"|"-") float "i"] .
func (p *parser) parseConstValue() (val exact.Value, typ types.Type) {
switch p.tok {
case scanner.String:
str := p.parseString()
val = exact.MakeString(str)
typ = types.Typ[types.UntypedString]
return
case scanner.Ident:
b := false
switch p.lit {
case "false":
case "true":
b = true
default:
p.errorf("expected const value, got %s (%q)", scanner.TokenString(p.tok), p.lit)
}
p.next()
val = exact.MakeBool(b)
typ = types.Typ[types.UntypedBool]
return
}
sign := ""
if p.tok == '-' {
p.next()
sign = "-"
}
switch p.tok {
case scanner.Int:
val = exact.MakeFromLiteral(sign+p.lit, token.INT)
if val == nil {
p.error("could not parse integer literal")
}
p.next()
if p.tok == '\'' {
p.next()
typ = types.Typ[types.UntypedRune]
} else {
typ = types.Typ[types.UntypedInt]
}
case scanner.Float:
re := sign + p.lit
p.next()
var im string
switch p.tok {
case '+':
p.next()
im = p.expect(scanner.Float)
case '-':
p.next()
im = "-" + p.expect(scanner.Float)
case scanner.Ident:
// re is in fact the imaginary component. Expect "i" below.
im = re
re = "0"
default:
val = exact.MakeFromLiteral(re, token.FLOAT)
if val == nil {
p.error("could not parse float literal")
}
typ = types.Typ[types.UntypedFloat]
return
}
p.expectKeyword("i")
reval := exact.MakeFromLiteral(re, token.FLOAT)
if reval == nil {
p.error("could not parse real component of complex literal")
}
imval := exact.MakeFromLiteral(im+"i", token.IMAG)
if imval == nil {
p.error("could not parse imag component of complex literal")
}
val = exact.BinaryOp(reval, token.ADD, imval)
typ = types.Typ[types.UntypedComplex]
default:
p.errorf("expected const value, got %s (%q)", scanner.TokenString(p.tok), p.lit)
}
return
}
示例13: builtin
//.........這裏部分代碼省略.........
}
x.mode = novalue
case _Complex:
if !check.complexArg(x) {
goto Error
}
var y operand
check.expr(&y, args[1])
if y.mode == invalid {
goto Error
}
if !check.complexArg(&y) {
goto Error
}
check.convertUntyped(x, y.typ)
if x.mode == invalid {
goto Error
}
check.convertUntyped(&y, x.typ)
if y.mode == invalid {
goto Error
}
if !IsIdentical(x.typ, y.typ) {
check.invalidArg(x.pos(), "mismatched types %s and %s", x.typ, y.typ)
goto Error
}
typ := x.typ.Underlying().(*Basic)
if x.mode == constant && y.mode == constant {
x.val = exact.BinaryOp(x.val, token.ADD, exact.MakeImag(y.val))
} else {
x.mode = value
}
switch typ.kind {
case Float32:
x.typ = Typ[Complex64]
case Float64:
x.typ = Typ[Complex128]
case UntypedInt, UntypedRune, UntypedFloat:
if x.mode == constant {
typ = defaultType(typ).(*Basic)
x.typ = Typ[UntypedComplex]
} else {
// untyped but not constant; probably because one
// operand is a non-constant shift of untyped lhs
typ = Typ[Float64]
x.typ = Typ[Complex128]
}
default:
check.invalidArg(x.pos(), "float32 or float64 arguments expected")
goto Error
}
if x.mode != constant {
// The arguments have now their final types, which at run-
// time will be materialized. Update the expression trees.
// If the current types are untyped, the materialized type
// is the respective default type.
// (If the result is constant, the arguments are never
// materialized and there is nothing to do.)
check.updateExprType(args[0], typ, true)