Skip to content

Commit 5ef5577

Browse files
committed
chapter 9: cleanups
1 parent cfdcb2a commit 5ef5577

File tree

4 files changed

+8
-27
lines changed

4 files changed

+8
-27
lines changed

ast.go

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"cmp"
5-
"errors"
65
"fmt"
76
"reflect"
87
"strings"
@@ -160,25 +159,6 @@ func isEq(arg1 Expr, arg2 Expr) bool {
160159
}
161160
}
162161

163-
func newHashMap(seq Expr) (Expr, error) {
164-
list, err := checkIsSeq(seq)
165-
if err != nil {
166-
return nil, err
167-
}
168-
if (len(list) % 2) != 0 {
169-
return nil, errors.New("odd number of arguments to NewHashMap")
170-
}
171-
hash_map := ExprHashMap{}
172-
for i := 1; i < len(list); i += 2 {
173-
str, ok := list[i-1].(ExprStr)
174-
if !ok {
175-
return nil, errors.New("expected hash-map key string")
176-
}
177-
hash_map[str] = list[i]
178-
}
179-
return hash_map, nil
180-
}
181-
182162
func str(srcLike bool, args ...Expr) string {
183163
var buf strings.Builder
184164
for i, arg := range args {

reader.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,7 @@ func readList(r Reader, start string, end string) (Expr, error) {
8080
}
8181

8282
var ast_list ExprList
83-
token = r.peek()
84-
for ; true; token = r.peek() {
83+
for token = r.peek(); true; token = r.peek() {
8584
if token == nil {
8685
return nil, errors.New("expected '" + end + "', got EOF")
8786
}
@@ -103,16 +102,15 @@ func readVec(r Reader) (Expr, error) {
103102
if err != nil {
104103
return nil, err
105104
}
106-
vec := ExprVec(list.(ExprList))
107-
return vec, nil
105+
return ExprVec(list.(ExprList)), nil
108106
}
109107

110108
func readHashMap(r Reader) (Expr, error) {
111109
list, err := readList(r, "{", "}")
112110
if err != nil {
113111
return nil, err
114112
}
115-
return newHashMap(list)
113+
return stdHashmap(list.(ExprList))
116114
}
117115

118116
func readForm(r Reader) (Expr, error) {

special_forms.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,12 @@ func stdQuasiQuote(env *Env, args []Expr) (*Env, Expr, error) {
255255
}
256256

257257
func stdTryCatch(env *Env, args []Expr) (*Env, Expr, error) {
258-
if err := checkArgsCount(2, 2, args); err != nil {
258+
if err := checkArgsCount(1, 2, args); err != nil {
259259
return nil, nil, err
260260
}
261+
if len(args) == 1 {
262+
return env, args[0], nil
263+
}
261264

262265
catch, ok, err := isListStartingWithIdent(args[1], "catch", 3)
263266
if err != nil {

std.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ func stdHashmapDel(args []Expr) (Expr, error) {
543543

544544
new_hashmap := make(ExprHashMap, len(hashmap)-len(keys_to_delete))
545545
for k, v := range hashmap {
546-
if !slices.ContainsFunc(keys_to_delete, func(it Expr) bool { return it.(ExprIdent) == ExprIdent(k) }) {
546+
if !slices.ContainsFunc(keys_to_delete, func(it Expr) bool { return it.(ExprStr) == ExprStr(k) }) {
547547
new_hashmap[k] = v
548548
}
549549
}

0 commit comments

Comments
 (0)