Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better environment, presentations, guile, and MIT support. #16

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 35 additions & 10 deletions common/base.scm
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
(hash-table-ref/default *handlers* name #f))

(define (interactive-eval sexp)
(let-values ((vals (eval sexp (param:environment)))) ;; TODO environment
(let-values ((vals (eval sexp ($current-environment))))
vals))

(define *presentations* (make-hash-table))
Expand Down Expand Up @@ -167,14 +167,28 @@ The secondary value indicates the absence of an entry."
object))))

(define (replace-readtime-lookup-presented-object-or-lose string)
"For presentations, emacs passes something that common lisp evals at read time. The resulting object is very different than what gerbil or gambits #. tries to do, so we do everything at run time"
"For presentations, emacs passes something that common lisp evals at read time. The resulting object is very different than what gerbil or gambits #. tries to do, so strip the #. and pattern-match to evaluate this form at runtime."
(let* ((pattern "#.(swank:lookup-presented-object-or-lose ")
(start (string-contains string pattern)))
(if start
(replace-readtime-lookup-presented-object-or-lose
(string-replace string "" start (+ 2 start)))
string)))

(define (replace-lookup-presented-object-or-lose form)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I see, it's an workaround until proper lookup is implemented, right? In which cases slime sends these commands? Perhaps some TODO-comments with context information would be helpful.

(define (recons form x y)
(if (and (eq? x (car form))
(eq? y (cdr form)))
form
(cons x y)))
(if (pair? form)
(if (eq? (car form) 'swank:lookup-presented-object-or-lose)
(swank:lookup-presented-object-or-lose (cadr form))
(recons form
(replace-lookup-presented-object-or-lose (car form))
(replace-lookup-presented-object-or-lose (cdr form))))
form))

(define last-presentation-id 0)
(define (next-presentation-id)
(set! last-presentation-id (+ last-presentation-id 1))
Expand Down Expand Up @@ -264,7 +278,7 @@ The secondary value indicates the absence of an entry."
(if (and (list? x)
(eq? (car x) 'quote))
(cond ((string? (cadr x))
(cadr x))
(read-from-string (cadr x)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not really familiar with the code, but why should unquote-string return something different than a string?

((eq? (cadr x) 'nil)
"(user)")
(else ;; TODO
Expand All @@ -285,7 +299,8 @@ The secondary value indicates the absence of an entry."
(define (find-expression-containing-swank-cursor-marker expr)
(define (f expr exit)
(if (list? expr)
(if (member '|swank::%cursor-marker%| expr)
(if (or (member '|swank::%cursor-marker%| expr)
(member 'swank::%cursor-marker% expr))
expr
(let ((res (find (lambda (ex)
(f ex exit))
Expand All @@ -296,17 +311,20 @@ The secondary value indicates the absence of an entry."

(define (highlight-at-cursor signature expr)
(let* ((form (find-expression-containing-swank-cursor-marker (cdr expr)))
(index (list-index (lambda (el) (eq? el '|swank::%cursor-marker%|)) form)))
(index (list-index (lambda (el)
(or (eq? el '|swank::%cursor-marker%|)
(eq? el 'swank::%cursor-marker%)))
form)))
(if index
(wrap-item/index (improper->proper-list signature) (- index 1) '===> '<===)
'())))

(define (find-string-before-swank-cursor-marker expr)
(let ((ex (find-expression-containing-swank-cursor-marker expr)))
(if ex
(if (string? (car ex))
(car ex)
#f))))
(and ex
(if (string? (car ex))
(car ex)
#f))))

(define (wrap-item/index lst index before-marker after-marker)
(let loop ((i 0)
Expand Down Expand Up @@ -527,7 +545,7 @@ The secondary value indicates the absence of an entry."
(stream))))))

(define (binding-value symbol)
(call/cc (lambda (k) (with-exception-handler (lambda (c) (k #f)) (lambda () (eval symbol (param:environment)))))))
(call/cc (lambda (k) (with-exception-handler (lambda (c) (k #f)) (lambda () (eval symbol ($current-environment)))))))

(define (object-documentation name object)
(let ((doc ($binding-documentation object)))
Expand Down Expand Up @@ -787,6 +805,13 @@ The secondary value indicates the absence of an entry."
(write-message `(:inspect ,(inspect-object object) nil nil))
#t)

(define (environment-name-as-string environment)
(write-to-string ($environment-name environment)))
(define (package-string->environment string)
(if (string=? string "COMMON-LISP-USER")
(interaction-environment)
($environment (read-from-string string))))

;;;; swank image support
(define-record-type swank-image
(make-swank-image type filename data string)
Expand Down
21 changes: 14 additions & 7 deletions common/handlers.scm
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@
((define-slime-handler (name . params) body0 body1 ...)
(register-slime-handler! 'name (lambda params body0 body1 ...)))))

(define-slime-handler (:emacs-rex sexp env-name thread id)
(define-slime-handler (:emacs-rex sexp package-string thread id)
(call-with-current-continuation
(lambda (exit)
(parameterize ((param:abort (lambda (message)
(exit `(:return (:abort ,message)
,id))))
(param:environment ($environment env-name))
(param:environment
(package-string->environment package-string))
(param:current-id id))
`(:return (:ok ,(process-form sexp env-name))
`(:return (:ok ,(process-form sexp package-string))
,id)))))

(define-slime-handler (swank:connection-info)
Expand All @@ -31,7 +32,8 @@
:machine (:instance "host" :type "X86-64")
:features (:swank)
:modules ("SWANK-ARGLISTS" "SWANK-REPL" "SWANK-PRESENTATIONS")
:package (:name "(user)" :prompt "(user)")
:package (:name ,(environment-name-as-string ($current-environment))
:prompt ,(environment-name-as-string ($current-environment)))
:version "2.28"))

(define-slime-handler (swank:swank-require packages)
Expand All @@ -49,11 +51,16 @@
(swank:lookup-presented-object num))

(define-slime-handler (swank-repl:create-repl . args)
(list "(user)" "(user)"))
(list (environment-name-as-string ($current-environment))
(environment-name-as-string ($current-environment))))

(define-slime-handler (swank-repl:listener-eval form)
(let* ((form (replace-readtime-lookup-presented-object-or-lose form))
(results ($output-to-repl (lambda () (interactive-eval (cons 'begin (read-all (open-input-string form))))))))
(results ($output-to-repl (lambda ()
(interactive-eval
(cons 'begin
(replace-lookup-presented-object-or-lose
(read-all (open-input-string form)))))))))
(for-each (lambda (val)
(if (presentations?)
(present val ':repl-result)
Expand Down Expand Up @@ -87,7 +94,7 @@
`(:compilation-result nil t 0.001 nil nil)))

(define-slime-handler (swank:load-file filename)
(let ((results ($output-to-repl (lambda () (load filename (param:environment))))))
(let ((results ($output-to-repl (lambda () (load filename ($current-environment))))))
'loaded))

(define-slime-handler (swank:set-package name)
Expand Down
2 changes: 2 additions & 0 deletions guile-swank.scm
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
vector->list)
(ice-9 exceptions)
(ice-9 pretty-print)
(only (ice-9 session)
procedure-arguments)
(system vm frame)
(system vm program)
(only (system repl debug)
Expand Down
4 changes: 4 additions & 0 deletions specific/bigloo.scm
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,12 @@

(define ($error-description error)
error)
(define ($environment-name environment)
'(user))
(define ($environment name)
(interaction-environment))
(define ($current-environment)
(interaction-environment))
(define $pretty-print pp)
(define string->utf8 (lambda (x) x))
(define utf8->string (lambda (x) x))
Expand Down
3 changes: 3 additions & 0 deletions specific/chez.ss
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@
;; (write val o)
;; (get-output-string o)))

(define ($environment-name environment)
'(user))

(define ($environment env-name)
;; don't support any changes
(interaction-environment))
Expand Down
6 changes: 6 additions & 0 deletions specific/chibi.scm
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,15 @@
(define ($set-package name)
(list "(user)" "(user)"))

(define ($environment-name environment)
'(user))

(define ($environment name)
(interaction-environment))

(define ($current-environment)
(interaction-environment))

(define ($condition-trace condition)
'())

Expand Down
6 changes: 6 additions & 0 deletions specific/chicken.scm
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,15 @@
(define ($set-package name)
(list "(user)" "(user)"))

(define ($environment-name environment)
'(user))

(define ($environment name)
(interaction-environment))

(define ($current-environment)
(interaction-environment))

(define ($condition-trace condition)
'())

Expand Down
6 changes: 6 additions & 0 deletions specific/cyclone.ss
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,15 @@
(define ($set-package name)
(list "(user)" "(user)"))

(define ($environment-name environment)
'(user))

(define ($environment name)
(interaction-environment))

(define ($current-environment)
(interaction-environment))

(define ($condition-trace condition)
'())

Expand Down
5 changes: 5 additions & 0 deletions specific/gauche.scm
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,14 @@
(list "(user)" "(user)"))

(define env (interaction-environment))
(define ($environment-name environment)
'(user))
(define ($environment name)
env)

(define ($current-environment)
(interaction-environment))

(define ($condition-trace condition)
'())

Expand Down
5 changes: 5 additions & 0 deletions specific/gerbil.scm
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,13 @@
(let ((name (string->symbol env-name)))
(find (lambda (e) (eq? (expander-context-id e) name))
(filter module-context? (map cdr (hash->list (current-expander-module-registry)))))))))
(define ($environment-name environment)
'(user))
(define $environment env-name->environment)

(define ($current-environment)
(interaction-environment))

(define (environment-bindings env-name)
(let ((env (env-name->environment env-name)))
(if env
Expand Down
64 changes: 50 additions & 14 deletions specific/guile.scm
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,25 @@
(write error o)
(get-output-string o)))

(define repl-output-port
(make-soft-port
(vector (lambda (c)
(write-message `(:write-string ,(string c))))
(lambda (s)
(write-message `(:write-string ,s)))
(lambda () #f)
#f
(lambda () #f)
#f)
"w"))

(define ($output-to-repl thunk)
;; basic implementation, print all output at the end, this should
;; be replaced with a custom output port
(let ((o (open-output-string)))
(with-output-to-port o (lambda ()
(with-error-to-port o (lambda ()
(let-values ((x (thunk)))
(swank/write-string (get-output-string o) #f)
(apply values x))))))))
(with-output-to-port repl-output-port
(lambda ()
(with-error-to-port repl-output-port thunk))))

(define (env-name->environment env-name)
;; TODO
(interaction-environment))
(resolve-module env-name))
(define (environment-bindings env)
(define (env-binds env)
(let ((results '()))
Expand Down Expand Up @@ -143,7 +149,9 @@
(set! stored-frames frames)
(vector->list
(vector-map (lambda (fr)
(format #f "~a" (frame-procedure-name fr)))
(format #f "~a ~a"
(frame-procedure-name fr)
(frame-arguments fr)))
Copy link
Contributor

@rgherdt rgherdt Dec 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alternatively, one can use frame-call-representation (see PR #17 ). But we can leave this change said PR.

frames))))


Expand Down Expand Up @@ -183,8 +191,31 @@
(define ($handle-condition exception)
(invoke-sldb exception))

(define (procedure-parameters procedure)
(let ((arguments (procedure-arguments procedure)))
(and arguments
(let ((required (cdar arguments))
(optional (cdadr arguments))
(keyword (cdaddr arguments))
(allow-other-keys? (cdar (cdddr arguments)))
(rest (cdar (cddddr arguments))))
(append required
(cons '#:optional optional)
(cons '#:key keyword)
(if allow-other-keys?
(list '#:allow-other-keys?)
'())
(cons '#:rest rest))))))

(define ($function-parameters-and-documentation name)
(cons #f #f))
(let ((thing (module-variable (current-module)
(string->symbol name))))
(if (and thing
(procedure? (variable-ref thing)))
(let ((procedure (variable-ref thing)))
(cons (cons (string->symbol name) (procedure-parameters procedure))
(procedure-documentation procedure)))
(cons #f #f))))

(define (get-valid-module-name name)
(with-input-from-string name read))
Expand All @@ -195,8 +226,13 @@
(set-current-module mod)))
(list name name))

(define ($environment name)
(interaction-environment))
(define ($environment-name environment)
(module-name environment))

(define $environment env-name->environment)

(define ($current-environment)
(current-module))

(define $pretty-print pretty-print)

Expand Down
6 changes: 6 additions & 0 deletions specific/kawa.scm
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,15 @@
(define ($set-package name)
(list "(user)" "(user)"))

(define ($environment-name environment)
'(user))

(define ($environment name)
(interaction-environment))

(define ($current-environment)
(interaction-environment))

(define ($condition-trace condition)
(map *:toString ($condition-links condition)))

Expand Down
Loading