summaryrefslogtreecommitdiff
path: root/vm/type.go
diff options
context:
space:
mode:
authorMarc Vertes <mvertes@free.fr>2024-04-23 14:36:57 +0200
committerGitHub <noreply@github.com>2024-04-23 14:36:57 +0200
commit0063922f5c8a07b78603b2da7f6a3c2094711c3d (patch)
tree613cd8f53e86f33678a2f96fa0e776edfa555101 /vm/type.go
parent1bff92c52b27b9a516599e172fe9852c3d99be38 (diff)
feat: initial and partial support of composite expressions (#9)
A new `Composite` token is created. Literal composite expressions are recognized and partially handled by the parser but not yet by the code generator. Other cosmetic changes are present.
Diffstat (limited to 'vm/type.go')
-rw-r--r--vm/type.go18
1 files changed, 16 insertions, 2 deletions
diff --git a/vm/type.go b/vm/type.go
index 16e3733..49215db 100644
--- a/vm/type.go
+++ b/vm/type.go
@@ -10,10 +10,19 @@ type Type struct {
Rtype reflect.Type
}
+func (t *Type) String() string {
+ if t.Name != "" {
+ return t.Name
+ }
+ return t.Rtype.String()
+}
+
+// Elem returns a type's element type.
func (t *Type) Elem() *Type {
return &Type{Rtype: t.Rtype.Elem()}
}
+// Out returns the type's i'th output parameter.
func (t *Type) Out(i int) *Type {
return &Type{Rtype: t.Rtype.Out(i)}
}
@@ -43,18 +52,22 @@ func ValueOf(v any) Value {
return Value{Data: reflect.ValueOf(v)}
}
+// PointerTo returns the pointer type with element t.
func PointerTo(t *Type) *Type {
return &Type{Rtype: reflect.PointerTo(t.Rtype)}
}
-func ArrayOf(size int, t *Type) *Type {
- return &Type{Rtype: reflect.ArrayOf(size, t.Rtype)}
+// ArrayOf returns the array type with the given length and element type.
+func ArrayOf(length int, t *Type) *Type {
+ return &Type{Rtype: reflect.ArrayOf(length, t.Rtype)}
}
+// SliceOf returns the slice type with the given element type.
func SliceOf(t *Type) *Type {
return &Type{Rtype: reflect.SliceOf(t.Rtype)}
}
+// FuncOf returns the function type with the given argument and result types.
func FuncOf(arg, ret []*Type, variadic bool) *Type {
a := make([]reflect.Type, len(arg))
for i, e := range arg {
@@ -67,6 +80,7 @@ func FuncOf(arg, ret []*Type, variadic bool) *Type {
return &Type{Rtype: reflect.FuncOf(a, r, variadic)}
}
+// StructOf returns the struct type with the given field types.
func StructOf(fields []*Type) *Type {
rf := make([]reflect.StructField, len(fields))
for i, f := range fields {