Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dsl/types: add types.Array and types.Slice #213

Merged
merged 1 commit into from
Feb 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions dsl/types/ext.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
package types

// AsArray is a type-assert like operation, x.(*Array), but never panics.
// Returns nil if type is not an array.
func AsArray(x Type) *Array { return nil }

// AsSlice is a type-assert like operation, x.(*Slice), but never panics.
// Returns nil if type is not an array.
func AsSlice(x Type) *Slice { return nil }

// AsPointer is a type-assert like operation, x.(*Pointer), but never panics.
// Returns nil if type is not a pointer.
func AsPointer(x Type) *Pointer { return nil }
Expand Down
4 changes: 4 additions & 0 deletions dsl/types/type_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ package types
//
// Nothing interesting here, hence it's moved to a separate file.

func (*Array) String() string { return "" }
func (*Slice) String() string { return "" }
func (*Pointer) String() string { return "" }
func (*Interface) String() string { return "" }

func (*Array) Underlying() Type { return nil }
func (*Slice) Underlying() Type { return nil }
func (*Pointer) Underlying() Type { return nil }
func (*Interface) Underlying() Type { return nil }
23 changes: 23 additions & 0 deletions dsl/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,36 @@ type Type interface {
}

type (
// An Array represents an array type.
Array struct{}

// A Slice represents a slice type.
Slice struct{}

// A Pointer represents a pointer type.
Pointer struct{}

// An Interface represents an interface type.
Interface struct{}
)

// NewArray returns a new array type for the given element type and length.
// A negative length indicates an unknown length.
func NewArray(elem Type, len int) *Array { return nil }

// Elem returns element type of array.
func (*Array) Elem() Type { return nil }

// NewSlice returns a new slice type for the given element type.
func NewSlice(elem Type) *Slice { return nil }

// Elem returns element type of slice.
func (*Slice) Elem() Type { return nil }

// Len returns the length of array.
// A negative result indicates an unknown length.
func (*Array) Len() int { return 0 }

// NewPointer returns a new pointer type for the given element (base) type.
func NewPointer(elem Type) *Pointer { return nil }

Expand Down
76 changes: 76 additions & 0 deletions ruleguard/libdsl.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ func initEnv(state *engineState, env *quasigo.Env) {
`github.com/quasilyte/go-ruleguard/dsl/types.Type`: dslTypesType{},
`*github.com/quasilyte/go-ruleguard/dsl/types.Interface`: dslTypesInterface{},
`*github.com/quasilyte/go-ruleguard/dsl/types.Pointer`: dslTypesPointer{},
`*github.com/quasilyte/go-ruleguard/dsl/types.Array`: dslTypesArray{},
`*github.com/quasilyte/go-ruleguard/dsl/types.Slice`: dslTypesSlice{},
}

for qualifier, typ := range nativeTypes {
Expand Down Expand Up @@ -89,6 +91,55 @@ func (dslTypesInterface) String(stack *quasigo.ValueStack) {
stack.Push(stack.Pop().(*types.Interface).String())
}

type dslTypesSlice struct{}

func (native dslTypesSlice) funcs() map[string]func(*quasigo.ValueStack) {
return map[string]func(*quasigo.ValueStack){
"Underlying": native.Underlying,
"String": native.String,
"Elem": native.Elem,
}
}

func (dslTypesSlice) Underlying(stack *quasigo.ValueStack) {
stack.Push(stack.Pop().(*types.Slice).Underlying())
}

func (dslTypesSlice) String(stack *quasigo.ValueStack) {
stack.Push(stack.Pop().(*types.Slice).String())
}

func (dslTypesSlice) Elem(stack *quasigo.ValueStack) {
stack.Push(stack.Pop().(*types.Slice).Elem())
}

type dslTypesArray struct{}

func (native dslTypesArray) funcs() map[string]func(*quasigo.ValueStack) {
return map[string]func(*quasigo.ValueStack){
"Underlying": native.Underlying,
"String": native.String,
"Elem": native.Elem,
"Len": native.Len,
}
}

func (dslTypesArray) Underlying(stack *quasigo.ValueStack) {
stack.Push(stack.Pop().(*types.Array).Underlying())
}

func (dslTypesArray) String(stack *quasigo.ValueStack) {
stack.Push(stack.Pop().(*types.Array).String())
}

func (dslTypesArray) Elem(stack *quasigo.ValueStack) {
stack.Push(stack.Pop().(*types.Array).Elem())
}

func (dslTypesArray) Len(stack *quasigo.ValueStack) {
stack.PushInt(int(stack.Pop().(*types.Array).Len()))
}

type dslTypesPointer struct{}

func (native dslTypesPointer) funcs() map[string]func(*quasigo.ValueStack) {
Expand Down Expand Up @@ -117,7 +168,11 @@ func (native dslTypesPackage) funcs() map[string]func(*quasigo.ValueStack) {
return map[string]func(*quasigo.ValueStack){
"Implements": native.Implements,
"Identical": native.Identical,
"NewArray": native.NewArray,
"NewSlice": native.NewSlice,
"NewPointer": native.NewPointer,
"AsArray": native.AsArray,
"AsSlice": native.AsSlice,
"AsPointer": native.AsPointer,
"AsInterface": native.AsInterface,
}
Expand All @@ -135,11 +190,32 @@ func (dslTypesPackage) Identical(stack *quasigo.ValueStack) {
stack.Push(xtypes.Identical(x, y))
}

func (dslTypesPackage) NewArray(stack *quasigo.ValueStack) {
length := stack.PopInt()
typ := stack.Pop().(types.Type)
stack.Push(types.NewArray(typ, int64(length)))
}

func (dslTypesPackage) NewSlice(stack *quasigo.ValueStack) {
typ := stack.Pop().(types.Type)
stack.Push(types.NewSlice(typ))
}

func (dslTypesPackage) NewPointer(stack *quasigo.ValueStack) {
typ := stack.Pop().(types.Type)
stack.Push(types.NewPointer(typ))
}

func (dslTypesPackage) AsArray(stack *quasigo.ValueStack) {
typ, _ := stack.Pop().(types.Type).(*types.Array)
stack.Push(typ)
}

func (dslTypesPackage) AsSlice(stack *quasigo.ValueStack) {
typ, _ := stack.Pop().(types.Type).(*types.Slice)
stack.Push(typ)
}

func (dslTypesPackage) AsPointer(stack *quasigo.ValueStack) {
typ, _ := stack.Pop().(types.Type).(*types.Pointer)
stack.Push(typ)
Expand Down