mirror of
https://github.com/golang/go
synced 2024-11-23 04:50:06 -07:00
9211a7d413
pointer types, supports literals, identifiers, type-checking most unary and binary operators, "compiling" a few unary and binary operators, and assignment and declaration statements. R=rsc APPROVED=rsc DELTA=1751 (1751 added, 0 deleted, 0 changed) OCL=31309 CL=31691
51 lines
1023 B
Go
51 lines
1023 B
Go
// Copyright 2009 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package eval
|
|
|
|
import (
|
|
"bignum";
|
|
"eval";
|
|
"fmt";
|
|
"go/token";
|
|
)
|
|
|
|
// TODO(austin): Maybe add to bignum in more general form
|
|
func ratToString(rat *bignum.Rational) string {
|
|
n, dnat := rat.Value();
|
|
d := bignum.MakeInt(false, dnat);
|
|
w, frac := n.QuoRem(d);
|
|
out := w.String();
|
|
if frac.IsZero() {
|
|
return out;
|
|
}
|
|
|
|
r := frac.Abs();
|
|
r = r.Mul(bignum.Nat(1e6));
|
|
dec, tail := r.DivMod(dnat);
|
|
// Round last digit
|
|
if tail.Cmp(dnat.Div(bignum.Nat(2))) >= 0 {
|
|
dec = dec.Add(bignum.Nat(1));
|
|
}
|
|
// Strip zeros
|
|
ten := bignum.Nat(10);
|
|
for !dec.IsZero() {
|
|
dec2, r2 := dec.DivMod(ten);
|
|
if !r2.IsZero() {
|
|
break;
|
|
}
|
|
dec = dec2;
|
|
}
|
|
out += "." + dec.String();
|
|
return out;
|
|
}
|
|
|
|
func diag(p token.Position, format string, args ...) {
|
|
if p.IsValid() {
|
|
fmt.Printf("%s:%d.%d: ", p.Filename, p.Line, p.Column);
|
|
}
|
|
fmt.Printf(format, args);
|
|
fmt.Print("\n");
|
|
}
|