gofmt-ify eval

R=rsc
http://go/go-review/1016054
This commit is contained in:
Robert Griesemer 2009-11-05 14:41:56 -08:00
parent d2af73136e
commit 9e48df682c
17 changed files with 1060 additions and 977 deletions

View File

@ -43,11 +43,12 @@ func newUniverse() *Scope {
offset: 0,
scope: sc,
global: true,
defs: make(map[string] Def)
defs: make(map[string]Def),
};
return sc;
}
var universe *Scope = newUniverse();
var universe *Scope = newUniverse()
// TODO(austin) These can all go in stmt.go now

View File

@ -16,6 +16,7 @@ import (
// Print each statement or expression before parsing it
var noisy = false
func init() {
flag.BoolVar(&noisy, "noisy", false, "chatter during eval tests");
}
@ -104,37 +105,37 @@ func match(t *testing.T, err os.Error, pat string) bool {
// Expression compile error
func CErr(expr string, cerr string) test {
return test([]job{job{code: expr, cerr: cerr}})
return test([]job{job{code: expr, cerr: cerr}});
}
// Expression runtime error
func RErr(expr string, rterr string) test {
return test([]job{job{code: expr, rterr: rterr}})
return test([]job{job{code: expr, rterr: rterr}});
}
// Expression value
func Val(expr string, val interface{}) test {
return test([]job{job{code: expr, val: toValue(val)}})
return test([]job{job{code: expr, val: toValue(val)}});
}
// Statement runs without error
func Run(stmts string) test {
return test([]job{job{code: stmts, noval: true}})
return test([]job{job{code: stmts, noval: true}});
}
// Two statements without error.
// TODO(rsc): Should be possible with Run but the parser
// won't let us do both top-level and non-top-level statements.
func Run2(stmt1, stmt2 string) test {
return test([]job{job{code: stmt1, noval: true}, job{code: stmt2, noval: true}})
return test([]job{job{code: stmt1, noval: true}, job{code: stmt2, noval: true}});
}
// Statement runs and test one expression's value
func Val1(stmts string, expr1 string, val1 interface{}) test {
return test([]job{
job{code: stmts, noval: true},
job{code: expr1, val: toValue(val1)}
})
job{code: expr1, val: toValue(val1)},
});
}
// Statement runs and test two expressions' values
@ -142,8 +143,8 @@ func Val2(stmts string, expr1 string, val1 interface{}, expr2 string, val2 inter
return test([]job{
job{code: stmts, noval: true},
job{code: expr1, val: toValue(val1)},
job{code: expr2, val: toValue(val2)}
})
job{code: expr2, val: toValue(val2)},
});
}
/*
@ -210,7 +211,7 @@ func toValue(val interface{}) Value {
* Default test scope
*/
type testFunc struct {};
type testFunc struct{}
func (*testFunc) NewFrame() *Frame {
return &Frame{nil, &[2]Value{}};
@ -224,7 +225,7 @@ func (*testFunc) Call(t *Thread) {
t.f.Vars[1].(IntValue).Set(t, res);
}
type oneTwoFunc struct {};
type oneTwoFunc struct{}
func (*oneTwoFunc) NewFrame() *Frame {
return &Frame{nil, &[2]Value{}};
@ -235,7 +236,7 @@ func (*oneTwoFunc) Call(t *Thread) {
t.f.Vars[1].(IntValue).Set(t, 2);
}
type voidFunc struct {};
type voidFunc struct{}
func (*voidFunc) NewFrame() *Frame {
return &Frame{nil, []Value{}};
@ -247,9 +248,7 @@ func (*voidFunc) Call(t *Thread) {
func newTestWorld() *World {
w := NewWorld();
def := func(name string, t Type, val interface{}) {
w.DefineVar(name, t, toValue(val));
};
def := func(name string, t Type, val interface{}) { w.DefineVar(name, t, toValue(val)) };
w.DefineConst("c", IdealIntType, toValue(bignum.Int(1)));
def("i", IntType, 1);

View File

@ -175,9 +175,7 @@ func (a *expr) convertToInt(max int64, negErr string, errOp string) *expr {
// Convert to int
na := a.newExpr(IntType, a.desc);
af := a.asUint();
na.eval = func(t *Thread) int64 {
return int64(af(t));
};
na.eval = func(t *Thread) int64 { return int64(af(t)) };
return na;
case *intType:
@ -359,9 +357,7 @@ func (a *assignCompiler) compile(b *block, lt Type) (func(Value, *Thread)) {
};
} else {
rf := a.rs[0].asMulti();
effect = func(t *Thread) {
t.f.Vars[tempIdx] = multiV(rf(t));
};
effect = func(t *Thread) { t.f.Vars[tempIdx] = multiV(rf(t)) };
}
orig := a.rs[0];
a.rs = make([]*expr, len(a.rmt.Elems));
@ -911,9 +907,7 @@ func (a *exprInfo) compileSelectorExpr(v *expr, name string) *expr {
}
expr := a.newExpr(ft, "selector expression");
pf := parent.asStruct();
evalAddr := func(t *Thread) Value {
return pf(t).Field(t, index);
};
evalAddr := func(t *Thread) Value { return pf(t).Field(t, index) };
expr.genValue(evalAddr);
return sub(expr);
};
@ -983,7 +977,7 @@ func (a *exprInfo) compileSliceExpr(arr, lo, hi *expr) *expr {
if lo > hi || hi > bound || lo < 0 {
t.Abort(SliceError{lo, hi, bound});
}
return Slice{arr.Sub(lo, bound - lo), hi - lo, bound - lo}
return Slice{arr.Sub(lo, bound-lo), hi-lo, bound-lo};
};
case *SliceType:
@ -993,7 +987,7 @@ func (a *exprInfo) compileSliceExpr(arr, lo, hi *expr) *expr {
if lo > hi || hi > arr.Cap || lo < 0 {
t.Abort(SliceError{lo, hi, arr.Cap});
}
return Slice{arr.Base.Sub(lo, arr.Cap - lo), hi - lo, arr.Cap - lo}
return Slice{arr.Base.Sub(lo, arr.Cap - lo), hi-lo, arr.Cap - lo};
};
case *stringType:
@ -1007,7 +1001,7 @@ func (a *exprInfo) compileSliceExpr(arr, lo, hi *expr) *expr {
t.Abort(SliceError{lo, hi, int64(len(arr))});
}
return arr[lo:hi];
}
};
default:
log.Crashf("unexpected left operand type %T", arr.t.lit());
@ -1108,7 +1102,7 @@ func (a *exprInfo) compileIndexExpr(l, r *expr) *expr {
t.Abort(IndexError{r, int64(len(l))});
}
return uint64(l[r]);
}
};
case *MapType:
lf := l.asMap();
@ -1233,15 +1227,11 @@ func (a *exprInfo) compileBuiltinCallExpr(b *block, ft *FuncType, as []*expr) *e
// TODO(austin) It would be nice if this could
// be a constant int.
v := t.Len;
expr.eval = func(t *Thread) int64 {
return v;
};
expr.eval = func(t *Thread) int64 { return v };
case *SliceType:
vf := arg.asSlice();
expr.eval = func(t *Thread) int64 {
return vf(t).Cap;
};
expr.eval = func(t *Thread) int64 { return vf(t).Cap };
//case *ChanType:
@ -1260,23 +1250,17 @@ func (a *exprInfo) compileBuiltinCallExpr(b *block, ft *FuncType, as []*expr) *e
switch t := arg.t.lit().(type) {
case *stringType:
vf := arg.asString();
expr.eval = func(t *Thread) int64 {
return int64(len(vf(t)));
};
expr.eval = func(t *Thread) int64 { return int64(len(vf(t))) };
case *ArrayType:
// TODO(austin) It would be nice if this could
// be a constant int.
v := t.Len;
expr.eval = func(t *Thread) int64 {
return v;
};
expr.eval = func(t *Thread) int64 { return v };
case *SliceType:
vf := arg.asSlice();
expr.eval = func(t *Thread) int64 {
return vf(t).Len;
};
expr.eval = func(t *Thread) int64 { return vf(t).Len };
case *MapType:
vf := arg.asMap();
@ -1398,9 +1382,7 @@ func (a *exprInfo) compileBuiltinCallExpr(b *block, ft *FuncType, as []*expr) *e
t := as[0].valType;
expr := a.newExpr(NewPtrType(t), "new");
expr.eval = func(*Thread) Value {
return t.Zero();
};
expr.eval = func(*Thread) Value { return t.Zero() };
return expr;
case panicType, paniclnType, printType, printlnType:
@ -1416,7 +1398,9 @@ func (a *exprInfo) compileBuiltinCallExpr(b *block, ft *FuncType, as []*expr) *e
print(" ");
}
v := eval(t);
type stringer interface { String() string }
type stringer interface {
String() string;
}
switch v1 := v.(type) {
case bool:
print(v1);
@ -1444,7 +1428,7 @@ func (a *exprInfo) compileBuiltinCallExpr(b *block, ft *FuncType, as []*expr) *e
expr.exec = func(t *Thread) {
printer(t);
t.Abort(os.NewError("panic"));
}
};
}
return expr;
}
@ -1607,15 +1591,11 @@ func (a *exprInfo) compileBinaryExpr(op token.Token, l, r *expr) *expr {
// Useful type predicates
// TODO(austin) CL 33668 mandates identical types except for comparisons.
compat := func() bool {
return l.t.compat(r.t, false);
};
compat := func() bool { return l.t.compat(r.t, false) };
integers := func() bool {
return l.t.isInteger() && r.t.isInteger();
};
floats := func() bool {
return l.t.isFloat() && r.t.isFloat();
};
floats := func() bool { return l.t.isFloat() && r.t.isFloat() };
strings := func() bool {
// TODO(austin) Deal with named types
return l.t == StringType && r.t == StringType;

File diff suppressed because it is too large Load Diff

View File

@ -22,7 +22,7 @@ var implLimit = "implementation limit"
var mustBeUnsigned = "must be unsigned"
var divByZero = "divide by zero"
var hugeInteger = bignum.Int(1).Shl(64);
var hugeInteger = bignum.Int(1).Shl(64)
var exprTests = []test{
Val("i", 1),

View File

@ -40,14 +40,14 @@ type Type struct {
var (
boolType = &Type{Repr: "*boolType", Value: "BoolValue", Native: "bool", As: "asBool"};
uintType = &Type{Repr: "*uintType", Value: "UintValue", Native: "uint64", As: "asUint",
Sizes: []Size{ Size{8, "uint8"}, Size{16, "uint16"}, Size{32, "uint32"}, Size{64, "uint64"}, Size{0, "uint"}}
Sizes: []Size{Size{8, "uint8"}, Size{16, "uint16"}, Size{32, "uint32"}, Size{64, "uint64"}, Size{0, "uint"}},
};
intType = &Type{Repr: "*intType", Value: "IntValue", Native: "int64", As: "asInt",
Sizes: []Size{Size{8, "int8"}, Size{16, "int16"}, Size{32, "int32"}, Size{64, "int64"}, Size{0, "int"}}
Sizes: []Size{Size{8, "int8"}, Size{16, "int16"}, Size{32, "int32"}, Size{64, "int64"}, Size{0, "int"}},
};
idealIntType = &Type{Repr: "*idealIntType", Value: "IdealIntValue", Native: "*bignum.Integer", As: "asIdealInt", IsIdeal: true};
floatType = &Type{Repr: "*floatType", Value: "FloatValue", Native: "float64", As: "asFloat",
Sizes: []Size{Size{32, "float32"}, Size{64, "float64"}, Size{0, "float"}}
Sizes: []Size{Size{32, "float32"}, Size{64, "float64"}, Size{0, "float"}},
};
idealFloatType = &Type{Repr: "*idealFloatType", Value: "IdealFloatValue", Native: "*bignum.Rational", As: "asIdealFloat", IsIdeal: true};
stringType = &Type{Repr: "*stringType", Value: "StringValue", Native: "string", As: "asString"};
@ -117,10 +117,10 @@ var binOps = []Op{
Op{Name: "Xor", Expr: "l ^ r", ConstExpr: "l.Xor(r)", Types: integers},
Op{Name: "AndNot", Expr: "l &^ r", ConstExpr: "l.AndNot(r)", Types: integers},
Op{Name: "Shl", Expr: "l << r", ConstExpr: "l.Shl(uint(r.Value()))",
AsRightName: "asUint", Types: shiftable
AsRightName: "asUint", Types: shiftable,
},
Op{Name: "Shr", Expr: "l >> r", ConstExpr: "l.Shr(uint(r.Value()))",
AsRightName: "asUint", Types: shiftable
AsRightName: "asUint", Types: shiftable,
},
Op{Name: "Lss", Expr: "l < r", ConstExpr: "l.Cmp(r) < 0", ReturnType: "bool", Types: addable},
Op{Name: "Gtr", Expr: "l > r", ConstExpr: "l.Cmp(r) > 0", ReturnType: "bool", Types: addable},

View File

@ -14,7 +14,7 @@ import (
"os";
)
var filename = flag.String("f", "", "file to run");
var filename = flag.String("f", "", "file to run")
func main() {
flag.Parse();
@ -89,4 +89,3 @@ func main() {
}
}
}

View File

@ -122,7 +122,7 @@ func (b *block) DefineVar(name string, pos token.Position, t Type) (*Variable, D
}
func (b *block) DefineTemp(t Type) *Variable {
return b.defineSlot(t, true)
return b.defineSlot(t, true);
}
func (b *block) defineSlot(t Type, temp bool) *Variable {

View File

@ -228,9 +228,7 @@ func (a *stmtCompiler) defineVar(ident *ast.Ident, t Type) *Variable {
// Initialize the variable
index := v.Index;
if v.Index >= 0 {
a.push(func(v *Thread) {
v.f.Vars[index] = t.Zero();
});
a.push(func(v *Thread) { v.f.Vars[index] = t.Zero() });
}
return v;
}
@ -979,9 +977,7 @@ func (a *stmtCompiler) compileIfStmt(s *ast.IfStmt) {
if s.Else != nil {
// Skip over else if we executed the body
a.flow.put1(false, &endPC);
a.push(func(v *Thread) {
v.pc = endPC;
});
a.push(func(v *Thread) { v.pc = endPC });
elsePC = a.nextPC();
bc.compileStmt(s.Else);
} else {

View File

@ -6,7 +6,7 @@ package eval
import "testing"
var atLeastOneDecl = "at least one new variable must be declared";
var atLeastOneDecl = "at least one new variable must be declared"
var stmtTests = []test{
// Short declarations

View File

@ -65,7 +65,7 @@ type BoundedType interface {
maxVal() *bignum.Rational;
}
var universePos = token.Position{"<universe>", 0, 0, 0};
var universePos = token.Position{"<universe>", 0, 0, 0}
/*
* Type array maps. These are used to memoize composite types.
@ -96,7 +96,7 @@ func newTypeArrayMap() typeArrayMap {
return make(map[uintptr]*typeArrayMapEntry);
}
func (m typeArrayMap) Get(key []Type) (interface{}) {
func (m typeArrayMap) Get(key []Type) interface{} {
ent, ok := m[hashTypeArray(key)];
if !ok {
return nil;
@ -132,8 +132,7 @@ func (m typeArrayMap) Put(key []Type, v interface{}) interface{} {
* Common type
*/
type commonType struct {
}
type commonType struct{}
func (commonType) isBoolean() bool {
return false;
@ -216,7 +215,8 @@ var (
func (t *uintType) compat(o Type, conv bool) bool {
t2, ok := o.lit().(*uintType);
return ok && t == t2;;
return ok && t == t2;
;
}
func (t *uintType) lit() Type {
@ -483,7 +483,7 @@ type idealFloatType struct {
commonType;
}
var IdealFloatType Type = &idealFloatType{};
var IdealFloatType Type = &idealFloatType{}
func (t *idealFloatType) compat(o Type, conv bool) bool {
_, ok := o.lit().(*idealFloatType);
@ -673,9 +673,9 @@ func (t *StructType) compat(o Type, conv bool) bool {
e2 := t2.Elems[i];
// XXX(Spec) An anonymous and a non-anonymous field
// are neither identical nor compatible.
if (e.Anonymous != e2.Anonymous ||
if e.Anonymous != e2.Anonymous ||
(!e.Anonymous && e.Name != e2.Name) ||
!e.Type.compat(e2.Type, conv)) {
!e.Type.compat(e2.Type, conv) {
return false;
}
}

View File

@ -22,7 +22,7 @@ type typeCompiler struct {
//
// TODO(austin) This will probably have to change after we
// eliminate forward declarations.
lateCheck func() bool
lateCheck func() bool;
}
func (a *typeCompiler) compileIdent(x *ast.Ident, allowRec bool) Type {

View File

@ -20,7 +20,7 @@ type World struct {
frame *Frame;
}
func NewWorld() (*World) {
func NewWorld() *World {
w := new(World);
w.scope = universe.ChildScope();
w.scope.global = true; // this block's vars allocate directly
@ -187,4 +187,3 @@ func (w *World) DefineVar(name string, t Type, val Value) os.Error {
v.Init = val;
return nil;
}