-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
elisp: fix new tests, byte-compile, various improvements
The original motivation is to fix the new (= nil ()) and core_apply_accepts_macros tests. Improve speed and warnings with byte compilation. mal/core.el: Wrap core functions during the loop in main, instead of writing the conversion in each line of core-ns. Use apply built-in concatenation of last argument. Move handling of metadata to types.el. mal/env.el: Represent environments as cons cells instead of vectors. mal/func.el: Merged into types.el, it is not a special case anymore. mal/printer.el: Add macro case. Define a pr-join helper for sequences and core.el. mal/reader.el: Rename the tokens local variable in reader.el (compiler warning). mal/types.el: Use type-specific accessors returning nil for the wrong type (structural pattern matching would be better, but is too slow). Represent native types directly when possible, and inline some trivial accessors. Use dedicated records instead of vectors. Implement metadata only when required. Represent keywords as strings (easyer, no counterpart). run: Run byte-compiled version. steps: Backport good ideas from stepA to step1, reducing the diff between steps for future maintenance. Implement 'do with a simple iteration (without map and butlast). Make the repl-env local to main (compiler warning). Make the code more idiomatic * prefer loop over recursion (search in environments) * declare variable and reassign them when convenient (exit of the TCO loop) * car cdr cadr and so on instead of nth * remove various vector <-> list conversions.
- Loading branch information
1 parent
1afe9ee
commit 2259e1d
Showing
19 changed files
with
1,274 additions
and
1,227 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
all: | ||
emacs -Q --batch -L . --eval '(byte-recompile-directory "." 0)' | ||
|
||
# For debugging, it is sometimes useful to attempt a run without byte compation. | ||
nocompile: clean | ||
exec emacs -Q --batch -L . --eval "(setq text-quoting-style 'straight)" --load stepA_mal.el | ||
|
||
clean: | ||
rm -f *.elc *~ mal/*.elc mal/*~ |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,27 @@ | ||
(require 'mal/types) | ||
|
||
;; An env is represented by an elisp list of hash-tables. In other words | ||
;; * car: a hash-table | ||
;; * cdr: the outer environment or () | ||
;; Keys are elisp symbols. | ||
|
||
(defun mal-env (&optional outer binds exprs) | ||
(let ((env (vector 'env (vector (make-hash-table :test 'eq) outer)))) | ||
(while binds | ||
(let ((key (pop binds))) | ||
(if (eq key '&) | ||
(let ((key (pop binds)) | ||
(value (mal-list exprs))) | ||
(mal-env-set env key value) | ||
(setq binds nil | ||
exprs nil)) | ||
(let ((value (pop exprs))) | ||
(mal-env-set env key value))))) | ||
(let ((env (cons (make-hash-table :test 'eq) outer)) | ||
key) | ||
(while (setq key (pop binds)) | ||
(if (eq key '&) | ||
(mal-env-set env (pop binds) (mal-list exprs)) | ||
(mal-env-set env key (pop exprs)))) | ||
env)) | ||
|
||
(defun mal-env-set (env key value) | ||
(let ((data (aref (aref env 1) 0))) | ||
(let ((data (car env))) | ||
(puthash key value data))) | ||
|
||
(defun mal-env-get (env key) | ||
(let* ((data (aref (aref env 1) 0)) | ||
(value (gethash key data))) | ||
(or value | ||
(let ((outer (aref (aref env 1) 1))) | ||
(if outer | ||
(mal-env-get outer key) | ||
nil))))) | ||
(let (value) | ||
(while (and (not (setq value (gethash key (pop env)))) | ||
env)) | ||
value)) | ||
|
||
(provide 'mal/env) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,60 @@ | ||
(require 'cl-lib) | ||
(require 'mal/types) | ||
|
||
(defun pr-str (form &optional print-readably) | ||
(let ((value (mal-value form))) | ||
(cl-ecase (mal-type form) | ||
('nil | ||
(defun pr-str (form print-readably) | ||
(let (value) | ||
(cond | ||
((eq mal-nil form) | ||
"nil") | ||
(true | ||
((eq mal-true form) | ||
"true") | ||
(false | ||
((eq mal-false form) | ||
"false") | ||
(number | ||
((setq value (mal-number-value form)) | ||
(number-to-string value)) | ||
(string | ||
((setq value (mal-string-value form)) | ||
(if print-readably | ||
(let ((print-escape-newlines t)) | ||
(prin1-to-string value)) | ||
value)) | ||
((symbol keyword) | ||
((setq value (mal-symbol-value form)) | ||
(symbol-name value)) | ||
(list | ||
((setq value (mal-keyword-value form)) | ||
value) | ||
((setq value (mal-list-value form)) | ||
(pr-list value print-readably)) | ||
(vector | ||
((mal-list-p form) | ||
"()") | ||
((setq value (mal-vector-value form)) | ||
(pr-vector value print-readably)) | ||
(map | ||
((setq value (mal-map-value form)) | ||
(pr-map value print-readably)) | ||
(fn | ||
"#<fn>") | ||
(func | ||
"#<func>") | ||
(atom | ||
(format "(atom %s)" (pr-str value print-readably)))))) | ||
((or (mal-fn-core-value form) (mal-func-value form)) | ||
"#<function>") | ||
((mal-macro-value form) | ||
"#<macro>") | ||
((setq value (mal-atom-value form)) | ||
(format "(atom %s)" (pr-str value print-readably))) | ||
(t (error "pr-str: unknown type: %s" form))))) | ||
|
||
(defun pr-list (form print-readably) | ||
(let ((items (mapconcat | ||
(lambda (item) (pr-str item print-readably)) | ||
form " "))) | ||
(let ((items (pr-join form print-readably " "))) | ||
(concat "(" items ")"))) | ||
|
||
(defun pr-vector (form print-readably) | ||
(let ((items (mapconcat | ||
(lambda (item) (pr-str item print-readably)) | ||
(append form nil) " "))) | ||
(let ((items (pr-join form print-readably " "))) | ||
(concat "[" items "]"))) | ||
|
||
(defun pr-map (form print-readably) | ||
(let (pairs) | ||
(maphash | ||
(lambda (key value) | ||
(push (cons (pr-str key print-readably) | ||
(pr-str value print-readably)) | ||
pairs)) | ||
(push value pairs) | ||
(push key pairs)) | ||
form) | ||
(let ((items (mapconcat | ||
(lambda (item) (concat (car item) " " (cdr item))) | ||
(nreverse pairs) " "))) | ||
(let ((items (pr-join pairs print-readably " "))) | ||
(concat "{" items "}")))) | ||
|
||
(defun pr-join (forms print-readably separator) | ||
(mapconcat (lambda (item) (pr-str item print-readably)) forms separator)) | ||
|
||
(provide 'mal/printer) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
#!/bin/bash | ||
exec emacs -Q --batch -L $(dirname $0) --eval "(setq text-quoting-style 'straight)" --load $(dirname $0)/${STEP:-stepA_mal}.el "${@}" | ||
#!/bin/sh | ||
dir=$(dirname $0) | ||
exec emacs -Q --batch -L $dir --eval "(setq text-quoting-style 'straight)" --load $dir/${STEP:-stepA_mal}.elc "${@}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.