Skip to content

Commit

Permalink
Port of clojure/pprint to Basilisp
Browse files Browse the repository at this point in the history
  • Loading branch information
ikappaki committed Aug 16, 2024
1 parent f15e9ef commit 67e51c3
Show file tree
Hide file tree
Showing 15 changed files with 764 additions and 457 deletions.
10 changes: 10 additions & 0 deletions docs/api/pprint.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
basilisp.pprint
=============================

.. toctree::
:maxdepth: 2
:caption: Contents:

.. autonamespace:: basilisp.pprint
:members:
:undoc-members:
2 changes: 1 addition & 1 deletion docs/differencesfromclojure.rst
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ Support for Clojure libs is `planned <https://github.com/basilisp-lang/basilisp/
basilisp.core
-------------

- :lpy:fn:`basilisp.core/int` coerces its argument to an integer. When given a string input, Basilisp will try to interpret it as a base 10 number, whereas in Clojure, it will return its ASCII/Unicode index if it is a character (or fail if it is a string).
- :lpy:fn:`basilisp.core/int` coerces its argument to an integer. When given a string input, Basilisp will try to interpret it as a base 10 number, whereas in Clojure, it will return its ASCII/Unicode index if it is a character (or fail if it is a string). Use `lpy:fn:`ord` instead to return the character index if required.

- :lpy:fn:`basilisp.core/float` coerces its argument to a floating-point number. When given a string input, Basilisp will try to parse it as a floating-point number, whereas Clojure will raise an error if the input is a character or a string.

Expand Down
374 changes: 191 additions & 183 deletions src/basilisp/contrib/pprint/cl_format.lpy

Large diffs are not rendered by default.

31 changes: 13 additions & 18 deletions src/basilisp/contrib/pprint/column_writer.lpy
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,14 @@

;; This module implements a column-aware wrapper around an instance of java.io.Writer

(in-ns 'clojure.pprint)

(import [clojure.lang IDeref]
[java.io Writer])
(in-ns 'basilisp.pprint)

(def ^:dynamic ^{:private true} *default-page-width* 72)

(defn- get-field [^Writer this sym]
(defn- get-field [ this sym]
(sym @@this))

(defn- set-field [^Writer this sym new-val]
(defn- set-field [^Writer this sym new-val]
(alter @this assoc sym new-val))

(defn- get-column [this]
Expand All @@ -44,31 +41,31 @@
(defn- get-writer [this]
(get-field this :base))

(defn- c-write-char [^Writer this ^Integer c]
(dosync (if (= c (int \newline))
(defn- c-write-char [^Writer this ^int c]
(dosync (if (= c (ord \newline))
(do
(set-field this :cur 0)
(set-field this :line (inc (get-field this :line))))
(set-field this :cur (inc (get-field this :cur)))))
(.write ^Writer (get-field this :base) c))
(.write ^Writer (get-field this :base) (chr c)))

(defn- column-writer
([writer] (column-writer writer *default-page-width*))
([^Writer writer max-columns]
(let [fields (ref {:max max-columns, :cur 0, :line 0 :base writer})]
(proxy [Writer IDeref] []
(proxy [Writer basilisp.lang.interfaces/IDeref] []
(deref [] fields)
(flush []
(.flush writer))
(write
([^chars cbuf ^Integer off ^Integer len]
(let [^Writer writer (get-field this :base)]
([^str cbuf ^int off ^int len]
(let [^Writer writer (get-field this :base)]
(.write writer cbuf off len)))
([x]
(condp = (class x)
String
(let [^String s x
nl (.lastIndexOf s (int \newline))]
python/str
(let [^str s x
nl (.rfind s \newline)]
(dosync (if (neg? nl)
(set-field this :cur (+ (get-field this :cur) (count s)))
(do
Expand All @@ -77,7 +74,5 @@
(count (filter #(= % \newline) s)))))))
(.write ^Writer (get-field this :base) s))

Integer
(c-write-char this x)
Long
python/int
(c-write-char this x))))))))
Loading

0 comments on commit 67e51c3

Please sign in to comment.