eliscript 0.0.4 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/editor/eliscript-abi.el +170 -0
- package/editor/eliscript-async.el +375 -0
- package/editor/eliscript-bind.el +822 -0
- package/editor/eliscript-compat.el +184 -0
- package/editor/eliscript-contract.el +334 -0
- package/editor/eliscript-data.el +1544 -0
- package/editor/eliscript-js.el +318 -0
- package/editor/eliscript-mode.el +1 -1
- package/editor/eliscript-module.el +340 -0
- package/editor/eliscript-repl.el +1 -1
- package/package.json +3 -1
- package/runtime/emacs-compat.eli +2 -0
- package/runtime/emacs-compat.mjs +2507 -0
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
;;; eliscript-compat.el --- Unified 0.1.0 entry point, definition-identity activation, and evaluator -*- lexical-binding: t; -*-
|
|
2
|
+
|
|
3
|
+
;;; Commentary:
|
|
4
|
+
|
|
5
|
+
;; Provides the umbrella `(require 'eliscript)` integration, definition-identity
|
|
6
|
+
;; extension tracking, conflict detection, static contract validation for
|
|
7
|
+
;; standard `defun`, and `eliscript-compat-eval-string` for Eliscript 0.1.0.
|
|
8
|
+
|
|
9
|
+
;;; Code:
|
|
10
|
+
|
|
11
|
+
(require 'cl-lib)
|
|
12
|
+
(require 'eliscript-contract)
|
|
13
|
+
(require 'eliscript-data)
|
|
14
|
+
(require 'eliscript-bind)
|
|
15
|
+
(require 'eliscript-module)
|
|
16
|
+
(require 'eliscript-async)
|
|
17
|
+
(require 'eliscript-js)
|
|
18
|
+
(require 'eliscript-abi)
|
|
19
|
+
|
|
20
|
+
(defconst eliscript-compat--extension-exports
|
|
21
|
+
'((eliscript-data
|
|
22
|
+
persistent-list persistent-list-p persistent-vector persistent-vector-p
|
|
23
|
+
persistent-map persistent-map-p persistent-set persistent-set-p
|
|
24
|
+
persistent-queue persistent-queue-p persistent-sorted-map persistent-sorted-map-p
|
|
25
|
+
persistent-sorted-set persistent-sorted-set-p
|
|
26
|
+
value-get value-nth value-put value-assoc value-remove value-dissoc
|
|
27
|
+
value-conj value-count value-empty-p value-into value-equal-p value-hash
|
|
28
|
+
value-compare value-meta value-with-meta
|
|
29
|
+
cl-defstruct-persistent struct-validate struct-json-schema struct-to-json struct-from-json
|
|
30
|
+
defprotocol extend-protocol defclass-protocol reify-protocol
|
|
31
|
+
cl-defgeneric-value cl-defmethod-value
|
|
32
|
+
state-cell state-cell-p cell-value cell-update! cell-reset! cell-watch
|
|
33
|
+
delay force realized-p memoize lazy-seq lazy-map lazy-filter lazy-take lazy-range
|
|
34
|
+
map-transducer filter-transducer take-transducer seq-transduce
|
|
35
|
+
with-transient conj! assoc! dissoc!
|
|
36
|
+
result-ok result-error result-ok-p result-error-p result-value result-error-value result-bind
|
|
37
|
+
option-some option-none option-some-p option-none-p option-value
|
|
38
|
+
data-symbol data-symbol-p data-symbol-name data-symbol-namespace)
|
|
39
|
+
(eliscript-bind
|
|
40
|
+
let-bind let*-bind defun-bind lambda-bind pcase-value cond-predicate
|
|
41
|
+
defun-case lambda-case let-const let*-const
|
|
42
|
+
let-loop defun-tail lambda-tail recur trampoline
|
|
43
|
+
thread-first thread-last thread-as thread-cond-first thread-cond-last
|
|
44
|
+
thread-some-first thread-some-last
|
|
45
|
+
fn-compose fn-partial fn-juxt fn-complement)
|
|
46
|
+
(eliscript-async
|
|
47
|
+
defun-async lambda-async await future-p future-then future-cancel
|
|
48
|
+
with-async-scope with-async-timeout with-async-bindings bind-async
|
|
49
|
+
make-async-channel async-channel-p channel-put! channel-take! channel-close!
|
|
50
|
+
engine-bind engine-revoke! engine-handle-p engine-call eliscript-async-wait)
|
|
51
|
+
(eliscript-module
|
|
52
|
+
defmodule defun-lexical defvar-lexical defconst-lexical
|
|
53
|
+
export-functions export-values import-functions import-values
|
|
54
|
+
defmacro-checked macro-gensym macro-read-file)
|
|
55
|
+
(eliscript-contract
|
|
56
|
+
eliscript-contract-check-value)
|
|
57
|
+
(eliscript-js
|
|
58
|
+
js-false js-null js-undefined js-false-p js-null-p js-undefined-p js-nullish-p
|
|
59
|
+
js-import js-array js-object js-get js-get-raw js-put js-call js-call-raw
|
|
60
|
+
js-new js-expr js-divide js-remainder js-length js-substring
|
|
61
|
+
js-regexp js-regexp-p js-regexp-find js-regexp-matches js-regexp-seq js-regexp-replace)
|
|
62
|
+
(eliscript-abi
|
|
63
|
+
with-abi-memory mem-size mem-grow
|
|
64
|
+
mem-load-u8 mem-load-u16 mem-load-u32 mem-load-f64
|
|
65
|
+
mem-store-u8 mem-store-u16 mem-store-u32 mem-store-f64
|
|
66
|
+
word-add word-sub word-mul word-div word-and word-or word-xor word-shl word-shr))
|
|
67
|
+
"Exported symbols per `1 + 5 + 2' extension library.")
|
|
68
|
+
|
|
69
|
+
(defvar eliscript-compat--canonical-fn-cells (make-hash-table :test 'eq)
|
|
70
|
+
"Canonical function cells for all extension symbols.")
|
|
71
|
+
|
|
72
|
+
(dolist (group eliscript-compat--extension-exports)
|
|
73
|
+
(dolist (sym (cdr group))
|
|
74
|
+
(when (fboundp sym)
|
|
75
|
+
(puthash sym (symbol-function sym) eliscript-compat--canonical-fn-cells))))
|
|
76
|
+
|
|
77
|
+
(defvar eliscript-compat--session-user-defs (make-hash-table :test 'eq)
|
|
78
|
+
"Symbols defined by user code in the current evaluation session.")
|
|
79
|
+
|
|
80
|
+
(defvar eliscript-compat--session-active-libs nil
|
|
81
|
+
"List of extension libraries activated in the current session.")
|
|
82
|
+
|
|
83
|
+
(defvar eliscript-compat--session-touched-symbols nil
|
|
84
|
+
"Symbols created or modified during the current evaluation session.")
|
|
85
|
+
|
|
86
|
+
(progn
|
|
87
|
+
(defun eliscript-compat-reset-state! ()
|
|
88
|
+
"Restore clean 0.1.0 evaluation state and canonical extension bindings."
|
|
89
|
+
(eliscript-contract-reset-state!)
|
|
90
|
+
(eliscript-data-reset-state!)
|
|
91
|
+
(eliscript-module-reset-state!)
|
|
92
|
+
(eliscript-async-reset-state!)
|
|
93
|
+
(dolist (sym eliscript-compat--session-touched-symbols)
|
|
94
|
+
(unless (gethash sym eliscript-compat--canonical-fn-cells)
|
|
95
|
+
(fmakunbound sym)
|
|
96
|
+
(makunbound sym)
|
|
97
|
+
(setplist sym nil)))
|
|
98
|
+
(maphash (lambda (sym fn) (fset sym fn))
|
|
99
|
+
eliscript-compat--canonical-fn-cells)
|
|
100
|
+
(clrhash eliscript-compat--session-user-defs)
|
|
101
|
+
(setq eliscript-compat--session-active-libs nil)
|
|
102
|
+
(setq eliscript-compat--session-touched-symbols nil)))
|
|
103
|
+
|
|
104
|
+
(defun eliscript-compat--activate-lib (lib-sym)
|
|
105
|
+
"Activate extension library LIB-SYM, checking for user symbol conflicts."
|
|
106
|
+
(if (eq lib-sym 'eliscript)
|
|
107
|
+
(dolist (sub '(eliscript-data eliscript-bind eliscript-async eliscript-module eliscript-contract))
|
|
108
|
+
(eliscript-compat--activate-lib sub))
|
|
109
|
+
(let ((exports (cdr (assq lib-sym eliscript-compat--extension-exports))))
|
|
110
|
+
(when exports
|
|
111
|
+
(dolist (sym exports)
|
|
112
|
+
(when (gethash sym eliscript-compat--session-user-defs)
|
|
113
|
+
(signal 'eliscript-compile-error
|
|
114
|
+
(list (format "ELI-M0001: extension `%S' symbol `%S' conflicts with existing user definition"
|
|
115
|
+
lib-sym sym))))
|
|
116
|
+
(let ((canon (gethash sym eliscript-compat--canonical-fn-cells)))
|
|
117
|
+
(when canon
|
|
118
|
+
(fset sym canon))))
|
|
119
|
+
(cl-pushnew lib-sym eliscript-compat--session-active-libs)))))
|
|
120
|
+
|
|
121
|
+
(defun eliscript-compat--preprocess-form (form)
|
|
122
|
+
"Pre-scan and transform FORM for definition-identity and static contracts."
|
|
123
|
+
(if (not (consp form))
|
|
124
|
+
form
|
|
125
|
+
(let ((head (car form)))
|
|
126
|
+
(cond
|
|
127
|
+
((eq head 'quote) form)
|
|
128
|
+
((eq head 'progn)
|
|
129
|
+
`(progn ,@(mapcar #'eliscript-compat--preprocess-form (cdr form))))
|
|
130
|
+
((and (eq head 'require)
|
|
131
|
+
(consp (cadr form))
|
|
132
|
+
(eq (caadr form) 'quote))
|
|
133
|
+
(let ((lib (cadadr form)))
|
|
134
|
+
(if (or (eq lib 'eliscript)
|
|
135
|
+
(assq lib eliscript-compat--extension-exports))
|
|
136
|
+
(progn
|
|
137
|
+
(eliscript-compat--activate-lib lib)
|
|
138
|
+
`',lib)
|
|
139
|
+
form)))
|
|
140
|
+
((eq head 'defun)
|
|
141
|
+
(let* ((name (nth 1 form))
|
|
142
|
+
(args (nth 2 form))
|
|
143
|
+
(raw-body (nthcdr 3 form))
|
|
144
|
+
(extracted (eliscript-contract-extract-declarations raw-body name))
|
|
145
|
+
(plist (plist-get extracted :eliscript))
|
|
146
|
+
(doc (plist-get extracted :doc))
|
|
147
|
+
(clean-body (plist-get extracted :body)))
|
|
148
|
+
(puthash name t eliscript-compat--session-user-defs)
|
|
149
|
+
(push name eliscript-compat--session-touched-symbols)
|
|
150
|
+
(when plist
|
|
151
|
+
(eliscript-contract-validate-context 'defun name plist args clean-body))
|
|
152
|
+
(when (memq 'eliscript-async eliscript-compat--session-active-libs)
|
|
153
|
+
(dolist (bform clean-body)
|
|
154
|
+
(eliscript-async--scan-forbidden-await bform nil name)))
|
|
155
|
+
(let ((wrapped (if plist
|
|
156
|
+
(eliscript-contract-wrap-body name plist args clean-body)
|
|
157
|
+
clean-body)))
|
|
158
|
+
`(defun ,name ,args
|
|
159
|
+
,@(when doc (list doc))
|
|
160
|
+
,@wrapped))))
|
|
161
|
+
((memq head '(defvar defconst defmacro))
|
|
162
|
+
(let ((name (nth 1 form)))
|
|
163
|
+
(when (symbolp name)
|
|
164
|
+
(push name eliscript-compat--session-touched-symbols)))
|
|
165
|
+
form)
|
|
166
|
+
(t form)))))
|
|
167
|
+
|
|
168
|
+
(progn
|
|
169
|
+
(defun eliscript-compat-eval-string (source)
|
|
170
|
+
"Read, statically check, and evaluate all forms in SOURCE string."
|
|
171
|
+
(with-temp-buffer
|
|
172
|
+
(insert source)
|
|
173
|
+
(goto-char (point-min))
|
|
174
|
+
(let ((last-val nil))
|
|
175
|
+
(condition-case nil
|
|
176
|
+
(while t
|
|
177
|
+
(let* ((raw-form (read (current-buffer)))
|
|
178
|
+
(checked-form (eliscript-compat--preprocess-form raw-form)))
|
|
179
|
+
(setq last-val (eval checked-form t))))
|
|
180
|
+
(end-of-file last-val))))))
|
|
181
|
+
|
|
182
|
+
(provide 'eliscript-compat)
|
|
183
|
+
|
|
184
|
+
;;; eliscript-compat.el ends here
|
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
;;; eliscript-contract.el --- Static declarations and runtime contracts for Eliscript 0.1.0 -*- lexical-binding: t; -*-
|
|
2
|
+
|
|
3
|
+
;;; Commentary:
|
|
4
|
+
|
|
5
|
+
;; Implements `(declare (eliscript ...))` parsing, static contract validation
|
|
6
|
+
;; (`:pure`, `:portable`, `:checked`, `:async`, `:destructure`, `:abi`,
|
|
7
|
+
;; `:export`, `:inputs`), runtime pre/post-condition assertions (`:params`,
|
|
8
|
+
;; `:returns`, `:requires`, `:ensures`), and Emacs `defun-declarations-alist`
|
|
9
|
+
;; integration.
|
|
10
|
+
|
|
11
|
+
;;; Code:
|
|
12
|
+
|
|
13
|
+
(require 'cl-lib)
|
|
14
|
+
|
|
15
|
+
(define-error 'eliscript-compile-error "Eliscript compile error")
|
|
16
|
+
(define-error 'eliscript-declaration-error "Eliscript declaration error" 'eliscript-compile-error)
|
|
17
|
+
(define-error 'eliscript-contract-error "Eliscript static contract error" 'eliscript-compile-error)
|
|
18
|
+
(define-error 'eliscript-runtime-contract-error "Eliscript runtime contract violation")
|
|
19
|
+
|
|
20
|
+
(defconst eliscript-contract--valid-keys
|
|
21
|
+
'(:pure :portable :checked :async :destructure
|
|
22
|
+
:params :returns :requires :ensures
|
|
23
|
+
:abi :export :inputs)
|
|
24
|
+
"Valid keys allowed inside `(declare (eliscript ...))`.")
|
|
25
|
+
|
|
26
|
+
(defconst eliscript-contract--impure-Heads
|
|
27
|
+
'(puthash remhash clrhash setcar setcdr aset fset set put
|
|
28
|
+
insert delete-region write-region message princ prin1 print
|
|
29
|
+
random current-time float-time
|
|
30
|
+
cell-update! cell-reset!
|
|
31
|
+
channel-put! channel-take! channel-close!
|
|
32
|
+
engine-call engine-revoke!
|
|
33
|
+
mem-store-u8 mem-store-u16 mem-store-u32 mem-store-f64 mem-grow)
|
|
34
|
+
"Operators forbidden inside `:pure t` functions.")
|
|
35
|
+
|
|
36
|
+
(defconst eliscript-contract--non-portable-heads
|
|
37
|
+
'(current-buffer set-buffer with-current-buffer get-buffer get-buffer-create
|
|
38
|
+
kill-buffer buffer-string buffer-substring point goto-char
|
|
39
|
+
insert delete-region selected-window selected-frame window-buffer
|
|
40
|
+
start-process make-process call-process process-send-string)
|
|
41
|
+
"Host-specific Emacs operators forbidden inside `:portable t` functions.")
|
|
42
|
+
|
|
43
|
+
(defconst eliscript-contract--abi-forbidden-alloc-heads
|
|
44
|
+
'(list cons vector make-vector make-hash-table
|
|
45
|
+
persistent-list persistent-vector persistent-map persistent-set
|
|
46
|
+
persistent-queue persistent-sorted-map persistent-sorted-set
|
|
47
|
+
concat format make-string eval funcall apply)
|
|
48
|
+
"Heap-allocating or dynamic operators forbidden inside `:abi` kernels.")
|
|
49
|
+
|
|
50
|
+
(defvar eliscript-contract--checked-functions (make-hash-table :test 'eq)
|
|
51
|
+
"Registry of functions validated with static contracts.")
|
|
52
|
+
|
|
53
|
+
(progn
|
|
54
|
+
(defun eliscript-contract-reset-state! ()
|
|
55
|
+
"Reset cached contract state for isolated test runs."
|
|
56
|
+
(clrhash eliscript-contract--checked-functions)))
|
|
57
|
+
|
|
58
|
+
(defun eliscript-contract--merge-plist (target source context-name)
|
|
59
|
+
"Merge SOURCE eliscript declaration plist into TARGET for CONTEXT-NAME."
|
|
60
|
+
(let ((cursor source)
|
|
61
|
+
(out (copy-sequence target)))
|
|
62
|
+
(while cursor
|
|
63
|
+
(unless (cdr cursor)
|
|
64
|
+
(signal 'eliscript-declaration-error
|
|
65
|
+
(list (format "ELI-D0003: odd number of elements in (declare (eliscript ...)) for %s"
|
|
66
|
+
context-name))))
|
|
67
|
+
(let ((k (car cursor))
|
|
68
|
+
(v (cadr cursor)))
|
|
69
|
+
(unless (memq k eliscript-contract--valid-keys)
|
|
70
|
+
(signal 'eliscript-declaration-error
|
|
71
|
+
(list (format "ELI-D0004: unknown eliscript declaration key %S in %s"
|
|
72
|
+
k context-name))))
|
|
73
|
+
(if (plist-member out k)
|
|
74
|
+
(let ((existing (plist-get out k)))
|
|
75
|
+
(unless (equal existing v)
|
|
76
|
+
(signal 'eliscript-declaration-error
|
|
77
|
+
(list (format "ELI-D0005: contradictory declaration for %S in %s (%S vs %S)"
|
|
78
|
+
k context-name existing v)))))
|
|
79
|
+
(setq out (plist-put out k v))))
|
|
80
|
+
(setq cursor (cddr cursor)))
|
|
81
|
+
out))
|
|
82
|
+
|
|
83
|
+
(progn
|
|
84
|
+
(defun eliscript-contract-extract-declarations (body &optional context-name)
|
|
85
|
+
"Extract docstring and `(declare ...)` forms from BODY for CONTEXT-NAME.
|
|
86
|
+
Return a plist `(:doc DOC :eliscript PLIST :declarations DECLS :body BODY)'."
|
|
87
|
+
(let ((doc nil)
|
|
88
|
+
(eli-plist nil)
|
|
89
|
+
(other-decls nil)
|
|
90
|
+
(rest body)
|
|
91
|
+
(scanning t))
|
|
92
|
+
(while (and scanning rest)
|
|
93
|
+
(let ((form (car rest)))
|
|
94
|
+
(cond
|
|
95
|
+
((and (stringp form) (null doc) (cdr rest))
|
|
96
|
+
(setq doc form)
|
|
97
|
+
(setq rest (cdr rest)))
|
|
98
|
+
((and (consp form) (eq (car form) 'declare))
|
|
99
|
+
(dolist (spec (cdr form))
|
|
100
|
+
(if (and (consp spec) (eq (car spec) 'eliscript))
|
|
101
|
+
(setq eli-plist
|
|
102
|
+
(eliscript-contract--merge-plist
|
|
103
|
+
eli-plist (cdr spec) (or context-name 'anonymous)))
|
|
104
|
+
(push spec other-decls)))
|
|
105
|
+
(setq rest (cdr rest)))
|
|
106
|
+
(t
|
|
107
|
+
(setq scanning nil)))))
|
|
108
|
+
(list :doc doc
|
|
109
|
+
:eliscript eli-plist
|
|
110
|
+
:declarations (nreverse other-decls)
|
|
111
|
+
:body rest))))
|
|
112
|
+
|
|
113
|
+
(defun eliscript-contract--extract-arg-symbols (arglist)
|
|
114
|
+
"Extract bound variable symbols from ARGLIST."
|
|
115
|
+
(let ((syms nil))
|
|
116
|
+
(dolist (item arglist)
|
|
117
|
+
(cond
|
|
118
|
+
((and (symbolp item)
|
|
119
|
+
(not (memq item '(&optional &rest &aux)))
|
|
120
|
+
(not (keywordp item)))
|
|
121
|
+
(push item syms))
|
|
122
|
+
((consp item)
|
|
123
|
+
(when (symbolp (car item))
|
|
124
|
+
(push (car item) syms)))))
|
|
125
|
+
syms))
|
|
126
|
+
|
|
127
|
+
(defun eliscript-contract--scan-pure (form local-vars context-name)
|
|
128
|
+
"Verify FORM contains no impure operations given LOCAL-VARS in CONTEXT-NAME."
|
|
129
|
+
(when (consp form)
|
|
130
|
+
(let ((head (car form)))
|
|
131
|
+
(when (memq head eliscript-contract--impure-Heads)
|
|
132
|
+
(signal 'eliscript-contract-error
|
|
133
|
+
(list (format "ELI-C0001: impure operation `%S' is forbidden in :pure function %s"
|
|
134
|
+
head context-name))))
|
|
135
|
+
(cond
|
|
136
|
+
((eq head 'quote) nil)
|
|
137
|
+
((eq head 'setq)
|
|
138
|
+
(let ((pairs (cdr form)))
|
|
139
|
+
(while pairs
|
|
140
|
+
(let ((sym (car pairs))
|
|
141
|
+
(val (cadr pairs)))
|
|
142
|
+
(unless (memq sym local-vars)
|
|
143
|
+
(signal 'eliscript-contract-error
|
|
144
|
+
(list (format "ELI-C0001: mutating non-local variable `%S' via setq is forbidden in :pure function %s"
|
|
145
|
+
sym context-name))))
|
|
146
|
+
(eliscript-contract--scan-pure val local-vars context-name))
|
|
147
|
+
(setq pairs (cddr pairs)))))
|
|
148
|
+
((memq head '(let let* let-const let*-const let-loop))
|
|
149
|
+
(let* ((has-name (and (eq head 'let-loop) (symbolp (cadr form))))
|
|
150
|
+
(bindings (if has-name (nth 2 form) (cadr form)))
|
|
151
|
+
(body (if has-name (nthcdr 3 form) (cddr form)))
|
|
152
|
+
(extended local-vars))
|
|
153
|
+
(dolist (b bindings)
|
|
154
|
+
(if (consp b)
|
|
155
|
+
(progn
|
|
156
|
+
(eliscript-contract--scan-pure (cadr b) extended context-name)
|
|
157
|
+
(when (symbolp (car b))
|
|
158
|
+
(push (car b) extended)))
|
|
159
|
+
(when (symbolp b)
|
|
160
|
+
(push b extended))))
|
|
161
|
+
(dolist (sub body)
|
|
162
|
+
(eliscript-contract--scan-pure sub extended context-name))))
|
|
163
|
+
((memq head '(lambda lambda-async lambda-tail lambda-bind))
|
|
164
|
+
(let ((ext (append (eliscript-contract--extract-arg-symbols (cadr form))
|
|
165
|
+
local-vars)))
|
|
166
|
+
(dolist (sub (cddr form))
|
|
167
|
+
(eliscript-contract--scan-pure sub ext context-name))))
|
|
168
|
+
((memq head '(dolist dotimes))
|
|
169
|
+
(let* ((spec (cadr form))
|
|
170
|
+
(var (car spec))
|
|
171
|
+
(ext (cons var local-vars)))
|
|
172
|
+
(eliscript-contract--scan-pure (cadr spec) local-vars context-name)
|
|
173
|
+
(dolist (sub (cddr form))
|
|
174
|
+
(eliscript-contract--scan-pure sub ext context-name))))
|
|
175
|
+
(t
|
|
176
|
+
(dolist (sub (cdr form))
|
|
177
|
+
(eliscript-contract--scan-pure sub local-vars context-name)))))))
|
|
178
|
+
|
|
179
|
+
(defun eliscript-contract--scan-portable (form context-name)
|
|
180
|
+
"Verify FORM contains no non-portable host calls in CONTEXT-NAME."
|
|
181
|
+
(when (consp form)
|
|
182
|
+
(let ((head (car form)))
|
|
183
|
+
(unless (eq head 'quote)
|
|
184
|
+
(when (memq head eliscript-contract--non-portable-heads)
|
|
185
|
+
(signal 'eliscript-contract-error
|
|
186
|
+
(list (format "ELI-C0002: host-specific operator `%S' is forbidden in :portable function %s"
|
|
187
|
+
head context-name))))
|
|
188
|
+
(dolist (sub (cdr form))
|
|
189
|
+
(eliscript-contract--scan-portable sub context-name))))))
|
|
190
|
+
|
|
191
|
+
(defun eliscript-contract--scan-abi (form abi-kind context-name)
|
|
192
|
+
"Verify FORM conforms to ABI-KIND in CONTEXT-NAME."
|
|
193
|
+
(cond
|
|
194
|
+
((and (eq abi-kind :numeric-f64) (or (stringp form) (vectorp form)))
|
|
195
|
+
(signal 'eliscript-contract-error
|
|
196
|
+
(list (format "ELI-B0001: heap literal `%S' is forbidden in :abi :numeric-f64 kernel %s"
|
|
197
|
+
form context-name))))
|
|
198
|
+
((consp form)
|
|
199
|
+
(let ((head (car form)))
|
|
200
|
+
(cond
|
|
201
|
+
((eq head 'quote)
|
|
202
|
+
(signal 'eliscript-contract-error
|
|
203
|
+
(list (format "ELI-B0001: quoted literal is forbidden in :abi %S kernel %s"
|
|
204
|
+
abi-kind context-name))))
|
|
205
|
+
((memq head eliscript-contract--abi-forbidden-alloc-heads)
|
|
206
|
+
(signal 'eliscript-contract-error
|
|
207
|
+
(list (format "ELI-B0001: dynamic or allocating operator `%S' is forbidden in :abi %S kernel %s"
|
|
208
|
+
head abi-kind context-name))))
|
|
209
|
+
(t
|
|
210
|
+
(dolist (sub (cdr form))
|
|
211
|
+
(eliscript-contract--scan-abi sub abi-kind context-name))))))))
|
|
212
|
+
|
|
213
|
+
(progn
|
|
214
|
+
(defun eliscript-contract-validate-context (context-kind name plist arglist body)
|
|
215
|
+
"Validate declaration PLIST for CONTEXT-KIND NAME with ARGLIST and BODY."
|
|
216
|
+
(when (memq context-kind '(defun lambda))
|
|
217
|
+
(when (plist-get plist :async)
|
|
218
|
+
(signal 'eliscript-declaration-error
|
|
219
|
+
(list (format "ELI-D0001: ordinary %s `%s' cannot declare :async t; use defun-async or lambda-async"
|
|
220
|
+
context-kind name))))
|
|
221
|
+
(when (plist-get plist :destructure)
|
|
222
|
+
(signal 'eliscript-declaration-error
|
|
223
|
+
(list (format "ELI-D0002: ordinary %s `%s' cannot declare :destructure t; use defun-bind or lambda-bind"
|
|
224
|
+
context-kind name)))))
|
|
225
|
+
(when (and (plist-get plist :inputs)
|
|
226
|
+
(not (eq context-kind 'defmacro-checked)))
|
|
227
|
+
(signal 'eliscript-declaration-error
|
|
228
|
+
(list (format "ELI-D0006: :inputs is only valid on defmacro-checked (in %s)" name))))
|
|
229
|
+
(let ((abi (plist-get plist :abi)))
|
|
230
|
+
(when abi
|
|
231
|
+
(unless (memq abi '(:numeric-f64 :memory-u32))
|
|
232
|
+
(signal 'eliscript-declaration-error
|
|
233
|
+
(list (format "ELI-B0001: unsupported :abi %S in %s (expected :numeric-f64 or :memory-u32)"
|
|
234
|
+
abi name))))
|
|
235
|
+
(dolist (form body)
|
|
236
|
+
(eliscript-contract--scan-abi form abi name))))
|
|
237
|
+
(when (plist-get plist :export)
|
|
238
|
+
(let ((exp (plist-get plist :export)))
|
|
239
|
+
(unless (and (stringp exp) (> (length exp) 0))
|
|
240
|
+
(signal 'eliscript-declaration-error
|
|
241
|
+
(list (format "ELI-D0007: :export must be a non-empty string in %s" name))))))
|
|
242
|
+
(let ((locals (eliscript-contract--extract-arg-symbols arglist)))
|
|
243
|
+
(when (plist-get plist :pure)
|
|
244
|
+
(dolist (form body)
|
|
245
|
+
(eliscript-contract--scan-pure form locals name)))
|
|
246
|
+
(when (plist-get plist :portable)
|
|
247
|
+
(dolist (form body)
|
|
248
|
+
(eliscript-contract--scan-portable form name))))
|
|
249
|
+
(when (and name (symbolp name))
|
|
250
|
+
(puthash name plist eliscript-contract--checked-functions))
|
|
251
|
+
t)
|
|
252
|
+
|
|
253
|
+
(defun eliscript-contract-check-value (value spec context-label)
|
|
254
|
+
"Check whether VALUE satisfies type or predicate SPEC in CONTEXT-LABEL."
|
|
255
|
+
(let ((ok
|
|
256
|
+
(cond
|
|
257
|
+
((eq spec :f64) (numberp value))
|
|
258
|
+
((eq spec :i32) (integerp value))
|
|
259
|
+
((eq spec :u32) (and (integerp value) (>= value 0)))
|
|
260
|
+
((eq spec :bool) (booleanp value))
|
|
261
|
+
((eq spec :string) (stringp value))
|
|
262
|
+
((eq spec :any) t)
|
|
263
|
+
((symbolp spec)
|
|
264
|
+
(if (fboundp spec)
|
|
265
|
+
(funcall spec value)
|
|
266
|
+
t))
|
|
267
|
+
((functionp spec)
|
|
268
|
+
(funcall spec value))
|
|
269
|
+
(t t))))
|
|
270
|
+
(unless ok
|
|
271
|
+
(signal 'eliscript-runtime-contract-error
|
|
272
|
+
(list (format "Contract violation in %s: expected %S, got %S"
|
|
273
|
+
context-label spec value))))
|
|
274
|
+
t))
|
|
275
|
+
|
|
276
|
+
(defun eliscript-contract-wrap-body (name plist _arglist body)
|
|
277
|
+
"Wrap BODY with runtime checks for `:params', `:requires', `:ensures'."
|
|
278
|
+
(let ((params-spec (plist-get plist :params))
|
|
279
|
+
(requires-spec (plist-get plist :requires))
|
|
280
|
+
(returns-spec (plist-get plist :returns))
|
|
281
|
+
(ensures-spec (plist-get plist :ensures)))
|
|
282
|
+
(if (and (null params-spec)
|
|
283
|
+
(null requires-spec)
|
|
284
|
+
(null returns-spec)
|
|
285
|
+
(null ensures-spec))
|
|
286
|
+
body
|
|
287
|
+
(let ((param-checks nil)
|
|
288
|
+
(req-checks nil)
|
|
289
|
+
(ens-checks nil)
|
|
290
|
+
(res-sym (make-symbol "contract-result")))
|
|
291
|
+
(dolist (item params-spec)
|
|
292
|
+
(when (and (consp item)
|
|
293
|
+
(symbolp (car item))
|
|
294
|
+
(not (memq (car item) '(&optional &rest))))
|
|
295
|
+
(let ((p-var (car item))
|
|
296
|
+
(p-type (cadr item)))
|
|
297
|
+
(push `(eliscript-contract-check-value
|
|
298
|
+
,p-var ',p-type ,(format "%s parameter %s" name p-var))
|
|
299
|
+
param-checks))))
|
|
300
|
+
(dolist (req requires-spec)
|
|
301
|
+
(push `(unless ,req
|
|
302
|
+
(signal 'eliscript-runtime-contract-error
|
|
303
|
+
(list ,(format "Precondition (:requires %S) failed in %s" req name))))
|
|
304
|
+
req-checks))
|
|
305
|
+
(dolist (ens ensures-spec)
|
|
306
|
+
(push `(unless ,ens
|
|
307
|
+
(signal 'eliscript-runtime-contract-error
|
|
308
|
+
(list ,(format "Postcondition (:ensures %S) failed in %s" ens name))))
|
|
309
|
+
ens-checks))
|
|
310
|
+
`(,@(nreverse param-checks)
|
|
311
|
+
,@(nreverse req-checks)
|
|
312
|
+
(let ((,res-sym (progn ,@body)))
|
|
313
|
+
,@(when returns-spec
|
|
314
|
+
`((eliscript-contract-check-value
|
|
315
|
+
,res-sym ',returns-spec ,(format "%s return value" name))))
|
|
316
|
+
,@(when ens-checks
|
|
317
|
+
`((let ((result ,res-sym))
|
|
318
|
+
(ignore result)
|
|
319
|
+
,@(nreverse ens-checks))))
|
|
320
|
+
,res-sym)))))))
|
|
321
|
+
|
|
322
|
+
(defun eliscript-contract--defun-declaration-handler (fn-name args &rest specs)
|
|
323
|
+
"Handle `(declare (eliscript ...))` inside standard Emacs `defun'."
|
|
324
|
+
(let ((plist (eliscript-contract--merge-plist nil specs fn-name)))
|
|
325
|
+
(eliscript-contract-validate-context 'defun fn-name plist args nil)
|
|
326
|
+
`(put ',fn-name 'eliscript-contract ',plist)))
|
|
327
|
+
|
|
328
|
+
(unless (assq 'eliscript defun-declarations-alist)
|
|
329
|
+
(push (list 'eliscript #'eliscript-contract--defun-declaration-handler)
|
|
330
|
+
defun-declarations-alist))
|
|
331
|
+
|
|
332
|
+
(provide 'eliscript-contract)
|
|
333
|
+
|
|
334
|
+
;;; eliscript-contract.el ends here
|