evalPtr func(f *Frame) Value;
evalFunc func(f *Frame) Func;
evalSlice func(f *Frame) Slice;
+ evalMap func(f *Frame) Map;
evalMulti func(f *Frame) []Value;
+ // Map index expressions permit special forms of assignment,
+ // for which we need to know the Map and key.
+ evalMapValue func(f *Frame) (Map, interface{});
// Evaluate to the "address of" this value; that is, the
// settable Value object. nil for expressions whose address
// cannot be taken.
return a.evalSlice;
}
+func (a *exprCompiler) asMap() (func(f *Frame) Map) {
+ if a.evalMap == nil {
+ log.Crashf("tried to get %v node as MapType", a.t);
+ }
+ return a.evalMap;
+}
+
func (a *exprCompiler) asMulti() (func(f *Frame) []Value) {
if a.evalMulti == nil {
log.Crashf("tried to get %v node as MultiType", a.t);
return a.evalMulti;
}
+func (a *exprCompiler) asInterface() (func(f *Frame) interface {}) {
+ switch _ := a.t.lit().(type) {
+ case *boolType:
+ sf := a.asBool();
+ return func(f *Frame) interface {} { return sf(f) };
+ case *uintType:
+ sf := a.asUint();
+ return func(f *Frame) interface {} { return sf(f) };
+ case *intType:
+ sf := a.asInt();
+ return func(f *Frame) interface {} { return sf(f) };
+ case *floatType:
+ sf := a.asFloat();
+ return func(f *Frame) interface {} { return sf(f) };
+ case *stringType:
+ sf := a.asString();
+ return func(f *Frame) interface {} { return sf(f) };
+ case *PtrType:
+ sf := a.asPtr();
+ return func(f *Frame) interface {} { return sf(f) };
+ case *FuncType:
+ sf := a.asFunc();
+ return func(f *Frame) interface {} { return sf(f) };
+ case *MapType:
+ sf := a.asMap();
+ return func(f *Frame) interface {} { return sf(f) };
+ default:
+ log.Crashf("unexpected expression node type %v at %v", a.t, a.pos);
+ }
+ panic();
+}
+
/*
* Common expression manipulations
*/
rmt *MultiType;
// Whether this is an unpack assignment (case 3).
isUnpack bool;
+ // Whether map special assignment forms are allowed.
+ allowMap bool;
+ // Whether this is a "r, ok = a[x]" assignment.
+ isMapUnpack bool;
// The operation name to use in error messages, such as
// "assignment" or "function call".
errOp string;
return c, ok;
}
+func (a *assignCompiler) allowMapForms(nls int) {
+ a.allowMap = true;
+
+ // Update unpacking info if this is r, ok = a[x]
+ if nls == 2 && len(a.rs) == 1 && a.rs[0].evalMapValue != nil {
+ a.isUnpack = true;
+ a.rmt = NewMultiType([]Type {a.rs[0].t, BoolType});
+ a.isMapUnpack = true;
+ }
+}
+
// compile type checks and compiles an assignment operation, returning
// a function that expects an l-value and the frame in which to
// evaluate the RHS expressions. The l-value must have exactly the
bc := a.rs[0].block;
temp := bc.DefineSlot(a.rmt);
tempIdx := temp.Index;
- rf := a.rs[0].asMulti();
- effect = func(f *Frame) {
- f.Vars[tempIdx] = multiV(rf(f));
- };
+ if a.isMapUnpack {
+ rf := a.rs[0].evalMapValue;
+ vt := a.rmt.Elems[0];
+ effect = func(f *Frame) {
+ m, k := rf(f);
+ v := m.Elem(k);
+ found := boolV(true);
+ if v == nil {
+ found = boolV(false);
+ v = vt.Zero();
+ }
+ f.Vars[tempIdx] = multiV([]Value {v, &found});
+ };
+ } else {
+ rf := a.rs[0].asMulti();
+ effect = func(f *Frame) {
+ f.Vars[tempIdx] = multiV(rf(f));
+ };
+ }
orig := a.rs[0];
a.rs = make([]*exprCompiler, len(a.rmt.Elems));
for i, t := range a.rmt.Elems {
// Now len(a.rs) == len(a.rmt) and we've reduced any unpacking
// to multi-assignment.
- // TODO(austin) Deal with assignment special cases. This is
- // tricky in the unpack case, since some of the conversions
- // can apply to single types within the multi-type.
+ // TODO(austin) Deal with assignment special cases.
// Values of any type may always be assigned to variables of
// compatible static type.
at = Uint8Type;
intIndex = true;
- // TODO(austin) Uncomment when there is a MapType
- // case *MapType:
- // log.Crash("Index into map not implemented");
+ case *MapType:
+ at = lt.Elem;
+ if r.t.isIdeal() {
+ r = r.convertTo(lt.Key);
+ if r == nil {
+ return;
+ }
+ }
+ if !lt.Key.compat(r.t, false) {
+ a.diag("cannot use %s as index into %s", r.t, lt);
+ return;
+ }
default:
a.diag("cannot index into %v", l.t);
}
a.t = at;
+ a.desc = "index expression";
// Compile
switch lt := l.t.lit().(type) {
case *SliceType:
// TODO(austin) Bounds check
+ // TODO(austin) Can this be done with genValue?
a.genIndexSlice(l, r);
lf := l.asSlice();
rf := r.asInt();
return uint64(lf(f)[rf(f)]);
}
+ case *MapType:
+ // TODO(austin) Bounds check
+ lf := l.asMap();
+ rf := r.asInterface();
+ a.genValue(func(f *Frame) Value {
+ m := lf(f);
+ k := rf(f);
+ e := m.Elem(k);
+ if e == nil {
+ // TODO(austin) Use an exception
+ panic("key ", k, " not found in map");
+ }
+ return e;
+ });
+ // genValue makes things addressable, but map values
+ // aren't addressable.
+ a.evalAddr = nil;
+ a.evalMapValue = func(f *Frame) (Map, interface{}) {
+ return lf(f), rf(f);
+ };
+
default:
- log.Crashf("Compilation of index into %T not implemented", l.t);
+ log.Crashf("unexpected left operand type %T", l.t.lit());
}
}
}
// Useful type predicates
+ // TODO(austin) CL 33668 mandates identical types except for comparisons.
compat := func() bool {
return l.t.compat(r.t, false);
};
case *SliceType:
val := v.(SliceValue).Get();
a.evalSlice = func(f *Frame) Slice { return val };
+ case *MapType:
+ val := v.(MapValue).Get();
+ a.evalMap = func(f *Frame) Map { return val };
default:
log.Crashf("unexpected constant type %v at %v", a.t, a.pos);
}
a.evalFunc = func(f *Frame) Func { return f.Get(level, index).(FuncValue).Get() };
case *SliceType:
a.evalSlice = func(f *Frame) Slice { return f.Get(level, index).(SliceValue).Get() };
+ case *MapType:
+ a.evalMap = func(f *Frame) Map { return f.Get(level, index).(MapValue).Get() };
default:
log.Crashf("unexpected identifier type %v at %v", a.t, a.pos);
}
a.evalFunc = func(f *Frame) Func { return lf(f).Elem(rf(f)).(FuncValue).Get() };
case *SliceType:
a.evalSlice = func(f *Frame) Slice { return lf(f).Elem(rf(f)).(SliceValue).Get() };
+ case *MapType:
+ a.evalMap = func(f *Frame) Map { return lf(f).Elem(rf(f)).(MapValue).Get() };
default:
log.Crashf("unexpected result type %v at %v", a.t, a.pos);
}
a.evalFunc = func(f *Frame) Func { return lf(f).Base.Elem(rf(f)).(FuncValue).Get() };
case *SliceType:
a.evalSlice = func(f *Frame) Slice { return lf(f).Base.Elem(rf(f)).(SliceValue).Get() };
+ case *MapType:
+ a.evalMap = func(f *Frame) Map { return lf(f).Base.Elem(rf(f)).(MapValue).Get() };
default:
log.Crashf("unexpected result type %v at %v", a.t, a.pos);
}
a.evalFunc = func(f *Frame) Func { return call(f)[0].(FuncValue).Get() };
case *SliceType:
a.evalSlice = func(f *Frame) Slice { return call(f)[0].(SliceValue).Get() };
+ case *MapType:
+ a.evalMap = func(f *Frame) Map { return call(f)[0].(MapValue).Get() };
case *MultiType:
a.evalMulti = func(f *Frame) []Value { return call(f) };
default:
a.evalFunc = func(f *Frame) Func { return vf(f).(FuncValue).Get() };
case *SliceType:
a.evalSlice = func(f *Frame) Slice { return vf(f).(SliceValue).Get() };
+ case *MapType:
+ a.evalMap = func(f *Frame) Map { return vf(f).(MapValue).Get() };
default:
log.Crashf("unexpected result type %v at %v", a.t, a.pos);
}
lf := l.asFunc();
rf := r.asFunc();
a.evalBool = func(f *Frame) bool { return lf(f) == rf(f) };
+ case *MapType:
+ lf := l.asMap();
+ rf := r.asMap();
+ a.evalBool = func(f *Frame) bool { return lf(f) == rf(f) };
default:
log.Crashf("unexpected left operand type %v at %v", l.t, a.pos);
}
lf := l.asFunc();
rf := r.asFunc();
a.evalBool = func(f *Frame) bool { return lf(f) != rf(f) };
+ case *MapType:
+ lf := l.asMap();
+ rf := r.asMap();
+ a.evalBool = func(f *Frame) bool { return lf(f) != rf(f) };
default:
log.Crashf("unexpected left operand type %v at %v", l.t, a.pos);
}
case *SliceType:
rf := r.asSlice();
return func(lv Value, f *Frame) { lv.(SliceValue).Set(rf(f)) };
+ case *MapType:
+ rf := r.asMap();
+ return func(lv Value, f *Frame) { lv.(MapValue).Set(rf(f)) };
default:
log.Crashf("unexpected left operand type %v at %v", lt, r.pos);
}
commonType;
}
-var BoolType = universe.DefineType("bool", universePos, &boolType{});
+var BoolType = universe.DefineType("bool", universePos, &boolType{})
func (t *boolType) compat(o Type, conv bool) bool {
t2, ok := o.lit().(*boolType);
panic("unexpected float bit count: ", t.Bits);
}
-var maxFloat32Val = bignum.MakeRat(bignum.Int(0xffffff).Shl(127-23), bignum.Nat(1));
-var maxFloat64Val = bignum.MakeRat(bignum.Int(0x1fffffffffffff).Shl(1023-52), bignum.Nat(1));
-var minFloat32Val = maxFloat32Val.Neg();
-var minFloat64Val = maxFloat64Val.Neg();
+var maxFloat32Val = bignum.MakeRat(bignum.Int(0xffffff).Shl(127-23), bignum.Nat(1))
+var maxFloat64Val = bignum.MakeRat(bignum.Int(0x1fffffffffffff).Shl(1023-52), bignum.Nat(1))
+var minFloat32Val = maxFloat32Val.Neg()
+var minFloat64Val = maxFloat64Val.Neg()
func (t *floatType) minVal() *bignum.Rational {
bits := t.Bits;
commonType;
}
-var StringType = universe.DefineType("string", universePos, &stringType{});
+var StringType = universe.DefineType("string", universePos, &stringType{})
func (t *stringType) compat(o Type, conv bool) bool {
t2, ok := o.lit().(*stringType);
Elem Type;
}
-var arrayTypes = make(map[int64] map[Type] *ArrayType);
+var arrayTypes = make(map[int64] map[Type] *ArrayType)
// Two array types are identical if they have identical element types
// and the same array length.
Out []Type;
}
-var funcTypes = newTypeArrayMap();
-var variadicFuncTypes = newTypeArrayMap();
+var funcTypes = newTypeArrayMap()
+var variadicFuncTypes = newTypeArrayMap()
// Two function types are identical if they have the same number of
// parameters and result values and if corresponding parameter and
}
/*
+ * Map type
+ */
+
type MapType struct {
- // TODO(austin)
+ commonType;
+ Key Type;
+ Elem Type;
+}
+
+var mapTypes = make(map[Type] map[Type] *MapType)
+
+func NewMapType(key Type, elem Type) *MapType {
+ ts, ok := mapTypes[key];
+ if !ok {
+ ts = make(map[Type] *MapType);
+ mapTypes[key] = ts;
+ }
+ t, ok := ts[elem];
+ if !ok {
+ t = &MapType{commonType{}, key, elem};
+ ts[elem] = t;
+ }
+ return t;
+}
+
+func (t *MapType) compat(o Type, conv bool) bool {
+ t2, ok := o.lit().(*MapType);
+ if !ok {
+ return false;
+ }
+ return t.Elem.compat(t2.Elem, conv) && t.Key.compat(t2.Key, conv);
+}
+
+func (t *MapType) lit() Type {
+ return t;
+}
+
+func (t *MapType) String() string {
+ return "map[" + t.Key.String() + "] " + t.Elem.String();
}
+func (t *MapType) Zero() Value {
+ return &mapV{nil};
+}
+
+/*
type ChanType struct {
// TODO(austin)
}
return true;
}
-var EmptyType Type = NewMultiType([]Type{});
+var EmptyType Type = NewMultiType([]Type{})
func (t *MultiType) lit() Type {
return t;