Skip to content

An implementation of A Prettier Printer in Common Lisp.

Notifications You must be signed in to change notification settings

remexre/cl-wadler-pprint

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cl-wadler-pprint

An implementation of "A Prettier Printer" by Philip Wadler in Common Lisp.

Example

(defclass foo ()
  ((bar :initarg :bar :reader bar)
   (baz :initarg :baz :reader quux)))

(def-pretty-object foo ()
  (bar baz))

; at width 80
(pretty t (make-instance 'foo :bar 1 :baz 2))
; #<FOO :BAR 1 :BAZ 2>

; at width 20
(pretty t (make-instance 'foo :bar 1 :baz 2))
; #<FOO :BAR 1
;       :BAZ 2>

TODOs

  • It might make sense to use laziness and/or memoization to speed up pretty-printing.

Notes

ABCL

In my entrypoint (written in Java), I've got a method:

import java.io.IOException;
import java.util.Scanner;
import org.armedbear.lisp.JavaObject;
import org.armedbear.lisp.Packages;

public static void updateRightMargin() {
	int rightMargin;
	try {
		Process proc = new ProcessBuilder("tput", "cols")
			.inheritIO()
			.redirectOutput(ProcessBuilder.Redirect.PIPE)
			.start();
		rightMargin = new Scanner(proc.getInputStream()).nextInt();
		if(proc.waitFor() != 0) {
			logError("Failed to update *PRINT-RIGHT-MARGIN*.");
			return;
		}
	} catch(InterruptedException ex) {
		logError("Failed to update *PRINT-RIGHT-MARGIN*: " + ex);
		return;
	} catch(IOException ex) {
		logError("Failed to update *PRINT-RIGHT-MARGIN*: " + ex);
		return;
	}

	Packages.findPackage("COMMON-LISP")
		.findAccessibleSymbol("*PRINT-RIGHT-MARGIN*")
		.setSymbolValue(JavaObject.getInstance(rightMargin, true));
}

and in main, code like:

import sun.misc.Signal;
import sun.misc.SignalHandler;

Signal.handle(new Signal("WINCH"), new SignalHandler() {
	public void handle(Signal sig) {
		Main.updateRightMargin();
	}
});
updateRightMargin();

About

An implementation of A Prettier Printer in Common Lisp.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published