1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
|
# 05nov25 Software Lab. Alexander Burger
# Prompt
(de *Prompt
(casq (car (symbols)) (pico) (T @)) )
(private) (_who _match nest nst1 nst2 C D E M S X Y Z Fun Prg Who dep1 dep2)
# Edit history
(de h ()
(let F (tmp "history")
(out F
(mapc prinl (history)) )
(and
(vi (cons T F))
(history
(in F
(make (while (line T) (link @))) ) )
T ) ) )
# Browsing
(de help (Sym Ex)
(when (; Sym doc)
(prinl "========================================")
(in @
(from
(pack
"<dt><a id=\""
(replace (chop Sym)
"%" "%25"
"<" "%3C"
">" "%3E"
"\^" "%5E"
"|" "%7C")
"\">" ) )
(out '("w3m" "-T" "text/html" "-dump")
(prin "<dt>")
(echo "</a>")
(echo "<dd>")
(prinl "<br/>")
(echo "\n<pre>")
(ifn Ex
(prinl "<br/><br/>")
(prin "<pre>")
(prinl (echo "\n</pre>")) ) ) ) )
Sym )
(de docs (Dir)
(when (=T (car (info Dir)))
(let All (all)
(for F (dir Dir)
(when (match '("r" "e" "f" @ "." "h" "t" "m" "l") (chop F))
(let P (pack Dir F)
(in P
(while (from "<dt><a id=\"")
(let (L (till "\"") S (ht:Pack L T))
(cond
((member S All) (put (car @) 'doc P))
((and
(not (cddr (setq L (split L "/"))))
(format (cadr L))
(member (pack (car L)) All) )
(put (intern S) 'doc P) ) ) ) ) ) ) ) ) ) ) )
(de doc (Sym Browser)
(call (or Browser (sys "BROWSER") "w3m")
(pack
"file:"
(and (= `(char '/) (char (path "@"))) "//")
(path
(if (get Sym 'doc)
(pack @ "#"
(replace (chop Sym)
"%" "%25"
"<" "%3C"
">" "%3E"
"\^" "%5E"
"|" "%7C" ) )
"@doc/ref.html" ) ) ) ) )
(de more (M Fun)
(let *Dbg NIL
(if (pair M)
((default Fun println) (++ M))
(println (type M))
(setq
Fun (list '(X) (list 'pp 'X (lit M)))
M (mapcar car (filter pair (val M))) ) )
(loop
(T (atom M))
(T (= "\e" (key)) T)
(Fun (++ M)) ) ) )
(de what (S)
(let *Dbg NIL
(setq S (chop S))
(filter
'(("X") (match S (chop "X")))
(all) ) ) )
(de who (X . Prg)
(let (*Dbg NIL Who '(Who @ @@ @@@))
(make (mapc _who (all))) ) )
(and noLint (@ 'who 'Prg))
(de _who (Y)
(unless (or (ext? Y T) (memq Y Who))
(push 'Who Y)
(ifn (= `(char "+") (char Y))
(and (pair (val Y)) (nest @) (link Y))
(for Z (pair (val Y))
(if (atom Z)
(and (_match Z) (link Y))
(when (nest (cdr Z))
(link (cons (car Z) Y)) ) ) )
(maps
'((Z)
(if (atom Z)
(and (_match Z) (link Y))
(when (nest (car Z))
(link (cons (cdr Z) Y)) ) ) )
Y ) ) ) )
(de nest (Y)
(nst1 Y)
(nst2 Y) )
(de nst1 (Y)
(let Z (setq Y (strip Y))
(loop
(T (atom Y) (and (sym? Y) (_who Y)))
(and (sym? (car Y)) (_who (car Y)))
(and (pair (car Y)) (nst1 @))
(T (== Z (setq Y (cdr Y)))) ) ) )
(de nst2 (Y)
(let Z (setq Y (strip Y))
(loop
(T (atom Y) (_match Y))
(T (or (_match (car Y)) (nst2 (car Y)))
T )
(T (== Z (setq Y (cdr Y)))) ) ) )
(de _match (D)
(and
(cond
((str? X) (and (str? D) (= X D)))
((sym? X) (== X D))
(T (match X D)) )
(or
(not Prg)
(let *Dbg (up 2 *Dbg) (run Prg)) ) ) )
(de has (X)
(let *Dbg NIL
(filter
'((S) (= X (val S)))
(all) ) ) )
(de can (X)
(let *Dbg NIL
(extract
'((Y)
(and
(= `(char "+") (char Y))
(asoq X (val Y))
(cons X Y) ) )
(all) ) ) )
(private) (Flg Nsp Lst Sym N L S)
# Namespaces nested in current search order
(de namespaces (Flg)
(let N 3
(make
(for Nsp (symbols)
(recur (Nsp N)
(link Nsp)
(when Flg
(space N)
(println Nsp) )
(for S (all Nsp)
(and
(pair (val S))
(== '\~ (car @))
(not (memq S (made)))
(recurse S (+ N 3)) ) ) ) ) ) ) )
# Namespace shadowing
(de shadows (Flg)
(let Lst (mapcan all (symbols))
(make
(while (cdr Lst)
(let Sym (++ Lst)
(unless (member Sym (made))
(let? L
(filter
'((S)
(and
(= S Sym)
(n== S Sym)
(val S) ) )
Lst )
(when Flg
(space 3)
(apply println L Sym) )
(link Sym) ) ) ) ) ) ) )
# Class dependencies
(de dep (C)
(let *Dbg NIL
(dep1 0 C)
(dep2 3 C)
C ) )
(de dep1 (N C)
(for X (type C)
(dep1 (+ 3 N) X) )
(space N)
(println C) )
(de dep2 (N C)
(for X (all)
(when
(and
(= `(char "+") (char X))
(memq C (type X)) )
(space N)
(println X)
(dep2 (+ 3 N) X) ) ) )
# Inherited methods
(de methods (Obj)
(make
(let Mark NIL
(recur (Obj)
(for X (val Obj)
(nond
((pair X) (recurse X))
((memq (car X) Mark)
(link (cons (car X) Obj))
(push 'Mark (car X)) ) ) ) ) ) ) )
(private) (_dbg _dbg2 dbg ubg traced? U)
# Single-Stepping
(de _dbg (Lst)
(or
(atom (car Lst))
(num? (caar Lst))
(flg? (caar Lst))
(== '! (caar Lst))
(set Lst (cons '! (car Lst))) ) )
(de _dbg2 (Lst)
(map
'((L)
(if (and (pair (car L)) (flg? (caar L)))
(map _dbg (cdar L))
(_dbg L) ) )
Lst ) )
(de dbg (Lst)
(when (pair Lst)
(casq (++ Lst)
((case casq state)
(_dbg Lst)
(for L (cdr Lst)
(map _dbg (cdr L)) ) )
((cond nond)
(for L Lst
(map _dbg L) ) )
(quote
(when (fun? Lst)
(map _dbg (cdr Lst)) ) )
((job use let let? recur)
(map _dbg (cdr Lst)) )
(loop
(_dbg2 Lst) )
((bind do)
(_dbg Lst)
(_dbg2 (cdr Lst)) )
(for
(and (pair (car Lst)) (map _dbg (cdar Lst)))
(_dbg2 (cdr Lst)) )
(T (map _dbg Lst)) )
T ) )
(de d ()
(let *Dbg NIL
(dbg ^) ) )
(de -debug ()
(debug (intern (opt))) )
(de debug (X C)
(ifn (traced? X C)
(let *Dbg NIL
(when (pair X)
(setq C (cdr X) X (car X)) )
(or
(dbg (if C (method X C) (getd X)))
(quit "Can't debug" X) ) )
(untrace X C)
(debug X C)
(trace X C) ) )
(de ubg (Lst)
(when (pair Lst)
(map
'((L)
(when (pair (car L))
(when (== '! (caar L))
(set L (cdar L)) )
(ubg (car L)) ) )
Lst )
T ) )
(de u ()
(let *Dbg NIL
(ubg ^) ) )
(de unbug (X C)
(let *Dbg NIL
(when (pair X)
(setq C (cdr X) X (car X)) )
(or
(ubg (if C (method X C) (getd X)))
(quit "Can't unbug" X) ) ) )
# Tracing
(de traced? (X C)
(setq X
(if C
(method X C)
(getd X) ) )
(and
(pair X)
(pair (cadr X))
(== '$ (caadr X)) ) )
# Convert ((X Y) A B) --> ((X Y) ($ foo (X Y) A B))
(de -trace ()
(trace (intern (opt))) )
(de trace (X C)
(let *Dbg NIL
(when (pair X)
(setq C (cdr X) X (car X)) )
(if C
(unless (traced? X C)
(or (method X C) (quit "Can't trace" X))
(con @
(cons
(conc
(list '$ (cons X C) (car @))
(cdr @) ) ) ) )
(unless (traced? X)
(and (sym? (getd X)) (quit "Can't trace" X))
(and (num? (getd X)) (expr X))
(set X
(list
(car (getd X))
(conc (list '$ X) (getd X)) ) ) ) )
X ) )
# Convert ((X Y) ($ foo (X Y) A B)) --> ((X Y) A B)
(de untrace (X C)
(let *Dbg NIL
(when (pair X)
(setq C (cdr X) X (car X)) )
(if C
(when (traced? X C)
(con
(method X C)
(cdddr (cadr (method X C))) ) )
(when (traced? X)
(let Y (set X (cddadr (getd X)))
(and
(== '@ (++ Y))
(=1 (length Y))
(= 2 (length (car Y)))
(== 'pass (caar Y))
(sym? (cdadr Y))
(subr X) ) ) ) )
X ) )
(de *NoTrace
@ @@ @@@
pp show more led
what who can dep d e debug u unbug trace untrace )
(de traceAll (Excl)
(let *Dbg NIL
(for X (all)
(or
(memq X Excl)
(memq X *NoTrace)
(= `(char "*") (char X))
(cond
((= `(char "+") (char X))
(mapc trace
(extract
'((Y)
(and
(pair Y)
(fun? (cdr Y))
(cons (car Y) X) ) )
(val X) ) ) )
((pair (getd X))
(trace X) ) ) ) ) ) )
# Process Listing
(when (member *OS '("Android" "Linux"))
(de proc @
(apply call
(make (while (args) (link "-C" (next))))
"ps" "-H" "-o" "pid,ppid,start,size,pcpu,stat,cmd" ) ) )
# Benchmarking
(de bench Prg
(let U (usec)
(prog1
(run Prg 1)
(out 2
(when (>= (setq U (- (usec) U)) 60000000)
(prin "[" (tim$ (/ U 1000000)) "] ") )
(prinl (format (/ U 1000) 3) " sec") ) ) ) )
# Backtrace
(de bt (Flg)
(let (S NIL *Dbg)
(for (L (trail T) L)
(if (pair (car L))
(let E (++ L)
(push 'S
(list
(if (getd (box? (car E)))
(cons @ (cdr E))
E ) ) ) )
(conc
(car (default S (cons (cons))))
(cons (cons (++ L) (++ L))) ) )
(T (== '^ (car L)))
(T
(and
(pair (car L))
(== 'bt (caar L)) ) ) )
(for L S
(let? X (++ L)
(pretty
(cons
(or
(and (sym? (car X)) (car X))
(find
'((S) (== (car X) (val S)))
(all) )
(car X) )
(less (cdr X)) ) ) )
(prinl)
(while L
(space 3)
(println (caar L) (less (cdr (++ L)))) )
(NIL (or Flg (<> "\e" (key))) T) ) ) )
# Source code
`(info "@lib/map")
(symbols 'llvm 'pico)
(in "@lib/map"
(while (read)
(let Sym @
(if (get Sym '*Dbg)
(set @ (read))
(put Sym '*Dbg (cons (read))) ) ) ) )
|