How to pprint all globals?


#1

Hi,

I want to pprint all defined items and I tried:

(mapc pprint (globals))

but this gives me only the names of each symbol, each in one line, and at the end the first item pprinted.

Is it possible to print out all function definitions with one command (or one function)?

Thanks,
Kaef


Ulisp-2.4-esp with 4 MByte PSRAM support
#2

Sorry, last post was wrong. The result is the items each in one line, returning a list with all items.


#3

What you want is:

(defun pp ()
  (mapc 
   (lambda (x) (pprint (eval x)) (terpri))
   (globals))
  nothing)

Run it by typing:

(pp)

Alternatively, a fancier version that prints each definition starting with defun is:

(defun pp ()
  (mapc 
   (lambda (x)
     (let ((v (eval x)))
       (unless (eq x 'pp)
         (when (eq (car v) 'lambda)
           (pprint (cons 'defun (cons x (cdr v)))))
         (terpri))))
   (globals))
  nothing)

#4

Yes, that’s what I wanted, thank you very much!


#5

As of uLisp Version 2.3 this feature is now built in to uLisp, using the function:

(pprintall)

#6

(pprintall) is nice, but why it ignores non-functions? If I want to see all global variables I still have to map over globals. By default my images are loaded on boot, and at times I have to unbound some function or variable. so if pprintall could print also variables - it would be great!

Regards, rsm


#7

Good suggestion. Change the definition of fn_pprintall() to:

object *fn_pprintall (object *args, object *env) {
  (void) args, (void) env;
  object *globals = GlobalEnv;
  while (globals != NULL) {
    object *pair = first(globals);
    object *var = car(pair);
    object *val = cdr(pair);
    pln(pserial);
    if (consp(val) && symbolp(car(val)) && car(val)->name == LAMBDA) {
      superprint(cons(symbol(DEFUN), cons(var, cdr(val))), 0, pserial);
    } else {
      superprint(cons(symbol(DEFVAR), cons(var, cons(val, NULL))), 0, pserial);
    }
    pln(pserial);
    globals = cdr(globals);
  }
  return symbol(NOTHING);
}

This will probably get incorporated into the next release.


#8

Tested, works great! Thanks! :)


#9

This works great, thank you very much.
I tried:

7827> (defvar a '(1 2 3))
a
7810> (defvar b 42)
b
7801> (pprintall)
(defvar b 42)
(defvar a (1 2 3))
7787> 

Shouldn’t “a” be written with a quote (ei. “defvar a (quote(1 2 3)))”) ?

Best regards,
Kaef


#10

Yes, I was a bit hasty. It should be this:

object *fn_pprintall (object *args, object *env) {
  (void) args, (void) env;
  object *globals = GlobalEnv;
  while (globals != NULL) {
    object *pair = first(globals);
    object *var = car(pair);
    object *val = cdr(pair);
    pln(pserial);
    if (consp(val) && symbolp(car(val)) && car(val)->name == LAMBDA) {
      superprint(cons(symbol(DEFUN), cons(var, cdr(val))), 0, pserial);
    } else {
      superprint(cons(symbol(DEFVAR),cons(var,cons(cons(symbol(QUOTE),cons(val,NULL))
      ,NULL))), 0, pserial);
    }
    pln(pserial);
    globals = cdr(globals);
  }
  return symbol(NOTHING);
}

Thanks for spotting that,
David


#11

This is now incorporated into uLisp Version 2.8.


#12