Compare commits

..

No commits in common. "ca2485b725205dd0366cce4b04481a4708e7c348" and "465c959414c554b6696805620799db8c0c87b343" have entirely different histories.

4 changed files with 12 additions and 56 deletions

View File

@ -49,13 +49,8 @@ func (this *HashMap) Conj(key any, val any) *HashMap {
return Conj(this, key, val) return Conj(this, key, val)
} }
func (this *HashMap) IsEmpty() bool { func String(this *HashMap) string {
return len(this._map) == 0 if this == nil {
}
func (this *HashMap) String() string {
if this.IsEmpty() {
return "{}" return "{}"
} }
var sb strings.Builder var sb strings.Builder
@ -68,5 +63,8 @@ func (this *HashMap) String() string {
sb.WriteRune(' ') sb.WriteRune(' ')
} }
return sb.String()[:sb.Len()-2] + "}" return sb.String()[:sb.Len()-2] + "}"
}
func (this *HashMap) String() string {
return String(this)
} }

View File

@ -1,7 +1,6 @@
package read package read
import ( import (
"fmt"
"mal-go/hash_map" "mal-go/hash_map"
"mal-go/symbol" "mal-go/symbol"
"mal-go/vector" "mal-go/vector"
@ -48,8 +47,6 @@ func readForm(s string, pos int, ns string) (any, any, int) {
res, err, pos = readVector(s, pos, ns) res, err, pos = readVector(s, pos, ns)
case '(': case '(':
res, err, pos = readList(s, pos, ns) res, err, pos = readList(s, pos, ns)
default:
return nil, "unexpected char: '" + fmt.Sprint(c) + "'", pos
} }
} }
return res, err, pos return res, err, pos
@ -57,10 +54,7 @@ func readForm(s string, pos int, ns string) (any, any, int) {
func readMap(s string, pos int, ns string) (any, any, int) { func readMap(s string, pos int, ns string) (any, any, int) {
m := hash_map.New() m := hash_map.New()
if s[pos+1] == '}' { return m, "unimplemented", pos
return m, nil, pos + 2
}
return nil, "unimplemented", pos
} }
func readList(s string, pos int, ns string) (any, any, int) { func readList(s string, pos int, ns string) (any, any, int) {

View File

@ -1,15 +1,10 @@
package read package read
import ( import (
"fmt" "github.com/stretchr/testify/assert"
"mal-go/hash_map"
"mal-go/list" "mal-go/list"
"mal-go/symbol" "mal-go/symbol"
"mal-go/vector"
"testing" "testing"
"github.com/stretchr/testify/assert"
"pgregory.net/rapid"
) )
func TestReadInt(t *testing.T) { func TestReadInt(t *testing.T) {
@ -52,33 +47,3 @@ func TestReadList(t *testing.T) {
assert.Equal(t, nil, err, "should read without error") assert.Equal(t, nil, err, "should read without error")
assert.Equal(t, len(input), pos, "should read the whole string") assert.Equal(t, len(input), pos, "should read the whole string")
} }
func StringifiedFormGen() *rapid.Generator[string] {
return rapid.OneOf(rapid.Custom(func(t *rapid.T) string {
myList := list.Empty()
i := 200
t.Repeat(map[string]func(*rapid.T){
"conj": func(t *rapid.T) {
if i < 50 {
myList = myList.Conj(StringifiedFormGen().Draw(t, "el"))
}
},
})
return myList.String()
}),
rapid.Just(list.Empty().String()),
rapid.Just(hash_map.New().String()),
rapid.Just(vector.New().String()),
rapid.Custom(func(t *rapid.T) string {
return fmt.Sprint(rapid.Int().Draw(t, "i"))
}),
)
}
func TestRead(t *testing.T) {
rapid.Check(t, func(t *rapid.T) {
input := StringifiedFormGen().Draw(t, "input")
_, err, _ := readForm(input, 0, "user")
assert.Nil(t, err, err)
})
}

View File

@ -30,8 +30,8 @@ func (this *Vector) Conj(data any) *Vector {
return newVec return newVec
} }
func (this *Vector) String() string { func String(this *Vector) string {
if this.IsEmpty() { if this == nil {
return "[]" return "[]"
} }
var sb strings.Builder var sb strings.Builder
@ -41,11 +41,10 @@ func (this *Vector) String() string {
sb.WriteRune(' ') sb.WriteRune(' ')
} }
return sb.String()[:sb.Len()-1] + "]" return sb.String()[:sb.Len()-1] + "]"
} }
func (this *Vector) IsEmpty() bool { func (this *Vector) String() string {
return len(this._slice) == 0 return String(this)
} }
func ToList(this *Vector) list.IList { func ToList(this *Vector) list.IList {