2019-10-09 13:13:56 +02:00
|
|
|
;;; scope.el --- Work with a scope data structure -*- lexical-binding: t -*-
|
2020-09-01 11:17:43 +02:00
|
|
|
|
2019-10-09 13:13:56 +02:00
|
|
|
;; Author: William Carroll <wpcarro@gmail.com>
|
2020-09-01 11:17:43 +02:00
|
|
|
;; Version: 0.0.1
|
|
|
|
;; URL: https://git.wpcarro.dev/wpcarro/briefcase
|
|
|
|
;; Package-Requires: ((emacs "24"))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
|
|
|
;;; 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:
|
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; Dependencies
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2019-10-09 13:13:56 +02:00
|
|
|
(require 'alist)
|
|
|
|
(require 'stack)
|
|
|
|
(require 'struct)
|
|
|
|
(require 'macros)
|
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; Create
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(cl-defstruct scope scopes)
|
|
|
|
|
|
|
|
(defun scope-new ()
|
2019-10-09 13:13:56 +02:00
|
|
|
"Return an empty scope."
|
2020-09-01 11:17:43 +02:00
|
|
|
(make-scope :scopes (->> (stack-new)
|
|
|
|
(stack-push (alist-new)))))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun scope-flatten (xs)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Return a flattened representation of the scope, XS.
|
|
|
|
The newest bindings eclipse the oldest."
|
|
|
|
(->> xs
|
|
|
|
scope-scopes
|
2020-09-01 11:17:43 +02:00
|
|
|
stack-to-list
|
|
|
|
(list-reduce (alist-new)
|
2019-10-09 13:13:56 +02:00
|
|
|
(lambda (scope acc)
|
2020-09-01 11:17:43 +02:00
|
|
|
(alist-merge acc scope)))))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun scope-push-new (xs)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Push a new, empty scope onto XS."
|
2020-09-01 00:28:47 +02:00
|
|
|
(struct-update scope
|
2019-10-09 13:13:56 +02:00
|
|
|
scopes
|
2020-09-01 11:17:43 +02:00
|
|
|
(>> (stack-push (alist-new)))
|
2019-10-09 13:13:56 +02:00
|
|
|
xs))
|
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; Read
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun scope-get (k xs)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Return K from XS if it's in scope."
|
|
|
|
(->> xs
|
2020-09-01 11:17:43 +02:00
|
|
|
scope-flatten
|
|
|
|
(alist-get k)))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun scope-current (xs)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Return the newest scope from XS."
|
|
|
|
(let ((xs-copy (copy-scope xs)))
|
|
|
|
(->> xs-copy
|
|
|
|
scope-scopes
|
2020-09-01 11:17:43 +02:00
|
|
|
stack-peek)))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; Update
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun scope-set (k v xs)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Set value, V, at key, K, in XS for the current scope."
|
2020-09-01 00:28:47 +02:00
|
|
|
(struct-update scope
|
2019-10-09 13:13:56 +02:00
|
|
|
scopes
|
2020-09-01 11:17:43 +02:00
|
|
|
(>> (stack-map-top (>> (alist-set k v))))
|
2019-10-09 13:13:56 +02:00
|
|
|
xs))
|
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; Delete
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun scope-pop (xs)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Return a new scope without the top element from XS."
|
|
|
|
(->> xs
|
|
|
|
scope-scopes
|
2020-09-01 11:17:43 +02:00
|
|
|
stack-pop))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; Predicates
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun scope-defined? (k xs)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Return t if K is in scope of XS."
|
|
|
|
(->> xs
|
2020-09-01 11:17:43 +02:00
|
|
|
scope-flatten
|
|
|
|
(alist-has-key? k)))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
|
|
|
;; TODO: Find a faster way to write aliases like this.
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun scope-instance? (xs)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Return t if XS is a scope struct."
|
|
|
|
(scope-p xs))
|
|
|
|
|
|
|
|
(provide 'scope)
|
|
|
|
;;; scope.el ends here
|