-
Notifications
You must be signed in to change notification settings - Fork 373
Homoiconic REPL #1246
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
Merged
+405
−109
Merged
Homoiconic REPL #1246
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
32e76ca
Refactor test_bin
Kodiologist bf2f90a
Add hy.contrib.hy-repr
Kodiologist 33a696d
Add a command-line option --repl-output-fn (especially for hy.contrib…
Kodiologist ca1bd0f
Add a test for as-> in the REPL
Kodiologist e478008
Fix HyMacroExpansionError underline alignment
Kodiologist File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
================== | ||
Hy representations | ||
================== | ||
|
||
.. versionadded:: 0.13.0 | ||
|
||
``hy.contrib.hy-repr`` is a module containing a single function. | ||
To import it, say:: | ||
|
||
(import [hy.contrib.hy-repr [hy-repr]]) | ||
|
||
To make the Hy REPL use it for output, invoke Hy like so:: | ||
|
||
$ hy --repl-output-fn=hy.contrib.hy-repr.hy-repr | ||
|
||
.. _hy-repr-fn: | ||
|
||
hy-repr | ||
------- | ||
|
||
Usage: ``(hy-repr x)`` | ||
|
||
This function is Hy's equivalent of Python's built-in ``repr``. | ||
It returns a string representing the input object in Hy syntax. | ||
|
||
.. code-block:: hy | ||
|
||
=> (hy-repr [1 2 3]) | ||
'[1 2 3]' | ||
=> (repr [1 2 3]) | ||
'[1, 2, 3]' | ||
|
||
If the input object has a method ``__hy-repr__``, it will be called | ||
instead of doing anything else. | ||
|
||
.. code-block:: hy | ||
|
||
=> (defclass C [list] [__hy-repr__ (fn [self] "cuddles")]) | ||
=> (hy-repr (C)) | ||
'cuddles' | ||
|
||
When ``hy-repr`` doesn't know how to handle its input, it falls back | ||
on ``repr``. | ||
|
||
Like ``repr`` in Python, ``hy-repr`` can round-trip many kinds of | ||
values. Round-tripping implies that given an object ``x``, | ||
``(eval (read-str (hy-repr x)))`` returns ``x``, or at least a value | ||
that's equal to ``x``. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,3 +16,4 @@ Contents: | |
profile | ||
sequences | ||
walk | ||
hy_repr |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
(import [hy._compat [PY3 str-type bytes-type long-type]]) | ||
(import [hy.models [HyObject HyExpression HySymbol HyKeyword HyInteger HyList HyDict HySet HyString HyBytes]]) | ||
|
||
(defn hy-repr [obj] | ||
(setv seen (set)) | ||
; We keep track of objects we've already seen, and avoid | ||
; redisplaying their contents, so a self-referential object | ||
; doesn't send us into an infinite loop. | ||
(defn f [x q] | ||
; `x` is the current object being stringified. | ||
; `q` is True if we're inside a single quote, False otherwise. | ||
(setv old? (in (id x) seen)) | ||
(.add seen (id x)) | ||
(setv t (type x)) | ||
(defn catted [] | ||
(if old? "..." (.join " " (list-comp (f it q) [it x])))) | ||
(setv prefix "") | ||
(if (and (not q) (instance? HyObject x)) | ||
(setv prefix "'" q True)) | ||
(+ prefix (if | ||
(hasattr x "__hy_repr__") | ||
(.__hy-repr__ x) | ||
(is t HyExpression) | ||
(if (and x (symbol? (first x))) | ||
(if | ||
(= (first x) 'quote) | ||
(+ "'" (f (second x) True)) | ||
(= (first x) 'quasiquote) | ||
(+ "`" (f (second x) q)) | ||
(= (first x) 'unquote) | ||
(+ "~" (f (second x) q)) | ||
(= (first x) 'unquote_splice) | ||
(+ "~@" (f (second x) q)) | ||
; else | ||
(+ "(" (catted) ")")) | ||
(+ "(" (catted) ")")) | ||
(is t tuple) | ||
(+ "(," (if x " " "") (catted) ")") | ||
(in t [list HyList]) | ||
(+ "[" (catted) "]") | ||
(is t HyDict) | ||
(+ "{" (catted) "}") | ||
(is t dict) | ||
(+ | ||
"{" | ||
(if old? "..." (.join " " (list-comp | ||
(+ (f k q) " " (f v q)) | ||
[[k v] (.items x)]))) | ||
"}") | ||
(in t [set HySet]) | ||
(+ "#{" (catted) "}") | ||
(is t frozenset) | ||
(+ "(frozenset #{" (catted) "})") | ||
(is t HySymbol) | ||
x | ||
(or (is t HyKeyword) (and (is t str-type) (.startswith x HyKeyword.PREFIX))) | ||
(cut x 1) | ||
(in t [str-type HyString bytes-type HyBytes]) (do | ||
(setv r (.lstrip (repr x) "ub")) | ||
(+ (if (in t [bytes-type HyBytes]) "b" "") (if (.startswith "\"" r) | ||
; If Python's built-in repr produced a double-quoted string, use | ||
; that. | ||
r | ||
; Otherwise, we have a single-quoted string, which isn't valid Hy, so | ||
; convert it. | ||
(+ "\"" (.replace (cut r 1 -1) "\"" "\\\"") "\"")))) | ||
(and (not PY3) (is t int)) | ||
(.format "(int {})" (repr x)) | ||
(and (not PY3) (in t [long_type HyInteger])) | ||
(.rstrip (repr x) "L") | ||
(is t complex) | ||
(.strip (repr x) "()") | ||
(is t fraction) | ||
(.format "{}/{}" (f x.numerator q) (f x.denominator q)) | ||
; else | ||
(repr x)))) | ||
(f obj False)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Last thing, I swear!! :O
Would this be better as:
that way the
(do ...)
doesn't appear in the stack traces/compile errors?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the current implementation,
(do ...)
already doesn't appear. (Your example above no longer hasdo
in its output.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, but the underline from a
HyMacroExpansionError
seems to be misaligned. I'll take a look at it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.