-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathlist_access.go
146 lines (113 loc) · 2.15 KB
/
list_access.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright 2014 SteelSeries ApS. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// This package implements a basic LISP interpretor for embedding in a go program for scripting.
// This file implements data elements.
package golisp
// Cxr
func WalkList(d *Data, path string) *Data {
c := d
for index := len(path) - 1; index >= 0; index-- {
if c == nil {
return nil
}
if !PairP(c) && !AlistP(c) && !DottedPairP(c) {
return nil
}
switch path[index] {
case 'a':
c = Car(c)
case 'd':
c = Cdr(c)
default:
c = nil
}
}
return c
}
// Cxxr
func Caar(d *Data) *Data {
return WalkList(d, "aa")
}
func Cadr(d *Data) *Data {
return WalkList(d, "ad")
}
func Cdar(d *Data) *Data {
return WalkList(d, "da")
}
func Cddr(d *Data) *Data {
return WalkList(d, "dd")
}
// Cxxxr
func Caaar(d *Data) *Data {
return WalkList(d, "aaa")
}
func Caadr(d *Data) *Data {
return WalkList(d, "aad")
}
func Cadar(d *Data) *Data {
return WalkList(d, "ada")
}
func Caddr(d *Data) *Data {
return WalkList(d, "add")
}
func Cdaar(d *Data) *Data {
return WalkList(d, "daa")
}
func Cdadr(d *Data) *Data {
return WalkList(d, "dad")
}
func Cddar(d *Data) *Data {
return WalkList(d, "dda")
}
func Cdddr(d *Data) *Data {
return WalkList(d, "ddd")
}
// nth
func Nth(d *Data, n int) *Data {
if d == nil || n < 1 || n > Length(d) {
return nil
}
var c *Data = d
for i := n; i > 1; c, i = Cdr(c), i-1 {
}
return Car(c)
}
func First(d *Data) *Data {
return Nth(d, 1)
}
func Second(d *Data) *Data {
return Nth(d, 2)
}
func Third(d *Data) *Data {
return Nth(d, 3)
}
func Fourth(d *Data) *Data {
return Nth(d, 4)
}
func Fifth(d *Data) *Data {
return Nth(d, 5)
}
func Sixth(d *Data) *Data {
return Nth(d, 6)
}
func Seventh(d *Data) *Data {
return Nth(d, 7)
}
func Eighth(d *Data) *Data {
return Nth(d, 8)
}
func Ninth(d *Data) *Data {
return Nth(d, 9)
}
func Tenth(d *Data) *Data {
return Nth(d, 10)
}
func SetNth(list *Data, index int, value *Data) *Data {
for i := index; i > 1; list, i = Cdr(list), i-1 {
}
if !NilP(list) {
ConsValue(list).Car = value
}
return value
}