about summary refs log tree commit diff
path: root/users/wpcarro/emacs/.emacs.d/wpc/scope.el
blob: 99cdbd2b5e3a28f49eb3a78f7d112d78006fad68 (plain) (blame)
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
;;; scope.el --- Work with a scope data structure -*- lexical-binding: t -*-

;; Author: William Carroll <wpcarro@gmail.com>
;; Version: 0.0.1
;; Package-Requires: ((emacs "25.1"))

;;; Commentary:
;; Exposing an API for working with a scope data structure in a non-mutative
;; way.
;;
;; What's a scope?  Think of a scope as a stack of key-value bindings.

;;; Code:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Dependencies
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(require 'al)
(require 'stack)
(require 'struct)
(require '>)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Create
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(cl-defstruct scope scopes)

(defun scope-new ()
  "Return an empty scope."
  (make-scope :scopes (->> (stack-new)
                           (stack-push (al-new)))))

(defun scope-flatten (xs)
  "Return a flattened representation of the scope, XS.
The newest bindings eclipse the oldest."
  (->> xs
       scope-scopes
       stack-to-list
       (list-reduce (al-new)
                    (lambda (scope acc)
                      (al-merge acc scope)))))

(defun scope-push-new (xs)
  "Push a new, empty scope onto XS."
  (struct-update scope
                 scopes
                 (>-> (stack-push (al-new)))
                 xs))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Read
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun scope-get (k xs)
  "Return K from XS if it's in scope."
  (->> xs
       scope-flatten
       (al-get k)))

(defun scope-current (xs)
  "Return the newest scope from XS."
  (let ((xs-copy (copy-scope xs)))
    (->> xs-copy
         scope-scopes
         stack-peek)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Update
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun scope-set (k v xs)
  "Set value, V, at key, K, in XS for the current scope."
  (struct-update scope
                 scopes
                 (>-> (stack-map-top (>-> (al-set k v))))
                 xs))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Delete
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun scope-pop (xs)
  "Return a new scope without the top element from XS."
  (->> xs
       scope-scopes
       stack-pop))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Predicates
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun scope-defined? (k xs)
  "Return t if K is in scope of XS."
  (->> xs
       scope-flatten
       (al-has-key? k)))

;; TODO: Find a faster way to write aliases like this.
(defun scope-instance? (xs)
  "Return t if XS is a scope struct."
  (scope-p xs))

(provide 'scope)
;;; scope.el ends here