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.
@@ -0,0 +1,318 @@
1
+ ;;; eliscript-js.el --- Explicit JavaScript host interop, raw sentinels, and JS regexps -*- lexical-binding: t; -*-
2
+
3
+ ;;; Commentary:
4
+
5
+ ;; Implements `eliscript-js` for Eliscript 0.1.0:
6
+ ;; - Non-nil fidelity sentinels `js-false`, `js-null`, `js-undefined` and predicates
7
+ ;; - `js-import`, `js-array`, `js-object`, `js-get`, `js-get-raw`, `js-put`, `js-call`, `js-call-raw`, `js-new`, `js-expr`
8
+ ;; - `js-divide`, `js-remainder`, `js-length` (UTF-16 code units), `js-substring`
9
+ ;; - `js-regexp`, `js-regexp-find`, `js-regexp-matches`, `js-regexp-seq`, `js-regexp-replace` (preserving Emacs `match-data`)
10
+
11
+ ;;; Code:
12
+
13
+ (require 'cl-lib)
14
+ (require 'eliscript-data)
15
+
16
+ ;; ============================================================================
17
+ ;; 1. Non-nil Fidelity Sentinels (`js-false`, `js-null`, `js-undefined`)
18
+ ;; ============================================================================
19
+
20
+ (progn
21
+ (cl-defstruct (eliscript-js-sentinel (:constructor eliscript-js-sentinel--create))
22
+ kind))
23
+
24
+ (defconst js-false (eliscript-js-sentinel--create :kind 'false)
25
+ "Non-nil sentinel representing JavaScript `false' (truthy in Lisp conditionals).")
26
+
27
+ (defconst js-null (eliscript-js-sentinel--create :kind 'null)
28
+ "Non-nil sentinel representing JavaScript `null' (truthy in Lisp conditionals).")
29
+
30
+ (defconst js-undefined (eliscript-js-sentinel--create :kind 'undefined)
31
+ "Non-nil sentinel representing JS `undefined' (truthy in Lisp).")
32
+
33
+ (defun js-false-p (val)
34
+ "Return t if VAL is the `js-false' sentinel."
35
+ (and (eliscript-js-sentinel-p val)
36
+ (eq (eliscript-js-sentinel-kind val) 'false)
37
+ t))
38
+
39
+ (defun js-null-p (val)
40
+ "Return t if VAL is the `js-null' sentinel."
41
+ (and (eliscript-js-sentinel-p val)
42
+ (eq (eliscript-js-sentinel-kind val) 'null)
43
+ t))
44
+
45
+ (defun js-undefined-p (val)
46
+ "Return t if VAL is the `js-undefined' sentinel."
47
+ (and (eliscript-js-sentinel-p val)
48
+ (eq (eliscript-js-sentinel-kind val) 'undefined)
49
+ t))
50
+
51
+ (defun js-nullish-p (val)
52
+ "Return t if VAL is `js-null' or `js-undefined'."
53
+ (or (js-null-p val) (js-undefined-p val)))
54
+
55
+ (defun eliscript-js--normalize-key (key)
56
+ "Convert KEY to string property name."
57
+ (cond
58
+ ((keywordp key) (substring (symbol-name key) 1))
59
+ ((symbolp key) (symbol-name key))
60
+ ((stringp key) key)
61
+ (t (format "%s" key))))
62
+
63
+ (defun eliscript-js--to-default-lisp (raw-val)
64
+ "Convert RAW-VAL according to the default JS -> Lisp boundary table."
65
+ (cond
66
+ ((or (js-false-p raw-val)
67
+ (js-null-p raw-val)
68
+ (js-undefined-p raw-val))
69
+ nil)
70
+ (t raw-val)))
71
+
72
+ ;; ============================================================================
73
+ ;; 2. JS Host Containers & Operations (`js-object`, `js-array`, `js-get`, `js-get-raw`, etc.)
74
+ ;; ============================================================================
75
+
76
+ (progn
77
+ (cl-defstruct (eliscript-js-object (:constructor eliscript-js-object--create))
78
+ props))
79
+
80
+ (defun js-array (&rest items)
81
+ "Create a mutable JS array representation from ITEMS."
82
+ (vconcat items))
83
+
84
+ (defun js-object (&rest kvs)
85
+ "Create a mutable JS object representation from alternating KVS."
86
+ (let ((ht (make-hash-table :test 'equal))
87
+ (cursor kvs))
88
+ (while cursor
89
+ (let ((k (eliscript-js--normalize-key (car cursor)))
90
+ (v (cadr cursor)))
91
+ (puthash k v ht))
92
+ (setq cursor (cddr cursor)))
93
+ (eliscript-js-object--create :props ht)))
94
+
95
+ (defun js-get-raw (obj key)
96
+ "Read property KEY from OBJ preserving `js-false', `js-null', and `js-undefined'."
97
+ (let ((k (eliscript-js--normalize-key key))
98
+ (missing (make-symbol "js-missing")))
99
+ (cond
100
+ ((eliscript-js-object-p obj)
101
+ (let ((val (gethash k (eliscript-js-object-props obj) missing)))
102
+ (if (eq val missing)
103
+ js-undefined
104
+ (if (null val) js-null val))))
105
+ ((hash-table-p obj)
106
+ (let ((val (gethash k obj missing)))
107
+ (if (eq val missing) js-undefined val)))
108
+ ((vectorp obj)
109
+ (if (and (integerp key) (>= key 0) (< key (length obj)))
110
+ (aref obj key)
111
+ js-undefined))
112
+ (t js-undefined))))
113
+
114
+ (defun js-get (obj key)
115
+ "Read property KEY from OBJ converting `false'/`null'/`undefined' to `nil'."
116
+ (eliscript-js--to-default-lisp (js-get-raw obj key)))
117
+
118
+ (defun js-put (obj key val)
119
+ "Set property KEY on OBJ to VAL and return VAL."
120
+ (let ((k (eliscript-js--normalize-key key)))
121
+ (cond
122
+ ((eliscript-js-object-p obj)
123
+ (puthash k val (eliscript-js-object-props obj)))
124
+ ((hash-table-p obj)
125
+ (puthash k val obj))
126
+ ((vectorp obj)
127
+ (aset obj key val)))
128
+ val))
129
+
130
+ (defun js-call-raw (target method &rest args)
131
+ "Call METHOD on TARGET with ARGS preserving raw JS sentinels."
132
+ (let ((fn (if (functionp target)
133
+ target
134
+ (js-get-raw target method))))
135
+ (if (functionp fn)
136
+ (apply fn args)
137
+ (error "js-call-raw: property %S is not callable on %S" method target))))
138
+
139
+ (defun js-call (target method &rest args)
140
+ "Call METHOD on TARGET with ARGS using default JS -> Lisp conversion."
141
+ (eliscript-js--to-default-lisp (apply #'js-call-raw target method args)))
142
+
143
+ (defun js-new (ctor &rest args)
144
+ "Instantiate CTOR with ARGS."
145
+ (if (functionp ctor)
146
+ (apply ctor args)
147
+ (js-object :ctor ctor :args (vconcat args))))
148
+
149
+ (defmacro js-import (spec &rest opts)
150
+ "Bind imported JS module SPEC according to OPTS (`:as var')."
151
+ (let ((as-var (plist-get opts :as)))
152
+ (if as-var
153
+ `(defvar ,as-var (js-object :module ,spec))
154
+ `',spec)))
155
+
156
+ (defmacro js-expr (code-str)
157
+ "Represent explicit inline JS expression CODE-STR."
158
+ `,code-str)
159
+
160
+ ;; ============================================================================
161
+ ;; 3. JS Arithmetic & UTF-16 String Operations
162
+ ;; ============================================================================
163
+
164
+ (defun js-divide (a b)
165
+ "Perform IEEE 754 floating-point division `(/ (float A) (float B))'."
166
+ (/ (float a) (float b)))
167
+
168
+ (defun js-remainder (a b)
169
+ "Perform JavaScript `%` remainder with dividend sign."
170
+ (let ((fa (float a))
171
+ (fb (float b)))
172
+ (- fa (* (truncate (/ fa fb)) fb))))
173
+
174
+ (defun eliscript-js--to-utf16-units (str)
175
+ "Convert Lisp string STR into a list of 16-bit UTF-16 code units."
176
+ (let ((units nil))
177
+ (dolist (cp (string-to-list str) (nreverse units))
178
+ (if (> cp #xffff)
179
+ (let* ((u (- cp #x10000))
180
+ (high (+ #xd800 (ash u -10)))
181
+ (low (+ #xdc00 (logand u #x3ff))))
182
+ (push high units)
183
+ (push low units))
184
+ (push cp units)))))
185
+
186
+ (defun eliscript-js--from-utf16-units (units)
187
+ "Convert a list of 16-bit UTF-16 code UNITS back to a Lisp string."
188
+ (let ((cps nil)
189
+ (cursor units))
190
+ (while cursor
191
+ (let ((u1 (car cursor))
192
+ (u2 (cadr cursor)))
193
+ (if (and (>= u1 #xd800) (<= u1 #xdbff)
194
+ u2 (>= u2 #xdc00) (<= u2 #xdfff))
195
+ (progn
196
+ (push (+ #x10000 (ash (- u1 #xd800) 10) (- u2 #xdc00)) cps)
197
+ (setq cursor (cddr cursor)))
198
+ (push u1 cps)
199
+ (setq cursor (cdr cursor)))))
200
+ (apply #'string (nreverse cps))))
201
+
202
+ (defun js-length (str)
203
+ "Return the UTF-16 code unit length of STR."
204
+ (length (eliscript-js--to-utf16-units str)))
205
+
206
+ (defun js-substring (str start &optional end)
207
+ "Return substring of STR indexed by UTF-16 code units from START to END."
208
+ (let* ((units (eliscript-js--to-utf16-units str))
209
+ (sub (cl-subseq units start end)))
210
+ (eliscript-js--from-utf16-units sub)))
211
+
212
+ ;; ============================================================================
213
+ ;; 4. JS Regexp Operations (`js-regexp*`) without Mutating Emacs `match-data`
214
+ ;; ============================================================================
215
+
216
+ (progn
217
+ (cl-defstruct (eliscript-js-regexp (:constructor eliscript-js-regexp--create))
218
+ pattern flags emacs-pattern))
219
+
220
+ (defun eliscript-js--translate-pattern-to-emacs (pat)
221
+ "Translate simple JS regex grouping in PAT to Emacs regex syntax."
222
+ (let ((out "")
223
+ (len (length pat))
224
+ (i 0))
225
+ (while (< i len)
226
+ (let ((ch (aref pat i)))
227
+ (cond
228
+ ((= ch ?\\)
229
+ (if (< (1+ i) len)
230
+ (let ((next (aref pat (1+ i))))
231
+ (cond
232
+ ((= next ?d) (setq out (concat out "[0-9]")))
233
+ ((= next ?w) (setq out (concat out "[[:alnum:]_]")))
234
+ ((= next ?s) (setq out (concat out "[ \t\n\r]")))
235
+ ((memq next '(?\( ?\) ?|))
236
+ (setq out (concat out (string next))))
237
+ (t (setq out (concat out "\\" (string next)))))
238
+ (setq i (+ i 2)))
239
+ (setq out (concat out "\\"))
240
+ (setq i (1+ i))))
241
+ ((memq ch '(?\( ?\) ?|))
242
+ (setq out (concat out "\\" (string ch)))
243
+ (setq i (1+ i)))
244
+ (t
245
+ (setq out (concat out (string ch)))
246
+ (setq i (1+ i))))))
247
+ out))
248
+
249
+ (defun js-regexp (pattern &optional flags)
250
+ "Compile JS regular expression PATTERN with optional FLAGS."
251
+ (let ((fl (or flags "")))
252
+ (dotimes (i (length fl))
253
+ (unless (memq (aref fl i) '(?g ?i ?m ?s ?u ?y ?d))
254
+ (error "Invalid JS regexp flag: %c" (aref fl i))))
255
+ (eliscript-js-regexp--create
256
+ :pattern pattern
257
+ :flags fl
258
+ :emacs-pattern (eliscript-js--translate-pattern-to-emacs pattern))))
259
+
260
+ (defun js-regexp-p (obj)
261
+ "Return t if OBJ is a `js-regexp'."
262
+ (and (eliscript-js-regexp-p obj) t))
263
+
264
+ (defun js-regexp-find (rx str &optional start-pos)
265
+ "Find first match of `js-regexp' RX in STR without modifying Emacs `match-data'."
266
+ (let ((re (if (eliscript-js-regexp-p rx)
267
+ (eliscript-js-regexp-emacs-pattern rx)
268
+ (eliscript-js--translate-pattern-to-emacs rx)))
269
+ (case-fold-search (and (eliscript-js-regexp-p rx)
270
+ (string-match-p "i" (eliscript-js-regexp-flags rx))
271
+ t)))
272
+ (save-match-data
273
+ (if (string-match re str (or start-pos 0))
274
+ (let ((full (match-string 0 str))
275
+ (m-start (match-beginning 0))
276
+ (m-end (match-end 0))
277
+ (groups nil)
278
+ (g-idx 1))
279
+ (while (match-beginning g-idx)
280
+ (push (match-string g-idx str) groups)
281
+ (setq g-idx (1+ g-idx)))
282
+ (persistent-map
283
+ :match full
284
+ :start m-start
285
+ :end m-end
286
+ :groups (apply #'persistent-vector (nreverse groups))))
287
+ nil))))
288
+
289
+ (defun js-regexp-matches (rx str)
290
+ "Return t if `js-regexp' RX matches in STR without altering `match-data'."
291
+ (and (js-regexp-find rx str) t))
292
+
293
+ (defun js-regexp-seq (rx str)
294
+ "Return a persistent-vector of all non-overlapping matches of RX in STR."
295
+ (let ((acc nil)
296
+ (pos 0)
297
+ (len (length str))
298
+ (keep-going t))
299
+ (while (and (<= pos len) keep-going)
300
+ (let ((m (js-regexp-find rx str pos)))
301
+ (if m
302
+ (let ((m-end (value-get m :end)))
303
+ (push m acc)
304
+ (setq pos (if (= m-end pos) (1+ pos) m-end)))
305
+ (setq keep-going nil))))
306
+ (apply #'persistent-vector (nreverse acc))))
307
+
308
+ (defun js-regexp-replace (rx str replacement)
309
+ "Replace matches of `js-regexp' RX in STR with REPLACEMENT cleanly."
310
+ (let ((re (if (eliscript-js-regexp-p rx)
311
+ (eliscript-js-regexp-emacs-pattern rx)
312
+ (eliscript-js--translate-pattern-to-emacs rx))))
313
+ (save-match-data
314
+ (replace-regexp-in-string re replacement str t t))))
315
+
316
+ (provide 'eliscript-js)
317
+
318
+ ;;; eliscript-js.el ends here
@@ -2,7 +2,7 @@
2
2
 
3
3
  ;; Author: 刘冲 <mail@liuchong.dev>
4
4
  ;; Maintainer: 刘冲 <mail@liuchong.dev>
5
- ;; Version: 0.0.2
5
+ ;; Version: 0.1.0
6
6
  ;; Package-Requires: ((emacs "29.1"))
7
7
  ;; Keywords: languages, lisp
8
8
  ;; URL: https://github.com/liuchong/eliscript
@@ -0,0 +1,340 @@
1
+ ;;; eliscript-module.el --- Module system, lexical private bindings, and checked macros -*- lexical-binding: t; -*-
2
+
3
+ ;;; Commentary:
4
+
5
+ ;; Implements `eliscript-module` for Eliscript 0.1.0:
6
+ ;; - `defmodule` with `defun-lexical`, `defvar-lexical`, `defconst-lexical`
7
+ ;; - Dual-namespace `export-functions`, `export-values`, `import-functions`, `import-values`
8
+ ;; - Live value binding propagation and module reload semantics
9
+ ;; - Deterministic checked macros (`defmacro-checked`, `macro-gensym`, `macro-read-file`)
10
+
11
+ ;;; Code:
12
+
13
+ (require 'cl-lib)
14
+ (require 'eliscript-contract)
15
+
16
+ (defvar eliscript-module--registry (make-hash-table :test 'eq)
17
+ "Registry of defined modules keyed by module symbol.")
18
+
19
+ (defvar eliscript-module--active-macro-inputs nil
20
+ "List of declared `:inputs' paths for the currently expanding `defmacro-checked'.")
21
+
22
+ (defvar eliscript-module--gensym-counter 0
23
+ "Deterministic counter for `macro-gensym' within an expansion session.")
24
+
25
+ (progn
26
+ (defun eliscript-module-reset-state! ()
27
+ "Reset module registry and macro counters for isolated test runs."
28
+ (clrhash eliscript-module--registry)
29
+ (setq eliscript-module--active-macro-inputs nil)
30
+ (setq eliscript-module--gensym-counter 0)))
31
+
32
+ ;; ============================================================================
33
+ ;; 1. Module Definition (`defmodule`) and Lexical Privacy
34
+ ;; ============================================================================
35
+
36
+ (defun eliscript-module--check-const-assignments (form const-names mod-name)
37
+ "Signal `eliscript-compile-error' if FORM assigns to any symbol in CONST-NAMES."
38
+ (when (and const-names (consp form))
39
+ (let ((head (car form)))
40
+ (cond
41
+ ((eq head 'quote) nil)
42
+ ((memq head '(setq setf))
43
+ (let ((cursor (cdr form)))
44
+ (while cursor
45
+ (let ((target (car cursor))
46
+ (val (cadr cursor)))
47
+ (when (and (symbolp target) (memq target const-names))
48
+ (signal 'eliscript-compile-error
49
+ (list (format "ELI-M0002: cannot reassign defconst-lexical `%S' in module %s"
50
+ target mod-name))))
51
+ (eliscript-module--check-const-assignments val const-names mod-name))
52
+ (setq cursor (cddr cursor)))))
53
+ (t
54
+ (dolist (sub (cdr form))
55
+ (eliscript-module--check-const-assignments sub const-names mod-name)))))))
56
+
57
+ (defun eliscript-module--register (mod-name fn-alist val-getter-alist)
58
+ "Register or reload module MOD-NAME with FN-ALIST and VAL-GETTER-ALIST."
59
+ (let* ((existing (gethash mod-name eliscript-module--registry))
60
+ (subscribers (if existing (plist-get existing :subscribers) (make-hash-table :test 'eq)))
61
+ (fn-table (make-hash-table :test 'eq))
62
+ (val-table (make-hash-table :test 'eq)))
63
+ (dolist (pair fn-alist)
64
+ (puthash (car pair) (cdr pair) fn-table))
65
+ (dolist (pair val-getter-alist)
66
+ (puthash (car pair) (cdr pair) val-table))
67
+ (puthash mod-name
68
+ (list :name mod-name
69
+ :functions fn-table
70
+ :values val-table
71
+ :subscribers subscribers)
72
+ eliscript-module--registry)
73
+ (eliscript-module--sync-subscribers mod-name)
74
+ mod-name))
75
+
76
+ (defun eliscript-module--sync-subscribers (mod-name)
77
+ "Update all live-bound value subscribers for module MOD-NAME."
78
+ (let ((entry (gethash mod-name eliscript-module--registry)))
79
+ (when entry
80
+ (let ((val-table (plist-get entry :values))
81
+ (subscribers (plist-get entry :subscribers)))
82
+ (maphash
83
+ (lambda (exported-sym target-syms)
84
+ (let ((getter (gethash exported-sym val-table)))
85
+ (when getter
86
+ (let ((current-val (funcall getter)))
87
+ (dolist (tsym target-syms)
88
+ (set tsym current-val))))))
89
+ subscribers)))))
90
+
91
+ (defmacro defmodule (name &rest body)
92
+ "Define module NAME with private `*-lexical' bindings and explicit exports."
93
+ (let* ((extracted (eliscript-contract-extract-declarations body name))
94
+ ( clean-body (plist-get extracted :body) )
95
+ (lex-vars nil)
96
+ (const-names nil)
97
+ (lex-fns nil)
98
+ (pub-defuns nil)
99
+ (other-forms nil)
100
+ (exported-fns nil)
101
+ (exported-vals nil))
102
+ (dolist (form clean-body)
103
+ (if (not (consp form))
104
+ (push form other-forms)
105
+ (let ((head (car form)))
106
+ (cond
107
+ ((eq head 'defconst-lexical)
108
+ (let ((sym (nth 1 form))
109
+ (init (nth 2 form)))
110
+ (push sym const-names)
111
+ (push (list sym init) lex-vars)))
112
+ ((eq head 'defvar-lexical)
113
+ (let ((sym (nth 1 form))
114
+ (init (nth 2 form)))
115
+ (push (list sym init) lex-vars)))
116
+ ((eq head 'defun-lexical)
117
+ (let ((fn-sym (nth 1 form))
118
+ (fn-args (nth 2 form))
119
+ (fn-body (nthcdr 3 form)))
120
+ (push (list fn-sym fn-args fn-body) lex-fns)))
121
+ ((eq head 'defun)
122
+ (let ((fn-sym (nth 1 form))
123
+ (fn-args (nth 2 form))
124
+ (fn-body (nthcdr 3 form)))
125
+ (push (list fn-sym fn-args fn-body) pub-defuns)))
126
+ ((eq head 'export-functions)
127
+ (dolist (s (cdr form))
128
+ (push s exported-fns)))
129
+ ((eq head 'export-values)
130
+ (dolist (s (cdr form))
131
+ (push s exported-vals)))
132
+ (t
133
+ (push form other-forms))))))
134
+ (setq lex-vars (nreverse lex-vars))
135
+ (setq const-names (nreverse const-names))
136
+ (setq lex-fns (nreverse lex-fns))
137
+ (setq pub-defuns (nreverse pub-defuns))
138
+ (setq other-forms (nreverse other-forms))
139
+ (setq exported-fns (nreverse exported-fns))
140
+ (setq exported-vals (nreverse exported-vals))
141
+ ;; Static check for assignments to defconst-lexical
142
+ (dolist (form clean-body)
143
+ (unless (and (consp form) (eq (car form) 'defconst-lexical))
144
+ (eliscript-module--check-const-assignments form const-names name)))
145
+ (let ((label-defs
146
+ (mapcar (lambda (lf)
147
+ `(,(nth 0 lf) ,(nth 1 lf) ,@(nth 2 lf)))
148
+ lex-fns))
149
+ (pub-defs
150
+ (mapcar (lambda (pf)
151
+ (let ((psym (nth 0 pf))
152
+ (pargs (nth 1 pf))
153
+ (pbody (nth 2 pf))
154
+ (res-sym (make-symbol "mod-fn-res")))
155
+ `(fset ',psym
156
+ (lambda ,pargs
157
+ (let ((,res-sym (progn ,@pbody)))
158
+ (eliscript-module--sync-subscribers ',name)
159
+ ,res-sym)))))
160
+ pub-defuns))
161
+ (fn-export-pairs
162
+ (mapcar (lambda (fsym)
163
+ `(cons ',fsym (symbol-function ',fsym)))
164
+ exported-fns))
165
+ (val-export-pairs
166
+ (mapcar (lambda (vsym)
167
+ `(cons ',vsym (lambda () ,vsym)))
168
+ exported-vals)))
169
+ `(let* ,lex-vars
170
+ (cl-labels ,label-defs
171
+ ,@other-forms
172
+ ,@pub-defs
173
+ (eliscript-module--register
174
+ ',name
175
+ (list ,@fn-export-pairs)
176
+ (list ,@val-export-pairs)))))))
177
+
178
+ ;; Top-level fallback macros if used outside `defmodule'
179
+ (defmacro defun-lexical (name args &rest body)
180
+ "Define lexical function NAME with ARGS and BODY."
181
+ `(defun ,name ,args ,@body))
182
+
183
+ (defmacro defvar-lexical (name &optional initvalue docstring)
184
+ "Define lexical variable NAME with INITVALUE and DOCSTRING."
185
+ `(defvar ,name ,initvalue ,@(when docstring (list docstring))))
186
+
187
+ (defmacro defconst-lexical (name initvalue &optional docstring)
188
+ "Define lexical constant NAME with INITVALUE and DOCSTRING."
189
+ `(defconst ,name ,initvalue ,@(when docstring (list docstring))))
190
+
191
+ (defmacro export-functions (&rest symbols)
192
+ "Declare exported function SYMBOLS."
193
+ `',symbols)
194
+
195
+ (defmacro export-values (&rest symbols)
196
+ "Declare exported value SYMBOLS."
197
+ `',symbols)
198
+
199
+ ;; ============================================================================
200
+ ;; 2. Dual-Namespace Imports (`import-functions`, `import-values`)
201
+ ;; ============================================================================
202
+
203
+ (defun eliscript-module--parse-import-spec (spec)
204
+ "Parse `:only', `:rename', and `:as' options from import SPEC."
205
+ (let ((only (plist-get spec :only))
206
+ (rename (plist-get spec :rename))
207
+ (as-prefix (plist-get spec :as)))
208
+ (list :only only :rename rename :as as-prefix)))
209
+
210
+ (defun eliscript-module--target-symbol (sym rename-alist as-prefix)
211
+ "Compute local imported symbol name for SYM given RENAME-ALIST and AS-PREFIX."
212
+ (let* ((renamed (cadr (assq sym rename-alist)))
213
+ (base (or renamed sym)))
214
+ (if as-prefix
215
+ (intern (format "%s%s" as-prefix base))
216
+ base)))
217
+
218
+ (defun eliscript-module--import-functions-runtime (mod-name specs)
219
+ "Import functions from MOD-NAME according to SPECS."
220
+ (let ((entry (gethash mod-name eliscript-module--registry)))
221
+ (unless entry
222
+ (signal 'eliscript-compile-error
223
+ (list (format "ELI-M0003: unknown module `%S' in import-functions" mod-name))))
224
+ (let* ((fn-table (plist-get entry :functions))
225
+ (parsed (eliscript-module--parse-import-spec specs))
226
+ (only (plist-get parsed :only))
227
+ (rename (plist-get parsed :rename))
228
+ (as-prefix (plist-get parsed :as))
229
+ (symbols-to-import
230
+ (or only
231
+ (let (ks)
232
+ (maphash (lambda (k _v) (push k ks)) fn-table)
233
+ ks))))
234
+ (dolist (sym symbols-to-import)
235
+ (let ((fn (gethash sym fn-table)))
236
+ (unless fn
237
+ (signal 'eliscript-compile-error
238
+ (list (format "ELI-M0004: function `%S' is not exported by module %s"
239
+ sym mod-name))))
240
+ (let ((target (eliscript-module--target-symbol sym rename as-prefix)))
241
+ (fset target
242
+ (lambda (&rest args)
243
+ (let* ((cur-entry (gethash mod-name eliscript-module--registry))
244
+ (cur-fn (gethash sym (plist-get cur-entry :functions))))
245
+ (apply cur-fn args))))))))))
246
+
247
+ (defun eliscript-module--import-values-runtime (mod-name specs)
248
+ "Import live value bindings from MOD-NAME according to SPECS."
249
+ (let ((entry (gethash mod-name eliscript-module--registry)))
250
+ (unless entry
251
+ (signal 'eliscript-compile-error
252
+ (list (format "ELI-M0003: unknown module `%S' in import-values" mod-name))))
253
+ (let* ((val-table (plist-get entry :values))
254
+ (subscribers (plist-get entry :subscribers))
255
+ (parsed (eliscript-module--parse-import-spec specs))
256
+ (only (plist-get parsed :only))
257
+ (rename (plist-get parsed :rename))
258
+ (as-prefix (plist-get parsed :as))
259
+ (symbols-to-import
260
+ (or only
261
+ (let (ks)
262
+ (maphash (lambda (k _v) (push k ks)) val-table)
263
+ ks))))
264
+ (dolist (sym symbols-to-import)
265
+ (let ((getter (gethash sym val-table)))
266
+ (unless getter
267
+ (signal 'eliscript-compile-error
268
+ (list (format "ELI-M0005: value `%S' is not exported by module %s"
269
+ sym mod-name))))
270
+ (let ((target (eliscript-module--target-symbol sym rename as-prefix)))
271
+ (set target (funcall getter))
272
+ (let ((existing-subs (gethash sym subscribers)))
273
+ (unless (memq target existing-subs)
274
+ (puthash sym (cons target existing-subs) subscribers)))))))))
275
+
276
+ (defmacro import-functions (mod-name &rest specs)
277
+ "Import exported functions from MOD-NAME with optional `:only', `:rename', `:as'."
278
+ `(eliscript-module--import-functions-runtime ',mod-name ',specs))
279
+
280
+ (defmacro import-values (mod-name &rest specs)
281
+ "Import exported live values from MOD-NAME using SPECS."
282
+ `(eliscript-module--import-values-runtime ',mod-name ',specs))
283
+
284
+ ;; ============================================================================
285
+ ;; 3. Checked Deterministic Macros (`defmacro-checked`, `macro-gensym`, `macro-read-file`)
286
+ ;; ============================================================================
287
+
288
+ (defun macro-gensym (&optional prefix)
289
+ "Generate a deterministic hygienic symbol with optional PREFIX."
290
+ (setq eliscript-module--gensym-counter (1+ eliscript-module--gensym-counter))
291
+ (make-symbol (format "eli-gensym-%s-%d" (or prefix "g") eliscript-module--gensym-counter)))
292
+
293
+ (defun macro-read-file (rel-path)
294
+ "Read declared input file REL-PATH inside `defmacro-checked'."
295
+ (unless (and (stringp rel-path)
296
+ (not (file-name-absolute-p rel-path))
297
+ (not (string-match-p "\\.\\." rel-path)))
298
+ (signal 'eliscript-compile-error
299
+ (list (format "ELI-M0010: invalid or non-relative path `%S' in macro-read-file" rel-path))))
300
+ (unless (member rel-path eliscript-module--active-macro-inputs)
301
+ (signal 'eliscript-compile-error
302
+ (list (format "ELI-M0011: undeclared file `%s' in macro-read-file; declare it in (declare (eliscript :inputs (...)))"
303
+ rel-path))))
304
+ (with-temp-buffer
305
+ (insert-file-contents rel-path)
306
+ (buffer-string)))
307
+
308
+ (defun eliscript-module--scan-macro-read-file (form inputs macro-name)
309
+ "Statistically verify any `macro-read-file' in FORM is declared in INPUTS."
310
+ (when (consp form)
311
+ (let ((head (car form)))
312
+ (unless (eq head 'quote)
313
+ (when (eq head 'macro-read-file)
314
+ (let ((arg (cadr form)))
315
+ (when (or (null inputs)
316
+ (and (stringp arg) (not (member arg inputs)))
317
+ (and (stringp arg) (or (file-name-absolute-p arg)
318
+ (string-match-p "\\.\\." arg))))
319
+ (signal 'eliscript-compile-error
320
+ (list (format "ELI-M0011: undeclared or invalid file `%S' in defmacro-checked %s"
321
+ arg macro-name))))))
322
+ (dolist (sub (cdr form))
323
+ (eliscript-module--scan-macro-read-file sub inputs macro-name))))))
324
+
325
+ (defmacro defmacro-checked (name args &rest body)
326
+ "Define a deterministic, reproducible macro NAME with ARGS and BODY."
327
+ (let* ((extracted (eliscript-contract-extract-declarations body name))
328
+ (plist (plist-get extracted :eliscript))
329
+ (clean-body (plist-get extracted :body))
330
+ (inputs (plist-get plist :inputs)))
331
+ (eliscript-contract-validate-context 'defmacro-checked name plist args clean-body)
332
+ (dolist (form clean-body)
333
+ (eliscript-module--scan-macro-read-file form inputs name))
334
+ `(defmacro ,name ,args
335
+ (let ((eliscript-module--active-macro-inputs ',inputs))
336
+ ,@clean-body))))
337
+
338
+ (provide 'eliscript-module)
339
+
340
+ ;;; eliscript-module.el ends here
@@ -2,7 +2,7 @@
2
2
 
3
3
  ;; Author: 刘冲 <mail@liuchong.dev>
4
4
  ;; Maintainer: 刘冲 <mail@liuchong.dev>
5
- ;; Version: 0.0.2
5
+ ;; Version: 0.1.0
6
6
  ;; Package-Requires: ((emacs "29.1"))
7
7
  ;; Keywords: languages, lisp
8
8
  ;; URL: https://github.com/liuchong/eliscript
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eliscript",
3
- "version": "0.0.4",
3
+ "version": "0.1.0",
4
4
  "description": "An Emacs Lisp-flavored language that compiles to modern JavaScript.",
5
5
  "license": "GPL-3.0-or-later",
6
6
  "repository": {
@@ -20,6 +20,8 @@
20
20
  },
21
21
  "exports": {
22
22
  "./runtime/math.mjs": "./runtime/math.mjs",
23
+ "./runtime/emacs-compat": "./runtime/emacs-compat.mjs",
24
+ "./runtime/emacs-compat.mjs": "./runtime/emacs-compat.mjs",
23
25
  "./runtime/unicode-case": "./runtime/unicode-case.mjs",
24
26
  "./runtime/unicode-case.mjs": "./runtime/unicode-case.mjs",
25
27
  "./compiler": "./dist/bootstrap/compiler.mjs",