Skip to content

Commit

Permalink
Implement crude pretty-printing
Browse files Browse the repository at this point in the history
Added a print buffer for rendering values along with the evaluation result without affecting the form's return value.
  • Loading branch information
bobbicodes authored Sep 7, 2023
2 parents a0ab2ab + b6297a9 commit 99b587c
Show file tree
Hide file tree
Showing 6 changed files with 284 additions and 117 deletions.
8 changes: 2 additions & 6 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ import testSuites from './test/tests.json';
import { evalString, deftests, clearTests } from "./src/interpreter"

let editorState = EditorState.create({
doc: `((fn ([[exponent bit]]
(if (= "1" bit)
(Math/pow 2 exponent)
0)))
[0 "1"])`,
doc: `(+ (do (print "hi") (+ 1 1)) 1)`,
extensions: [basicSetup, clojure()]
})

Expand Down Expand Up @@ -170,7 +166,7 @@ function testExercisesUntilFail() {
}

//testSolution(randExercise())
//testSolution("two_fer")
//testSolution("veitch")
//loadExercise("all_your_base")
//testExercisesUntilFail()
//testExercises()
30 changes: 29 additions & 1 deletion scratch.clj
Original file line number Diff line number Diff line change
Expand Up @@ -408,4 +408,32 @@
full-str (add-english-scales parts-strs 0)]
(str (if (neg? n) "minus ") full-str)))))

(number 1)
(number 1)


{:a 1 :b 2 :c 3 :d 4}

(clojure.pprint/pprint
(into {} (for [x (range 11)
y (range 11)]
[x y])))

(def I #{#{'a 'B 'C 'd}
#{'A 'b 'c 'd}
#{'A 'b 'c 'D}
#{'A 'b 'C 'd}
#{'A 'b 'C 'D}
#{'A 'B 'c 'd}
#{'A 'B 'c 'D}
#{'A 'B 'C 'd}})

#{#{'a 'B 'C 'd}
#{'A 'b 'c 'd}
#{'A 'b 'c 'D}
#{'A 'b 'C 'd}
#{'A 'b 'C 'D}
#{'A 'B 'c 'd}
#{'A 'B 'c 'D}
#{'A 'B 'C 'd}}

(print ['a :b "\n" \space "c"])
80 changes: 80 additions & 0 deletions src/clj/pprint.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
(ns pprint {:clj-kondo/ignore true})

;; adapted from clojure.pprint

(def *print-pretty* true)
(def *print-pprint-dispatch* nil)
(def *print-right-margin* 72)
(def *print-miser-width* 40)
(def *print-lines* nil)
(def *print-circle* nil)
(def *print-shared* nil)
(def *print-suppress-namespaces* nil)
(def *print-radix* nil)
(def *print-base* 10)
(def *current-level* 0)
(def *current-length* nil)
(def format-simple-number)
(def orig-pr pr)

(defn- pr-with-base [x]
(if-let [s (format-simple-number x)]
(print s)
(orig-pr x)))

(def write-option-table
{;:array *print-array*
:base '*print-base*,
;;:case *print-case*,
:circle '*print-circle*,
;;:escape *print-escape*,
;;:gensym *print-gensym*,
:length '*print-length*,
:level '*print-level*,
:lines '*print-lines*,
:miser-width '*print-miser-width*,
:dispatch '*print-pprint-dispatch*,
:pretty '*print-pretty*,
:radix '*print-radix*,
:readably '*print-readably*,
:right-margin '*print-right-margin*,
:suppress-namespaces '*print-suppress-namespaces*})

;; basic MAL pretty printer

(defn spaces- [indent]
(if (> indent 0)
(str " " (spaces- (- indent 1)))
""))

(defn pp-seq- [obj indent]
(let* [xindent (+ 1 indent)]
(apply str (pp- (first obj) 0)
(map (fn [x] (str "\n" (spaces- xindent)
(pp- x xindent)))
(rest obj)))))

(defn pp-map- [obj indent]
(let* [ks (keys obj)
kindent (+ 1 indent)
kwidth (count (seq (str (first ks))))
vindent (+ 1 (+ kwidth kindent))]
(apply str (pp- (first ks) 0)
" "
(pp- (get obj (first ks)) 0)
(map (fn [k] (str "\n" (spaces- kindent)
(pp- k kindent)
" "
(pp- (get obj k) vindent)))
(rest (keys obj))))))

(defn pp- [obj indent]
(cond
(list? obj) (str "(" (pp-seq- obj indent) ")")
(vector? obj) (str "[" (pp-seq- obj indent) "]")
(set? obj) (str "#{" (pp-seq- obj indent) "}")
(map? obj) (str "{" (pp-map- obj indent) "}")
:else (pr-str obj)))

(defn pprint [obj]
(pp- obj 0))
43 changes: 35 additions & 8 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,28 @@ import * as types from './types.js'
import { repl_env, evalString } from './interpreter.js';
import zip from './clj/zip.clj?raw'

export var out_buffer = ""

export function appendBuffer(s) {
out_buffer = out_buffer + s
}

export function clearBuffer() {
out_buffer = ""
}

function _print(s) {
appendBuffer(s)
}

function _pr(s) {
if (arguments.length > 1) {
appendBuffer(Array.from(arguments).join(' '))
} else {
appendBuffer(s)
}
}

// Errors/Exceptions
function mal_throw(exc) { throw new Error(exc); }

Expand All @@ -29,14 +51,17 @@ function str() {
}).join("");
}

function prn() {
_println.apply({}, Array.prototype.map.call(arguments, function (exp) {
return _pr_str(exp, true);
}));
function prn(s) {
if (arguments.length > 1) {
appendBuffer(Array.from(arguments).join(' ') + "\n")
} else {
appendBuffer(s + "\n")
}
}

function println() {
_println.apply({}, Array.prototype.map.call(arguments, function (exp) {
appendBuffer(exp + "\n")
return _pr_str(exp, false);
}));
}
Expand Down Expand Up @@ -716,7 +741,7 @@ function sort(x) {
}

function makeComparator(f) {
return function(x, y) {
return function (x, y) {
let r = f(x, y)
if (types._number_Q(r)) {
return r
Expand Down Expand Up @@ -895,15 +920,17 @@ function map_indexed(f, coll) {
let ret = [];
let i = 0;
for (const x of coll) {
ret.push(f(i, x));
i++;
ret.push(f(i, x));
i++;
}
return ret;
}
}

// types.ns is namespace of type functions
export var ns = {
'env': printEnv,
'print': _print,
'pr': _pr,
'spit-json': spit_json,
'LazySeq': LazySeq,
'require': require,
Expand Down
Loading

0 comments on commit 99b587c

Please sign in to comment.