shithub: pprolog

Download patch

ref: d5ac41480097d6f382fbb2385974e57ec21030a4
parent: 85adea62d7e8eee9d0e3525d572325db4e58d21a
author: Peter Mikkelsen <[email protected]>
date: Wed Jun 30 19:48:25 EDT 2021

Prettyprint lists for reals

--- a/misc.c
+++ b/misc.c
@@ -97,4 +97,4 @@
 	Term *t = mkterm(StringTerm);
 	t->text = text;
 	return t;
-}
\ No newline at end of file
+}
--- a/prettyprint.c
+++ b/prettyprint.c
@@ -5,6 +5,8 @@
 #include "fns.h"
 
 Rune *prettyprintlist(Term *, Rune *, int);
+Rune *printlist(Term *);
+int islist(Term *);
 
 Rune *
 prettyprint(Term *t)
@@ -14,9 +16,13 @@
 
 	switch(t->tag){
 	case CompoundTerm:
-		args = prettyprintlist(t->children, L", ", 0);
-		result = runesmprint("%S(%S)", t->text, args);
-		free(args);
+		args = printlist(t);
+		if(args == nil){
+			args = prettyprintlist(t->children, L", ", 0);
+			result = runesmprint("%S(%S)", t->text, args);
+			free(args);
+		}else
+			result = runesmprint("[%S]", args);
 		break;
 	case AtomTerm:
 		result = runesmprint("%S", t->text);
@@ -62,5 +68,33 @@
 
 	free(str);
 	free(rest);
+	return result;
+}
+
+/* printlist prints a list's elements but not the surrounding [ and ] */
+Rune *
+printlist(Term *list)
+{
+	if(list->tag != CompoundTerm || list->arity != 2 || runestrcmp(L".", list->text) != 0)
+		return nil;
+
+	Term *head = list->children;
+	Term *tail = head->next;
+
+	Rune *headstr = prettyprint(head);
+	Rune *tailstr = nil;
+	Rune *result;
+
+	if(tail->tag == CompoundTerm && tail->arity == 2 && runestrcmp(L".", tail->text) == 0){
+		tailstr = printlist(tail);
+		result = runesmprint("%S, %S", headstr, tailstr);
+	}else if(tail->tag == AtomTerm && runestrcmp(L"[]", tail->text) == 0){
+		result = runesmprint("%S", headstr);
+	}else{
+		tailstr = prettyprint(tail);
+		result = runesmprint("%S | %S", headstr, tailstr);
+	}
+	free(headstr);
+	free(tailstr);
 	return result;
 }
\ No newline at end of file