Skip to content
This repository was archived by the owner on Feb 6, 2026. It is now read-only.

Commit fac7fb2

Browse files
committed
feat(types): list
1 parent 53333de commit fac7fb2

2 files changed

Lines changed: 100 additions & 0 deletions

File tree

parser/types/value.go

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

3+
import "fmt"
4+
35
type Value interface {
46
Type() Type
57
Value() interface{}
@@ -8,6 +10,7 @@ type Value interface {
810
type ValueContainer interface {
911
AddValues(...Value)
1012
GetValues() []Value
13+
CanContain(Value) bool
1114
}
1215

1316
type Tuple []Value
@@ -32,6 +35,44 @@ func (t *Tuple) GetValues() []Value {
3235
return *t
3336
}
3437

38+
func (t *Tuple) CanContain(Value) bool {
39+
return true
40+
}
41+
42+
type List []Value
43+
44+
func (l *List) Type() Type {
45+
return NewListType((*l)[0].Type()) // if the list is empty, the program crashes
46+
}
47+
48+
func (l *List) Value() interface{} {
49+
return l.GetValues()
50+
}
51+
52+
func (l *List) AddValues(v ...Value) {
53+
for _, value := range v {
54+
if len(*l) == 0 {
55+
*l = append(*l, value)
56+
} else {
57+
if !value.Type().Is((*l)[0].Type()) {
58+
panic(fmt.Errorf("invalid list: cannot create a non homogene list"))
59+
}
60+
*l = append(*l, value)
61+
}
62+
}
63+
}
64+
65+
func (l *List) GetValues() []Value {
66+
return *l
67+
}
68+
69+
func (l *List) CanContain(v Value) bool {
70+
if len(*l) == 0 {
71+
return true
72+
}
73+
return v.Type().Is((*l)[0].Type())
74+
}
75+
3576
type Literal struct {
3677
string
3778
t Type

parser/types/value_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,63 @@ func TestTuple(t *testing.T) {
5252
t.Error("Expected C, got", v)
5353
}
5454
}
55+
if !tuple.CanContain(new(List)) {
56+
t.Error("Tuple must be able to contain anything")
57+
}
58+
}
59+
60+
func TestList(t *testing.T) {
61+
list := new(List)
62+
list.AddValues(Int(1), Int(2), Int(3))
63+
if !list.Type().Is(NewListType(IntType)) {
64+
t.Error("Expected int, got", list.Type())
65+
}
66+
val := list.Value().([]Value)
67+
for i, value := range list.GetValues() {
68+
if val[i] != value {
69+
t.Error("Expected", value, "got", val[i])
70+
}
71+
}
72+
if len(val) != 3 {
73+
t.Error("Expected 3, got", len(val))
74+
}
75+
if !val[0].Type().Is(IntType) {
76+
t.Error("Expected string, got", val[0].Type())
77+
}
78+
v, ok := val[0].Value().(int)
79+
if !ok {
80+
t.Error("Cannot convert value to string")
81+
} else {
82+
if v != 1 {
83+
t.Error("Expected 1, got", v)
84+
}
85+
}
86+
if !val[1].Type().Is(IntType) {
87+
t.Error("Expected int, got", val[1].Type())
88+
}
89+
vi, ok := val[1].Value().(int)
90+
if !ok {
91+
t.Error("Cannot convert value to int")
92+
} else {
93+
if vi != 2 {
94+
t.Error("Expected 2, got", vi)
95+
}
96+
}
97+
if !val[2].Type().Is(IntType) {
98+
t.Error("Expected int, got", val[2].Type())
99+
}
100+
v, ok = val[2].Value().(int)
101+
if !ok {
102+
t.Error("Cannot convert value to string")
103+
} else {
104+
if v != 3 {
105+
t.Error("Expected 3, got", v)
106+
}
107+
}
108+
if !list.CanContain(Int(0)) {
109+
t.Error("List must be able to contain the same type")
110+
}
111+
if list.CanContain(String("A")) {
112+
t.Error("List must contain only values with the same type")
113+
}
55114
}

0 commit comments

Comments
 (0)