Added array type
This commit is contained in:
18
src/types/Array.go
Normal file
18
src/types/Array.go
Normal file
@ -0,0 +1,18 @@
|
||||
package types
|
||||
|
||||
var String = &Array{Of: Int8}
|
||||
|
||||
// Array is the address of an object.
|
||||
type Array struct {
|
||||
Of Type
|
||||
}
|
||||
|
||||
// Name returns the type name.
|
||||
func (a *Array) Name() string {
|
||||
return "[]" + a.Of.Name()
|
||||
}
|
||||
|
||||
// Size returns the total size in bytes.
|
||||
func (a *Array) Size() int {
|
||||
return 8
|
||||
}
|
@ -17,6 +17,17 @@ func ByName(name string, pkg string, structs map[string]*Struct) Type {
|
||||
return nil
|
||||
}
|
||||
|
||||
if strings.HasPrefix(name, "[]") {
|
||||
to := strings.TrimPrefix(name, "[]")
|
||||
typ := ByName(to, pkg, structs)
|
||||
|
||||
if typ != nil {
|
||||
return &Array{Of: typ}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
switch name {
|
||||
case "Int":
|
||||
return Int
|
||||
@ -35,7 +46,7 @@ func ByName(name string, pkg string, structs map[string]*Struct) Type {
|
||||
case "Float32":
|
||||
return Float32
|
||||
case "Pointer":
|
||||
return PointerAny
|
||||
return AnyPointer
|
||||
}
|
||||
|
||||
typ, exists := structs[pkg+"."+name]
|
||||
|
@ -17,5 +17,17 @@ func Is(a Type, b Type) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
aArray, aIsArray := a.(*Array)
|
||||
|
||||
if aIsArray && bIsPointer && (bPointer.To == nil || aArray.Of == bPointer.To) {
|
||||
return true
|
||||
}
|
||||
|
||||
bArray, bIsArray := b.(*Array)
|
||||
|
||||
if aIsArray && bIsArray && aArray.Of == bArray.Of {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
package types
|
||||
|
||||
var PointerAny = &Pointer{To: nil}
|
||||
var AnyPointer = &Pointer{To: nil}
|
||||
|
||||
// Pointer is the address of an object.
|
||||
type Pointer struct {
|
||||
@ -13,7 +13,7 @@ func (p *Pointer) Name() string {
|
||||
return "Pointer"
|
||||
}
|
||||
|
||||
return "Pointer:" + p.To.Name()
|
||||
return "*" + p.To.Name()
|
||||
}
|
||||
|
||||
// Size returns the total size in bytes.
|
||||
|
@ -9,8 +9,10 @@ import (
|
||||
|
||||
func TestName(t *testing.T) {
|
||||
assert.Equal(t, types.Int.Name(), "Int64")
|
||||
assert.Equal(t, types.PointerAny.Name(), "Pointer")
|
||||
assert.Equal(t, (&types.Pointer{To: types.Int}).Name(), "Pointer:Int64")
|
||||
assert.Equal(t, types.AnyPointer.Name(), "Pointer")
|
||||
assert.Equal(t, (&types.Pointer{To: types.Int}).Name(), "*Int64")
|
||||
assert.Equal(t, (&types.Array{Of: types.Int}).Name(), "[]Int64")
|
||||
assert.Equal(t, types.String.Name(), "[]Int8")
|
||||
}
|
||||
|
||||
func TestSize(t *testing.T) {
|
||||
@ -19,7 +21,8 @@ func TestSize(t *testing.T) {
|
||||
assert.Equal(t, types.Int16.Size(), 2)
|
||||
assert.Equal(t, types.Int32.Size(), 4)
|
||||
assert.Equal(t, types.Int64.Size(), 8)
|
||||
assert.Equal(t, types.PointerAny.Size(), 8)
|
||||
assert.Equal(t, types.AnyPointer.Size(), 8)
|
||||
assert.Equal(t, types.String.Size(), 8)
|
||||
assert.Equal(t, (&types.Pointer{To: types.Int}).Size(), 8)
|
||||
}
|
||||
|
||||
@ -37,7 +40,7 @@ func TestStruct(t *testing.T) {
|
||||
|
||||
func TestBasics(t *testing.T) {
|
||||
assert.True(t, types.Is(types.Int, types.Int))
|
||||
assert.True(t, types.Is(types.PointerAny, types.PointerAny))
|
||||
assert.True(t, types.Is(types.AnyPointer, types.AnyPointer))
|
||||
assert.False(t, types.Is(types.Int, types.Float))
|
||||
assert.False(t, types.Is(&types.Pointer{To: types.Int}, &types.Pointer{To: types.Float}))
|
||||
}
|
||||
@ -50,6 +53,7 @@ func TestSpecialCases(t *testing.T) {
|
||||
|
||||
// Case #2:
|
||||
// A pointer pointing to a known type fulfills the requirement of a pointer to anything.
|
||||
assert.True(t, types.Is(&types.Pointer{To: types.Int}, &types.Pointer{To: nil}))
|
||||
assert.True(t, types.Is(&types.Pointer{To: types.Float}, &types.Pointer{To: nil}))
|
||||
assert.True(t, types.Is(&types.Pointer{To: types.Int}, types.AnyPointer))
|
||||
assert.True(t, types.Is(&types.Pointer{To: types.Float}, types.AnyPointer))
|
||||
assert.True(t, types.Is(&types.Array{Of: types.Int}, types.AnyPointer))
|
||||
}
|
||||
|
Reference in New Issue
Block a user