-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcj-cmp-location.scm
59 lines (51 loc) · 1.53 KB
/
cj-cmp-location.scm
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
;;; Copyright 2017 by Christian Jaeger <ch@christianjaeger.ch>
;;; This file is free software; you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License (GPL) as published
;;; by the Free Software Foundation, either version 2 of the License, or
;;; (at your option) any later version.
(require cj-source ;; basic location stuff
cj-cmp
test)
(define (location-cmp a b)
(define (err n v)
(error (string-append
"location-cmp: " n " argument is not a location object:") v))
(if (location? a)
(if (location? b)
(let ((a-file (location-container a))
(b-file (location-container b)))
(if (eq? a-file b-file)
(let ((a-pos (location-position a))
(b-pos (location-position b)))
(let ((a-line (position-line a-pos))
(b-line (position-line b-pos)))
(if (< a-line b-line)
'lt
(if (= a-line b-line)
(let ((a-col (position-column a-pos))
(b-col (position-column b-pos)))
(if (< a-col b-col)
'lt
(if (= a-col b-col)
'eq
'gt)))
'gt))))
(error "locations are not of the same container:" a b)))
(err "second" b))
(err "first" a)))
(TEST
> (def c "foo")
> (def (loc line col)
(location c (position line col)))
> (location-cmp (loc 10 10) (loc 10 10))
eq
> (location-cmp (loc 9 10) (loc 10 10))
lt
> (location-cmp (loc 10 9) (loc 10 10))
lt
> (location-cmp (loc 9 10) (loc 10 11))
lt
> (location-cmp (loc 10 11) (loc 9 10))
gt
> (location-cmp (loc 10 11) (loc 9 11))
gt)