id
int64 0
45.1k
| file_name
stringlengths 4
68
| file_path
stringlengths 14
193
| content
stringlengths 32
9.62M
| size
int64 32
9.62M
| language
stringclasses 1
value | extension
stringclasses 6
values | total_lines
int64 1
136k
| avg_line_length
float64 3
903k
| max_line_length
int64 3
4.51M
| alphanum_fraction
float64 0
1
| repo_name
stringclasses 779
values | repo_stars
int64 0
882
| repo_forks
int64 0
108
| repo_open_issues
int64 0
90
| repo_license
stringclasses 8
values | repo_extraction_date
stringclasses 146
values | sha
stringlengths 64
64
| __index_level_0__
int64 0
45.1k
| exdup_ids_cmlisp_stkv2
sequencelengths 1
47
|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
8,688 | simple-gui.asd | ailisp_simple-gui/simple-gui.asd | ;;;; cl-gui.asd
(defsystem #:simple-gui
:name "simple-gui"
:description "A declarative GUI definition tool for Common Lisp"
:author "Bo Yao"
:license "BSD"
:version "0.1"
:serial t
:components ((:file "package")
(:file "utils" )
(:file "qt-utils")
(:file "simple-gui"))
:depends-on (:qt :alexandria :cl-ppcre :named-readtables))
| 377 | Common Lisp | .asd | 13 | 24.307692 | 66 | 0.62259 | ailisp/simple-gui | 15 | 5 | 0 | LGPL-3.0 | 9/19/2024, 11:26:41 AM (Europe/Amsterdam) | e5881fbf73811b93edc7d8758dee5b5ebb4b8bf1e77b37463d3d091f84f5fa51 | 8,688 | [
-1
] |
8,712 | package.lisp | bohonghuang_cl-gobject-introspection-wrapper/package.lisp | ;;;; package.lisp
;;;; Copyright (C) 2022-2023 Bohong Huang
;;;;
;;;; This program is free software: you can redistribute it and/or modify
;;;; it under the terms of the GNU Lesser General Public License as published by
;;;; the Free Software Foundation, either version 3 of the License, or
;;;; (at your option) any later version.
;;;;
;;;; This program is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;;;; GNU Lesser General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU Lesser General Public License
;;;; along with this program. If not, see <https://www.gnu.org/licenses/>.
(defpackage gobject-introspection-wrapper
(:use #:cl #:alexandria)
(:nicknames #:gir-wrapper)
(:export #:*quoted-name-alist*
#:*class*
#:*namespace*
#:define-gir-class
#:define-gir-namespace
#:define-gir-constant
#:define-gir-enum
#:define-gir-function
#:pointer-object
#:object-pointer))
| 1,153 | Common Lisp | .lisp | 28 | 36.357143 | 80 | 0.673197 | bohonghuang/cl-gobject-introspection-wrapper | 17 | 4 | 1 | LGPL-3.0 | 9/19/2024, 11:26:41 AM (Europe/Amsterdam) | d368ad83301eb44890d3b93d686c40e236f6c9c62b61e135bc0cef39ab53b1bc | 8,712 | [
-1
] |
8,713 | util.lisp | bohonghuang_cl-gobject-introspection-wrapper/util.lisp | ;;;; util.lisp
;;;; Copyright (C) 2022-2023 Bohong Huang
;;;;
;;;; This program is free software: you can redistribute it and/or modify
;;;; it under the terms of the GNU Lesser General Public License as published by
;;;; the Free Software Foundation, either version 3 of the License, or
;;;; (at your option) any later version.
;;;;
;;;; This program is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;;;; GNU Lesser General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU Lesser General Public License
;;;; along with this program. If not, see <https://www.gnu.org/licenses/>.
(in-package #:gir)
(defmethod info-of ((desc callable-desc))
(slot-value desc 'info))
(in-package #:gir-wrapper)
(defgeneric pointer-object (pointer type)
(:documentation "Construct GObject from a CFFI pointer."))
(eval-when (:compile-toplevel :load-toplevel :execute)
(setf (fdefinition 'object-pointer) (fdefinition 'gir::this-of)))
(defun subclassp (class-a class-b)
(loop :for class := class-a :then (gir:parent-of class)
:while class
:thereis (gir:info-equal (gir::info-of class) (gir::info-of class-b))))
(defun class-instance-p (instance class)
(subclassp (typecase instance
(gir::object-instance (gir:gir-class-of instance))
(gir::struct-instance (gir::struct-class-of instance))
(t (return-from class-instance-p nil)))
class))
(defun interface-instance-p (instance interface)
(loop :with interface-info := (gir::info-of interface)
:for info :in (gir::interface-infos-of (typecase instance
(gir::object-instance (gir:gir-class-of instance))
(gir::struct-instance (gir::struct-class-of instance))
(t (return-from interface-instance-p nil))))
:thereis (gir:info-equal info interface-info)) )
| 2,110 | Common Lisp | .lisp | 40 | 45.3 | 103 | 0.655507 | bohonghuang/cl-gobject-introspection-wrapper | 17 | 4 | 1 | LGPL-3.0 | 9/19/2024, 11:26:41 AM (Europe/Amsterdam) | d0323d3e0b963e741a350e980eaa0f48eb14043e950758f8dbc975558c5d6c6a | 8,713 | [
-1
] |
8,714 | desc.lisp | bohonghuang_cl-gobject-introspection-wrapper/desc.lisp | ;;;; desc.lisp
;;;; Copyright (C) 2022-2023 Bohong Huang
;;;;
;;;; This program is free software: you can redistribute it and/or modify
;;;; it under the terms of the GNU Lesser General Public License as published by
;;;; the Free Software Foundation, either version 3 of the License, or
;;;; (at your option) any later version.
;;;;
;;;; This program is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;;;; GNU Lesser General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU Lesser General Public License
;;;; along with this program. If not, see <https://www.gnu.org/licenses/>.
(in-package #:gir-wrapper)
(defparameter *namespace* nil)
(defparameter *class* nil)
(defparameter *quoted-name-alist* nil)
(defun quoted-name-symbol (name)
(multiple-value-bind (value exists-p) (assoc-value *quoted-name-alist* name :test #'equal)
(when (and (not value) exists-p)
(throw 'skip nil))
value))
(defun camel-case->lisp-name (phrase)
(string-right-trim "-" (nstring-upcase (cl-ppcre:regex-replace-all "((?<!\d)(\d+))|(([A-Z][a-z]+))|((?<![a-z])([a-z]+))|((?<![A-Z])(([A-Z](?![a-z]))+))" phrase "\\1\\3\\5\\7-"))))
(defun camel-case->lisp-symbol (phrase)
(intern (string-upcase (camel-case->lisp-name phrase))))
(defun underscores->lisp-name (phrase)
(substitute #\- #\_ phrase))
(defun underscores->lisp-symbol (phrase &optional case-sensitive-p)
(intern (funcall (if case-sensitive-p #'identity #'string-upcase) (underscores->lisp-name phrase))))
(defun callable-desc-argument-names (desc)
(let ((argument-name-case-sensitive-p nil))
(flet ((desc-args ()
(mapcar
(lambda (desc)
(let ((name (gir:name-of desc)))
(or (quoted-name-symbol name) (underscores->lisp-symbol name argument-name-case-sensitive-p))))
(gir:arguments-desc-of desc))))
(let ((args (desc-args)))
(unless (= (length (remove-duplicates args)) (length args))
(setf argument-name-case-sensitive-p t
args (desc-args)))
(values args)))))
(defun transform-class-desc (desc &optional (namespace *namespace*) (class *class*))
(catch 'skip
(let* ((class-symbol (or (quoted-name-symbol class) (camel-case->lisp-symbol class)))
(pred-symbol (symbolicate class-symbol (if (find #\- (symbol-name class-symbol)) '#:-p '#:p))))
`((defun ,pred-symbol (instance)
(class-instance-p instance (gir:nget ,namespace ,class)))
(deftype ,class-symbol ()
'(satisfies ,pred-symbol))
(defmethod gir-wrapper:pointer-object (pointer (type (eql ',class-symbol)))
(declare (ignore type))
(make-instance ',(etypecase desc
(gir::object-class 'gir::object-instance)
(gir::struct-class 'gir::struct-instance))
:class (gir:nget ,namespace ,class)
:this pointer))))))
(defun transform-interface-desc (desc &optional (namespace *namespace*) (class *class*))
(declare (ignore desc))
(catch 'skip
(let* ((interface-symbol (or (quoted-name-symbol class) (camel-case->lisp-symbol class)))
(pred-symbol (symbolicate interface-symbol (if (find #\- (symbol-name interface-symbol)) '#:-p '#:p))))
`((defun ,pred-symbol (instance)
(interface-instance-p instance (gir:nget ,namespace ,class)))
(deftype ,interface-symbol ()
'(satisfies ,pred-symbol))))))
(defparameter +getter-pattern-1-base+ "(?:(.+?(?=\\bIS\\b))?(?:IS-)?(.+))") ; xxx_is_xxx, xxx
(defparameter +getter-pattern-1+ (format nil "(?:(.*?(?=\\b(?:GET|IS)\\b))(?:GET-|IS-)~A)" +getter-pattern-1-base+)) ; get_xxx_is_xxx, get_xxx, is_xxx, get_is_xxx
(defparameter +getter-pattern-2+ "(.*?(?=\\b(?:HAS|SHOULD|CAN)\\b)(?:HAS|SHOULD|CAN)-.+)") ; xxx_should_xxx, xxx_has_xxx, has_xxx, should_xxx
(defparameter +getter-pattern+ (format nil "(?:~A|~A)" +getter-pattern-1+ +getter-pattern-2+))
(defparameter +setter-pattern+ (format nil "(?:SET-(?:~A|~A))" +getter-pattern-1-base+ +getter-pattern-2+))
(defparameter +constructor-pattern+ "^(NEW|CREATE)(-WITH|-FROM|-FOR|$)?(-.+|$)")
(defun scan-to-string (regex target-string)
(multiple-value-bind (match-string groups) (ppcre:scan-to-strings regex target-string)
(when (and match-string (= (length match-string) (length target-string)))
(loop :with string := (make-string (loop :for group :across groups :summing (length group)))
:for i := 0 :then (+ i (length group))
:for group :across groups
:if group
:do (loop :for char :across group
:for j :from 0
:do (setf (aref string (+ i j)) char))
:finally (return string)))))
(defun transform-method-desc (desc &optional (namespace *namespace*) (class *class*))
(declare (ignore namespace))
(catch 'skip
(let* ((info (gir::info-of desc))
(name (nstring-upcase (underscores->lisp-name (gir:info-get-name info))))
(symbol (intern name))
(args (callable-desc-argument-names desc))
(arg-types (mapcar #'gir:type-desc-of (gir:arguments-desc-of desc)))
(ret-types (mapcar #'gir:type-desc-of (gir:returns-desc-of desc)))
(class-name (or (quoted-name-symbol class) (camel-case->lisp-symbol class)))
(proc-arg-fn (loop :for (arg-text arg-len) :on args ; (const char* text, int len, ...) -> (text ... &aux (len (length text)))
:for (arg-text-type arg-len-type) :on arg-types
:for i :from 0
:when (and (eql arg-len-type 'integer)
(eql arg-text-type 'string)
(member (symbol-name arg-len) '("LENGTH" "LEN") :test #'string-equal))
:return (lambda (args)
(loop :for arg :in args
:for j :from 0
:when (/= (1+ i) j)
:collect arg :into result-args
:finally (return `(,@result-args &aux (,arg-len (length ,arg-text))))))
:finally (return #'identity)))
(proc-ret-fn (if (and (eq (car ret-types) :void) (cdr ret-types))
(lambda (body)
(let ((syms (loop :for tpe :in ret-types :collect (gensym))))
`(multiple-value-bind ,syms ,body
(declare (ignore ,(car syms)))
(values . ,(cdr syms)))))
#'identity)))
(if-let ((name-symbol (quoted-name-symbol (cons class (gir:info-get-name info)))))
`(defun ,name-symbol (instance ,@args)
,(funcall proc-ret-fn `(gir:invoke (instance ',symbol) ,@args)))
(cond
((and (not args)
(when-let ((name (scan-to-string +getter-pattern+ name)))
`(defun ,(intern (format nil (if (equal ret-types '(boolean)) "~A-~A-P" "~A-~A") class-name name)) (instance)
,(funcall proc-ret-fn `(gir:invoke (instance ',symbol)))))))
((and args
(when-let ((name (scan-to-string +setter-pattern+ name)))
`(defun (setf ,(intern (format nil (if (eql (car arg-types) 'boolean) "~A-~A-P" "~A-~A") class-name name))) (value instance)
(,@(if (cdr args) `(destructuring-bind ,args value) `(symbol-macrolet ((,(car args) value))))
,(funcall proc-ret-fn `(gir:invoke (instance ',symbol) ,@args)))))))
(t `(defun ,(intern (format nil "~A-~A" class-name name)) (instance ,@(funcall proc-arg-fn args))
,(funcall proc-ret-fn `(gir:invoke (instance ',symbol) ,@args)))))))))
(defun transform-constructor-desc (desc &optional (namespace *namespace*) (class *class*))
(catch 'skip
(let* ((info (gir::info-of desc))
(name (nstring-upcase (underscores->lisp-name (gir:info-get-name info))))
(symbol (intern name))
(args (callable-desc-argument-names desc))
(class-name (or (quoted-name-symbol class)
(camel-case->lisp-symbol class))))
(let ((body `(gir:invoke (,namespace ,class ',symbol) ,@args)))
(if-let ((name-symbol (quoted-name-symbol (cons class (gir:info-get-name info)))))
(values `(defun ,name-symbol ,args ,body) nil)
(if-let ((method (ppcre:register-groups-bind (verb prep method) (+constructor-pattern+ name)
(declare (ignore verb))
(if prep (list prep method) (list method)))))
(values `(defun ,(intern (format nil "MAKE~A-~A"
(ecase (length method)
(1 (first method))
(2 ""))
class-name))
(&key ,@args) ,body)
(and method
(every (compose #'plusp #'length) method)
(mapcar (lambda (str) (subseq str 1)) method)))
(values `(defun ,(intern (format nil "~A-~A" class-name name)) ,args ,body) nil)))))))
(defun merge-constructor-forms (forms descs subst-arg-names)
(let ((grouped nil))
(loop :for (defun-symbol name lambda-list body) :in forms
:for desc :in descs
:for subst-arg-name :in subst-arg-names
:when (eq (car lambda-list) '&key)
:do (pop lambda-list)
:do (push (list desc subst-arg-name body)
(assoc-value (assoc-value grouped name) lambda-list :test #'equal)))
(loop :with unmergeable-constructors
:for (name . arg-groups) :in grouped
:do (setf arg-groups
(mapcan (lambda (arg-group)
(destructuring-bind (args . bodies) arg-group
(if (> (length bodies) 1)
(if (= (length args) 1)
(loop :for (desc subst-arg-name body) :in bodies
:if subst-arg-name
:collect (let ((subst-symbol (intern (lastcar subst-arg-name))))
`((,subst-symbol) (let ((,(first args) ,subst-symbol)) ,body)))
:into result
:else
:count t :into no-subst-name-count
:and :collect `(,args ,body) :into result
:finally
(assert (<= no-subst-name-count 1))
(return result))
(loop :for (desc subst-arg-name body) :in bodies
:if subst-arg-name
:do (let ((*quoted-name-alist*
(cons (let ((name (gir:info-get-name (gir::info-of desc))))
(cons (cons *class* name)
(intern (format nil "MAKE-~A-~A-~A"
(camel-case->lisp-symbol *class*)
(first subst-arg-name)
(second subst-arg-name)))))
*quoted-name-alist*)))
(push (transform-constructor-desc desc) unmergeable-constructors))
:else
:collect `(,args ,(third bodies))))
(mapcar (compose (lambda (body) `(,args ,body)) #'third) bodies))))
(sort arg-groups #'> :key (compose #'length #'first))))
:collect `(defun ,name (&key ,@(mapcar (lambda (arg) `(,arg :unspecified))
(remove-duplicates (loop :for (args body) :in arg-groups
:append args))))
(cond
,@(mapcar (lambda (arg-group)
(destructuring-bind (args body) arg-group
`((not (or ,@(mapcar (lambda (arg) `(eql ,arg :unspecified)) args))) ,body)))
arg-groups)
(t (error "Invalid arguments for constructor ~A" ',name))))
:into merged-constructors
:finally (return (values merged-constructors unmergeable-constructors)))))
(defun transform-class-function-desc (desc &optional (namespace *namespace*) (class *class*))
(catch 'skip
(let* ((info (gir::info-of desc))
(name (nstring-upcase (underscores->lisp-name (gir:info-get-name info))))
(symbol (intern name))
(args (callable-desc-argument-names desc))
(arg-types (mapcar #'gir:type-desc-of (gir:arguments-desc-of desc)))
(ret-types (mapcar #'gir:type-desc-of (gir:returns-desc-of desc)))
(class-name (or (quoted-name-symbol class)
(camel-case->lisp-symbol class)))
(proc-arg-fn (loop :for (arg-text arg-len) :on args ; (const char* text, int len, ...) -> (text ... &aux (len (length text)))
:for (arg-text-type arg-len-type) :on arg-types
:for i :from 0
:when (and (eql arg-len-type 'integer)
(eql arg-text-type 'string)
(member (symbol-name arg-len) '("LENGTH" "LEN") :test #'string-equal))
:return (lambda (args)
(loop :for arg :in args
:for j :from 0
:when (/= (1+ i) j)
:collect arg :into result-args
:finally (return `(,@result-args &aux (,arg-len (length ,arg-text))))))
:finally (return #'identity)))
(proc-ret-fn (if (and (eq (car ret-types) :void) (cdr ret-types))
(lambda (body)
(let ((syms (loop :for tpe :in ret-types :collect (gensym))))
`(multiple-value-bind ,syms ,body
(declare (ignore ,(car syms)))
(values . ,(cdr syms)))))
#'identity)))
(if-let ((name-symbol (quoted-name-symbol (cons class (gir:info-get-name info)))))
`(defun ,name-symbol ,args
(gir:invoke (,namespace ,class ',symbol) ,@args))
(cond
((and (not args)
(when-let ((name (scan-to-string +getter-pattern+ name)))
`(defun ,(intern (format nil (if (equal ret-types '(boolean)) "~A-~A-P" "~A-~A") class-name name)) ()
,(funcall proc-ret-fn `(gir:invoke (,namespace ,class ',symbol)))))))
((and args
(when-let ((name (scan-to-string +setter-pattern+ name)))
`(defun (setf ,(intern (format nil (if (eql (car arg-types) 'boolean) "~A-~A-P" "~A-~A") class-name name))) (value)
(,@(if (cdr args) `(destructuring-bind ,args value) `(symbol-macrolet ((,(car args) value))))
,(funcall proc-ret-fn `(gir:invoke (,namespace ,class ',symbol) ,@args)))))))
(t `(defun ,(intern (format nil "~A-~A" class-name name)) ,(funcall proc-arg-fn args)
,(funcall proc-ret-fn `(gir:invoke (,namespace ,class ',symbol) ,@args)))))))))
(defun transform-function-desc (desc &optional (namespace *namespace*) (class *class*))
(declare (ignore class))
(catch 'skip
(let* ((info (gir::info-of desc))
(name (nstring-upcase (underscores->lisp-name (gir:info-get-name info))))
(symbol (intern name))
(args (callable-desc-argument-names desc))
(arg-types (mapcar #'gir:type-desc-of (gir:arguments-desc-of desc)))
(ret-type (gir:type-desc-of (car (gir:returns-desc-of desc)))))
(if-let ((name-symbol (quoted-name-symbol (gir:info-get-name info))))
`(defun ,name-symbol ,args
(gir:invoke (,namespace ',symbol) ,@args))
(cond
((and (not args)
(when-let ((name (scan-to-string +getter-pattern+ name)))
`(defun ,(intern (format nil (if (eql ret-type 'boolean) "~A-P" "~A") name)) ()
(gir:invoke (,namespace ',symbol))))))
((and args
(when-let ((name (scan-to-string +setter-pattern+ name)))
`(defun (setf ,(intern (format nil (if (eql (car arg-types) 'boolean) "~A-P" "~A") name))) (value)
(,@(if (cdr args) `(destructuring-bind ,args value) `(symbol-macrolet ((,(car args) value))))
(gir:invoke (,namespace ',symbol) ,@args))))))
(t `(defun ,(intern (format nil "~A" name)) ,args
(gir:invoke (,namespace ',symbol) ,@args))))))))
(defun transform-enum-desc (desc &optional (namespace *namespace*) (class *class*))
(declare (ignore namespace))
(catch 'skip
`(defconstant ,(or (quoted-name-symbol (cons class (car desc)))
(intern (format nil "+~A-~A+"
(or (quoted-name-symbol class)
(camel-case->lisp-symbol class))
(underscores->lisp-symbol (car desc)))))
,(cdr desc))))
(defun transform-constant-desc (desc &optional (namespace *namespace*) (class *class*))
(declare (ignore class))
(catch 'skip
`(define-constant ,(or (quoted-name-symbol desc)
(symbolicate '#:+ (underscores->lisp-symbol desc) '#:+))
(handler-case (gir:nget ,namespace ,desc)
(warning ()))
:test #'equal)))
| 19,180 | Common Lisp | .lisp | 294 | 45.768707 | 181 | 0.493186 | bohonghuang/cl-gobject-introspection-wrapper | 17 | 4 | 1 | LGPL-3.0 | 9/19/2024, 11:26:41 AM (Europe/Amsterdam) | 1fc7703eaab35bbc3201121670946e027df1394182728fc3c32b80555ff358c1 | 8,714 | [
-1
] |
8,715 | macro.lisp | bohonghuang_cl-gobject-introspection-wrapper/macro.lisp | ;;;; macro.lisp
;;;; Copyright (C) 2022-2023 Bohong Huang
;;;;
;;;; This program is free software: you can redistribute it and/or modify
;;;; it under the terms of the GNU Lesser General Public License as published by
;;;; the Free Software Foundation, either version 3 of the License, or
;;;; (at your option) any later version.
;;;;
;;;; This program is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;;;; GNU Lesser General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU Lesser General Public License
;;;; along with this program. If not, see <https://www.gnu.org/licenses/>.
(in-package #:gir-wrapper)
(defun defun-form->symbol (form)
(setf form (second form))
(etypecase form
(list (second form))
(symbol form)))
(defmacro define-gir-class (name &optional (namespace *namespace*))
(let ((desc (gir:nget-desc (eval namespace) name))
(*namespace* namespace)
(*class* name))
(let ((class (transform-class-desc desc))
(constructors (loop :for desc :in (gir:list-constructors-desc desc)
:for (form subst-arg-name) := (multiple-value-list (transform-constructor-desc desc))
:collect form :into forms
:collect subst-arg-name :into subst-arg-names
:collect desc :into descs
:finally (return (merge-constructor-forms forms descs subst-arg-names))))
(methods (mapcar #'transform-method-desc (gir:list-methods-desc desc)))
(class-functions (when (typep desc 'gir:object-class)
(mapcar #'transform-class-function-desc (gir:list-class-functions-desc desc)))))
`(progn
,@class
,@constructors
,@methods
,@class-functions
,(when-let ((symbols (delete-if #'null (mapcar #'defun-form->symbol (append (remove 'gir-wrapper:pointer-object class :key #'second) constructors methods class-functions)))))
`(export ',symbols))))))
(defmacro define-gir-interface (name &optional (namespace *namespace*))
(let ((desc (gir:nget-desc (eval namespace) name))
(*namespace* namespace)
(*class* name))
(let ((interface (transform-interface-desc desc))
(methods (mapcar #'transform-method-desc (gir:list-methods-desc desc))))
`(progn
,@interface
,@methods
,(when-let ((symbols (delete-if #'null (mapcar #'defun-form->symbol (append interface methods)))))
`(export ',symbols))))))
(defmacro define-gir-constant (name &optional (namespace *namespace*))
(let ((constant (transform-constant-desc name namespace)))
`(progn
,constant
,(when constant
`(export ',(second constant))))))
(defmacro define-gir-enum (name &optional (namespace *namespace*))
(let ((*namespace* namespace)
(*class* name))
(let ((members (mapcar #'transform-enum-desc (gir:values-of (gir:nget-desc (eval namespace) name)))))
`(progn
,@members
,(when members `(export ',(delete-if #'null (mapcar #'second members))))))))
(defmacro define-gir-function (name &optional (namespace *namespace*))
(let ((*namespace* namespace)
(*class* name))
(let ((function (transform-function-desc (gir:nget-desc (eval namespace) name))))
`(progn
,function
,(when-let ((symbol (defun-form->symbol function)))
`(export ',symbol))))))
(defmacro define-gir-namespace (name &optional version repository)
(let ((*namespace* (gir:require-namespace name version))
(namespace-symbol (intern "*NS*")))
`(progn
(eval-when (:execute :load-toplevel :compile-toplevel)
(defparameter ,namespace-symbol (gir:require-namespace ,name ,version)))
,@(mapcar (lambda (info)
(let ((name (gir:info-get-name info))
(type (gir:info-get-type info)))
(switch (type)
(:object `(define-gir-class ,name ,namespace-symbol))
(:struct (unless (ppcre:all-matches "Iface$" name)
`(define-gir-class ,name ,namespace-symbol)))
(:function `(define-gir-function ,name ,namespace-symbol))
(:constant `(define-gir-constant ,name ,namespace-symbol))
(:enum `(define-gir-enum ,name ,namespace-symbol))
(:flags `(define-gir-enum ,name ,namespace-symbol))
(:interface `(define-gir-interface ,name ,namespace-symbol)))))
(gir:repository-get-infos repository name)))))
| 4,842 | Common Lisp | .lisp | 93 | 42.204301 | 183 | 0.611603 | bohonghuang/cl-gobject-introspection-wrapper | 17 | 4 | 1 | LGPL-3.0 | 9/19/2024, 11:26:41 AM (Europe/Amsterdam) | eb13b609ffa8b443969c72b822a050de006b566118abb8c044d5cd00688020cf | 8,715 | [
-1
] |
8,716 | cl-gobject-introspection-wrapper.asd | bohonghuang_cl-gobject-introspection-wrapper/cl-gobject-introspection-wrapper.asd | (defsystem cl-gobject-introspection-wrapper
:version "1.0.0"
:author "Bohong Huang <[email protected]>"
:maintainer "Bohong Huang <[email protected]>"
:license "lgpl3"
:description "Wrap and call GObject Introspection FFI function in LISP style, based on cl-gobject-introspection."
:homepage "https://github.com/BohongHuang/cl-gobject-introspection-wrapper"
:bug-tracker "https://github.com/BohongHuang/cl-gobject-introspection-wrapper/issues"
:source-control (:git "https://github.com/BohongHuang/cl-gobject-introspection-wrapper.git")
:serial t
:components ((:file "package")
(:file "util")
(:file "desc")
(:file "macro"))
:depends-on (#:alexandria #:cl-gobject-introspection #:cl-ppcre))
(uiop:register-image-dump-hook
(lambda ()
(setf (symbol-value (find-symbol "*NAMESPACE-CACHE*" :gir))
(make-hash-table :test #'equal))))
| 909 | Common Lisp | .asd | 19 | 42.578947 | 115 | 0.699663 | bohonghuang/cl-gobject-introspection-wrapper | 17 | 4 | 1 | LGPL-3.0 | 9/19/2024, 11:26:41 AM (Europe/Amsterdam) | 6f3b3bf393a35a9bc9f4afcaf6a4ec4ad5a1ae1ab557ebeb918f4e069a602839 | 8,716 | [
-1
] |
8,737 | package2.lisp | gregcman_lisp-in-small-pieces/package2.lisp | (defpackage #:lisp-in-small-pieces
(:use #:cl #:utility #:scheme2common-lisp)
(:nicknames #:lisp))
(in-package #:lisp)
(setf *print-case* :downcase)
(setf *print-circle* t)
(defparameter *dir* (asdf:system-source-directory :lisp-in-small-pieces))
(defparameter *scheme-test-file* (rebase-path "scheme.tst" *dir*))
| 319 | Common Lisp | .lisp | 8 | 38.25 | 73 | 0.722581 | gregcman/lisp-in-small-pieces | 11 | 2 | 0 | GPL-3.0 | 9/19/2024, 11:26:41 AM (Europe/Amsterdam) | 2e36fe412f6a4ccc50f38ebdb0baf222fab66788a34efa662fbd91c992e5f495 | 8,737 | [
-1
] |
8,738 | tester.lisp | gregcman_lisp-in-small-pieces/tester.lisp | (in-package :lisp)
;;; *********************************************
;;; An engine to test interpreters or compilers
;;; Christian Queinnec
;;; \'Ecole Polytechnique & INRIA-Rocquencourt
;;; *********************************************
;;; This file contains the basic facility to easily run an interpreter
;;; defining a language. It can ask the user for expressions to be
;;; evaluated and print their results or take expressions from a file,
;;; evaluate them and compare their value to the expected value. If an
;;; error occurs then the test suite is aborted.
;;; A validation suite is a sequence of expressions followed by their
;;; expected results. The expression is evaluated in the language being
;;; tested then its result is compared to the expected result.
;;; An example of a test suite is:
;;; /------------------------
;;; | (car '(a b))
;;; | a
;;; | (car (list))
;;; | *** ; means that an error is expected
;;; | (oblist)
;;; | --- ; means that no error is expected but the value is unimportant
;;; It is an error of course to expect an error if no error occurrs.
;;; This package may use the tester-error function in case of an
;;; internal error. It is not defined here since errors are not
;;; portable. You must have a location to define that variable for
;;; some compilers. Two cases may trigger this error: a suite test
;;; missing an expecting result or a (toplevel) form returning an
;;; unprinted value. Other errors are caught within engine-tester.
;;; The testing engine can be parameterized in various ways.
;;; Apart the engine itself, two functions are offered that ease its use.
;;; These are the `interpreter' and `suite-test' functions:
;;; This is the read function to use. It is possible to tailor this
;;; function to suit particular needs (for instance when generating
;;; tests instead of reading them out of a file).
(defparameter +eof+ (cons "end of" "file"))
(alias0 tester-read (lambda (&optional (stream *standard-input*))
(read stream nil +eof+)))
(defun eof-object? (obj)
(eq obj +eof+))
;;; This is an internal variable that, in real Scheme, may be true or
;;; false without perceivable difference. It does make a difference
;;; however in non compliant Scheme systems. If your Scheme only
;;; offers dynamic-extent continuations, you must set it to false (in
;;; that case, all continuations created by tester.scm will be used in
;;; their dynamic extent). Alternatively, in Scheme->C which seems to
;;; have problems to garbage collect continuations, set it to true to
;;; only take one continuation and use it out of its dynamic extent.
(defparameter tester-take-only-one-continuation +false+)
;;; This is the call/cc to use. It is possible to use dynamic-extent
;;; continuations instead. For instance, in Bigloo, you may use:
;;; (set! tester-call/cc (lambda (f)
;;; (bind-exit (k) (f k))))
;;; But don't forget to define tester-take-only-one-continuation to false.
(alias0 tester-call/cc call/cc)
;;; The interpreter function takes four arguments:
;;; -- an input prompt. This string will be printed whenever the
;;; interpreter wants to read an expression to evaluate.
;;; -- an output prompt. Values are printed preceded by this string.
;;; -- an error handler which is called whenever an error is detected.
;;; -- a make-toplevel function that will return a thunk implementing
;;; one interpreting step (read-eval-print).
;;; make-toplevel may be roughly defined as
;;; (lambda (read-exression display-value error-catcher)
;;; (lambda () (display-value (tested-eval (read-expression)))))
;;; The toplevel function will be repeatedly invoked from the
;;; interpreter. make-toplevel is invoked only once
;;; by the testing-engine with
;;; == (read-expression): a function that reads an expression and echoes it
;;; after printing the input-prompt. It returns the read expression.
;;; == (display-value v): the function that prints a value preceded by
;;; the output prompt. A new toplevel step is performed right after.
;;; == (error-catcher message . culprits): a function that reports the error,
;;; aborts the current computation and restarts a new cycle.
;;; You must write your interpreter so that when it detects errors,
;;; it calls this error-catcher function. You can have single error
;;; function or trap the native error function of your particular
;;; system.
(defun interpreter (prompt-in ; prompt to read expression
prompt-out ; prompt preceding results
continue? ; continue after unexpected error
make-toplevel) ; toplevel generator
;; display the result of an evaluation
(labels ((display-status (status expected v)
(case status
((unexpected-error)
(newline)
(display v)
(display " an unexpected ERROR occurs !!!")
(newline)
continue?)
((correct-result)
(display prompt-out)
(display v)
(newline)
+true+) ; continue iteration
(otherwise +false+)))) ; stop iteration
;; starts toplevel
(tester-call/cc
(lambda (exit) ; exit when test suite is finished
(engine-tester
(lambda () ; read expression
(display prompt-in)
(let ((e (tester-read)))
(if (eof-object? e)
(funcall exit 'end))
e))
(lambda () 'nothing) ; read expected result (useless)
(lambda (expected obtained) ; compare expected and obtained results
(cond ((eq? obtained '***) +false+)
((eq? obtained '---) +false+)
(t +true+)))
(function display-status)
make-toplevel)))))
;;; suite-test is similar to the preceding one except that tests are taken
;;; from a file, possibly echoed on the console and checked to be correct.
;;; The suite contains expressions followed by their expected result.
;;; The result of the evaluation is compared to this result, the
;;; suite is aborted if an error occurs.
;;; suite-test takes six arguments:
;;; -- a file-name: The file contains the expressions to be evaluated and
;;; the expected results.
;;; -- an input prompt
;;; -- an output prompt
;;; -- a boolean flag which governs if read expressions from the file are
;;; echoed on the console.
;;; -- a make-toplevel function that will return the toplevel function
;;; make-toplevel may be roughly defined as
;;; (lambda (test-read test-checker wrong)
;;; (lambda () (test-checker (tested-eval (test-read)))))
;;; The toplevel function will be repeatedly invoked from the
;;; interpreter. The arguments of make-toplevel are
;;; == (test-read): the function that reads an expression, echoing it
;;; after printing the input-prompt.
;;; == (test-checker v): this function takes a value, reads the expected
;;; result, compares them and if according, prints the value preceded by
;;; the output prompt. A new toplevel is started after that.
;;; == (wrong message . culprits): a function that reports the error,
;;; aborts the current computation and restart a new cycle.
;;; -- a (comparator) function that takes the obtained result and the
;;; expected result and compares them yielding a boolean.
;;; Very often, result-eval is just `equal?' but must
;;; recognize the *** and --- items which meaning is "an error is
;;; expected" or "an unimportant value (but no error)".
(defun suite-test (file ; the test suite
prompt-in ; the prompt to read
prompt-out ; the prompt to display
echo? ; echo expressions ?
make-toplevel ; a toplevel generator
compare) ; how to compare results
(let ((in (open-input-file file))
(native-display (function display))
(native-newline (function newline)))
(labels (
;; Two small utilities to display things
(display (exp)
(if echo? (funcall native-display exp)))
(newline ()
(if echo? (funcall native-newline)))
;; Display the result of the test, return a boolean to indicate
;; whether the tests should continue or not.
(display-status (status expected v)
(case status
((expected-error)
(set! echo? +true+)
(display prompt-out)
(display v)
(display " an ERROR was expected !!! ")
(newline)
+false+) ; stop iteration
((error-occurred)
(display " OK OK")
(newline)
+true+) ; continue iteration
((unexpected-error)
(newline)
(display v)
(set! echo? +true+)
(display " an unexpected ERROR occured !!!")
(newline)
(display " value expected: ")
(display expected)
(newline)
+false+) ; stop iteration
((correct-result)
(display prompt-out)
(display v)
(display " OK")
(newline)
+true+) ; continue iteration
((incorrect-result)
(set! echo? +true+)
(display prompt-out)
(display v)
(display " ERROR !!!")
(newline)
(display "value expected:")
(display expected)
(newline)
+false+) ; stop iteration
((uninteresting-result)
(display " OK")
(newline)
+true+) ; continue iteration
(else (display "No such status")
(newline)
+false+)))) ; stop iteration
(tester-call/cc
(lambda (exit) ; exit when test suite is finished
(engine-tester
(lambda () ; read test
(let ((e (tester-read in)))
(if (eof-object? e)
(begin (close-input-port in)
(funcall exit 'done)))
(display prompt-in)
(display e)
(newline)
e))
(lambda () ; read result
(let ((expected (tester-read in)))
(if (eof-object? expected)
(tester-error "Missing expected result" expected)
expected)))
compare
(function display-status)
make-toplevel))))))
;;; A test engine on top of which the two previous are written:
;;; (read-test) reads an expression to evaluate
;;; (read-result) reads the expected result
;;; (compare expected obtained) compares what was obtained from what
;;; was expected. The value of `expected' can also
;;; be *** or ---
;;; (display-status message expected obtained) displays the result of the
;;; test. It usually prints the result and a comment like `OK'.
;;; Testing is abandoned if display-status returns +false+.
;;; (make-toplevel read print error) returns a thunk implementing one step
;;; of the intepreter.
(defun engine-tester (read-test ; read a test
read-result ; read the expected result
compare ; compare the two
display-status ; display the comparison
make-toplevel) ; make a toplevel
(tester-call/cc
(lambda (abort) ; exit all tests
(let ((resume +false+)) ; will be initialized below.
;; compare the result V with what was expected. If that
;; function is called then no error ocurred (unless *** is
;; given to it simulating an error internally caught).
(labels ((check-result (v)
(let ((expected (funcall read-result)))
(if (cond ((funcall compare expected v)
(funcall display-status 'correct-result expected v))
((eq? expected '***)
(funcall display-status 'expected-error expected v))
((eq? expected '---)
(funcall display-status 'uninteresting-result expected v))
(t
(funcall display-status 'incorrect-result expected v)))
(funcall resume +true+)
(funcall abort +false+))))
;; This function is called whenever an error is detected.
(handle-exception (msg &rest culprits)
;;(write `(handle-exception called))(newline) ; DEBUG
(let ((expected (funcall read-result))
(v (cons msg culprits)))
(if (cond ((eq? expected '***)
(funcall display-status 'error-occurred expected v))
(t
(funcall display-status 'unexpected-error expected v)))
(funcall resume +true+)
(funcall abort +false+)))))
(let ((toplevel (funcall make-toplevel
read-test
(function check-result)
(function handle-exception))))
;; The goal is to call (toplevel) ever and ever but to ensure
;; that the continuation is correctly reset.
(named-let named-loop ()
(tester-call/cc
(lambda (k)
(if (and tester-take-only-one-continuation resume)
'nothing
(set! resume k))
(let ((r (funcall toplevel)))
;; if this error is triggered, see note below.
(tester-error "(toplevel) should not return!" r))))
(named-loop))))))))
(defun tester-error (msg &rest other)
(print other)
(error msg))
;;; Examples:
;;; Suppose you have written an interpreter called `evaluate', then the
;;; following will start a toplevel loop. Errors detected in evaluate
;;; are supposed to call the `wrong' function.
;;;(define (scheme)
;;; (interpreter "?? " " == " +true+
;;; (lambda (read print error)
;;; (set! wrong error) ;; Errors in the interpreter calls wrong
;;; (lambda () (print (evaluate (read)))))))
;;; The problem is that errors in the underlying system are not caught.
;;; Suppose at that time to have something to trap errors, say catch-error
;;; as in Mac-Lisp (it returns the result in a pair or the string that names
;;; the error if any), then you can write:
;;;(define (scheme)
;;; (interpreter "?? " " == " +true+
;;; (lambda (read print error)
;;; (set! wrong error) ;; Errors in the interpreter calls wrong
;;; (lambda () (let ((r (catch-error (evaluate (read)))))
;;; (if (pair? r) (print (car r))
;;; (error r)))))))
;;; NOTE: Both the print and error functions (in interpreter and
;;; suite-test) have a control effect. They restart a new toplevel
;;; iteration. So it is *important* not to forget to call them to
;;; reiterate the toplevel. If you return a value from toplevel
;;; without calling print or error, you'll get an internal error (ie
;;; an invocation of tester-error). [The reason lies with toplevel
;;; returning more than once in some concurrent interpreter I wrote].
;;; If you have a file containing a test suite, say suite.tst, then you
;;; can try it with:
;;;(define (test-scheme)
;;; (suite-test "suite.tst"
;;; "?? " "== " +true+
;;; (lambda (read print error)
;;; (set! wrong error)
;;; (lambda ()
;;; (print (eval (read)))))
;;; equal?))
;;; Another comparison function could be:
;;; (lambda (expected obtained)
;;; (cond ((or (eq? obtained '---)(eq? obtained '***))
;;; (equal? expected obtained))
;;; (else (member obtained expected))))
;;; Other suggestions: tests and results can be read from two different files.
;;; You can use other compare functions such as member, set-equal? or even
;;; use pattern-matching. Here are the two lastly mentioned comparators.
;;; Compares if sets X and Y have the same (with equal?) elements.
(defun set-equal? (x y)
(labels ((remove-one (item list)
(if (pair? list)
(if (equal? item (car list))
(cdr list)
(cons (car list) (remove-one item (cdr list))))
'())))
(if (pair? x)
(and (member (car x) y)
(set-equal? (cdr x) (remove-one (car x) y)))
(null? y))))
;;; Compares if the expression fits the pattern. Two special patterns exist:
;;; ?- which accepts anything
;;; ??- which accepts a (possibly empty) sequence of anything.
;;; Otherwise comparisons are performed with equal?.
(defun naive-match (pattern expression)
(labels ((naive-match-list (patterns expressions)
(if (pair? patterns)
(if (eq? (car patterns) '??-) ; accepts any sequence of things
(or (naive-match-list (cdr patterns) expressions)
(and (pair? expressions)
(naive-match-list patterns (cdr expressions))))
(and (pair? expressions)
(naive-match (car patterns) (car expressions))
(naive-match-list (cdr patterns) (cdr expressions))))
(naive-match patterns expressions))))
(or (eq? pattern '?-) ; accepts anything
(if (pair? pattern)
(naive-match-list pattern expression)
(equal? pattern expression)))))
;;; AGAIN A NOTE:
;;; To catch the errors of the underlying Scheme is difficult.
;;; This tester engine has been used since 1992 on a wide variety of
;;; interpreters, some of which are concurrent and/or return multiple
;;; results.
;;; end of tester.scm
| 16,940 | Common Lisp | .lisp | 373 | 41.796247 | 79 | 0.637804 | gregcman/lisp-in-small-pieces | 11 | 2 | 0 | GPL-3.0 | 9/19/2024, 11:26:41 AM (Europe/Amsterdam) | 7428a8f53885891c72cc95c076b9f230453b4cae18963239278867ce221324ab | 8,738 | [
-1
] |
8,739 | chap7d.lisp | gregcman_lisp-in-small-pieces/chap7d.lisp | (in-package :lisp)
;;; Refinement of chap6d and chap7c. This interpreter introduces a
;;; *val* register and a *stack* to save/restore arguments that wait
;;; to be stored in an activation block. Functions now take their
;;; activation frame in the *val* register. Code is now a list of
;;; bytes.
;;; Load chap6d before.
;;;oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
;;; The runtime machine
;(defparameter *env* +false+) ; already appears in chap6d
(defparameter *val* +false+)
(defparameter *fun* +false+)
(defparameter *arg1* +false+)
(defparameter *arg2* +false+)
(defparameter *pc* 0)
(defparameter *code* (vector 20))
(defparameter *constants* (vector))
;;; Some tests depend on 100 being the depth of the stack.
(defparameter *stack* (make-vector 100))
(defparameter *stack-index* 0)
;;;oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
(defun stack-push (v)
(vector-set! *stack* *stack-index* v)
(incf *stack-index*))
(defun stack-pop ()
(decf *stack-index*)
(vector-ref *stack* *stack-index*))
(defun save-stack ()
(let ((copy (make-vector *stack-index*)))
(vector-copy! *stack* copy 0 *stack-index*)
copy))
(defun restore-stack (copy)
(set! *stack-index* (vector-length copy))
(vector-copy! copy *stack* 0 *stack-index*))
;;; Copy vector old[start..end[ into vector new[start..end[
(defun vector-copy! (old new start end)
(named-let copy ((i start))
(when (< i end)
(vector-set! new i (vector-ref old i))
(copy (+ i 1)))))
(defun quotation-fetch (i)
(vector-ref *constants* i))
;;;oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
;;; make them inherit from invokable.
(progn
(defclass primitive ()
((address :initarg address)))
(defun primitive? (obj)
(typep obj 'primitive))
(defun primitive-address (obj)
(slot-value obj 'address))
(defun set-primitive-address! (obj new)
(setf (slot-value obj 'address) new))
(defun make-primitive (address)
(make-instance 'primitive
'address address)))
(progn
(defclass continuation ()
((stack :initarg stack)))
(defun continuation? (obj)
(typep obj 'continuation))
(defun continuation-stack (obj)
(slot-value obj 'stack))
(defun set-continuation-stack! (obj new)
(setf (slot-value obj 'stack) new))
(defun make-continuation (stack)
(make-instance 'continuation
'stack stack)))
;;;oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
;;; This global variable holds at preparation time all the interesting
;;; quotations. It will be converted into *constants* for run-time.
;;; Quotations are not compressed and can appear multiply.
(defparameter *quotations* (list))
;;;oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
;;; Combinators that just expand into instructions.
(defun SHALLOW-ARGUMENT-SET! (j m)
(append m
(SET-SHALLOW-ARGUMENT! j)))
(defun DEEP-ARGUMENT-SET! (i j m)
(append m
(SET-DEEP-ARGUMENT! i j)))
(defun GLOBAL-SET! (i m)
(append m
(SET-GLOBAL! i)))
;;; GOTO is not necessary if m2 is a tail-call but don't care.
;;; This one changed since chap7c.scm
(defun ALTERNATIVE (m1 m2 m3)
(let ((mm2 (append m2 (GOTO (length m3)))))
(append m1 (JUMP-FALSE (length mm2)) mm2 m3)))
(defun %SEQUENCE% (m m+)
(append m
m+))
(defun TR-FIX-LET (m* m+)
(append m*
(EXTEND-ENV)
m+))
(defun FIX-LET (m* m+)
(append m*
(EXTEND-ENV)
m+
(UNLINK-ENV)))
(defun FIX-CLOSURE (m+ arity)
(let* ((the-function
(append (ARITY=? (+ arity 1))
(EXTEND-ENV)
m+
(%RET%)))
(the-goto (GOTO (length the-function))))
(append (CREATE-CLOSURE (length the-goto))
the-goto
the-function)))
(defun NARY-CLOSURE (m+ arity)
(let* ((the-function
(append (ARITY>=? (+ arity 1))
(PACK-FRAME! arity)
(EXTEND-ENV)
m+
(%RET%)))
(the-goto (GOTO (length the-function))))
(append (CREATE-CLOSURE (length the-goto))
the-goto
the-function)))
(defun TR-REGULAR-CALL (m m*)
(append m
(PUSH-VALUE)
m*
(POP-FUNCTION)
(FUNCTION-GOTO)))
(defun REGULAR-CALL (m m*)
(append m
(PUSH-VALUE)
m*
(POP-FUNCTION)
(PRESERVE-ENV)
(FUNCTION-INVOKE)
(RESTORE-ENV)))
(defun STORE-ARGUMENT (m m* rank)
(append m
(PUSH-VALUE)
m*
(POP-FRAME! rank)))
(defun CONS-ARGUMENT (m m* arity)
(append m
(PUSH-VALUE)
m*
(POP-CONS-FRAME! arity)))
;;;oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
;;; Instructions definers
;;; This uses the global fetch-byte function that increments *pc*.
(eval-always
(defparameter *instructions* (make-array 256 :initial-element nil))
(defparameter *instruction-names* (make-array 256 :initial-element nil))
(defparameter *instruction-arity* (make-array 256 :initial-element nil)))
(defmacro define-instruction ((name &rest args) n &body body)
(setf (aref *instruction-names* n) name)
(setf (aref *instructions* n) `(lambda ,args ,@body))
(setf (aref *instruction-arity* n) (length args)))
(eval-always
(defun instructionp (byte)
(aref *instructions* byte)))
(defmacro define-instruction-set ()
`(defun dispatch-instruction (instruction)
(case instruction
,@(let (acc)
(dotimes (byte 256)
(when (instructionp byte)
(push `((,byte) (,(aref *instructions* byte)
,@(make-list (aref *instruction-arity* byte)
:initial-element
'(fetch-byte))))
acc)))
(nreverse acc)))))
#+nil
(defparameter *debugging* t)
(defun run ()
(let ((instruction (fetch-byte)))
#+nil
(when *debugging*
(inspect *val*)
(print (aref *instructions* instruction))
(format t ": ~s" (instruction-decode *code* (1- *pc*))))
(dispatch-instruction instruction))
(run))
(defun instruction-size (code pc)
(let ((instruction (vector-ref code pc)))
(+ 1 (aref *instruction-arity* instruction))))
(defun instruction-decode (code pc)
(labels ((fetch-byte ()
(prog1 (vector-ref code pc)
(incf pc))))
(let ((instruction (fetch-byte)))
(let ((dump (list (aref *instruction-names* instruction))))
(dotimes (x (aref *instruction-arity* instruction))
(push (fetch-byte) dump))
(nreverse dump)))))
;;; Combinators
(defun check-byte (j)
(unless (and (<= 0 j) (<= j 255))
(static-wrong "Cannot pack this number within a byte" j)))
(defun SHALLOW-ARGUMENT-REF (j)
(check-byte j)
(list 5 j))
(define-instruction (SHALLOW-ARGUMENT-REF j) 5
(set! *val* (activation-frame-argument *env* j)))
(defun PREDEFINED (i)
(check-byte i)
(list 19 i))
(define-instruction (PREDEFINED i) 19
(set! *val* (predefined-fetch i)))
(defun DEEP-ARGUMENT-REF (i j)
(list 6 i j))
(define-instruction (DEEP-ARGUMENT-REF i j) 6
(set! *val* (deep-fetch *env* i j)))
(defun SET-SHALLOW-ARGUMENT! (j)
(list 25 j))
(define-instruction (SET-SHALLOW-ARGUMENT! j) 25
(set-activation-frame-argument! *env* j *val*))
(defun SET-DEEP-ARGUMENT! (i j)
(list 26 i j))
(define-instruction (SET-DEEP-ARGUMENT! i j) 26
(deep-update! *env* i j *val*))
(defun GLOBAL-REF (i)
(list 7 i))
(define-instruction (GLOBAL-REF i) 7
(set! *val* (global-fetch i)))
(defun CHECKED-GLOBAL-REF (i)
(list 8 i))
(define-instruction (CHECKED-GLOBAL-REF i) 8
(set! *val* (global-fetch i))
(when (eq? *val* undefined-value)
(signal-exception
+true+ (list "Uninitialized global variable" i))))
#+nil
(define-instruction (CHECKED-GLOBAL-REF i) 8
(set! *val* (global-fetch i))
(if (eq? *val* undefined-value)
(signal-exception +true+ (list "Uninitialized global variable" i))
(vector-set! *code* (- *pc* 2) 7)))
(defun SET-GLOBAL! (i)
(list 27 i))
(define-instruction (SET-GLOBAL! i) 27
(global-update! i *val*))
(defun CONSTANT (value)
(if (and (integer? value) ; immediate value
(<= 0 value)
(< value 255))
(list 79 value)
(EXPLICIT-CONSTANT value)))
(define-instruction (SHORT-NUMBER value) 79
(set! *val* value))
(defun EXPLICIT-CONSTANT (value)
(set! *quotations* (append *quotations* (list value)))
(list 9 (- (length *quotations*) 1)))
(define-instruction (CONSTANT i) 9
(set! *val* (quotation-fetch i)))
;;; All gotos have positive offsets (due to the generation)
(defun GOTO (offset)
(cond ((< offset 255) (list 30 offset))
((< offset (+ 255 (* 255 256)))
(let ((offset1 (modulo offset 256))
(offset2 (quotient offset 256)))
(list 28 offset1 offset2)))
(t (static-wrong "too long jump" offset))))
(define-instruction (SHORT-GOTO offset) 30
(incf *pc* offset))
(define-instruction (LONG-GOTO offset1 offset2) 28
(let ((offset (+ offset1 (* 256 offset2))))
(incf *pc* offset)))
(defun JUMP-FALSE (offset)
(cond ((< offset 255) (list 31 offset))
((< offset (+ 255 (* 255 256)))
(let ((offset1 (modulo offset 256))
(offset2 (quotient offset 256)))
(list 29 offset1 offset2)))
(t (static-wrong "too long jump" offset))))
(define-instruction (SHORT-JUMP-FALSE offset) 31
(when (not *val*)
(incf *pc* offset)))
(define-instruction (LONG-JUMP-FALSE offset1 offset2) 29
(let ((offset (+ offset1 (* 256 offset2))))
(when (not *val*)
(incf *pc* offset))))
(defun EXTEND-ENV ()
(list 32))
(define-instruction (EXTEND-ENV) 32
(set! *env* (sr-extend* *env* *val*)))
(defun UNLINK-ENV ()
(list 33))
(define-instruction (UNLINK-ENV) 33
(set! *env* (activation-frame-next *env*)))
(defun PUSH-VALUE ()
(list 34))
(define-instruction (PUSH-VALUE) 34
(stack-push *val*))
(defun POP-ARG1 ()
(list 35))
(define-instruction (POP-ARG1) 35
(set! *arg1* (stack-pop)))
(defun POP-ARG2 ()
(list 36))
(define-instruction (POP-ARG2) 36
(set! *arg2* (stack-pop)))
(defun CREATE-CLOSURE (offset)
(list 40 offset))
(define-instruction (CREATE-CLOSURE offset) 40
(set! *val* (make-closure (+ *pc* offset) *env*)))
(defun ARITY=? (arity+1)
(list 75 arity+1))
(define-instruction (ARITY=? arity+1) 75
(unless (= (activation-frame-argument-length *val*) arity+1)
(signal-exception +false+ (list "Incorrect arity"))))
(defun %RET% ()
(list 43))
(define-instruction (%RET%) 43
(set! *pc* (stack-pop)))
(defun PACK-FRAME! (arity)
(list 44 arity))
(define-instruction (PACK-FRAME! arity) 44
(listify! *val* arity))
(defun ARITY>=? (arity+1)
(list 78 arity+1))
(define-instruction (ARITY>=? arity+1) 78
(unless (>= (activation-frame-argument-length *val*) arity+1)
(signal-exception +false+ (list "Too less arguments for a nary function"))))
(defun FUNCTION-GOTO ()
(list 46))
(define-instruction (FUNCTION-GOTO) 46
(invoke *fun* +true+))
(defun POP-FUNCTION ()
(list 39))
(define-instruction (POP-FUNCTION) 39
(set! *fun* (stack-pop)))
(defun FUNCTION-INVOKE ()
(list 45))
(define-instruction (FUNCTION-INVOKE) 45
(invoke *fun* +false+))
(defun PRESERVE-ENV ()
(list 37))
(define-instruction (PRESERVE-ENV) 37
(preserve-environment))
(defun RESTORE-ENV ()
(list 38))
(define-instruction (RESTORE-ENV) 38
(restore-environment))
(defun POP-FRAME! (rank)
(list 64 rank))
(define-instruction (POP-FRAME! rank) 64
(set-activation-frame-argument! *val* rank (stack-pop)))
(defun POP-CONS-FRAME! (arity)
(list 47 arity))
(define-instruction (POP-CONS-FRAME! arity) 47
(set-activation-frame-argument!
*val* arity (cons (stack-pop)
(activation-frame-argument *val* arity))))
(defun ALLOCATE-FRAME (size)
(list 55 (+ size 1)))
(define-instruction (ALLOCATE-FRAME size+1) 55
(set! *val* (allocate-activation-frame size+1)))
(defun ALLOCATE-DOTTED-FRAME (arity)
(list 56 (+ arity 1)))
(define-instruction (ALLOCATE-DOTTED-FRAME arity) 56
(let ((v* (allocate-activation-frame arity)))
(set-activation-frame-argument! v* (- arity 1) '())
(set! *val* v*)))
(defun FINISH ()
(list 20))
(define-instruction (FINISH) 20
(funcall *exit* *val*))
(define-instruction-set)
;;;oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
;;; Preserve the state of the machine ie the three environments.
(defun preserve-environment ()
(stack-push *env*))
(defun restore-environment ()
(set! *env* (stack-pop)))
;;;oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
(defun fetch-byte ()
(let ((byte (vector-ref *code* *pc*)))
(incf *pc*)
byte))
;;;oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
;;; Disassemble code
(defun %disassemble% (code)
(named-let rec ((result '())
(pc 0))
(if (>= pc (vector-length code))
(reverse! result)
(rec (cons (instruction-decode code pc) result)
(+ pc (instruction-size code pc))))))
;;;oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
;;; If tail? is +true+ then the return address is on top of stack so no
;;; need to push another one.
(defmethod invoke ((f t) tail?)
(signal-exception +false+ (list "Not a function" f)))
(defmethod invoke ((f closure) tail?)
(unless tail? (stack-push *pc*))
(set! *env* (closure-closed-environment f))
(set! *pc* (closure-code f)))
(defmethod invoke ((f primitive) tail?)
(unless tail? (stack-push *pc*))
(funcall (primitive-address f)))
(defmethod invoke ((f continuation) tail?)
(if (= (+ 1 1) (activation-frame-argument-length *val*))
(begin
(restore-stack (continuation-stack f))
(set! *val* (activation-frame-argument *val* 0))
(set! *pc* (stack-pop)))
(signal-exception +false+ (list "Incorrect arity" 'continuation))))
;;;oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
(defmacro defprimitive0 (name value)
`(definitial ,name
(letrec ((arity+1 (+ 1 0))
(behavior
(lambda ()
(if (= arity+1 (activation-frame-argument-length *val*))
(begin
(set! *val* (,value))
(set! *pc* (stack-pop)))
(signal-exception +true+ (list "Incorrect arity" ',name))))))
(description-extend! ',name `(function ,',value))
(make-primitive behavior))))
(defmacro defprimitive1 (name value)
`(definitial ,name
(letrec ((arity+1 (+ 1 1))
(behavior
(lambda ()
(if (= arity+1 (activation-frame-argument-length *val*))
(let ((arg1 (activation-frame-argument *val* 0)))
(set! *val* (,value arg1))
(set! *pc* (stack-pop)))
(signal-exception +true+ (list "Incorrect arity" ',name))))))
(description-extend! ',name `(function ,',value a))
(make-primitive behavior))))
(defmacro defprimitive2 (name value)
`(definitial ,name
(letrec ((arity+1 (+ 2 1))
(behavior
(lambda ()
(if (= arity+1 (activation-frame-argument-length *val*))
(let ((arg1 (activation-frame-argument *val* 0))
(arg2 (activation-frame-argument *val* 1)))
(set! *val* (,value arg1 arg2))
(set! *pc* (stack-pop)))
(signal-exception +true+ (list "Incorrect arity" ',name))))))
(description-extend! ',name `(function ,',value a b))
(make-primitive behavior))))
(definitial t +true+)
(definitial f +false+)
(definitial nil '())
(defprimitive cons cons 2)
(defprimitive car car 1)
(defprimitive cdr cdr 1)
(defprimitive pair? pair? 1)
(defprimitive symbol? symbol? 1)
(defprimitive eq? eq? 2)
(defprimitive set-car! set-car! 2)
(defprimitive set-cdr! set-cdr! 2)
(defprimitive + + 2)
(defprimitive - - 2)
(defprimitive = = 2)
(defprimitive < < 2)
(defprimitive > > 2)
(defprimitive * * 2)
(defprimitive <= <= 2)
(defprimitive >= >= 2)
(defprimitive remainder remainder 2)
(defprimitive display display 1)
(defprimitive read read 0)
(defprimitive primitive? primitive? 1)
(defprimitive continuation? continuation? 1)
(defprimitive null? null? 1)
(defprimitive newline newline 0)
(defprimitive eof-object? eof-object? 1)
;;; The function which is invoked by call/cc always waits for an
;;; activation frame.
(definitial call/cc
(let* ((arity 1)
(arity+1 (+ arity 1)))
(make-primitive
(lambda ()
(if (= arity+1 (activation-frame-argument-length *val*))
(let ((f (activation-frame-argument *val* 0))
(frame (allocate-activation-frame (+ 1 1))))
(set-activation-frame-argument!
frame 0 (make-continuation (save-stack)))
(set! *val* frame)
(set! *fun* f) ; useful for debug
(invoke f +true+))
(signal-exception +true+ (list "Incorrect arity"
'call/cc)))))))
(definitial apply
(let* ((arity 2)
(arity+1 (+ arity 1)))
(make-primitive
(lambda ()
(if (>= (activation-frame-argument-length *val*) arity+1)
(let* ((proc (activation-frame-argument *val* 0))
(args-number (activation-frame-argument-length *val*))
(last-arg-index (- args-number 2))
(last-arg (activation-frame-argument *val* last-arg-index))
(size (+ last-arg-index (length last-arg)))
(frame (allocate-activation-frame size)))
(do ((i 1 (+ i 1)))
((= i last-arg-index))
(set-activation-frame-argument!
frame (- i 1) (activation-frame-argument *val* i)))
(do ((i (- last-arg-index 1) (+ i 1))
(last-arg last-arg (cdr last-arg)))
((null? last-arg))
(set-activation-frame-argument! frame i (car last-arg)))
(set! *val* frame)
(set! *fun* proc) ; useful for debug
(invoke proc +true+))
(signal-exception +false+ (list "Incorrect arity" 'apply)))))))
(definitial list
(make-primitive
(lambda ()
(let ((args-number (- (activation-frame-argument-length *val*) 1))
(result '()))
(do ((i args-number (- i 1)))
((= i 0))
(set! result (cons (activation-frame-argument *val* (- i 1))
result)))
(set! *val* result)
(set! *pc* (stack-pop))))))
;;; Reserve some variables for future use in future chapters.
(defmacro defreserve (name)
`(definitial ,name
(make-primitive
(lambda ()
(signal-exception +false+ (list "Not yet implemented" ',name))))))
(defreserve global-value)
(defreserve load)
(defreserve eval)
(defreserve eval/at)
(defreserve eval/b)
(defreserve enrich)
(defreserve procedure->environment)
(defreserve procedure->definition)
(defreserve variable-value)
(defreserve set-variable-value!)
(defreserve variable-defined?)
;;;oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
;;; Use Meroon show functions to describe the inner working.
(defparameter *debug* +false+)
(defun show-registers (message)
(when *debug*
(format +true+ "~%----------------~A" message)
(format +true+ "~%ENV = ")
(show *env*)
(format +true+ "~%VAL = ")
(show *val*)
(format +true+ "~%FUN = ")
(show *fun*)
(show-stack (save-stack))
(format +true+
"~%(PC = ~A), next INSTR to be executed = ~A~%"
*pc*
(instruction-decode *code* *pc*))))
(defun show-stack (stack)
(let ((n (vector-length stack)))
(do ((i 0 (+ i 1)))
((= i n))
(format +true+ "~%STK[~A]= " i)
(show (vector-ref *stack* i)))))
(defmethod show ((f t) &optional (stream *standard-output*))
(format stream "~A" f))
(defmethod show ((f closure) &optional (stream *standard-output*))
(format stream "#<Closure(pc=~A)>" (closure-code f)))
(defmethod show ((a activation-frame) &optional (stream *standard-output*))
(display "[Frame next=" stream)
(show (activation-frame-next a) stream)
(display ", content=" stream)
(do ((i 0 (+ 1 i)))
((= i (activation-frame-argument-length a)))
(show (activation-frame-argument a i) stream)
(display " & " stream))
(display "]" stream))
;;;oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
(defun code-prologue ()
(set! finish-pc 0)
(FINISH))
(defun make-code-segment (m)
(apply (function vector) (append (code-prologue) m (%RET%))))
(defun chapter7d-interpreter ()
(labels ((toplevel ()
(display (funcall (stand-alone-producer7d (read)) 100))
(toplevel)))
(toplevel)))
(defun stand-alone-producer7d (e)
(set! g.current (original.g.current))
(set! *quotations* '())
(let* ((code (make-code-segment (meaning e r.init +true+)))
(start-pc (length (code-prologue)))
(global-names (mapcar (function car) (reverse g.current)))
(constants (apply (function vector) *quotations*)))
(lambda (stack-size)
(run-machine stack-size start-pc code
constants global-names))))
(defun run-machine (stack-size pc code constants global-names)
#+nil
(when *debugging*
(mapc (function print) (%disassemble% code)))
(set! sg.current (make-vector (length global-names)
undefined-value))
(set! sg.current.names global-names)
(set! *constants* constants)
(set! *code* code)
(set! *env* sr.init)
(set! *stack* (make-vector stack-size))
(set! *stack-index* 0)
(set! *val* 'anything)
(set! *fun* 'anything)
(set! *arg1* 'anything)
(set! *arg2* 'anything)
(stack-push finish-pc) ; pc for FINISH
(set! *pc* pc)
(call/cc
(lambda (exit)
(set! *exit* exit)
(run))))
;;; Patch run to show registers in debug mode.
(let ((native-run (function run)))
(setf (symbol-function 'run)
(lambda ()
(when *debug* (show-registers ""))
(funcall native-run))))
(let ((native-run-machine (function run-machine)))
(setf (symbol-function 'run-machine)
(lambda (stack-size pc code constants global-names)
(when *debug* ; DEBUG
(format +true+ "Code= ~A~%" (%disassemble% code)))
(funcall native-run-machine stack-size pc code constants global-names))))
;;;oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
;;; Tests
(defun %eval (form)
(funcall (stand-alone-producer7d form) 100)
*val*)
(defun scheme7d ()
(interpreter
"Scheme? "
"Scheme= "
+true+
(lambda (read print error)
(setup-wrong-functions error)
(lambda ()
(funcall (stand-alone-producer7d (funcall read)) 100)
(funcall print *val*)))))
(defun test-scheme7d (&optional (file *scheme-test-file*))
(suite-test
file
"Scheme? "
"Scheme= "
+true+
(lambda (read check error)
(setup-wrong-functions error)
(lambda ()
(funcall (stand-alone-producer7d (funcall read)) 100)
(funcall check *val*)))
(function equal?)))
(defun setup-wrong-functions (error)
(setf (symbol-function 'signal-exception)
(lambda (c &rest args)
#+nil
(declare (ignorable c))
(apply error args)))
(setf (symbol-function 'wrong)
(lambda (&rest args)
(format +true+ "
>>>>>>>>>>>>>>>>>>RunTime PANIC<<<<<<<<<<<<<<<<<<<<<<<<<
~A~%" (activation-frame-argument *val* 1))
(apply error args)))
(setf (symbol-function 'static-wrong)
(lambda (&rest args)
(format +true+ "
>>>>>>>>>>>>>>>>>>Static WARNING<<<<<<<<<<<<<<<<<<<<<<<<<
~A~%" args)
(apply error args))))
;;; Missing global variables
(defparameter signal-exception 'wait)
(defparameter finish-pc 'wait)
(defparameter *exit* 'wait)
;;; end of chap7d.scm
| 23,749 | Common Lisp | .lisp | 683 | 30.225476 | 84 | 0.645701 | gregcman/lisp-in-small-pieces | 11 | 2 | 0 | GPL-3.0 | 9/19/2024, 11:26:41 AM (Europe/Amsterdam) | f991828a36794b9e57eabf6fe31a53de429e95719ea1b1fd15e161e8c9f9059d | 8,739 | [
-1
] |
8,740 | chap6d.lisp | gregcman_lisp-in-small-pieces/chap6d.lisp | (in-package :lisp)
;;; Threaded interpreter.
;;; Environment is held by a global variable. This is bad for //ism.
;;; Continuation are now implicit and call/cc is a magical operator.
;;; Also try to introduce combinators as much as possible.
;;; Closures are explicitely represented.
(defparameter *env* '())
;;;oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
;;; Determine the nature of a variable.
;;; Three different answers. Or the variable is local (ie appears in R)
;;; then return (LOCAL index . depth)
;;; global (ie created by the user) then return
;;; (GLOBAL . index)
;;; or predefined (and immutable) then return
;;; (PREDEFINED . index)
(defun compute-kind (r n)
(or (local-variable? r 0 n)
(global-variable? g.current n)
(global-variable? g.init n)))
(defun local-variable? (r i n)
(and (pair? r)
(named-let scan ((names (car r))
(j 0))
(cond ((pair? names)
(if (eq? n (car names))
`(local ,i ,@j)
(scan (cdr names) (+ 1 j))))
((null? names)
(local-variable? (cdr r) (+ i 1) n))
((eq? n names)
`(local ,i ,@j))))))
(defun global-variable? (g n)
(let ((var (assq n g)))
(and (pair? var)
(cdr var))))
;;;oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
;;; Representation of local environments, they contain the values of
;;; the local variables (but global and predefined variables).
;;; Runtime environment or, activation frames, are represented by
;;; vectors (named v*). They have the following structure:
;;; +------------+
;;; | next | ---> next V*
;;; | argument 0 | value of the first argument
;;; | argument 1 | value of the second argument
;;; . .
;;; | free slot | Free slot for nary variable
;;; +------------+
;;; The number of arguments can be extracted from the size of the
;;; activation frame.
;;; A direct implementation with inlined vectors is approximatively
;;; 7 times faster under sci.
(progn
(defclass environment ()
((next :initarg next)))
(defun environment? (obj)
(typep obj 'environment))
(defun environment-next (obj)
(slot-value obj 'next))
(defun set-environment-next! (obj new)
(setf (slot-value obj 'next) new))
(defun make-environment (next)
(make-instance 'environment
'next next)))
(progn
(defclass activation-frame (environment)
((argument :initarg argument)))
(defun activation-frame? (obj)
(typep obj 'activation-frame))
(defun activation-frame-argument (obj index)
(aref (slot-value obj 'argument) index))
(defun activation-frame-argument-length (obj)
(array-total-size (slot-value obj 'argument)))
(defun set-activation-frame-argument! (obj index new)
(setf (aref (slot-value obj 'argument) index) new))
(defun activation-frame-next (obj)
(slot-value obj 'next))
(defun set-activation-frame-next! (obj new)
(setf (slot-value obj 'next) new))
(defun allocate-activation-frame (n)
(make-instance 'activation-frame
'argument (make-vector n))))
(defun sr-extend* (sr v*)
(set-environment-next! v* sr)
v*)
(defparameter sr.init '())
;;; Fetch the value of the Ith argument of the Jth frame.
(defun deep-fetch (sr i j)
(if (= i 0)
(activation-frame-argument sr j)
(deep-fetch (environment-next sr)
(- i 1)
j)))
(defun deep-update! (sr i j v)
(if (= i 0)
(set-activation-frame-argument! sr j v)
(deep-update! (environment-next sr)
(- i 1)
j
v)))
;;; R is the static representation of the runtime local environment.
;;; It is represented by a list of list of variables (the classical
;;; rib cage).
(defun r-extend* (r n*)
(cons n* r))
(defparameter r.init '())
;;;oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
;;; User-defined global environment definition. This environment is
;;; initially completely empty and can be extended by the user.
;;; It actually tolerates only 100 new global variables.
;;; G.CURRENT represents the `static' user-defined global environment.
;;; It is represented by the list of the symbols naming these global
;;; variables. Their values are held in the SG.CURRENT vector.
(defparameter g.current '())
(defparameter sg.current (make-vector 100))
(defparameter sg.current.names (list 'foo))
(defun g.current-extend! (n)
(let ((level (length g.current)))
(push (cons n `(global ,@level)) g.current)
level))
(defun global-fetch (i)
(vector-ref sg.current i))
(defun global-update! (i v)
(vector-set! sg.current i v))
(defun g.current-initialize! (name)
(let ((kind (compute-kind r.init name)))
(if kind
(case (car kind)
((global)
(vector-set! sg.current (cdr kind) undefined-value))
(otherwise (static-wrong "Wrong redefinition" name)))
(let ((index (g.current-extend! name)))
(vector-set! sg.current index undefined-value))))
name)
;;; This tag is used in the value cell of uninitialized variables.
(defparameter undefined-value (cons 'undefined 'value))
;;;oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
;;; Predefined global environment definition. This global environment
;;; is immutable. G.INIT represents the static predefined global
;;; environment and is represented by the list of the symbols naming
;;; these global variables. Their values are held in the SG.INIT vector.
(defparameter g.init '())
(defparameter sg.init (make-vector 100))
(defun predefined-fetch (i)
(vector-ref sg.init i))
(defun g.init-extend! (n)
(let ((level (length g.init)))
(push (cons n `(predefined ,@level)) g.init)
level))
;;; Add that value is associated to name in the predefined global environment.
(defun g.init-initialize! (name value)
(let ((kind (compute-kind r.init name)))
(if kind
(case (car kind)
((predefined)
(vector-set! sg.init (cdr kind) value))
(otherwise (static-wrong "Wrong redefinition" name)))
(let ((index (g.init-extend! name)))
(vector-set! sg.init index value))))
name)
;;; Definitial allows to redefine immutable global variables. Useful
;;; when debugging interactively.
(defmacro definitial (name value)
`(g.init-initialize! ',name ,value))
;;;oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
;;; Some free global locations:
;;; Define a location in the user global environment.
(defmacro defvariable (name)
`(g.current-initialize! ',name))
(defvariable x)
(defvariable y)
(defvariable z)
(defvariable a)
(defvariable b)
(defvariable c)
(defvariable foo)
(defvariable bar)
(defvariable hux)
(defvariable fib)
(defvariable fact)
(defvariable visit)
(defvariable length)
(defvariable primes)
;;; Preserve the current modifiable global environment (containing a,
;;; b, foo, fact, fib etc.) All tests will be compiled in that environment.
(let ((g g.current))
(defun original.g.current ()
g))
(defmacro defprimitive (name value number)
(ecase number
(0 `(defprimitive0 ,name ,value))
(1 `(defprimitive1 ,name ,value))
(2 `(defprimitive2 ,name ,value))
(3 `(defprimitive3 ,name ,value))))
;;;oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
;;; Representation of functions. A redefinition with inlined vectors
;;; for more speed.
(progn
(defclass closure ()
((code :initarg code)
(closed-environment :initarg closed-environment)))
(defun closure? (obj)
(typep obj 'closure))
(defun closure-code (obj)
(slot-value obj 'code))
(defun closure-closed-environment (obj)
(slot-value obj 'closed-environment))
(defun set-closure-code! (obj new)
(setf (slot-value obj 'code) new))
(defun set-closure-closed-environment! (obj)
(setf (slot-value obj 'closed-environment) obj))
(defun make-closure (code closed-environment)
(make-instance 'closure
'code code
'closed-environment closed-environment)))
;;;oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
;;; Describe a predefined value.
;;; The description language only represents primitives with their arity:
;;; (FUNCTION address . variables-list)
;;; with variables-list := () | (a) | (a b) | (a b c)
;;; Only the structure of the VARIABLES-LIST is interesting (not the
;;; names of the variables). ADDRESS is the address of the primitive
;;; to use when inlining an invokation to it. This address is
;;; represented by a Scheme procedure.
(defparameter desc.init '())
(defun description-extend! (name description)
(push (cons name description) desc.init)
name)
;;; Return the description or +false+ if absent.
(defun get-description (name)
(let ((p (assq name desc.init)))
(and (pair? p)
(cdr p))))
;;;oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
;;; The threaded interpreter.
;;; E is the expression to evaluate
;;; SR is the representation of the local lexical environment
;;; TAIL? is a boolean that indicates if E is a terminal call (also
;;; means whether the *env* register should be restored or not).
(defun meaning (e r tail?)
(if (atom? e)
(if (symbol? e)
(meaning-reference e r tail?)
(meaning-quotation e r tail?))
(case (car e)
((quote) (meaning-quotation (cadr e) r tail?))
((lambda) (meaning-abstraction (cadr e) (cddr e) r tail?))
((if) (meaning-alternative (cadr e) (caddr e) (cadddr e) r tail?))
((begin) (meaning-sequence (cdr e) r tail?))
((set!) (meaning-assignment (cadr e) (caddr e) r tail?))
(otherwise (meaning-application (car e) (cdr e) r tail?)))))
(defun meaning-reference (n r tail?)
(let ((kind (compute-kind r n)))
(if kind
(case (car kind)
((local)
(let ((i (cadr kind))
(j (cddr kind)))
(if (= i 0)
(SHALLOW-ARGUMENT-REF j)
(DEEP-ARGUMENT-REF i j))))
((global)
(let ((i (cdr kind)))
(CHECKED-GLOBAL-REF i)))
((predefined)
(let ((i (cdr kind)))
(PREDEFINED i))))
(static-wrong "No such variable" n))))
(defun meaning-quotation (v r tail?)
(CONSTANT v))
(defun meaning-alternative (e1 e2 e3 r tail?)
(let ((m1 (meaning e1 r +false+))
(m2 (meaning e2 r tail?))
(m3 (meaning e3 r tail?)))
(ALTERNATIVE m1 m2 m3)))
(defun meaning-assignment (n e r tail?)
(let ((m (meaning e r +false+))
(kind (compute-kind r n)))
(if kind
(case (car kind)
((local)
(let ((i (cadr kind))
(j (cddr kind)))
(if (= i 0)
(SHALLOW-ARGUMENT-SET! j m)
(DEEP-ARGUMENT-SET! i j m))))
((global)
(let ((i (cdr kind)))
(GLOBAL-SET! i m)))
((predefined)
(static-wrong "Immutable predefined variable" n)))
(static-wrong "No such variable" n))))
(defun meaning-sequence (e+ r tail?)
(if (pair? e+)
(if (pair? (cdr e+))
(meaning*-multiple-sequence (car e+) (cdr e+) r tail?)
(meaning*-single-sequence (car e+) r tail?))
(static-wrong "Illegal syntax: (begin)")))
(defun meaning*-single-sequence (e r tail?)
(meaning e r tail?))
(defun meaning*-multiple-sequence (e e+ r tail?)
(let ((m1 (meaning e r +false+))
(m+ (meaning-sequence e+ r tail?)))
(%SEQUENCE% m1 m+)))
(defun meaning-abstraction (nn* e+ r tail?)
(named-let parse ((n* nn*)
(regular '()))
(cond
((pair? n*) (parse (cdr n*) (cons (car n*) regular)))
((null? n*) (meaning-fix-abstraction nn* e+ r tail?))
(t (meaning-dotted-abstraction
(reverse regular) n* e+ r tail?)))))
(defun meaning-fix-abstraction (n* e+ r tail?)
(let* ((arity (length n*))
(r2 (r-extend* r n*))
(m+ (meaning-sequence e+ r2 +true+)))
(FIX-CLOSURE m+ arity)))
(defun meaning-dotted-abstraction (n* n e+ r tail?)
(let* ((arity (length n*))
(r2 (r-extend* r (append n* (list n))))
(m+ (meaning-sequence e+ r2 +true+)))
(NARY-CLOSURE m+ arity)))
;;; Application meaning.
(defun meaning-application (e e* r tail?)
(if (and (pair? e)
(eq? 'lambda (car e)))
(meaning-closed-application e e* r tail?)
(meaning-regular-application e e* r tail?)))
;;; Parse the variable list to check the arity and detect wether the
;;; abstraction is dotted or not.
(defun meaning-closed-application (e ee* r tail?)
(let ((nn* (cadr e)))
(named-let parse ((n* nn*)
(e* ee*)
(regular '()))
(cond
((pair? n*)
(if (pair? e*)
(parse (cdr n*) (cdr e*) (cons (car n*) regular))
(static-wrong "Too less arguments" e ee*)))
((null? n*)
(if (null? e*)
(meaning-fix-closed-application
nn* (cddr e) ee* r tail?)
(static-wrong "Too much arguments" e ee*)))
(t (meaning-dotted-closed-application
(reverse regular) n* (cddr e) ee* r tail?))))))
(defun meaning-fix-closed-application (n* body e* r tail?)
(let* ((m* (meaning* e* r (length e*) +false+))
(r2 (r-extend* r n*))
(m+ (meaning-sequence body r2 tail?)))
(if tail?
(TR-FIX-LET m* m+)
(FIX-LET m* m+))))
(defun meaning-dotted-closed-application (n* n body e* r tail?)
(let* ((m* (meaning-dotted* e* r (length e*) (length n*) +false+))
(r2 (r-extend* r (append n* (list n))))
(m+ (meaning-sequence body r2 tail?)))
(if tail?
(TR-FIX-LET m* m+)
(FIX-LET m* m+))))
;;; In a regular application, the invocation protocol is to call the
;;; function with an activation frame and a continuation: (f v* k).
(defun meaning-regular-application (e e* r tail?)
(let* ((m (meaning e r +false+))
(m* (meaning* e* r (length e*) +false+)))
(if tail?
(TR-REGULAR-CALL m m*)
(REGULAR-CALL m m*))))
(defun meaning* (e* r size tail?)
(if (pair? e*)
(meaning-some-arguments (car e*) (cdr e*) r size tail?)
(meaning-no-argument r size tail?)))
(defun meaning-dotted* (e* r size arity tail?)
(if (pair? e*)
(meaning-some-dotted-arguments (car e*) (cdr e*)
r size arity tail?)
(meaning-no-dotted-argument r size arity tail?)))
(defun meaning-some-arguments (e e* r size tail?)
(let ((m (meaning e r +false+))
(m* (meaning* e* r size tail?))
(rank (- size (+ (length e*) 1))))
(STORE-ARGUMENT m m* rank)))
(defun meaning-some-dotted-arguments (e e* r size arity tail?)
(let ((m (meaning e r +false+))
(m* (meaning-dotted* e* r size arity tail?))
(rank (- size (+ (length e*) 1))))
(if (< rank arity)
(STORE-ARGUMENT m m* rank)
(CONS-ARGUMENT m m* arity))))
(defun meaning-no-argument (r size tail?)
(ALLOCATE-FRAME size))
(defun meaning-no-dotted-argument (r size arity tail?)
(ALLOCATE-DOTTED-FRAME arity))
;;; Gather into a list all arguments from arity+1 to the end of the
;;; activation frame and store this list into the arity+1th slot.
(defun listify! (v* arity)
(named-let rec ((index (- (activation-frame-argument-length v*) 1))
(result '()))
(if (= arity index)
(set-activation-frame-argument! v* arity result)
(rec (- index 1)
(cons (activation-frame-argument v* (- index 1))
result)))))
| 15,529 | Common Lisp | .lisp | 403 | 33.875931 | 79 | 0.644662 | gregcman/lisp-in-small-pieces | 11 | 2 | 0 | GPL-3.0 | 9/19/2024, 11:26:41 AM (Europe/Amsterdam) | 7df6dbe5e16291be4b08953d4fa4c10bd5eecfc34c397509ed0fb0de1e19c8bf | 8,740 | [
-1
] |
8,741 | other2.lisp | gregcman_lisp-in-small-pieces/other2.lisp | (in-package :lisp)
(defun static-wrong (message &rest args)
(print args)
(error message))
(defun wrong (message &rest args)
(print args)
(error message))
(defun signal-exception (message args)
(declare (ignore message))
(print args))
| 249 | Common Lisp | .lisp | 10 | 22.4 | 40 | 0.724576 | gregcman/lisp-in-small-pieces | 11 | 2 | 0 | GPL-3.0 | 9/19/2024, 11:26:41 AM (Europe/Amsterdam) | 06be063794a4de4b54a333045d0e1bb4f3ae11e222f536e93bbf304baf7bef87 | 8,741 | [
-1
] |
8,742 | chap7d-unused.lisp | gregcman_lisp-in-small-pieces/unused/chap7d-unused.lisp | #+nil
(define-class primitive Object
(address))
#+nil
(define-class continuation Object
(stack))
#+nil
(progn
(defun CALL0 (address)
(INVOKE0 address))
(defun CALL1 (address m1)
(append m1
(INVOKE1 address)))
(defun CALL2 (address m1 m2)
(append m1
(PUSH-VALUE)
m2
(POP-ARG1)
(INVOKE2 address)))
(defun CALL3 (address m1 m2 m3)
(append m1
(PUSH-VALUE)
m2
(PUSH-VALUE)
m3
(POP-ARG2)
(POP-ARG1)
(INVOKE3 address))))
(defun SHALLOW-ARGUMENT-REF (j)
(check-byte j)
(case j
((0 1 2 3) (list (+ 1 j)))
(otherwise (list 5 j))))
(defun PREDEFINED (i)
(check-byte i)
(case i
;; 0=\+true+, 1=\+false+, 2=(), 3=cons, 4=car, 5=cdr, 6=pair?, 7=symbol?, 8=eq?
((0 1 2 3 4 5 6 7 8) (list (+ 10 i)))
(otherwise (list 19 i))))
(defun SET-SHALLOW-ARGUMENT! (j)
(case j
((0 1 2 3) (list (+ 21 j)))
(otherwise (list 25 j))))
(defun CONSTANT (value)
(cond
((eq? value +true+) (list 10))
((eq? value +false+) (list 11))
((eq? value '()) (list 12))
((equal? value -1) (list 80))
((equal? value 0) (list 81))
((equal? value 1) (list 82))
((equal? value 2) (list 83))
((equal? value 4) (list 84))
((and (integer? value) ; immediate value
(<= 0 value)
(< value 255))
(list 79 value))
(t (EXPLICIT-CONSTANT value))))
(defun INVOKE0 (address)
(case address
((read) (list 89))
((newline) (list 88))
(otherwise (static-wrong "Cannot integrate" address))))
#+nil
(defun INVOKE1 (address)
(case address
((car) (list 90))
((cdr) (list 91))
((pair?) (list 92))
((symbol?) (list 93))
((display) (list 94))
(otherwise (static-wrong "Cannot integrate" address))))
;;; The same one with other unary primitives.
(defun INVOKE1 (address)
(case address
((car) (list 90))
((cdr) (list 91))
((pair?) (list 92))
((symbol?) (list 93))
((display) (list 94))
((primitive?) (list 95))
((null?) (list 96))
((continuation?) (list 97))
((eof-object?) (list 98))
(otherwise (static-wrong "Cannot integrate" address))))
(defun INVOKE2 (address)
(case address
((cons) (list 100))
((eq?) (list 101))
((set-car!) (list 102))
((set-cdr!) (list 103))
((+) (list 104))
((-) (list 105))
((=) (list 106))
((<) (list 107))
((>) (list 108))
((*) (list 109))
((<=) (list 110))
((>=) (list 111))
((remainder)(list 112))
(otherwise (static-wrong "Cannot integrate" address))))
(defun INVOKE3 (address)
(static-wrong "No ternary integrated procedure" address))
(defun ARITY=? (arity+1)
(case arity+1
((1 2 3 4) (list (+ 70 arity+1)))
(otherwise (list 75 arity+1))))
(defun POP-FRAME! (rank)
(case rank
((0 1 2 3) (list (+ 60 rank)))
(otherwise (list 64 rank))))
(defun ALLOCATE-FRAME (size)
(case size
((0 1 2 3 4) (list (+ 50 size)))
(otherwise (list 55 (+ size 1)))))
| 3,086 | Common Lisp | .lisp | 115 | 22.634783 | 83 | 0.551234 | gregcman/lisp-in-small-pieces | 11 | 2 | 0 | GPL-3.0 | 9/19/2024, 11:26:41 AM (Europe/Amsterdam) | 11420fd9e5b47d7f1d4db0fc328ede60a7ec218766a8278e3453fe1b87de1e9e | 8,742 | [
-1
] |
8,743 | chap6d-unused.lisp | gregcman_lisp-in-small-pieces/unused/chap6d-unused.lisp | (in-package :lisp)
;;;oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
#+nil
(defun invoke (f v*)
(if (closure? f)
(funcall (closure-code f) v* (closure-closed-environment f))
(wrong "Not a function" f)))
;;;oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
;;; Combinators
(defun SHALLOW-ARGUMENT-REF (j)
(lambda () (activation-frame-argument *env* j)))
(defun PREDEFINED (i)
(lambda () (predefined-fetch i)))
(defun DEEP-ARGUMENT-REF (i j)
(lambda () (deep-fetch *env* i j)))
(defun SHALLOW-ARGUMENT-SET! (j m)
(lambda () (set-activation-frame-argument! *env* j (m))))
(defun DEEP-ARGUMENT-SET! (i j m)
(lambda () (deep-update! *env* i j (m))))
(defun GLOBAL-REF (i)
(lambda () (global-fetch i)))
;;; Note that we lost the name of the variable, it must be retrieved
;;; from elsewhere. TOBEDONE
(defun CHECKED-GLOBAL-REF (i)
(lambda ()
(let ((v (global-fetch i)))
(if (eq? v undefined-value)
(wrong "Uninitialized variable")
v))))
(defun GLOBAL-SET! (i m)
(lambda () (global-update! i (m))))
(defun CONSTANT (value)
(lambda () value))
(defun ALTERNATIVE (m1 m2 m3)
(lambda () (if (m1) (m2) (m3))))
(defun %SEQUENCE% (m m+)
(lambda () (m) (m+)))
(defun TR-FIX-LET (m* m+)
(lambda ()
(set! *env* (sr-extend* *env* (m*)))
(m+)))
(defun FIX-LET (m* m+)
(lambda ()
(set! *env* (sr-extend* *env* (m*)))
(let ((result (m+)))
(set! *env* (environment-next *env*))
result)))
(defun CALL0 (address)
(lambda () (address)))
(defun CALL1 (address m1)
(lambda () (address (m1))))
(defun CALL2 (address m1 m2)
(lambda () (let ((v1 (m1)))
(address v1 (m2)))))
(defun CALL3 (address m1 m2 m3)
(lambda () (let* ((v1 (m1))
(v2 (m2)))
(address v1 v2 (m3)))))
(defun FIX-CLOSURE (m+ arity)
(let ((arity+1 (+ arity 1)))
(lambda ()
(labels ((the-function (v* sr)
(if (= (activation-frame-argument-length v*) arity+1)
(begin (set! *env* (sr-extend* sr v*))
(m+))
(wrong "Incorrect arity"))))
(make-closure
(function the-function)
*env*)))))
(defun NARY-CLOSURE (m+ arity)
(let ((arity+1 (+ arity 1)))
(lambda ()
(labels ((the-function (v* sr)
(if (>= (activation-frame-argument-length v*) arity+1)
(begin
(listify! v* arity)
(set! *env* (sr-extend* sr v*))
(m+))
(wrong "Incorrect arity"))))
(make-closure (function the-function) *env*)))))
(defun TR-REGULAR-CALL (m m*)
(lambda ()
(let ((f (m)))
(invoke f (m*)))))
(defun REGULAR-CALL (m m*)
(lambda ()
(let* ((f (m))
(v* (m*))
(sr *env*)
(result (invoke f v*)))
(set! *env* sr)
result)))
(defun STORE-ARGUMENT (m m* rank)
(lambda ()
(let* ((v (m))
(v* (m*)))
(set-activation-frame-argument! v* rank v)
v*)))
(defun CONS-ARGUMENT (m m* arity)
(lambda ()
(let* ((v (m))
(v* (m*)))
(set-activation-frame-argument!
v* arity (cons v (activation-frame-argument v* arity)))
v*)))
(defun ALLOCATE-FRAME (size)
(let ((size+1 (+ size 1)))
(lambda ()
(allocate-activation-frame size+1))))
(defun ALLOCATE-DOTTED-FRAME (arity)
(let ((arity+1 (+ arity 1)))
(lambda ()
(let ((v* (allocate-activation-frame arity+1)))
(set-activation-frame-argument! v* arity '())
v*))))
;;;oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
;;; Global environment initializers.
(defmacro defprimitive1 (name value)
`(definitial ,name
(letrec ((arity+1 (+ 1 1))
(behavior
(lambda (v* sr)
(if (= (activation-frame-argument-length v*)
arity+1)
(,value (activation-frame-argument v* 0))
(wrong "Incorrect arity" ',name)))))
(description-extend! ',name `(function ,',value a))
(make-closure behavior sr.init))))
(defmacro defprimitive2 (name value)
`(definitial ,name
(letrec ((arity+1 (+ 2 1))
(behavior
(lambda (v* sr)
(if (= (activation-frame-argument-length v*)
arity+1)
(value (activation-frame-argument v* 0)
(activation-frame-argument v* 1))
(wrong "Incorrect arity" ',name)))))
(description-extend! ',name `(function ,',value a b))
(make-closure behavior sr.init))))
;;;oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
;;; Initialization of the predefined global environment.
(defprimitive cons cons 2)
(defprimitive car car 1)
(defprimitive cdr cdr 1)
(defprimitive pair? pair? 1)
(defprimitive symbol? symbol? 1)
(defprimitive eq? eq? 2)
(defprimitive set-car! set-car! 2)
(defprimitive set-cdr! set-cdr! 2)
(defprimitive + + 2)
(defprimitive - - 2)
(defprimitive = = 2)
(defprimitive < < 2)
(defprimitive > > 2)
(defprimitive * * 2)
(defprimitive <= <= 2)
(defprimitive >= >= 2)
(defprimitive remainder remainder 2)
(defprimitive display display 1)
;;; We do not need to save the register *env* since call/cc is not a
;;; primitive (it is defined by definitial and not by defprimitive)
;;; and non-primitive invokations are regularly handled.
(definitial call/cc
(let* ((arity 1)
(arity+1 (+ arity 1)))
(make-closure
(lambda (v* sr)
(if (= arity+1 (activation-frame-argument-length v*))
(call/cc
(lambda (k)
(invoke
(activation-frame-argument v* 0)
(let ((frame (allocate-activation-frame (+ 1 1))))
(set-activation-frame-argument!
frame 0
(make-closure
(lambda (values r)
(if (= (activation-frame-argument-length values)
arity+1)
(k (activation-frame-argument values 0))
(wrong "Incorrect arity" 'continuation)))
sr.init))
frame))))
(wrong "Incorrect arity" 'call/cc)))
sr.init)))
(definitial apply
(let* ((arity 2)
(arity+1 (+ arity 1)))
(make-closure
(lambda (v* sr)
(if (>= (activation-frame-argument-length v*) arity+1)
(let* ((proc (activation-frame-argument v* 0))
(last-arg-index
(- (activation-frame-argument-length v*) 2))
(last-arg
(activation-frame-argument v* last-arg-index))
(size (+ last-arg-index (length last-arg)))
(frame (allocate-activation-frame size)))
(do ((i 1 (+ i 1)))
((= i last-arg-index))
(set-activation-frame-argument!
frame (- i 1) (activation-frame-argument v* i)))
(do ((i (- last-arg-index 1) (+ i 1))
(last-arg last-arg (cdr last-arg)))
((null? last-arg))
(set-activation-frame-argument!
frame i (car last-arg)))
(invoke proc frame))
(wrong "Incorrect arity" 'apply)))
sr.init)))
(definitial list (funcall (NARY-CLOSURE (SHALLOW-ARGUMENT-REF 0) 0)))
;;;oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
;;; Testing
(defun chapter63-interpreter ()
(labels ((toplevel ()
(set! *env* sr.init)
(display (funcall (meaning (read) r.init +true+)))
(toplevel)))
(toplevel)))
;;; This variant produces a table of symbols.
(defun stand-alone-producer (e)
(set! g.current (original.g.current))
(let* ((m (meaning e r.init +true+))
(size (length g.current))
(global-names (mapcar (function car) (reverse g.current))))
(lambda ()
(set! sg.current (make-vector size undefined-value))
(set! sg.current.names global-names)
(set! *env* sr.init)
(m))))
(defun CHECKED-GLOBAL-REF+ (i)
(lambda ()
(let ((v (global-fetch i)))
(if (eq? v undefined-value)
(wrong "Uninitialized variable"
(list-ref sg.current.names i))
v))))
;;; this one requires to close the name of the variables that must be
;;; checked. To use it you must also change meaning-reference that calls it.
(defun CHECKED-GLOBAL-REF- (i n)
(lambda ()
(let ((v (global-fetch i)))
(if (eq? v undefined-value)
(wrong "Uninitialized variable" n)
v))))
;;; retrofit for tests.
(setf (symbol-function 'CHECKED-GLOBAL-REF)
(function CHECKED-GLOBAL-REF+))
(defun scheme6d ()
(interpreter
"Scheme? "
"Scheme= "
+true+
(lambda (read print error)
(set! wrong error)
(set! static-wrong error)
(lambda ()
(set! *env* sr.init)
(print (funcall (stand-alone-producer (read))))))))
(defun test-scheme6d (file)
(suite-test
file
"Scheme? "
"Scheme= "
+true+
(lambda (read check error)
(set! wrong error)
(set! static-wrong error)
(lambda ()
(check (funcall (stand-alone-producer (read))))))
equal?))
;;; Pay attention to tail-rec in Scheme->C.
(defun bench6d (factor e)
(let ((start (get-internal-run-time))
(m (meaning e r.init +true+)))
(named-let rec ((factor factor))
(set! *env* sr.init)
(let ((v (m)))
(let ((duration (- (get-internal-run-time) start)))
(when (<= factor 1)
(display (list duration v))
(newline))))
(if (> factor 1)
(rec (- factor 1))))))
;;;ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
;;; The following code use
;;; pp to pretty-print expressions,
;;; and eval for a local hack (should be made of macros instead).
(defparameter combinator-names
'(SHALLOW-ARGUMENT-REF
PREDEFINED
DEEP-ARGUMENT-REF
SHALLOW-ARGUMENT-SET!
DEEP-ARGUMENT-SET!
GLOBAL-REF
CHECKED-GLOBAL-REF
GLOBAL-SET!
CONSTANT
ALTERNATIVE
%SEQUENCE%
TR-FIX-LET
FIX-LET
CALL0
CALL1
CALL2
CALL3
FIX-CLOSURE
NARY-CLOSURE
TR-REGULAR-CALL
REGULAR-CALL
STORE-ARGUMENT
CONS-ARGUMENT
ALLOCATE-FRAME
ALLOCATE-DOTTED-FRAME))
(let ((originals (mapcar (function symbol-function) combinator-names)))
(defun install-regular-combinators ()
(for-each (lambda (old-value name)
(eval `(set! ,name ',old-value)))
originals
combinator-names)))
(defun install-disassembling-combinators ()
(for-each (lambda (name)
(eval `(set! ,name (lambda args (,name ,@args)))))
combinator-names))
(defun %disassemble% (e)
(install-disassembling-combinators)
(pp (meaning e r.init +true+))
(install-regular-combinators)
(newline))
;;; (disassemble '(lambda (n) (if (= n 0) 1 (* n (fact (- n 1))))))
;;; (disassemble '(lambda (n) (if (= n 0) 1 (* (fact (- n 1)) n))))
;;; end of chap6d.scm
;;;;*****************************modified functions*******************************
(define-class environment Object
(next))
(define-class activation-frame environment
((* argument)))
(define-class closure Object
(code
closed-environment))
(defun meaning-application (e e* r tail?)
(cond
((and (symbol? e)
(let ((kind (compute-kind r e)))
(and (pair? kind)
(eq? 'predefined (car kind))
(let ((desc (get-description e)))
(and desc
(eq? 'function (car desc))
(or (= (length (cddr desc)) (length e*))
(static-wrong
"Incorrect arity for primitive" e)
))))))
(meaning-primitive-application e e* r tail?))
((and (pair? e)
(eq? 'lambda (car e)))
(meaning-closed-application e e* r tail?))
(t (meaning-regular-application e e* r tail?))))
;;; Handles a call to a predefined primitive. The arity is already checked.
;;; The optimization is to avoid the allocation of the activation frame.
;;; These primitives never change the *env* register nor have control effect.
(defun meaning-primitive-application (e e* r tail?)
(let* ((desc (get-description e))
;; desc = (function address . variables-list)
(address (cadr desc))
(size (length e*)))
(case size
((0) (CALL0 address))
((1)
(let ((m1 (meaning (car e*) r +false+)))
(CALL1 address m1)))
((2)
(let ((m1 (meaning (car e*) r +false+))
(m2 (meaning (cadr e*) r +false+)))
(CALL2 address m1 m2)))
((3)
(let ((m1 (meaning (car e*) r +false+))
(m2 (meaning (cadr e*) r +false+))
(m3 (meaning (caddr e*) r +false+)))
(CALL3 address m1 m2 m3)))
(otherwise (meaning-regular-application e e* r tail?)))))
| 12,660 | Common Lisp | .lisp | 382 | 27.041885 | 82 | 0.594236 | gregcman/lisp-in-small-pieces | 11 | 2 | 0 | GPL-3.0 | 9/19/2024, 11:26:41 AM (Europe/Amsterdam) | ee9dc7a42d2b884de03656023628b737a0c7abd460c90aff606eed2618e9fe38 | 8,743 | [
-1
] |
8,744 | chap7f.lisp | gregcman_lisp-in-small-pieces/unused/chap7f.lisp | ;;; Instruction set.
;;; This file is read by chap7d.scm
| 57 | Common Lisp | .lisp | 2 | 27.5 | 35 | 0.709091 | gregcman/lisp-in-small-pieces | 11 | 2 | 0 | GPL-3.0 | 9/19/2024, 11:26:41 AM (Europe/Amsterdam) | a5f423e40e6df73d88a9e9d2f28854b0c48438df690620256088f0b503865c68 | 8,744 | [
-1
] |
8,745 | chap7f-unused.lisp | gregcman_lisp-in-small-pieces/unused/chap7f-unused.lisp |
#+nil
(progn
(define-instruction (SHALLOW-ARGUMENT-REF0) 1
(set! *val* (activation-frame-argument *env* 0)))
(define-instruction (SHALLOW-ARGUMENT-REF1) 2
(set! *val* (activation-frame-argument *env* 1)))
(define-instruction (SHALLOW-ARGUMENT-REF2) 3
(set! *val* (activation-frame-argument *env* 2)))
(define-instruction (SHALLOW-ARGUMENT-REF3) 4
(set! *val* (activation-frame-argument *env* 3))))
#+nil
(progn
(define-instruction (PREDEFINED0) 10 ; \+true+
(set! *val* +true+))
(define-instruction (PREDEFINED1) 11 ; \+false+
(set! *val* +false+))
(define-instruction (PREDEFINED2) 12 ; ()
(set! *val* '()))
(define-instruction (PREDEFINED3) 13 ; cons
(set! *val* (predefined-fetch 3)))
(define-instruction (PREDEFINED4) 14 ; car
(set! *val* (predefined-fetch 4)))
(define-instruction (PREDEFINED5) 15 ; cdr
(set! *val* (predefined-fetch 5)))
(define-instruction (PREDEFINED6) 16 ; pair?
(set! *val* (predefined-fetch 6)))
(define-instruction (PREDEFINED7) 17 ; symbol?
(set! *val* (predefined-fetch 7)))
(define-instruction (PREDEFINED8) 18 ; eq?
(set! *val* (predefined-fetch 8))))
#+nil
(progn
(define-instruction (SET-SHALLOW-ARGUMENT!0) 21
(set-activation-frame-argument! *env* 0 *val*))
(define-instruction (SET-SHALLOW-ARGUMENT!1) 22
(set-activation-frame-argument! *env* 1 *val*))
(define-instruction (SET-SHALLOW-ARGUMENT!2) 23
(set-activation-frame-argument! *env* 2 *val*))
(define-instruction (SET-SHALLOW-ARGUMENT!3) 24
(set-activation-frame-argument! *env* 3 *val*)))
#+nil
(progn
(define-instruction (ALLOCATE-FRAME1) 50
(set! *val* (allocate-activation-frame 1)))
(define-instruction (ALLOCATE-FRAME2) 51
(set! *val* (allocate-activation-frame 2)))
(define-instruction (ALLOCATE-FRAME3) 52
(set! *val* (allocate-activation-frame 3)))
(define-instruction (ALLOCATE-FRAME4) 53
(set! *val* (allocate-activation-frame 4)))
(define-instruction (ALLOCATE-FRAME5) 54
(set! *val* (allocate-activation-frame 5))))
#+nil
(progn
(define-instruction (POP-FRAME!0) 60
(set-activation-frame-argument! *val* 0 (stack-pop)))
(define-instruction (POP-FRAME!1) 61
(set-activation-frame-argument! *val* 1 (stack-pop)))
(define-instruction (POP-FRAME!2) 62
(set-activation-frame-argument! *val* 2 (stack-pop)))
(define-instruction (POP-FRAME!3) 63
(set-activation-frame-argument! *val* 3 (stack-pop))))
#+nil
(progn
(define-instruction (ARITY=?1) 71
(unless (= (activation-frame-argument-length *val*) 1)
(signal-exception +false+ (list "Too much arguments for a thunk"))))
(define-instruction (ARITY=?2) 72
(unless (= (activation-frame-argument-length *val*) 2)
(signal-exception
+false+ (list "Incorrect arity for unary function"))))
(define-instruction (ARITY=?3) 73
(unless (= (activation-frame-argument-length *val*) 3)
(signal-exception +false+ (list "Incorrect arity for binary function"))))
(define-instruction (ARITY=?4) 74
(unless (= (activation-frame-argument-length *val*) 4)
(signal-exception +false+ (list "Incorrect arity for ternary function")))))
#+nil
(progn
(define-instruction (CONSTANT-1) 80
(set! *val* -1))
(define-instruction (CONSTANT0) 81
(set! *val* 0))
(define-instruction (CONSTANT1) 82
(set! *val* 1))
(define-instruction (CONSTANT2) 83
(set! *val* 2))
(define-instruction (CONSTANT4) 84
(set! *val* 4)))
#+nil
(progn
(define-instruction (CALL0-newline) 88
(set! *val* (newline)))
(define-instruction (CALL0-read) 89
(set! *val* (read)))
(define-instruction (CALL1-car) 90
(set! *val* (car *val*)))
(define-instruction (CALL1-cdr) 91
(set! *val* (cdr *val*)))
(define-instruction (CALL1-pair?) 92
(set! *val* (pair? *val*)))
(define-instruction (CALL1-symbol?) 93
(set! *val* (symbol? *val*)))
(define-instruction (CALL1-display) 94
(set! *val* (show *val*)))
(define-instruction (CALL1-primitive?) 95
(set! *val* (primitive? *val*)))
(define-instruction (CALL1-null?) 96
(set! *val* (null? *val*)))
(define-instruction (CALL1-continuation?) 97
(set! *val* (continuation? *val*)))
(define-instruction (CALL1-eof-object?) 98
(set! *val* (eof-object? *val*)))
(define-instruction (CALL2-cons) 100
(set! *val* (cons *arg1* *val*)))
(define-instruction (CALL2-eq?) 101
(set! *val* (eq? *arg1* *val*)))
(define-instruction (CALL2-set-car!) 102
(set! *val* (set-car! *arg1* *val*)))
(define-instruction (CALL2-set-cdr!) 103
(set! *val* (set-cdr! *arg1* *val*)))
(define-instruction (CALL2-+) 104
(set! *val* (+ *arg1* *val*)))
(define-instruction (CALL2--) 105
(set! *val* (- *arg1* *val*)))
(define-instruction (CALL2-=) 106
(set! *val* (= *arg1* *val*)))
(define-instruction (CALL2-<) 107
(set! *val* (< *arg1* *val*)))
(define-instruction (CALL2->) 108
(set! *val* (> *arg1* *val*)))
(define-instruction (CALL2-*) 109
(set! *val* (* *arg1* *val*)))
(define-instruction (CALL2-<=) 110
(set! *val* (<= *arg1* *val*)))
(define-instruction (CALL2->=) 111
(set! *val* (>= *arg1* *val*)))
(define-instruction (CALL2-remainder) 112
(set! *val* (remainder *arg1* *val*))))
(define-instruction (DYNAMIC-REF index) 240
(set! *val* (find-dynamic-value index)))
(define-instruction (DYNAMIC-POP) 241
(pop-dynamic-binding))
(define-instruction (DYNAMIC-PUSH index) 242
(push-dynamic-binding index *val*))
(define-instruction (NON-CONT-ERR) 245
(signal-exception +false+ (list "Non continuable exception continued")))
(define-instruction (PUSH-HANDLER) 246
(push-exception-handler))
(define-instruction (POP-HANDLER) 247
(pop-exception-handler))
(define-instruction (POP-ESCAPER) 250
(let* ((tag (stack-pop))
(escape (stack-pop)))
(restore-environment)))
(define-instruction (PUSH-ESCAPER offset) 251
(preserve-environment)
(let* ((escape (make-escape (+ *stack-index* 3)))
(frame (allocate-activation-frame 1)))
(set-activation-frame-argument! frame 0 escape)
(set! *env* (sr-extend* *env* frame))
(stack-push escape)
(stack-push escape-tag)
(stack-push (+ *pc* offset))))
;;; Used by chap8d.scm (eval as a special form)
(define-instruction (COMPILE-RUN) 255
(let ((v *val*)
(r (stack-pop)))
(if (program? v)
(compile-and-run v r +false+)
(signal-exception +true+ (list "Illegal program" v)))))
;;; Used by chap8h.scm (export special form)
(define-instruction (CREATE-1ST-CLASS-ENV) 254
(create-first-class-environment *val* *env*))
(define-instruction (CHECKED-DEEP-REF i j) 253
(set! *val* (deep-fetch *env* i j))
(when (eq? *val* undefined-value)
(signal-exception +true+ (list "Uninitialized local variable"))))
(define-instruction (CREATE-PSEUDO-ENV) 252
(create-pseudo-environment (stack-pop) *val* *env*))
(define-instruction (SHADOW-REF i j) 231
(shadowable-fetch *env* i j))
(define-instruction (SET-SHADOW! i j) 232
(shadowable-update! *env* i j *val*))
;;; end of chap7f.scm
| 7,191 | Common Lisp | .lisp | 185 | 34.762162 | 81 | 0.655637 | gregcman/lisp-in-small-pieces | 11 | 2 | 0 | GPL-3.0 | 9/19/2024, 11:26:41 AM (Europe/Amsterdam) | 11a498907bf3e61bf2316796d6fb6e7d350067f38c8142812048039425fce4d7 | 8,745 | [
-1
] |
8,746 | package.lisp | gregcman_lisp-in-small-pieces/interpreters/package.lisp | (defpackage #:lisp-in-small-pieces
(:use :cl)
(:shadow
#:defun
#:defparameter
#:fmakunbound
#:makunbound
#:boundp
#:fboundp
)
(:nicknames #:lisp))
(in-package #:lisp)
(defmacro defun (name args &body body)
`(progn (cl:defun ,name ,args ,@body)
(setf (gethash ',name *function-namespace*) t)
(setf (get
',name
'defun)
'(lambda ,args ,@body))
(setf (get
',name
'arity)
,(length args))))
(defmacro defparameter (name value)
`(progn (cl:defparameter ,name ,value)
(setf (gethash ',name *variable-namespace*) t)
(setf (get ',name 'defparameter)
',value)))
(defparameter *variable-namespace* (make-hash-table :test 'eq))
(defparameter *function-namespace* (make-hash-table :test 'eq))
(defparameter *not-found* (cons "not" "found"))
(defun prop-exists (name indicator)
(not (eq *not-found* (get name indicator *not-found*))))
(defun boundp (name)
(or (prop-exists name 'defvar)
(prop-exists name 'defparameter)))
(defun fboundp (name)
(prop-exists name 'defun))
(defun fmakunbound (name)
(remf name 'defun)
(remhash name *function-namespace*)
(cl:fmakunbound name))
(defun makunbound (name)
(remf name 'defparameter)
(remf name 'defvar)
(remhash name *variable-namespace*)
(cl:makunbound name))
(setf *print-case* :downcase)
(setf *print-circle* t)
| 1,337 | Common Lisp | .lisp | 49 | 24.367347 | 63 | 0.683801 | gregcman/lisp-in-small-pieces | 11 | 2 | 0 | GPL-3.0 | 9/19/2024, 11:26:41 AM (Europe/Amsterdam) | d5734decb2f08584c8eb54e59dea41bad91a95ee92f9560be962793f6fc1a67d | 8,746 | [
-1
] |
8,747 | other.lisp | gregcman_lisp-in-small-pieces/interpreters/other.lisp | (in-package :lisp)
;;;;values
(defparameter *the-false-value* nil)
(defun truep (value)
(not (eq *the-false-value* value)))
(defun booleanp (value)
(or (eq *the-false-value* value)
(eq t value)))
(defparameter *empty-progn* 69)
;;;;environments
;;;;represented as alists where each cell is (symbol . value)
(defparameter *initial-environment* '())
(defun lookup (id env)
(if (consp env)
(if (eq (car (car env)) id)
(cdr (car env))
(lookup id (cdr env)))
(error "no such binding ~s" id)))
(defun update! (id env value)
(if (consp env)
(if (eq (car (car env)) id)
(progn (rplacd (car env) value)
value)
(update! id (cdr env) value))
(error "no such binding ~s" id)))
(defun extend (env variables values)
(cond ((consp variables)
(if (consp values)
(cons (cons (car variables)
(car values))
(extend env
(cdr variables)
(cdr values)))
(error "too few values")))
((null variables)
(if (null values)
env
(error "too many values")))
((symbolp variables)
(cons (cons variables
values)
env))))
| 1,110 | Common Lisp | .lisp | 42 | 22.452381 | 61 | 0.624765 | gregcman/lisp-in-small-pieces | 11 | 2 | 0 | GPL-3.0 | 9/19/2024, 11:26:41 AM (Europe/Amsterdam) | a16affe2cfd431540e81e63edc9e23a6733c18c98263306d644e45d025d38fdc | 8,747 | [
-1
] |
8,748 | lisp-in-small-pieces2.lisp | gregcman_lisp-in-small-pieces/interpreters/lisp-in-small-pieces2.lisp | (in-package :lisp)
;;;;f.eval
(defun %eval (form env fenv)
(if (atom form)
(if (symbolp form)
(lookup form env)
form)
(case (car form)
(quote (car (cdr form)))
(if (if (truep (%eval (car (cdr form))
env
fenv))
(%eval (car (cdr (cdr form)))
env
fenv)
(%eval (car (cdr (cdr (cdr form))))
env
fenv)))
(progn (evaluate-progn (cdr form)
env
fenv))
(setq (update! (car (cdr form))
env
(%eval (car (cdr (cdr form)))
env
fenv)))
(lambda (make-function
(car (cdr form))
(cdr (cdr form))
env
fenv))
(function
(cond ((symbolp (car (cdr form)))
(lookup (car (cdr form))
fenv))
(t (error "incorrect function ~a" (car (cdr form))))))
(otherwise (evaluate-application
(car form)
(evlis (cdr form)
env
fenv)
env
fenv)))))
(defun evaluate-application (var params env fenv)
(cond ((symbolp var)
(%apply (lookup var fenv)
params))
((and (consp var)
(eq (car var)
'lambda))
(evaluate-progn
(cdr (cdr var))
(extend env (car (cdr var)) params)
fenv))
(t (error "incorrect functional term ~s" var))))
(defun evlis (params env fenv)
(if (consp params)
(let ((arg0 (%eval (car params)
env
fenv)))
(cons arg0
(evlis (cdr params)
env
fenv)))
nil))
(defun evaluate-progn (body env fenv)
(if (consp body)
(if (consp (cdr body))
(progn (%eval (car body)
env
fenv)
(evaluate-progn (cdr body)
env
fenv))
(%eval (car body)
env
fenv))
*empty-progn*))
(defun %apply (function args)
(if (functionp function)
(funcall function args)
(error "not a function ~s" function)))
(defun make-function (parameters body env fenv)
(lambda (values)
(evaluate-progn body
(extend env
parameters
values)
fenv)))
| 1,938 | Common Lisp | .lisp | 89 | 16.52809 | 62 | 0.568022 | gregcman/lisp-in-small-pieces | 11 | 2 | 0 | GPL-3.0 | 9/19/2024, 11:26:41 AM (Europe/Amsterdam) | 8b521cebb336a2d7edae4cab26bd1d0a6475ffa6c5c1ab248eb9f20fcbd2e760 | 8,748 | [
-1
] |
8,749 | lisp-in-small-pieces.lisp | gregcman_lisp-in-small-pieces/interpreters/lisp-in-small-pieces.lisp | (in-package #:lisp)
;;;;evaluation -> dynamic, no closures
(defun %eval (form env)
(if (atom form)
(if (symbolp form)
(lookup form env)
form)
(case (car form)
(quote (car (cdr form)))
(if (if (truep (%eval (car (cdr form))
env))
(%eval (car (cdr (cdr form)))
env)
(%eval (car (cdr (cdr (cdr form))))
env)))
(progn (evaluate-progn (cdr form)
env))
(setq (update! (car (cdr form))
env
(%eval (car (cdr (cdr form)))
env)))
(lambda (make-function
(car (cdr form))
(cdr (cdr form))
env))
(otherwise (%apply (%eval (car form) env)
(evlis (cdr form) env))))))
(defun evlis (params env)
(if (consp params)
(let ((arg0 (%eval (car params) env)))
(cons arg0
(evlis (cdr params) env)))
nil))
(defun evaluate-progn (body env)
(if (consp body)
(if (consp (cdr body))
(progn (%eval (car body)
env)
(evaluate-progn (cdr body)
env))
(%eval (car body) env))
*empty-progn*))
(defun %apply (function args)
(if (functionp function)
(funcall function args)
(error "not a function ~s" function)))
(defun make-function (parameters body env)
(lambda (values)
(evaluate-progn body
(extend env
parameters
values))))
;;;;evaluation.d
;;;;closures are created with the "function" special form
(defun %eval.d (form env)
(if (atom form)
(if (symbolp form)
(lookup form env)
form)
(case (car form)
(quote (car (cdr form)))
(if (if (truep (%eval.d (car (cdr form))
env))
(%eval.d (car (cdr (cdr form)))
env)
(%eval.d (car (cdr (cdr (cdr form))))
env)))
(progn (evaluate-progn.d (cdr form)
env))
(setq (update! (car (cdr form))
env
(%eval.d (car (cdr (cdr form)))
env)))
(function
(let* ((f (car (cdr form)))
(fun (make-function.d
(car (cdr f))
(cdr (cdr f))
env)))
(d.make-closure fun env)))
(lambda (make-function.d
(car (cdr form))
(cdr (cdr form))
env))
(otherwise (%apply.d (%eval.d (car form) env)
(evlis (cdr form) env)
env)))))
(defun evlis.d (params env)
(if (consp params)
(let ((arg0 (%eval.d (car params) env)))
(cons arg0
(evlis.d (cdr params) env)))
nil))
(defun evaluate-progn.d (body env)
(if (consp body)
(if (consp (cdr body))
(progn (%eval.d (car body)
env)
(evaluate-progn.d (cdr body)
env))
(%eval.d (car body)
env))
*empty-progn*))
(defun %apply.d (function args env)
(if (functionp function)
(funcall function args env)
(error "not a function ~s" function)))
(defun make-function.d (parameters body definition-env)
#+nil
(declare (ignore definition-env))
(lambda (values current-env)
(evaluate-progn body
(extend current-env
parameters
values))))
(defun d.make-closure (fun env)
(lambda (values current-env)
#+nil
(declare (ignore current-env))
(funcall fun values env)))
;;;;evaluation.s
;;;shallow binding is an implementation of dynamic binding
;;;where the symbol has a value cell that is changed and restored
(defun setvar (var new)
(setf (get var 'apval) new))
(defun getvar (var)
(get var 'apval))
(defun for-each (fun sequence &rest sequences)
(apply #'map nil fun sequence sequences))
(defun s.make-function (variables body env)
#+nil
(declare (ignore env))
(lambda (values current-env)
(let ((old-bindings
(mapcar
(lambda (var val)
(let ((old-value (getvar var)))
(setvar var val)
(cons var old-value)))
variables
values)))
(let ((result (evaluate-progn body current-env)))
(for-each (lambda (b)
(setvar (car b)
(cdr b)))
old-bindings)
result))))
(defun s.lookup (id env)
#+nil
(declare (ignore env))
(getvar id))
(defun s.update! (id env value)
#+nil
(declare (ignore env))
(setvar id value))
;;;misc
(defun %mapcar (function list)
(if (consp list)
(cons (funcall
function
(car list))
(%mapcar
function
(cdr list)))
(quote ())))
(defparameter *global-environment*
*initial-environment*)
(defmacro definitial (name &optional (value nil value-supplied-p))
`(progn (push (cons (quote ,name)
,(if value-supplied-p
value
''void))
*global-environment*)))
(defmacro defprimitive (name value arity)
`(definitial ,name
(lambda (values)
(if (= ,arity (length values))
(apply (function ,value) values)
(error "Incorrect arity ~s"
(list (quote ,name) values))))))
(definitial t t)
(definitial f *the-false-value*)
(definitial nil '())
(definitial foo)
(definitial fib)
(definitial bar)
(definitial fact)
(progn
(defprimitive cons cons 2)
(defprimitive car car 1)
(defprimitive rplacd rplacd 2)
(defprimitive eq eq 2))
(progn
(defprimitive + + 2)
(defprimitive < < 2))
;;;type ":exit" to exit
(defun chapter1-scheme ()
(labels ((toplevel ()
(let ((value (read)))
(unless (eq :exit value)
(print (%eval value *global-environment*))
(terpri)
(toplevel)))))
(toplevel)))
;#+nil
;#+nil
(maphash
(lambda (k v)
(declare (ignore v))
(push (cons k (get k 'defun))
*global-environment*))
*function-namespace*)
(maphash
(lambda (k v)
(declare (ignore v))
(eval `(definitial ,k ',(symbol-value k))))
*variable-namespace*)
(setf (cdr (assoc '*global-environment* *global-environment*))
*global-environment*)
(cl:defun test (form &optional (eval-nest 1))
(dotimes (times eval-nest)
(setf form `(%eval (quote ,form)
*global-environment*)))
(print form)
(eval form))
;;;tests
#+nil
((lambda (a b c)
(+ c (a b)))
(lambda (x)
(+ x x))
2
3)
| 5,772 | Common Lisp | .lisp | 233 | 20.506438 | 66 | 0.614172 | gregcman/lisp-in-small-pieces | 11 | 2 | 0 | GPL-3.0 | 9/19/2024, 11:26:41 AM (Europe/Amsterdam) | f5d389644dedb200010f7d11ab4b4478746a595f3bcedf85dd3d7a3ada900361 | 8,749 | [
-1
] |
8,750 | lisp-in-small-pieces.asd | gregcman_lisp-in-small-pieces/lisp-in-small-pieces.asd | (asdf:defsystem #:lisp-in-small-pieces
:depends-on (#:utility
#:scheme2common-lisp)
:serial t
:components
((:file "package2")
(:file "other2")
(:file "tester")
(:file "chap6d")
(:file "chap7d")
#+nil
(:file "package")
#+nil
(:module "interpreters"
#+nil
(:file "lisp-in-small-pieces")
(:file "other")
(:file "lisp-in-small-pieces2"))))
| 393 | Common Lisp | .asd | 18 | 17.666667 | 38 | 0.593583 | gregcman/lisp-in-small-pieces | 11 | 2 | 0 | GPL-3.0 | 9/19/2024, 11:26:41 AM (Europe/Amsterdam) | 17dc344fb3d295a13576ecc6e94b716cc1ef4fc6eb5ec1e2e2233a07975eef8f | 8,750 | [
-1
] |
8,780 | keybindings.lisp | szos_CLFM/keybindings.lisp | (in-package :clfm-2)
(make-command-table 'clfm-control-x :errorp nil)
(add-keystroke-to-command-table 'clfm '(#\x :control) :menu 'clfm-control-x
:errorp nil)
(add-keystroke-to-command-table 'clfm-control-x '(#\c :control)
:command '(com-exit)
:errorp nil)
;; (clim:add-keystroke-to-command-table 'clfm-control-x)
;;; this doesnt work, but it should.... hmmmmmm....
| 382 | Common Lisp | .lisp | 9 | 39.666667 | 75 | 0.696477 | szos/CLFM | 12 | 2 | 1 | GPL-3.0 | 9/19/2024, 11:26:41 AM (Europe/Amsterdam) | ecf5feaae995d47604a786bfbc397363f2a7dd6887f86ed5591fed2d69df25f4 | 8,780 | [
-1
] |
8,781 | package.lisp | szos_CLFM/package.lisp | ;;;; package.lisp
(defpackage #:clfm-2
(:use #:clim #:clim-lisp #:clim-tab-layout #:clim-extensions)
(:export "APP-MAIN"))
| 128 | Common Lisp | .lisp | 4 | 29.75 | 63 | 0.658537 | szos/CLFM | 12 | 2 | 1 | GPL-3.0 | 9/19/2024, 11:26:41 AM (Europe/Amsterdam) | e0d2277e0a919b04620637fad0eefb89acc0a98e9bcb6be3b135457ad42986f2 | 8,781 | [
-1
] |
8,782 | dialog.lisp | szos_CLFM/dialog.lisp | (in-package :clfm-2)
(define-application-frame clfm-notify-user-frame ()
((message-string :initarg :message-string)
(message-body :initarg :message-body)
(exit-boxes :initarg :exit-boxes)
(title :initarg :title)
(style :initarg :style)
(text-style :initarg :text-style)
(width :initarg :width)
(return-value :initarg nil :initform :abort))
(:pane (clfm-notify-user-dialog *application-frame*)))
(defun clfm-notify-generate-exit-box-buttons (specs)
(mapcar
(lambda (spec)
(destructuring-bind (action string &rest args) spec
(spacing (:thickness 2)
(apply #'make-pane
'push-button
:label string
:text-style (make-text-style :sans-serif :roman :small) ; XXX
:activate-callback
(lambda (gadget)
(declare (ignore gadget))
;; This is fboundp business is weird, and only implied by a
;; random message on the old CLIM list. Does the user function
;; take arguments?
(when (or (typep action 'function) (fboundp action))
(funcall action))
(setf (slot-value *application-frame* 'return-value) action)
;; This doesn't work:
#+NIL
(when (eql action :abort)
(and (find-restart 'abort)
(invoke-restart 'abort)))
(frame-exit *application-frame*))
args))))
specs))
(defun clfm-notify-user-dialog (frame)
(with-slots (message-string message-body exit-boxes text-style width) frame
(vertically ()
(spacing (:thickness 6)
(make-pane 'label-pane :label (or message-string "I'm speechless.") :text-style text-style))
(spacing (:thickness 6)
(make-pane 'application-pane
:display-function
(or (and message-body (eval `(lambda (frame pane)
(declare (ignore frame))
,message-body)))
(lambda (frame pane)
(declare (ignore frame)
(ignore pane))))
:width width))
(spacing (:thickness 4)
(make-pane 'hbox-pane :contents
(cons '+fill+
(clfm-notify-generate-exit-box-buttons exit-boxes)))))))
(defun clfm-notify-user-frame-manager
(frame-manager
&key frame associated-window message-body message-string width
(title "")
documentation
(exit-boxes '((:exit "OK")))
;; The 'name' arg is in the spec but absent from the Lispworks
;; manual, and I can't imagine what it would do differently
;; than 'title'.
name
style
(text-style (make-text-style :sans-serif :roman :small)))
(declare (ignore associated-window documentation))
(let ((frame (make-application-frame 'clfm-notify-user-frame
:calling-frame frame
:pretty-name title
:message-string message-string
:message-body message-body
:frame-manager frame-manager
:exit-boxes exit-boxes
:title (or name title)
:style style
:text-style text-style
:width width)))
(run-frame-top-level frame)
(slot-value frame 'return-value)))
(defparameter *clfm-notify-pass-to-notification* nil)
(defmacro clfm-notify ((frame message (&rest args-to-pass) &rest args)
&body message-body)
`(progn
(setf *clfm-notify-pass-to-notification*
`(,,@(loop for arg in args-to-pass
collect arg)))
(apply #'clfm-notify-user-frame-manager
(if ,frame (frame-manager ,frame) (find-frame-manager))
:message-string ,message
:message-body `(let ,(loop for arg in ',args-to-pass
for v in *clfm-notify-pass-to-notification*
collect `(,arg (handler-case ,v
(t ()
',v))))
,@',message-body)
:frame ,frame
',args)))
| 3,886 | Common Lisp | .lisp | 100 | 29.92 | 93 | 0.601113 | szos/CLFM | 12 | 2 | 1 | GPL-3.0 | 9/19/2024, 11:26:41 AM (Europe/Amsterdam) | b2a71b8fe025fd81732e365ba67bddc12be24572e56952eda809137ae3513388 | 8,782 | [
-1
] |
8,783 | chdir.lisp | szos_CLFM/chdir.lisp |
(in-package :clfm-2)
(defparameter *passwd-test* nil)
(define-clfm-command (com-change-directory) ((directory string))
(handler-case
(when (uiop:directory-exists-p directory)
(uiop:chdir directory))
(sb-posix:syscall-error (err)
(case
(notify-user *application-frame*
(let ((errno (sb-posix:syscall-errno err)))
(cond ((= errno 13)
"You don't have permission to access this directory
To access this directory CLFM needs to be running as root
To relaunch CLFM please ensure it is in your $PATH")
(t
(format nil "SB-POSIX Error Code: ~a" errno))))
:title "CLFM Error"
:name "CLFM Error"
:documentation "CLFM Error"
:text-style (make-text-style "ETBembo" "BoldLF" 18)
:exit-boxes '((t "OK")
(:sudo "Relaunch as root")))
((:sudo)
(and (notify-user *application-frame*
"WARNING: CLFM will now be running with root privilidges.
This is not safe, and should be avoided.
If you continue, type your password when presented with the blank window and use
M-RET (meta + return or Alt + return) to submit it"
:exit-boxes '((t "CONTINUE") (nil "ABORT"))
:text-style (make-text-style "ETBembo" "BoldLF" 18))
(let (passwd
(stream (frame-standard-input *application-frame*)))
(accepting-values (stream
:own-window t
:initially-select-query-identifier 'password)
(formatting-table (stream)
(formatting-column (stream)
(formatting-cell (stream)
(setf passwd (accept 'password
;; 'string
:prompt "sudo password"
:stream stream
:query-identifier 'password))))))
(uiop:launch-program
(format nil "echo \"~a\" | sudo -S clfm" passwd)))))))))
(define-clfm-command (com-chdir-~/ :name "Go Home") ()
(com-change-directory-temper "~/"))
| 1,863 | Common Lisp | .lisp | 47 | 33.255319 | 81 | 0.644162 | szos/CLFM | 12 | 2 | 1 | GPL-3.0 | 9/19/2024, 11:26:41 AM (Europe/Amsterdam) | b07186fbeddebe24961fc76e9148b5cfdf1ac26ec01a238be54d7f3b50245a45 | 8,783 | [
-1
] |
8,784 | open-files.lisp | szos_CLFM/open-files.lisp |
(in-package :clfm-2)
(defun open-file (file)
"Opens the provided file with the default application a la xdg-open"
(check-type file string)
(uiop:launch-program (format nil "xdg-open ~a" (escape-spaces file))))
(define-clfm-command (com-open-file) ((file string))
"This command opens a file after checking that it exists"
(when (uiop:file-exists-p (escape-spaces file))
(open-file file)))
(define-clfm-command (com-open-file-with) ((file string))
"Takes a file and prompts for a program to open it with"
(let (program
(stream (frame-standard-input *application-frame*))
;; (x (framex ))
)
(restart-case
(progn
(accepting-values (stream :own-window t
:label "Enter Program"
:initially-select-query-identifier 'program
:resynchronize-every-pass t
;; :resize-frame t
:height 150
:width 500)
(formatting-table (stream)
(formatting-column (stream)
(formatting-cell (stream)
(setf program (accept 'string
:prompt "Program"
:stream stream
:query-identifier 'program))))))
(and program (uiop:launch-program
(format nil "~a ~a" program (escape-spaces file)))))
(abort () ()))))
| 1,194 | Common Lisp | .lisp | 34 | 30.411765 | 72 | 0.67474 | szos/CLFM | 12 | 2 | 1 | GPL-3.0 | 9/19/2024, 11:26:41 AM (Europe/Amsterdam) | 8008087f2c24d996411043fb209edf7106ac8c4073e96f14b191cd652b62e625 | 8,784 | [
-1
] |
8,785 | command-utilities.lisp | szos_CLFM/command-utilities.lisp |
(in-package :clfm-2)
(defmacro accept-with-table ((stream) &body accept-statements)
"this simple macro wraps every accept statement in a formatting table, column,
and cell. "
`(formatting-table (,stream)
,@(loop for item in accept-statements
collect `(formatting-column (,stream)
(formatting-cell (,stream)
,item)))))
(defmacro make-popup ((stream label) &body body)
`(restart-case
(let ((stream ,stream))
(accepting-values (stream :own-window t
:label ,label)
,@body
(terpri stream)
(terpri stream)
(accept 'string
:prompt "DUMMY INPUT DO NOT USE"
:stream stream)))
(abort () ())))
(defun file-or-directory-exists-p (item)
(or (uiop:file-exists-p item)
(uiop:directory-exists-p)))
(defun escape-spaces (string)
(coerce (loop for char in (coerce string 'list)
if (char= #\space char)
collect #\\
and collect #\space
else collect char)
'string))
(defun test/escape-spaces (string)
(coerce (loop for char in (coerce string 'list)
if (char= #\space char)
collect #\\
and collect #\space
else collect char)
'string))
;; (nlet churner ((charlist '(#\\ #\space #\\ ))
;; (counter 1)) ; count down
;; (cond ((not (char= #\\ (car charlist)))
;; counter)
;; (t
;; (if (= counter 0)
;; (churner (cdr charlist) 1)
;; (churner (cdr charlist) 0)))))
;; (defun spaces-escaped (string)
;; (nlet spaceloop ((s (coerce string 'list))
;; (previous-chars '()))
;; (if (char= (car s) #\space)
;; (if (and (char= (car previous-chars) #\\)
;; (char= (cadr previous-chars #\\)))
;; ))
;; (cond ((char= (car s) #\\)
;; ))))
(defun string-contains (string character)
(let ((char (if (stringp character) (coerce character 'character) character)))
(loop for c across string
when (char= c char)
do (return-from string-contains t))))
(define-clfm-command (com-run-shell-command :name "Run Shell Command")
((command string :prompt "/bin/sh -c"))
(uiop:launch-program (format nil "/bin/sh -c ~a" command)))
(define-gesture-name :exit-clfm :keyboard (#\q :control :meta))
(define-clfm-command (com-quit :name "Quit" :keystroke :exit-clfm) ()
(frame-exit *application-frame*))
(define-clfm-command (com-exit :name "Exit") ()
(frame-exit *application-frame*))
(define-clfm-command (com-display-help-window) ()
(let ((frame (make-application-frame 'clfm-help)))
(run-frame-top-level frame)))
(define-clfm-command (com-loadrc :name "Load Init File") ()
(handler-case (load "~/.clfm.d/init.lisp")
(t () ())))
(define-clfm-command (com-show-marks) ()
(setf (frame-current-layout *application-frame*) 'default))
(define-clfm-command (com-hide-marks) ()
(setf (frame-current-layout *application-frame*) 'no-marks))
(define-clfm-command (com-toggle-hidden-files) ()
(setf *hide-files* (not *hide-files*))
(redisplay-frame-panes *application-frame* :force-p t))
| 2,958 | Common Lisp | .lisp | 81 | 33.271605 | 80 | 0.647552 | szos/CLFM | 12 | 2 | 1 | GPL-3.0 | 9/19/2024, 11:26:41 AM (Europe/Amsterdam) | b591b7937925673a33c93193d938f743d4a6b6466d9350e31c7c63306dd9da0f | 8,785 | [
-1
] |
8,786 | create-file.lisp | szos_CLFM/create-file.lisp |
(in-package :clfm-2)
(defparameter *touch-file* nil)
(define-clfm-command (com-touch) ((filename string))
(let ((file (if (string-contains filename "/")
filename
(concatenate 'string (namestring (uiop:getcwd)) filename))))
(open file :direction :probe :if-does-not-exist :create)))
(define-clfm-command (com-new-file) ()
(restart-case
(let (file
(stream (frame-standard-input *application-frame*)))
(accepting-values (stream :own-window t
:label "Enter a new file name"
:initially-select-query-identifier 'fileprompt)
(formatting-table (stream)
(formatting-column (stream)
(formatting-cell (stream)
(setf file (accept 'string
:prompt "Filename"
:stream stream
:query-identifier 'fileprompt))))))
(and file (com-touch file)))
(abort () ())))
| 829 | Common Lisp | .lisp | 23 | 31.217391 | 64 | 0.67207 | szos/CLFM | 12 | 2 | 1 | GPL-3.0 | 9/19/2024, 11:26:41 AM (Europe/Amsterdam) | 6d9cb0cd7173a73d7d172032dfa15c54c06cb26b40dbab4327b5ea0b56ba56e2 | 8,786 | [
-1
] |
8,787 | help.lisp | szos_CLFM/help.lisp |
(in-package :clfm-2)
(define-application-frame clfm-help () ()
(:menu-bar clfm-help-window-menu-bar)
(:panes
(keybindings :application
:display-function #'help-display-keybindings
:width 600)
(generic :application
:display-function #'help-display-generic
:width 600))
(:layouts
(default
generic)
(keybindings keybindings)))
(define-clfm-help-command (com-switch-to-generic-page) ()
(setf (frame-current-layout *application-frame*) 'default))
(define-clfm-help-command (com-switch-to-keybindings-page) ()
(setf (frame-current-layout *application-frame*) 'keybindings))
;; (define-clfm-help-command (com-switch-to-opening-files) ()
;; )
(make-command-table 'clfm-help-window-menu-bar
:errorp nil
:menu
'(("General" :command com-switch-to-generic-page)
("Keybindings" :command com-switch-to-keybindings-page)))
(defun help-display-keybindings (frame pane)
(declare (ignore frame))
(slim:with-table (pane)
(slim:row (slim:cell (format pane "Keybindings: ")))
(slim:row (slim:cell (format pane " ")))
(slim:row
(slim:cell (format pane "M-RET"))
(slim:cell (format pane "Submit Input")))))
(defun help-display-generic (frame pane)
(declare (ignore frame))
(with-end-of-line-action (pane :wrap*)
(with-etbembo (pane :bold)
(format pane "This is the help page for CLFM. It should contain documentation on the usage and settings of CLFM"))
(terpri pane)
(terpri pane)
(with-etbembo (pane)
(format pane "CLFM stands for ")
(with-etbembo (pane :italic)
(format pane "Common Lisp File Manager"))
(format pane ". It is primarily mouse driven, though it is planned to implement keybindings (and an init file where users can change them) to drive this application via the keyboard. "))))
| 1,822 | Common Lisp | .lisp | 45 | 36.222222 | 194 | 0.697569 | szos/CLFM | 12 | 2 | 1 | GPL-3.0 | 9/19/2024, 11:26:41 AM (Europe/Amsterdam) | 8774bcb29dc4a1fdbc77c8c385ab78deb298ee98d0d17386fc32ae07542a2c77 | 8,787 | [
-1
] |
8,788 | delete.lisp | szos_CLFM/delete.lisp |
(in-package :clfm-2)
(defparameter *directory-deletion-validation-function* 'validate-directory)
(defun validate-directory (directory)
(uiop:directory-exists-p directory))
(defun %delete-directory (dir)
(uiop:delete-directory-tree dir
:validate *directory-deletion-validation-function*))
(defun delete-directory (directory)
(let ((reter (clfm-notify (*application-frame* "IRREVERSIBLE OPERATION" nil
:exit-boxes ((t "PROCEED")
(nil "ABORT"))
:width 400)
(with-end-of-line-action (pane :wrap*)
(with-etbembo (pane nil 18)
(format pane "You are attempting to delete a file or directory. This operation is ")
(with-etbembo (pane :bold 18)
(format pane "IRREVERSIBLE"))
(format pane ". Proceed with caution."))))))
(when reter
(%delete-directory (pathname directory)))))
(defun delete-item (item)
(let ((reter (clfm-notify (*application-frame* "IRREVERSIBLE OPERATION" nil
:exit-boxes ((t "PROCEED")
(nil "ABORT"))
:width 400)
(with-end-of-line-action (pane :wrap*)
(with-etbembo (pane nil 18)
(format pane "You are attempting to delete a file or directory. This operation is ")
(with-etbembo (pane :bold 18)
(format pane "IRREVERSIBLE"))
(format pane ". Proceed with caution."))))))
(when reter
(cond ((uiop:directory-exists-p item)
(%delete-directory (pathname item)))
((uiop:file-exists-p item)
(delete-file item))))))
(define-clfm-command (com-delete) ((item string))
(cond ((uiop:directory-exists-p item)
(delete-directory item))
((uiop:file-exists-p item)
(delete-item item))))
| 1,678 | Common Lisp | .lisp | 41 | 35.097561 | 91 | 0.662577 | szos/CLFM | 12 | 2 | 1 | GPL-3.0 | 9/19/2024, 11:26:41 AM (Europe/Amsterdam) | 563fb68d609515d034c85ebe9760c197f237587a69b966efa2b2c9029768ef77 | 8,788 | [
-1
] |
8,789 | copy.lisp | szos_CLFM/copy.lisp |
(in-package :clfm-2)
(defun %copy-file (f1 f2)
(with-open-file (infile f1 :direction :input
:element-type '(unsigned-byte 8))
(with-open-file (outfile f2 :direction :output
:element-type '(unsigned-byte 8)
:if-exists :error)
(when infile
(loop for byte = (read-byte infile nil)
while byte
do (write-byte byte outfile))))))
(defun copy-file (&optional file new-file)
(restart-case
(let ((stream (frame-standard-input *application-frame*))
oldname newname)
(accepting-values (stream :own-window t
:label (or (and
file
(format nil "Copy ~a" file))
"Copy a file")
:initially-select-query-identifier
(or (and file 'newfile) 'file))
(accept-with-table (stream)
(or (and file (setf oldname file))
(setf oldname (accept 'string
:prompt "Filename"
:stream stream
:query-identifier 'file)))
(or (and new-file (setf newname new-file))
(setf newname (accept 'string
:prompt "Copy to"
:stream stream
:query-identifier 'newfile)))))
(unless (member #\/ (coerce newname 'list))
(setf newname (concatenate 'string
(namestring (uiop:getcwd))
newname)))
(and oldname newname (%copy-file oldname newname)))
(abort () ())))
(define-clfm-command (com-copy-file) ((file string) (copy-to string))
(copy-file file copy-to))
(define-clfm-command (com-copy-file-prompt) ((file string))
(copy-file file))
(defun %copy-directory-contents (from to)
"copy the contents of from to to. This is a helper"
(ensure-directories-exist to)
(labels
((copier (tdir fdir list)
(print fdir)
(print list)
(cond ((uiop:directory-exists-p (car list))
(let ((dirname
(coerce
(reverse
(loop for c in (cdr (coerce (reverse
(namestring (car list)))
'list))
while (not (char= c #\/))
collect c))
'string)))
(ensure-directories-exist (concatenate 'string
(namestring tdir)
dirname "/"))
(copier (concatenate 'string (namestring tdir) dirname "/")
(concatenate 'string (namestring fdir) dirname "/")
(append (uiop:subdirectories
(pathname
(concatenate 'string (namestring fdir)
dirname "/")))
(uiop:directory-files
(pathname
(concatenate 'string (namestring fdir)
dirname "/")))))
(copier tdir fdir (cdr list))))
((uiop:file-exists-p (car list))
(let ((cto
(coerce
(reverse
(loop for c in (coerce (reverse (namestring (car list)))
'list)
while (not (char= c #\/))
collect c))
'string)))
(%copy-file (car list) (concatenate 'string (namestring tdir)
cto))
(copier tdir fdir (cdr list)))))))
(copier to from (append (uiop:subdirectories (pathname from))
(uiop:directory-files (pathname from))))))
(defun copy-directory (old-dir new-dir)
(let (ndir
(n (cdr (reverse (coerce (namestring new-dir) 'list)))))
(setf ndir
(if (member #\/ n :test #'char=)
;; TODO:
;; if new-dir is an absolute path... no thats not what this does.
;; this checks if theres any other directories in the path...
;; so it would fail if we were in ~/docs/ and copying from
;; ~/docs/lisp to scheme/docs/new. This is not the behavior we
;; want, this needs to be reworked to allow relative paths with
;; subdirectories
new-dir
(concatenate 'string (namestring (uiop:getcwd))
(namestring new-dir))))
(ensure-directories-exist ndir)
(%copy-directory-contents old-dir ndir)))
(defun test/copy-directory (&optional old new)
(restart-case
(let ((stream (frame-standard-input *application-frame*))
oldname newname)
(accepting-values (stream :own-window t
:label (or (and
old
(format nil "Copy ~a" old))
"Copy a directory")
:initially-select-query-identifier
(or (and old 'newname) 'directory))
(accept-with-table (stream)
(or (and old (setf oldname old))
(setf oldname (accept 'string
:prompt "Directory"
:stream stream
:query-identifier 'directory)))
(or (and new (setf newname new))
(setf newname (accept 'string
:prompt "Copy to"
:stream stream
:query-identifier 'newname)))))
(ensure-directories-exist newname)
(%copy-directory-contents oldname newname))
(abort () ())))
(define-clfm-command (com-copy-directory) ((file string) (copy-to string))
(test/copy-directory file copy-to))
(define-clfm-command (com-copy-directory-prompt) ((file string))
(test/copy-directory file))
| 4,688 | Common Lisp | .lisp | 134 | 28.776119 | 74 | 0.629817 | szos/CLFM | 12 | 2 | 1 | GPL-3.0 | 9/19/2024, 11:26:41 AM (Europe/Amsterdam) | 0781077347a04b8c369c9437f345062ad40554ed8d649bed3c5dd36c50f05911 | 8,789 | [
-1
] |
8,790 | create-directory.lisp | szos_CLFM/create-directory.lisp |
(in-package :clfm-2)
(defun %new-directory (&optional redo)
(restart-case
(let (dir
(stream (frame-standard-input *application-frame*)))
(accepting-values (stream :own-window t
:label (or (and redo "That didnt appear to be a directory, please try again, and ensure theres a trailing “/”. ")
"Enter a new directory name")
:initially-select-query-identifier 'dirprompt)
(formatting-table (stream)
(formatting-column (stream)
(formatting-cell (stream)
(setf dir (accept 'string
:prompt "Directory"
:stream stream
:query-identifier 'dirprompt))))))
(and dir (com-make-directory dir)))
(abort () ())))
(define-clfm-command (com-new-dir) ()
(%new-directory))
(define-clfm-command (com-make-directory) ((dir 'string))
(let ((fd (coerce dir 'list))
(rd (reverse (coerce dir 'list))))
(if (char= #\/ (car rd))
(if (or (char= (car fd) #\/)
(char= (car fd) #\~))
(ensure-directories-exist dir)
(ensure-directories-exist
(concatenate 'string (namestring (uiop:getcwd)) dir)))
(%new-directory t))))
| 1,099 | Common Lisp | .lisp | 30 | 31.866667 | 119 | 0.649387 | szos/CLFM | 12 | 2 | 1 | GPL-3.0 | 9/19/2024, 11:26:41 AM (Europe/Amsterdam) | db23b40bfb00d13f6246da90fae7ada82c37b335e629ced669b07b055b16c657 | 8,790 | [
-1
] |
8,791 | commands-update.lisp | szos_CLFM/commands-update.lisp | (in-package :clfm-2)
(defparameter *override-characters-as-string*
'(("RET" . :return)
("SPC" . #\space)
("TAB" . #\tab)
("æ" . #\latin_small_letter_ae)
("ø" . #\LATIN_SMALL_LETTER_O_WITH_STROKE)
("å" . #\LATIN_SMALL_LETTER_A_WITH_RING_ABOVE)
("ł" . #\LATIN_SMALL_LETTER_L_WITH_STROKE)))
(defun key-chord (chord-def)
(let* ((elements (reverse (cl-ppcre:split "-" chord-def)))
(char (pop elements)))
(setf char
(if (assoc char *override-characters-as-string* :test #'string-equal)
(cdr (assoc char *override-characters-as-string*
:test #'string-equal))
(coerce char 'character)))
(cons char (remove-if #'not (loop for s in elements
collect (cond ((string= s "S")
(setf char (char-upcase char))
nil)
((string= s "C") :control)
((string= s "M") :Meta)
((string= s "s") :super)
((string= s "H") :Hyper)))))))
(defun kbd (key-seq)
"This function parses a string into a list of gestures. The string is split by
spaces, and every one of these is parsed into a single gesture. the final character
is the character, all others are modifiers, and hyphens are ignored. Modifier
translation is as follows: C - :control, M - :meta, H - :hyper, s - :super.
a capital S in the modifier position changes the character to a capital. "
(let* ((str-seq (cl-ppcre:split " " key-seq))
(seq (loop for str in str-seq
collect
(let* ((charbag (reverse (coerce str 'list)))
(char (pop charbag))
(mods (loop for c in charbag
unless (char= c #\-)
collect (cond ((char= c #\S)
(setf
char
(char-upcase char))
nil)
((char= c #\C) :control)
((char= c #\M) :Meta)
((char= c #\s) :super)
((char= c #\H) :Hyper)))))
(cons char (remove-if #'not mods))))))
seq))
(defun display-info (frame pane)
(declare (ignore frame))
(labels ((looper (input &optional ac)
(if input
(progn
(with-output-as-presentation
(pane (format nil "~{~a~^/~}/"
(append ac (list (car input))))
'chdir-presentation)
(format pane "~a/" (car input)))
(looper (cdr input) (append ac (list (car input)))))
(unless ac
(with-output-as-presentation (pane "/"
'chdir-presentation)
(format pane "/"))))))
(with-etbembo (pane :bold)
(format pane "Current Directory: "))
(with-etbembo (pane :italic-bold)
(looper (cl-ppcre:split "/" (namestring (uiop:getcwd))))
(with-etbembo (pane)
(format pane "[~a]"
(car (reverse (cl-ppcre:split "/" (namestring (current-item))))))))))
(define-clfm-command (com-change-directory-temper) ((directory string))
(handler-case
(when (uiop:directory-exists-p directory)
(change-directory directory))
(sb-posix:syscall-error (err)
(case
(notify-user *application-frame*
(let ((errno (sb-posix:syscall-errno err)))
(cond ((= errno 13)
"You don't have permission to access this directory
To access this directory CLFM needs to be running as root
To relaunch CLFM please ensure it is in your $PATH")
(t
(format nil "SB-POSIX Error Code: ~a" errno))))
:title "CLFM Error"
:name "CLFM Error"
:documentation "CLFM Error"
:text-style (make-text-style "ETBembo" "BoldLF" 18)
:exit-boxes '((t "OK")
(:sudo "Relaunch as root")))
((:sudo)
(and (notify-user *application-frame*
"WARNING: CLFM will now be running with root privilidges.
This is not safe, and should be avoided.
If you continue, type your password when presented with the blank window and use
M-RET (meta + return or Alt + return) to submit it"
:exit-boxes '((t "CONTINUE") (nil "ABORT"))
:text-style (make-text-style "ETBembo" "BoldLF" 18))
(let (passwd
(stream (frame-standard-input *application-frame*)))
(accepting-values (stream
:own-window t
:initially-select-query-identifier 'password)
(formatting-table (stream)
(formatting-column (stream)
(formatting-cell (stream)
(setf passwd (accept 'password
;; 'string
:prompt "sudo password"
:stream stream
:query-identifier 'password))))))
(uiop:launch-program
(format nil "echo \"~a\" | sudo -S clfm" passwd)))))))))
(define-clfm-command (com-add-mark) ((markl t))
(let ((mark (if (listp markl)
(namestring (car markl))
markl)))
(if (member mark *marks* :test #'string=)
(setf *marks* (remove mark *marks* :test #'string=))
(setf *marks* (cons mark *marks*)))))
(define-clfm-command (com-delete) ((thing t))
(let ((item (if (listp thing)
(namestring (car thing))
thing)))
(cond ((uiop:directory-exists-p item)
(delete-directory item))
((uiop:file-exists-p item)
(delete-item item)))))
(define-clfm-command (com-open) ((thing t))
(let ((item (if (listp thing)
(namestring (car thing))
thing)))
(cond ((uiop:directory-exists-p item)
(com-change-directory-temper item))
((uiop:file-exists-p item)
(open-file (namestring item))))))
(define-clfm-command (com-open-with) ((thing t))
(let (program
(item (if (listp thing)
(namestring (car thing))
thing)))
(cond ((uiop:file-exists-p item)
(restart-case
(progn
(accepting-values (stream :own-window t
:label "Enter Program"
:initially-select-query-identifier
'program
:resynchronize-every-pass t
;; :resize-frame t
:height 150
:width 500)
(formatting-table (stream)
(formatting-column (stream)
(formatting-cell (stream)
(setf program (accept 'string
:prompt "Program"
:stream stream
:query-identifier 'program))))))
(and program (uiop:launch-program
(format nil "~a ~a" program (escape-spaces item)))))
(abort () ()))) )))
(define-gesture-name :meta-click-left :pointer-button (:left :meta))
(define-gesture-name :meta-control :keyboard (:control :meta))
(define-gesture-name :prev-item :keyboard (#\p :control))
(define-gesture-name :mark-item :keyboard (#\m :control))
(define-presentation-type filesystem-presentation ())
(defun set-operation-order (&key (delete -1)
(open 4)
(open-with 3)
(rename 2)
(copy 1))
(define-presentation-to-command-translator up-selection
(filesystem-presentation com-move-up clfm
:gesture :control-p
:documentation "Up one selection")
(item)
(list item))
(define-presentation-to-command-translator down-selection
(filesystem-presentation com-move-down clfm
:gesture :control-n
:documentation "Down one selection")
(item)
(list item))
(define-presentation-to-command-translator filesystem-mark
(filesystem-presentation com-add-mark clfm
:gesture :meta-click-left
:documentation "Mark")
(item)
(list item))
(define-presentation-to-command-translator filesystem-delete
(filesystem-presentation com-delete clfm
:gesture :select
:documentation "Delete (PERMANENT)"
:priority delete)
(item)
(list item))
(define-presentation-to-command-translator filesystem-open
(filesystem-presentation com-open clfm
:gesture :select
:documentation "Open"
:priority open)
(item)
(list item))
(define-presentation-to-command-translator filesystem-open-with
(filesystem-presentation com-open-with clfm
:gesture :select
:documentation "Open With"
:priority open-with
:tester ((item) (uiop:file-exists-p (if (listp item)
(namestring (car item))
item))))
(item)
(list item))
(define-presentation-to-command-translator filesystem-rename
(filesystem-presentation com-rename clfm
:gesture :select
:documentation "Rename"
:priority rename)
(item)
(list item))
(define-presentation-to-command-translator filesystem-copy
(filesystem-presentation com-copy clfm
:gesture :select
:documentation "Copy"
:priority copy)
(item)
(list item)))
(set-operation-order)
(defun pane-scrolled-to-bottom-p (pane)
(multiple-value-bind (x y) (transform-position (sheet-transformation pane)
0 0)
(declare (ignore x))
(with-bounding-rectangle* (x1 y1 x2 y2) pane
(declare (ignore x1 y1 x2))
(with-bounding-rectangle* (ax1 ay1 ax2 ay2) (sheet-parent pane)
(declare (ignore ax1 ay1 ax2))
(<= (+ y y2) ay2)))))
(defun scroll-pane-to-bottom (pane)
(scroll-extent pane 0 (max 0 (- (bounding-rectangle-height pane)
(bounding-rectangle-height
(sheet-parent pane))))))
(defmacro with-pane-kept-scrolled-to-bottom ((pane-form) &body body)
"Ensure that the pane in PANE-FORM has the same scroll state
after BODY terminates as it had before:
If the pane is scrolled to some position before the end, it is
kept there. If the pane is at the bottom of the pane, the
viewport is reset to the then-current bottom after BODY is
finished."
(let ((pane (gensym))
(bottom-p (gensym)))
`(let* ((,pane ,pane-form)
(,bottom-p (pane-scrolled-to-bottom-p ,pane)))
(multiple-value-prog1 (progn ,@body)
(when ,bottom-p (scroll-pane-to-bottom ,pane))))))
(let ((pre-selection-contents nil)
(selected-item
(let ((all (append (sort (uiop:subdirectories (uiop:getcwd)) #'path<)
(sort (uiop:directory-files (uiop:getcwd)) #'path<))))
(if *hide-files*
(car (remove-if #'hidden-pathname-p all))
(car all))))
(post-selection-contents
(let ((all (append (sort (uiop:subdirectories (uiop:getcwd)) #'path<)
(sort (uiop:directory-files (uiop:getcwd)) #'path<))))
(if *hide-files*
(cdr (remove-if #'hidden-pathname-p all))
(cdr all)))))
(defun current-item () selected-item)
(defun add-current-selected-to-marks ()
(com-add-mark (namestring selected-item)))
(defun enter-or-open-current-selection ()
(com-open selected-item))
(defun change-directory (directory)
(uiop:chdir directory)
(let ((contents
(append (sort (uiop:subdirectories (uiop:getcwd)) #'path<)
(sort (uiop:directory-files (uiop:getcwd)) #'path<))))
(setf pre-selection-contents nil
selected-item (if *hide-files*
(car (remove-if #'hidden-pathname-p contents))
(car contents))
post-selection-contents (if *hide-files*
(cdr (remove-if #'hidden-pathname-p
contents))
(cdr contents))))
(redisplay-frame-panes *application-frame* :force-p t))
(defun move-selection-up ()
(when pre-selection-contents
(setf post-selection-contents (cons selected-item post-selection-contents))
(setf selected-item (pop pre-selection-contents))))
(defun move-selection-down ()
(when post-selection-contents
(setf pre-selection-contents (cons selected-item pre-selection-contents))
(setf selected-item (pop post-selection-contents))
;; (scroll-extent ())
))
(defun display-current-directory (frame pane)
(declare (ignore frame))
(display-current-directory-worker pane pre-selection-contents selected-item post-selection-contents)))
(defun display-current-directory-worker (pane pre-selection-contents selected-item post-selection-contents)
(slim:with-table (pane)
(labels ((display-item (path &optional current)
(let ((stat (handler-case (osicat-posix:stat path) (t () nil))))
(with-output-as-presentation (pane
(namestring path)
'filesystem-presentation
:single-box t)
(slim:row
(slim:cell
(if (member (namestring path)
*marks* :test #'string-equal)
(with-drawing-options (pane :ink +red+)
(format pane "*"))
(with-drawing-options (pane :ink +black+)
(format pane " "))))
(with-drawing-options
(pane :ink (if (member (namestring path) *marks*
:test #'string=)
(if current +purple+ +orange-red+)
(if current +purple+ +black+)))
(with-text-style
(pane (make-text-style
"ETBembo"
(cond ((member (namestring path) *marks*
:test #'string=)
"DisplayItalicBold")
;; (current "SuperBoldOSF")
(t "RomanLF"))
16))
(slim:cell
(if (uiop:directory-exists-p path)
(with-drawing-options
(pane :ink (if (member (namestring path)
*marks*
:test #'string=)
+orange-red+ +blue+))
(format pane "DIR"))
(with-drawing-options
(pane :ink (if (member (namestring path)
*marks*
:test #'string=)
+orange-red+ +green4+))
(format pane "FILE"))))
(slim:cell (format pane "~a" (file/directory-name
(namestring path))))
(slim:cell
(format pane "~a"
(cdr
(assoc
(format nil "~a"
(and stat
(osicat-posix:stat-uid stat)))
*uid-username* :test #'string-equal))))
)
(slim:cell
(format pane "~a"
(and stat (permissions-as-string
path))))
;; (let ((strs (permissions-as-list path)))
;; (loop for s in strs
;; do (slim:cell (format pane s))))
))))
))
(when pre-selection-contents
(loop for path in (reverse pre-selection-contents)
unless (and *hide-files* (hidden-pathname-p path))
do (display-item path)))
(when selected-item
(display-item selected-item t))
(when post-selection-contents
(loop for path in post-selection-contents
unless (and *hide-files* (hidden-pathname-p path))
do (display-item path)))
;; (when (< (length post-selection-contents) (length pre-selection-contents))
;; (scroll-pane-to-bottom pane))
)))
(define-gesture-name :next-item :keyboard (#\n :control))
(define-gesture-name :prev-item :keyboard (#\p :control))
(define-gesture-name :mark-item :keyboard (#\space :control))
(define-gesture-name :up-directory :keyboard (#\p :meta))
;; (define-gesture-name :open :keyboard (#\o :control))
(define-gesture-name :open :keyboard (:return :control))
(define-gesture-name :test :keyboard (#\t :control :meta)
;; (:return :control)
)
;; broken (define-gesture-name :carriage-return :keyboard (
))
(define-gesture-name :carriage-return :keyboard (#\m :control))
(define-clfm-command (com-move-up :name "Move Up Selection"
:keystroke :prev-item) ()
(move-selection-up))
(define-clfm-command (com-move-down :name "Move Down Selection"
:keystroke :next-item) ()
(move-selection-down))
(define-clfm-command (com-keyboard-mark :keystroke :mark-item) ()
(add-current-selected-to-marks))
(define-clfm-command (com-up-directory :keystroke :up-directory) ()
(change-directory (concatenate 'string (namestring (uiop:getcwd)) "../")))
(define-clfm-command (com-keyboard-open :keystroke :open) ()
(enter-or-open-current-selection))
;; (define-clfm-command (com-test-test-keyboard :keystroke ;; (#\k :control)
;; (:test-cc :test-mc)) ()
;; (enter-or-open-current-selection))
(define-clfm-command (com-keyboard-open :keystroke (#\i :shift :super)) ()
(enter-or-open-current-selection))
;; (add-input-editor-command
;; )
(define-gesture-name :test-cc :keyboard (#\c :control))
(define-gesture-name :test-mc :keyboard (#\c :meta))
;; ok this is broken... somehow... IDK why...
(defmacro define-main-keybinding (gesture &body body)
"Takes a gesture by name or by specification, and defines a command which runs
body. If gesture is a string, it MUST be a single key chord. "
(let ((command (intern (symbol-name (gensym "COM-ANONYMOUS-KEYBINDING-"))))
(key-seq (if (stringp gesture)
(key-chord gesture)
gesture)))
`(define-clfm-command (,command :keystroke ,key-seq) ()
,@body)))
;; (define-main-keybinding "C-ł"
;; ;; "M- " ;; (#\j :meta)
;; (add-current-selected-to-marks))
;; (define-main-keybinding (#\latin_small_letter_ae :control)
;; (add-current-selected-to-marks))
;; (define-main-keybinding (:return :control)
;; (enter-or-open-current-selection))
| 18,119 | Common Lisp | .lisp | 427 | 32.103044 | 107 | 0.571891 | szos/CLFM | 12 | 2 | 1 | GPL-3.0 | 9/19/2024, 11:26:41 AM (Europe/Amsterdam) | 29a6ae5760eebb1e6cb1da5cd1532e9ad0abac033708a653731854c13416e694 | 8,791 | [
-1
] |
8,792 | mark-and-operate.lisp | szos_CLFM/mark-and-operate.lisp |
(in-package :clfm-2)
(defparameter *marks* nil)
(define-clfm-command (com-add-mark) ((mark string))
(if (member mark *marks* :test #'string=)
(setf *marks* (remove mark *marks* :test #'string=))
(setf *marks* (cons mark *marks*))))
(define-clfm-command (com-add-mark) ((markl t))
(let ((mark (if (listp markl)
(namestring (car markl))
markl)))
(if (member mark *marks* :test #'string=)
(setf *marks* (remove mark *marks* :test #'string=))
(setf *marks* (cons mark *marks*)))))
(defun operate-on-marks (operation)
(let* ((op (symbol-name operation))
(function
(if (assoc operation *mark-operation-commands*)
(cdr (assoc operation *mark-operation-commands*))
(let ((reter
(clfm-notify (*application-frame* "Unknown Operation"
(op)
:exit-boxes ((t "Retry")
(nil "OK"))
:width 300
:text-style
("ETBembo" "BoldLF" 18))
(with-end-of-line-action (pane :wrap*)
(with-etbembo (pane nil 18)
(format pane "The operation \"~a\" is unknown"
op))))))
(when reter
(com-operate-on-marks))))))
(when function
(loop for thing in *marks*
do (handler-case (funcall function thing nil)
(t (err)
(let ((ret (notify-user *application-frame*
(format nil "Error '~a'~%was encountered while running~%function '~a'~%with argument '~a'" err function thing)
:exit-boxes
'((nil "Continue")
(:destroy-marks "Abort")
(t
"Abort Preserving Marks"))
:title "CLFM Mark Error")))
(when ret
(when (eq :destroy-marks ret)
(setf *marks* nil))
(return-from operate-on-marks))))))
(funcall function nil t)
(setf *marks* nil))))
(define-clfm-command (com-operate-on-marks) ()
;; ((function 'symbol :prompt "Function"))
(let (fun
(stream (frame-standard-input *application-frame*)))
(accepting-values (stream
:own-window t
:initially-select-query-identifier 'function)
(formatting-table (stream)
(formatting-column (stream)
(formatting-cell (stream)
(setf fun (accept 'symbol
:prompt "Operate Using Function"
:stream stream
:query-identifier 'function))))))
(operate-on-marks fun)
;; (if (assoc fun *mark-operation-commands*)
;; (operate-on-marks (cdr (assoc fun *mark-operation-commands*)))
;; (let ((reter
;; (clfm-notify (*application-frame* "Unknown Operation" (fun)
;; :exit-boxes ((t "Retry")
;; (nil "OK"))
;; :width 300
;; :text-style
;; ("ETBembo" "BoldLF" 18))
;; (with-end-of-line-action (pane :wrap*)
;; (with-etbembo (pane nil 18)
;; (format pane "The operation \"~a\" is unknown" fun))))))
;; (when reter
;; (com-operate-on-marks))))
))
;;; IDEA:
;;; Make a «defcommand» type thing and an accompanying accept type to use which
;;; searches through all commands and then calls one that matches, searching as
;;; strings! - this would mean that in the command com-operate-on-marks, we first
;;; accept a string or symbol, and then search in our faux-database for it, and
;;; call the associated function!
(defparameter *mark-operation-commands* nil)
(defun replace-alist-entry (alist key value &optional unintern-value)
(alexandria:when-let ((k (assoc key alist)))
(when unintern-value
(unintern (cdr k)))
(setf (cdr k) value)))
(defmacro define-operation (name args &body body)
"This macro defines a function and stores it in an alist. these functions can be
used to operate on marks. Each function must take two arguments - the item to be
operated upon, and a finished argument. This function will be called on every mark
with with the finished argument being nil. When no marks remain it will be called
one last time with the item being nil and the finished argument being t. The
function that is defined is named after a gensym and cannot be called normally.
When redefining a command the old gensym is uninterned to keep the function
namespace clean."
(let ((n (gensym (symbol-name name))))
`(progn
(unless (replace-alist-entry *mark-operation-commands* ',name ',n t)
(setf *mark-operation-commands*
(cons (cons ',name ',n) *mark-operation-commands*)))
(defun ,n ,args ,@body))))
(define-operation delete (item finished)
(declare (ignore finished))
(when item
(let ((reter (clfm-notify (*application-frame* "IRREVERSIBLE OPERATION" nil
:exit-boxes ((t "PROCEED")
(nil "ABORT"))
:width 400)
(with-end-of-line-action (pane :wrap*)
(with-etbembo (pane nil 18)
(format pane "You are attempting to delete a file or directory. This operation is ")
(with-etbembo (pane :bold 18)
(format pane "IRREVERSIBLE"))
(format pane ". Proceed with caution."))))))
(when reter
(cond ((uiop:directory-exists-p item)
(com-delete-directory item))
((uiop:file-exists-p item)
(com-rm-file item))
(t
(clfm-notify (*application-frame* "Not a file or directory" (item)
:width 400)
(with-etbembo (pane :italic 18)
(format pane "~a~%is not an existing file or directory, and cannot be deleted" item)))))))))
(define-operation display (item finished)
(declare (ignore finished))
(when item
(let ((message "The item is: "))
(test/clfm-notify (*application-frame* message (item)
:width 500)
(with-end-of-line-action (pane :wrap*)
(with-etbembo (pane)
(format pane "~a" item)))))))
(let (copy-to)
(define-operation copy (item finished)
(when finished (setf copy-to nil))
(when item
(unless copy-to
(let ((stream (frame-standard-input *application-frame*)))
(accepting-values (stream :own-window t
:initially-select-query-identifier 'copy)
(formatting-table (stream)
(formatting-column (stream)
(formatting-cell (stream)
(setf copy-to
(accept 'string
:prompt "Copy To"
:stream stream
:query-identifier 'copy))))))))
(ensure-directories-exist copy-to)
(if (uiop:file-exists-p item)
(%%copy-file item (concatenate 'string copy-to
(coerce
(reverse
(loop for c across (reverse item)
while (not (char= c #\/))
collect c))
'string)))
(%copy-directory-contents item copy-to)))))
;; (let (move-to)
;; (define-operation move (item finished)
;; (when finished (setf move-to nil))
;; (when item
;; (unless move-to
;; (let ((stream (frame-standard-input *application-frame*)))
;; (accepting-values (stream :own-window t
;; :initially-select-query-identifier 'move)
;; (formatting-table (stream)
;; (formatting-column (stream)
;; (formatting-cell (stream)
;; (setf move-to
;; (accept 'string
;; :prompt "Move To"
;; :stream stream
;; :query-identifier 'move))))))))
;; (com-move-to ))
;; ))
| 7,044 | Common Lisp | .lisp | 183 | 33.234973 | 118 | 0.630972 | szos/CLFM | 12 | 2 | 1 | GPL-3.0 | 9/19/2024, 11:26:41 AM (Europe/Amsterdam) | 0286688178b887f12de476be33f741715cb68b265226ebbe1b412b9f9ba8b405 | 8,792 | [
-1
] |
8,793 | rename-file-directory.lisp | szos_CLFM/rename-file-directory.lisp |
(in-package :clfm-2)
(defun %rename-file-directory (&optional old-name new-name)
(restart-case
(let ((stream (frame-standard-input *application-frame*))
oldname newname)
(accepting-values (stream :own-window t
:label (or (and
old-name
(format nil "Rename ~a" old-name))
"Rename a file")
:initially-select-query-identifier
(or (and old-name 'newfile) 'file))
(formatting-table (stream)
(or (and old-name (setf oldname old-name))
(formatting-row (stream)
(formatting-column (stream)
(formatting-cell (stream)
(setf oldname (accept 'string
:prompt "Filename "
:stream stream
:query-identifier 'file))))))
(or (and new-name (setf newname new-name))
(formatting-row (stream)
(formatting-column (stream)
(formatting-cell (stream)
(setf newname (accept 'string
:prompt "Rename to"
:stream stream
:query-identifier 'newfile))))))))
(and oldname newname (rename-file oldname newname)))
(abort () ())))
(define-clfm-command (com-rename) ((thing string) (newname string))
(rename-file thing newname))
(define-clfm-command (com-rename-this) ((thing string))
(%rename-file-directory thing))
(define-clfm-command (com-rename-this) ((thing t))
(%rename-file-directory thing))
(define-clfm-command (com-rename-prompt) ()
(%rename-file-directory))
| 1,423 | Common Lisp | .lisp | 39 | 30.615385 | 67 | 0.652395 | szos/CLFM | 12 | 2 | 1 | GPL-3.0 | 9/19/2024, 11:26:41 AM (Europe/Amsterdam) | 8f830b31f300700573dc194442d85ea7d91404935587d5de9261a90796bde796 | 8,793 | [
-1
] |
8,794 | password.lisp | szos_CLFM/password.lisp | (in-package :clfm-2)
(define-presentation-type password ()
:inherit-from '((string)
:description "Password")
:description "password")
(define-presentation-method present (password (type password) stream
(view textual-view)
&key acceptably)
(when acceptably (error "Not acceptably"))
(write-string (make-string (length password) :initial-element #\*) stream))
(define-presentation-method accept ((type password) stream view ;; (view textual-view)
&key)
(let* ((s (stream-scan-pointer stream))
(p (with-output-recording-options (stream :draw nil :record nil)
(read-token stream))))
(presentation-replace-input stream p 'password view
:buffer-start s)
(return-from accept p)))
;; (define-presentation-method accept ((type password) stream view ;; (view textual-view)
;; &key)
;; (let* ((s (stream-scan-pointer stream))
;; (p (clim:with-output-recording-options (stream :draw t :record nil)
;; (read-token stream))))
;; (presentation-replace-input stream p 'password view
;; :buffer-start s)
;; (return-from accept p)))
| 1,120 | Common Lisp | .lisp | 26 | 39.346154 | 89 | 0.677982 | szos/CLFM | 12 | 2 | 1 | GPL-3.0 | 9/19/2024, 11:26:41 AM (Europe/Amsterdam) | deb2e1a7aa8efd7fd316f95cd78d383ab59661f249ab78c6912df27a1b53025a | 8,794 | [
-1
] |
8,795 | move-file-directory.lisp | szos_CLFM/move-file-directory.lisp | (in-package :clfm-2)
#+NIL
(defun move-item (item new-item)
(unless (file-or-directory-exists-p new-item)
(let ((typeof-item (or (and (uiop:directory-exists-p item) :directory)
(and (uiop:file-exists-p item) :file)
:nonexistant))
(typeof-new-item (or (and (uiop:directory-exists-p item) :directory)
(and (uiop:file-exists-p item) :file)
:nonexistant)))
(cond ((eq typeof-item :nonexistant) nil)
((eq typeof-item :directory)
(when (eq typeof-new-item :nonexistant)
))
((eq typeof-item :file))))
(let ((r (reverse (coerce new-file 'list)))
newpath)
(cond ((uiop:directory-exists-p item)
(if (and (char= (car r) #\/) ; if it ends in a / and contains other /s
(string-contains (coerce (cdr r) 'string) #\/))
;; then move to another directory name
(rename-file item )))
((uiop:file-exists-p item)
)
)
(if (char= (car r) #\/)
(if (string-contains (coerce (cdr r) 'string) #\/)
;; then we want to move it to some other directory
nil
;; else we want to move it to something else in the current directory
(setf newpath (concatenate 'string (namestring (uiop:getcwd))
new-file))))))
(if (string-contains new-file )))
;; ok.. what we want to do here, is to immitate mv... we could just shell out,
;; but that isnt optimal...
;; (define-clfm-command )
| 1,409 | Common Lisp | .lisp | 36 | 33.444444 | 78 | 0.619708 | szos/CLFM | 12 | 2 | 1 | GPL-3.0 | 9/19/2024, 11:26:41 AM (Europe/Amsterdam) | 12d16178ea0f873b14e4e429e9994ae723c22b32e76635268c2c6a4eb5c2e2f7 | 8,795 | [
-1
] |
8,796 | commands.lisp | szos_CLFM/commands.lisp | (in-package :clfm-2)
(define-gesture-name :meta-click-left :pointer-button (:left :meta))
(define-gesture-name :meta-control :keyboard (:control :meta))
(define-presentation-type fopen-presentation ())
(defun set-file-command-order (&key
(delete -1)
(open 4)
(open-with 3)
(rename 2)
(copy 1)
;; (mark 0)
)
"the higher the number, the higher on the list it will be"
;; (define-presentation-to-command-translator fshift-click
;; (fopen-presentation com-mark-between-items))
(define-presentation-to-command-translator fmark
(fopen-presentation com-add-mark clfm
;; :gesture :select
:gesture :meta-click-left
:documentation "Mark File"
;; :priority mark
)
(file)
(list file))
(define-presentation-to-command-translator fdelete
(fopen-presentation com-rm-file clfm
:gesture :select
:documentation "Delete File (PERMANENT)"
:priority delete)
(f)
(list f))
(define-presentation-to-command-translator fopen
(fopen-presentation com-open-file clfm
:gesture :select
:documentation "Open File"
:priority open)
(f)
(list f))
(define-presentation-to-command-translator fopen-with
(fopen-presentation com-open-file-with clfm
:gesture :select
:documentation "Open File With"
:priority open-with)
(f)
(list f))
(define-presentation-to-command-translator frename
(fopen-presentation com-rename-this clfm
:gesture :select
:documentation "Rename this"
:priority rename)
(f)
(list f))
(define-presentation-to-command-translator fcopy
(fopen-presentation com-copy-file-prompt clfm
:gesture :select
:documentation "Copy this file"
:priority copy)
(f)
(list f)))
(set-file-command-order)
(define-presentation-type chdir-presentation ())
;; (define-gesture-name :double-click :pointer-button (:left :double))
(defun set-directory-command-order (&key
(change-directory 3)
(delete -1)
;; (mark 2)
(copy 1))
(define-presentation-to-command-translator dcopy
(chdir-presentation com-copy-directory-prompt clfm
:gesture :select
:documentation "Copy Directory"
:priority copy)
(dir)
(list dir))
(define-presentation-to-command-translator chdir
(chdir-presentation com-change-directory-temper clfm
:gesture :select
;; :gesture :double-click
:Documentation "Change to Directory"
:priority change-directory
)
(dir)
(list dir))
(define-presentation-to-command-translator dir-delete
(chdir-presentation com-delete-directory clfm
:gesture :select
:documentation "Delete Directory (PERMANENT)"
:priority delete)
(dir)
(list dir))
(define-presentation-to-command-translator dmark
(chdir-presentation com-add-mark clfm
;; :gesture :select
:gesture :meta-click-left
:documentation "Mark Directory"
;; :priority mark
)
(file)
(list file)))
(set-directory-command-order)
(defun file-prefer-marks ()
(set-file-command-order :mark 5))
(defun directory-prefer-marks ()
(set-directory-command-order :mark 3))
;; (define-drag-and-drop-translator drag-and-drop-directory
;; (chdir-presentation
;; chdir-presentation dcopy clfm
;; :documentation "drag and drop file"
;; :gesture ))
(make-command-table 'clfm-menu-bar
:errorp nil
:menu '(("Quit" :command com-quit)
("File" :menu clfm-file)
("Operate/Marks" :menu clfm-operation-menu)
("View" :menu clfm-view-menu)
("Help" :menu clfm-help-menu)))
(make-command-table 'clfm-file
:errorp nil
:menu '(("New File" :command com-new-file)
("New Directory" :command com-new-dir)
("Go Home" :command com-chdir-~/)
("Rename…" :command com-rename-prompt)
("Load Init File" :command com-loadrc)))
(make-command-table 'clfm-operation-menu
:errorp nil
:menu '(("Select Operation" :command com-operate-on-marks)))
(make-command-table 'clfm-view-menu
:errorp nil
:menu '(("Marks" :menu clfm-view-marks-menu)
("Display Hidden Files" :command
com-toggle-hidden-files)))
(make-command-table 'clfm-view-marks-menu
:errorp nil
:menu '(("Show Marks" :command com-show-marks)
("Hide Marks" :command com-hide-marks)))
(make-command-table 'clfm-help-menu
:errorp nil
:menu '(("Help Window" :command com-display-help-window)
("test" :command com-move-down
:keystroke (:n :control))))
| 4,685 | Common Lisp | .lisp | 140 | 27.442857 | 70 | 0.655393 | szos/CLFM | 12 | 2 | 1 | GPL-3.0 | 9/19/2024, 11:26:41 AM (Europe/Amsterdam) | d1444567d9377b650eda1f3dca7d4a20a69a0de890fc2e3e533fe4e4e07ebf6c | 8,796 | [
-1
] |
8,797 | init.lisp | szos_CLFM/init.lisp | ;;; This is an example init file, which has some basic configuration stuff.
(in-package :clfm-2)
(define-operation notify-me (item finished)
(declare (ignore finished))
(when item
(clfm-notify (*application-frame* "A Notification" (item))
(format pane "This is a notificaton of an item were operating on.")
(print item pane))))
(file-prefer-marks)
| 371 | Common Lisp | .lisp | 9 | 37.666667 | 75 | 0.718663 | szos/CLFM | 12 | 2 | 1 | GPL-3.0 | 9/19/2024, 11:26:41 AM (Europe/Amsterdam) | e6536c439544080c4679223add80dae1bceac70cb5c13211b93c6e95b6a759c9 | 8,797 | [
-1
] |
8,798 | clfm-2.asd | szos_CLFM/clfm-2.asd | ;;;; clfm-2.asd
(asdf:defsystem #:clfm-2
:description "Describe clfm-2 here"
:author "Your Name <[email protected]>"
:license "Specify license here"
:version "0.0.1"
:serial t
:depends-on (#:osicat
#:alexandria
#:mcclim
#:slim)
:components ((:file "package")
(:file "clfm-2")
(:file "dialog")
(:file "help")
(:file "password")
(:file "command-utilities")
(:file "open-files")
(:file "chdir")
(:file "delete")
(:file "create-file")
(:file "create-directory")
(:file "rename-file-directory")
(:file "copy")
(:file "move-file-directory")
(:file "mark-and-operate")
(:file "commands")
(:file "commands-update")
(:file "keybindings")))
| 818 | Common Lisp | .asd | 29 | 20.931034 | 45 | 0.536802 | szos/CLFM | 12 | 2 | 1 | GPL-3.0 | 9/19/2024, 11:26:41 AM (Europe/Amsterdam) | 5118b13c354dfb54a595f82f097f0be54af43bf2feea73fb4733bf965efcaf8c | 8,798 | [
-1
] |
8,815 | make-executable.sh | szos_CLFM/make-executable.sh | #!/bin/sh
LISP=$(which sbcl)
exit="$?"
if [ ! "$exit" -eq 0 ]; then
/bin/echo -e "\e[1;31mSBCL not installed, exiting\e[0m"
exit 1
else
/bin/echo -e "\e[1;32mSBCL is installed, proceeding\e[0m"
fi
sbcl --eval "(progn (handler-case (in-package :ql) (PACKAGE-DOES-NOT-EXIST () (exit :code 1))) (sb-ext:exit :code 0))" > /dev/null 2>&1
exit="$?"
if [ ! "$exit" -eq 0 ]; then
/bin/echo -e "\e[1;31mQuicklisp not installed, exiting\e[0m"
exit 1
else
/bin/echo -e "\e[1;32mQuicklisp is installed, proceeding\e[0m"
fi
sbcl --eval "(push #p\"./\" asdf:*central-registry*)" --eval "(ql:quickload :clfm-2)" --eval "(clfm-2::make-executable)"
exit="$?"
if [ ! "$exit" -eq 0 ]; then
/bin/echo -e "\e[1;31m:CLFM not found, aborting\e[0m"
exit 1
fi
| 776 | Common Lisp | .l | 23 | 30.956522 | 135 | 0.625 | szos/CLFM | 12 | 2 | 1 | GPL-3.0 | 9/19/2024, 11:26:41 AM (Europe/Amsterdam) | 65a7490718c702cde045042c24911cd5f2936872430b965c7051cac89b58bc2c | 8,815 | [
-1
] |
8,820 | et-book-display-italic-bold-old-style-figures.ttf | szos_CLFM/fonts/et-book-display-italic-bold-old-style-figures.ttf |