Commit 79fac7ca authored by Austin Clements's avatar Austin Clements

Implement array types and index expressions.

Some cleanup.  Elem() on PtrType is now just Elem and matches
with ArrayType.  Generators now switch over the result type
instead of the operand type.  Delete unused diag function.

R=rsc
APPROVED=rsc
DELTA=281  (219 added, 18 deleted, 44 changed)
OCL=31876
CL=31891
parent 1d51978f
......@@ -90,6 +90,15 @@ type StringValue interface {
Set(string);
}
type ArrayValue interface {
Value;
// TODO(austin) Get() is here for uniformity, but is
// completely useless. If a lot of other types have similarly
// useless Get methods, just special-case these uses.
Get() ArrayValue;
Elem(i int64) Value;
}
type PtrValue interface {
Value;
Get() Value;
......
This diff is collapsed.
......@@ -300,12 +300,47 @@ func (t *stringType) String() string {
func (t *stringType) value(v string) StringValue
/*
type ArrayType struct {
commonType;
elem Type;
Len int64;
Elem Type;
lit Type;
}
var arrayTypes = make(map[int64] map[Type] *ArrayType);
func NewArrayType(len int64, elem Type) *ArrayType {
ts, ok := arrayTypes[len];
if !ok {
ts = make(map[Type] *ArrayType);
arrayTypes[len] = ts;
}
t, ok := ts[elem];
if !ok {
t = &ArrayType{commonType{}, len, elem, nil};
ts[elem] = t;
}
return t;
}
func (t *ArrayType) literal() Type {
if t.lit == nil {
t.lit = NewArrayType(t.Len, t.Elem.literal());
}
return t.lit;
}
func (t *ArrayType) compatible(o Type) bool {
return t.literal() == o.literal();
}
func (t *ArrayType) String() string {
return "[]" + t.Elem.String();
}
func (t *ArrayType) value(v []Value) ArrayValue
/*
func (t *ArrayType) literal() Type {
// TODO(austin)
}
......@@ -318,7 +353,7 @@ type StructType struct {
type PtrType struct {
commonType;
elem Type;
Elem Type;
lit Type;
}
......@@ -333,13 +368,9 @@ func NewPtrType(elem Type) *PtrType {
return t;
}
func (t *PtrType) Elem() Type {
return t.elem;
}
func (t *PtrType) literal() Type {
if t.lit == nil {
t.lit = NewPtrType(t.elem.literal());
t.lit = NewPtrType(t.Elem.literal());
}
return t.lit;
}
......@@ -349,7 +380,7 @@ func (t *PtrType) compatible(o Type) bool {
}
func (t *PtrType) String() string {
return "*" + t.elem.String();
return "*" + t.Elem.String();
}
func (t *PtrType) value(v Value) PtrValue
......
......@@ -40,11 +40,3 @@ func ratToString(rat *bignum.Rational) string {
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");
}
......@@ -441,6 +441,33 @@ func (t *stringType) value(v string) StringValue {
return &res;
}
/*
* Array
*/
type arrayV []Value
func (*arrayV) Type() Type {
panic("Not implemented");
}
func (v *arrayV) String() string {
return fmt.Sprint(*v);
}
func (v *arrayV) Get() ArrayValue {
return v;
}
func (v *arrayV) Elem(i int64) Value {
return (*v)[i];
}
func (t *ArrayType) value(v []Value) ArrayValue {
res := arrayV(v);
return &res;
}
/*
* Pointer
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment