ref: 9c0e261a35c05718aa9c890f7164c441c868ceb4
parent: c7b18639af077c805669eacff2df7b501b39c4f8
author: spew <spew@cbza.org>
date: Tue Mar 18 18:03:08 EDT 2025
lsd: style, use when and and
--- a/src/plan9/lsd.lsp
+++ b/src/plan9/lsd.lsp
@@ -14,7 +14,7 @@
(def bptbl (table))
(def (procfile s . flags)
- (if (< pid 0) (error "no active pid"))
+ (when (< pid 0) (error "no active pid"))
(let ((path (str "/proc/" pid "/" s)))
(apply file (cons path flags))))
@@ -24,7 +24,7 @@
(io-close ctlf)))
(def (exited)
- (if (< pid 0) (error "no active pid"))
+ (when (< pid 0) (error "no active pid"))
(princ "process " pid " exited\n")
(set! pids (cdr pids))
(set! pid (if pids (car pids) -1))
@@ -45,7 +45,9 @@
(def (startstop) (writectl "startstop") (readnote))
(def (stop) (writectl "stop") (readnote))
-(def (follow addr) (reverse (lsd-follow addr)))
+(def (follow addr)
+ "Returns a list of the next possible executing instructions."
+ (lsd-follow addr))
(def (io-pread f off rest)
(io-seek f off)
@@ -80,7 +82,7 @@
(apply readcore (cons (symbol-addr symbol) rest)))
(let ((bp_init (λ (loc)
- (if (< pid 0) (error "no running process"))
+ (when (< pid 0) (error "no running process"))
(unless (eq? (status) 'Stopped)
(begin (princ "Waiting... " status "\n")
(stop)))
@@ -91,8 +93,8 @@
(else (error "symbol or number"))))))
(set! bpset (λ (loc)
(let ((addr (bp_init loc)))
- (if (has? bptbl addr)
- (error "breakpoint already set at " loc))
+ (when (has? bptbl addr)
+ (error "breakpoint already set at " loc))
(put! bptbl addr (readcore addr 'byte (length bpinst)))
(writecore addr bpinst))))
(set! bpdel (λ (loc)
@@ -127,10 +129,10 @@
`(bpdel (readreg PC))` ; remove breakpoint on current instruction.")
(def (detach)
- (if regsf (io-close regsf))
- (if fpregsf (io-close fpregsf))
- (if coref (io-close coref))
- (if textf (io-close textf))
+ (when regsf (io-close regsf))
+ (when fpregsf (io-close fpregsf))
+ (when coref (io-close coref))
+ (when textf (io-close textf))
(void))
(def (attach)
@@ -143,7 +145,7 @@
(def (new . args)
(let ((v (apply lsd-new args)))
- (if proc-stdin (io-close proc-stdin))
+ (when proc-stdin (io-close proc-stdin))
(set! bptbl (table))
(set! pid (aref v 0))
(set! proc-stdin (aref v 1))
@@ -162,7 +164,7 @@
(set! registers (aref v 1))
(set! bpinst (aref v 2))
(set! globals (make-global :text text :data data)))
- (if (>= pid 0) (attach)))
+ (and (>= pid 0) (attach)))
(def (status)
(let* ((sf (procfile 'status))
@@ -177,7 +179,7 @@
(def _stk (get tracers (os-getenv "objtype")))
-(def (curPC) (if (>= pid 0) (readreg PC)))
+(def (curPC) (and (>= pid 0) (readreg PC)))
(def (step (n 1))
"Step `n` assembly instructions. Returns the next instruction
@@ -186,13 +188,13 @@
(curPC)
(let* ((addr (readreg PC))
(on-bp (has? bptbl addr)))
- (if on-bp (writecore addr (get bptbl addr)))
+ (when on-bp (writecore addr (get bptbl addr)))
(let* ((f (follow addr))
(o (map (λ (a) (readcore a 'byte (length bpinst))) f)))
(for-each (λ (a) (writecore a bpinst)) f)
(startstop)
(map writecore f o)
- (if on-bp (writecore addr bpinst))
+ (when on-bp (writecore addr bpinst))
(step (1- n))))))
(def (cont)
@@ -199,7 +201,7 @@
"Continue program execution. Returns the next instruction
address to be executed or `NIL` if the program has exited."
(let ((addr (readreg PC)))
- (if (has? bptbl addr) (step))
+ (when (has? bptbl addr) (step))
(startstop)
(curPC)))
@@ -213,11 +215,11 @@
(if (<= n 0)
()
(let ((on-bp (has? bptbl addr)))
- (if on-bp (writecore addr (get bptbl addr)))
+ (when on-bp (writecore addr (get bptbl addr)))
(let* ((a (lsd-asm addr))
(next (car a))
(instr (cdr a)))
- (if on-bp (writecore addr bpinst))
+ (when on-bp (writecore addr bpinst))
(cons (cons addr instr) (asmlist (1- n) (+ addr next)))))))
(def (asm (n 5) (addr (readreg PC)))
@@ -257,7 +259,7 @@
(def (at-exit s)
- (if proc-stdin (io-close proc-stdin))
+ (when proc-stdin (io-close proc-stdin))
(detach)
(lsd-cleanup)
(for-each (λ (p) (princ "echo kill > /proc/" p "/ctl\n")) pids))