Skip to content

Commit cf264d4

Browse files
committed
feat: add lambda type
1 parent 3052ac0 commit cf264d4

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

dsl/types/types.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package types
22

3+
import (
4+
"bytes"
5+
)
6+
37
// Type is a type of a value
48
type Type interface {
59
// String returns a string like "<type %s>"
@@ -159,3 +163,43 @@ func (*anyType) String() string {
159163
func (*anyType) InstanceOf(_ Type) bool {
160164
return true
161165
}
166+
167+
// ===================== Lambda type ===================== //
168+
169+
// NewLambdaType creates lambda type instance.
170+
// Signature must have 1 type at least for a return type.
171+
func NewLambdaType(t Type, rest ...Type) Type {
172+
signature := append([]Type{t}, rest...)
173+
return &lambdaType{signature: signature}
174+
}
175+
176+
type lambdaType struct {
177+
signature []Type
178+
}
179+
180+
func (t *lambdaType) String() string {
181+
var arg bytes.Buffer
182+
for i := range t.signature {
183+
if i > 0 {
184+
arg.WriteString(",")
185+
}
186+
arg.WriteString(t.signature[i].String())
187+
}
188+
return "Lambda[" + arg.String() + "]"
189+
}
190+
191+
func (t *lambdaType) InstanceOf(t2 Type) bool {
192+
lambda, ok := t2.(*lambdaType)
193+
if !ok {
194+
return false
195+
}
196+
if len(t.signature) != len(lambda.signature) {
197+
return false
198+
}
199+
for i := range t.signature {
200+
if !t.signature[i].InstanceOf(lambda.signature[i]) {
201+
return false
202+
}
203+
}
204+
return true
205+
}

0 commit comments

Comments
 (0)