forked from bartuer/ydiff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
structs.rkt
269 lines (201 loc) · 6.44 KB
/
structs.rkt
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
;; ydiff - a language-aware tool for comparing programs
;; Copyright (C) 2011-2013 Yin Wang ([email protected])
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
#lang racket
(provide (all-defined-out))
;-------------------------------------------------------------
; data types
;-------------------------------------------------------------
;---------------------------- Node ---------------------------
(struct Node (type
start
end
elts
[size #:mutable]
[ctx #:mutable])
#:transparent)
(define comment?
(lambda (n)
(and (Node? n) (eq? 'comment (Node-type n)))))
(define phantom?
(lambda (n)
(and (Node? n) (eq? 'phantom (Node-type n)))))
(define token?
(lambda (n)
(and (Node? n) (eq? 'token (Node-type n)))))
(define str?
(lambda (n)
(and (Node? n) (eq? 'str (Node-type n)))))
(define character?
(lambda (n)
(and (Node? n) (eq? 'char (Node-type n)))))
(define newline?
(lambda (n)
(and (Node? n) (eq? 'newline (Node-type n)))))
;----------- node size function ------------
(define node-size
(lambda (node)
(cond
[(and (Node? node) (Node-size node))
(Node-size node)]
[(pair? node)
(apply + (map node-size node))]
[(or (token? node) (str? node) (character? node)) 1]
[(Node? node)
(let ([size (node-size (Node-elts node))])
(set-Node-size! node size)
size)]
[else 0])))
(define node-depth
(lambda (node)
(cond
[(null? node) 0]
[(pair? node)
(apply max (map node-depth node))]
[(Node? node)
(add1 (node-depth (Node-elts node)))]
[else 0])))
; (node-depth (parse-scheme "(lambda (x (x (y)) (y)) x)"))
(define set-node-context
(lambda (node ctx)
(cond
[(pair? node)
(map (lambda (n) (set-node-context n ctx)) node)]
[(Node? node)
(let ([name (or (get-name node) ctx)])
(set-Node-ctx! node name)
(set-node-context (Node-elts node) name))])))
;------------------ operations on nodes ---------------------
;; "virtual function" - get definition name
;; can be overridden by individual languages
(define get-name (lambda (node) #f))
(define set-get-name
(lambda (fun)
(set! get-name fun)))
;; "virtual function" - get node type
;; can be overridden by individual languages
(define get-type Node-type)
(define set-get-type
(lambda (fun)
(set! get-type fun)))
;; same-def? only depend on get-name, so they need not be overridden
;; by individual languages.
(define same-def?
(lambda (e1 e2)
(cond
[(not (eq? (get-type e1) (get-type e2)))
#f]
[else
(let ([name1 (get-name e1)]
[name2 (get-name e2)])
(and name1 name2 (equal? name1 name2)))])))
(define set-same-def
(lambda (fun)
(set! same-def? fun)))
;---------------------------- Change ---------------------------
;; Change - a change in the data structure
;; - old : the old version, #f for insertions
;; - new : the new version, #f for deletions
;; - cost : the cost of change from old to new
;; - type : insertion, deletion, or modification?
(struct Change (old new cost type) #:transparent)
(define ins?
(lambda (c)
(eq? 'ins (Change-type c))))
(define del?
(lambda (c)
(eq? 'del (Change-type c))))
(define mov?
(lambda (c)
(eq? 'mov (Change-type c))))
;----------------- utils for creating changes ----------------
(define ins
(lambda (node)
(let ([size (node-size node)])
(list (Change #f node size 'ins)))))
(define del
(lambda (node)
(let ([size (node-size node)])
(list (Change node #f size 'del)))))
(define mov
(lambda (node1 node2 cost)
(list (Change node1 node2 cost 'mov))))
;; create a "total change"
;; (delete node1 completely and then insert node2)
(define total
(lambda (node1 node2)
(let ([size1 (node-size node1)]
[size2 (node-size node2)])
(values (append (del node1) (ins node2))
(+ size1 size2)))))
;---------------------------- Tag ---------------------------
;; HTML tag structure used HTML generation code
(struct Tag (tag idx start) #:transparent)
(define get-symbol
(lambda (node)
(cond
[(token? node)
(string->symbol (Node-elts node))]
[else #f])))
;; Find the first node elements which matches a given tag.
(define get-tag
(lambda (node tag)
(cond
[(not (Node? node)) #f]
[(not (pair? (Node-elts node))) #f]
[(null? (Node-elts node)) #f]
[else
(let ([matches (filter (lambda (x)
(eq? (Node-type x) tag))
(Node-elts node))])
(cond
[(null? matches) #f]
[else (car matches)]))])))
;; (get-tag (car (parse1 $statement "function f(x) {}"))
;; 'name)
;; Find the first node containing a given path of tags.
;; For example: '(function parameter) could match a function's parameter
(define match-tags
(lambda (e tags)
(cond
[(not (Node? e)) #f]
[(null? tags) e]
[else
(match-tags (get-tag e (car tags)) (cdr tags))])))
;; (match-tags (car (parse1 $statement "function f(x) {}"))
;; '(function name))
(define uid
(let ([count 1]
[table (box '())])
(lambda (node)
(let ([p (assq node (unbox table))])
(cond
[(not p)
(let ([id count])
(set! count (add1 count))
(set-box! table (cons `(,node . ,id) (unbox table)))
id)]
[else
(cdr p)])))))
;; similarity string from a change
(define similarity
(lambda (change)
(let ([total (+ (node-size (Change-old change))
(node-size (Change-new change)))])
(cond
[(or (= 0 total) (= 0 (Change-cost change)))
"100%"]
[else
(string-append
(real->decimal-string
(* 100 (- 1.0 (/ (Change-cost change) total))) 1)
"%")]))))