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
|
# 07apr12abu
# (c) Software Lab. Alexander Burger
(de task (Key . Prg)
(nond
(Prg (del (assoc Key *Run) '*Run))
((num? Key) (quit "Bad Key" Key))
((assoc Key *Run)
(push '*Run
(conc
(make
(when (lt0 (link Key))
(link (+ (eval (pop 'Prg) 1))) ) )
(ifn (sym? (car Prg))
Prg
(cons
(cons 'job
(cons
(lit
(make
(while (atom (car Prg))
(link
(cons (pop 'Prg) (eval (pop 'Prg) 1)) ) ) ) )
Prg ) ) ) ) ) ) )
(NIL (quit "Key conflict" Key)) ) )
(de forked ()
(let N (caar *Run)
(when (gt0 N)
(push '*Fork (list 'close N)) )
(push '*Fork (list 'task N)) ) )
(de timeout (N)
(if2 N (assoc -1 *Run)
(set (cdr @) (+ N))
(push '*Run (list -1 (+ N) '(bye)))
(del @ '*Run) ) )
(de abort ("N" . "Prg")
(catch 'abort
(alarm "N" (throw 'abort))
(finally (alarm 0) (run "Prg")) ) )
(de macro "Prg"
(run (fill "Prg")) )
(de later ("@Var" . "@Prg")
(macro
(task (pipe (pr (prog . "@Prg")))
(setq "@Var" (in @ (rd)))
(task (close @)) ) )
"@Var" )
(de recur recurse
(run (cdr recurse)) )
(de curry "Z"
(let ("X" (pop '"Z") "Y" (pop '"Z") "P" (filter pat? "X"))
(if2 "P" (diff "X" "P")
(list "Y" (cons 'job (lit (env @)) (fill "Z" "P")))
(cons "Y" (fill "Z" "P"))
(list "Y" (cons 'job (lit (env @)) "Z"))
(cons "Y" "Z") ) ) )
(====)
### Definitions ###
(de expr ("F")
(set "F"
(list '@ (list 'pass (box (getd "F")))) ) )
(de subr ("F")
(set "F"
(getd (cadr (cadr (getd "F")))) ) )
(de undef ("X" "C")
(when (pair "X")
(setq "C" (cdr "X") "X" (car "X")) )
(ifn "C"
(prog1 (val "X") (set "X"))
(prog1
(cdr (asoq "X" (val "C")))
(set "C"
(delq (asoq "X" (val "C")) (val "C")) ) ) ) )
(de redef "Lst"
(let ("Old" (car "Lst") "New" (name "Old"))
(set
"New" (getd "Old")
"Old" "New"
"Old" (fill (cdr "Lst") "Old") )
"New" ) )
(de daemon ("X" . Prg)
(prog1
(nond
((pair "X")
(or (pair (getd "X")) (expr "X")) )
((pair (cdr "X"))
(method (car "X") (cdr "X")) )
(NIL
(method (car "X") (get (or (cddr "X") *Class) (cadr "X"))) ) )
(con @ (append Prg (cdr @))) ) )
(de patch ("Lst" "Pat" . "Prg")
(bind (fish pat? "Pat")
(recur ("Lst")
(loop
(cond
((match "Pat" (car "Lst"))
(set "Lst" (run "Prg")) )
((pair (car "Lst"))
(recurse @) ) )
(NIL (cdr "Lst"))
(T (atom (cdr "Lst"))
(when (match "Pat" (cdr "Lst"))
(con "Lst" (run "Prg")) ) )
(setq "Lst" (cdr "Lst")) ) ) ) )
(====)
(de cache ("Var" "Str" . Prg)
(nond
((setq "Var" (car (idx "Var" "Str" T)))
(set "Str" "Str" "Str" (run Prg 1)) )
((n== "Var" (val "Var"))
(set "Var" (run Prg 1)) )
(NIL (val "Var")) ) )
(====)
### I/O ###
(de tab (Lst . @)
(for N Lst
(let V (next)
(and (gt0 N) (space (- N (length V))))
(prin V)
(and (lt0 N) (args) (space (- 0 N (length V)))) ) )
(prinl) )
(de beep ()
(prin "^G") )
(de msg (X . @)
(out 2
(print X)
(pass prinl)
(flush) )
X )
(de script (File . @)
(load File) )
(de once Prg
(unless (idx '*Once (file) T)
(run Prg 1) ) )
(de pil @
(when (== "Pil" '"Pil")
(call 'mkdir "-p" (setq "Pil" `(pack (sys "HOME") "/.pil/"))) )
(pass pack "Pil") )
(de rc (File Key . @)
(ctl File
(let Lst (in File (read))
(ifn (args)
(cdr (assoc Key Lst))
(let Val (next)
(if (assoc Key Lst)
(con @ Val)
(push 'Lst (cons Key Val)) )
(protect
(out File (println Lst)) )
Val ) ) ) ) )
(de acquire (File)
(ctl File
(let P (in File (rd))
(or
(= P *Pid)
(unless (and P (kill P 0))
(out File (pr *Pid)) ) ) ) ) )
(de release (File)
(ctl File (out File)) )
# Temporary Files
(de tmp @
(unless *Tmp
(push '*Bye '(call 'rm "-r" *Tmp))
(push '*Fork '(off *Tmp) '(del '(call 'rm "-r" *Tmp) '*Bye))
(call 'mkdir "-p" (setq *Tmp (pil "tmp/" *Pid "/"))) )
(pass pack *Tmp) )
### List ###
(de insert (N Lst X)
(conc
(cut (dec N) 'Lst)
(cons X)
Lst ) )
(de remove (N Lst)
(conc
(cut (dec N) 'Lst)
(cdr Lst) ) )
(de place (N Lst X)
(conc
(cut (dec N) 'Lst)
(cons X)
(cdr Lst) ) )
(de uniq (Lst)
(let R NIL
(filter
'((X) (not (idx 'R X T)))
Lst ) ) )
(de group (Lst)
(make
(for X Lst
(if (assoc (car X) (made))
(conc @ (cons (cdr X)))
(link (list (car X) (cdr X))) ) ) ) )
### Symbol ###
(de qsym "Sym"
(cons (val "Sym") (getl "Sym")) )
(de loc (S X)
(if (and (str? X) (= S X))
X
(and
(pair X)
(or
(loc S (car X))
(loc S (cdr X)) ) ) ) )
(de local Lst
(mapc zap Lst) )
(de import Lst
(for Sym Lst
(unless (== Sym (intern Sym))
(quit "Import conflict" Sym) ) ) )
### OOP ###
(de class Lst
(let L (val (setq *Class (car Lst)))
(def *Class
(recur (L)
(if (atom (car L))
(cdr Lst)
(cons (car L) (recurse (cdr L))) ) ) ) ) )
(de object ("Sym" "Val" . @)
(putl "Sym")
(def "Sym" "Val")
(while (args)
(put "Sym" (next) (next)) )
"Sym" )
(de extend X
(setq *Class (car X)) )
# Class variables
(de var X
(if (pair (car X))
(put (cdar X) (caar X) (cdr X))
(put *Class (car X) (cdr X)) ) )
(de var: X
(apply meta X This) )
### Math ###
(de scl ("N" . "Prg")
(if "Prg"
(let *Scl "N" (run "Prg"))
(setq *Scl "N") ) )
(de sqrt (N F)
(cond
((lt0 N) (quit "Bad argument" N))
(N
(let (A 1 B 0)
(while (>= N A)
(setq A (>> -2 A)) )
(loop
(if (> (inc 'B A) N)
(dec 'B A)
(dec 'N B)
(inc 'B A) )
(setq B (>> 1 B) A (>> 2 A))
(T (=0 A)) )
(and F (> N B) (inc 'B))
B ) ) ) )
# (Knuth Vol.2, p.442)
(de ** (X N) # N th power of X
(let Y 1
(loop
(when (bit? 1 N)
(setq Y (* Y X)) )
(T (=0 (setq N (>> 1 N)))
Y )
(setq X (* X X)) ) ) )
(de accu (Var Key Val)
(when Val
(if (assoc Key (val Var))
(con @ (+ Val (cdr @)))
(push Var (cons Key Val)) ) ) )
### Pretty Printing ###
(de *PP
T NIL if ifn when unless while until do case state for
with catch finally co ! setq default push job use let let?
prog1 later recur redef =: in out ctl tab new )
(de *PP1 let let? for redef)
(de *PP2 setq default)
(de *PP3 if2)
(de pretty (X N . @)
(setq N (abs (space (or N 0))))
(while (args)
(printsp (next)) )
(if (or (atom X) (>= 12 (size X)))
(print X)
(while (== 'quote (car X))
(prin "'")
(pop 'X) )
(let Z X
(prin "(")
(cond
((memq (print (pop 'X)) *PP)
(cond
((memq (car Z) *PP1)
(if (and (pair (car X)) (pair (cdar X)))
(when (>= 12 (size (car X)))
(space)
(print (pop 'X)) )
(space)
(print (pop 'X))
(when (or (atom (car X)) (>= 12 (size (car X))))
(space)
(print (pop 'X)) ) ) )
((memq (car Z) *PP2)
(inc 'N 3)
(loop
(prinl)
(pretty (cadr X) N (car X))
(NIL (setq X (cddr X)) (space)) ) )
((or (atom (car X)) (>= 12 (size (car X))))
(space)
(print (pop 'X)) ) ) )
((and (memq (car Z) *PP3) (>= 12 (size (head 2 X))))
(space)
(print (pop 'X) (pop 'X)) ) )
(when X
(loop
(T (== Z X) (prin " ."))
(T (atom X) (prin " . ") (print X))
(prinl)
(pretty (pop 'X) (+ 3 N))
(NIL X) )
(space) )
(prin ")") ) ) )
(de pp ("X" C)
(let *Dbg NIL
(and (pair "X") (setq C (cdr "X")))
(prin "(")
(printsp (if C 'dm 'de))
(prog1 (printsp "X")
(setq "X"
(if C
(method (if (pair "X") (car "X") "X") C)
(val "X") ) )
(cond
((atom "X") (prin ". ") (print "X"))
((atom (cdr "X"))
(ifn (cdr "X")
(print (car "X"))
(print (car "X"))
(prin " . ")
(print @) ) )
(T
(let Z "X"
(print (pop '"X"))
(loop
(T (== Z "X") (prin " ."))
(NIL "X")
(T (atom "X")
(prin " . ")
(print "X") )
(prinl)
(pretty (pop '"X") 3) )
(space) ) ) )
(prinl ")") ) ) )
(de show ("X" . @)
(let *Dbg NIL
(setq "X" (pass get "X"))
(when (sym? "X")
(print "X" (val "X"))
(prinl)
(maps
'((X)
(space 3)
(if (atom X)
(println X)
(println (cdr X) (car X)) ) )
"X" ) )
"X" ) )
(de view (X Y)
(let *Dbg NIL
(if (=T Y)
(let N 0
(recur (N X)
(when X
(recurse (+ 3 N) (cddr X))
(space N)
(println (car X))
(recurse (+ 3 N) (cadr X)) ) ) )
(let Z X
(loop
(T (atom X) (println X))
(if (atom (car X))
(println '+-- (pop 'X))
(print '+---)
(view
(pop 'X)
(append Y (cons (if X "| " " "))) ) )
(NIL X)
(mapc prin Y)
(T (== Z X) (println '*))
(println '|)
(mapc prin Y) ) ) ) ) )
### Check ###
# Assertions
(de assert Prg
(when *Dbg
(cons
(list 'unless
(if (cdr Prg) (cons 'and Prg) (car Prg))
(list 'quit "'assert' failed" (lit (car Prg))) ) ) ) )
# Unit tests
(de test (Pat . Prg)
(bind (fish pat? Pat)
(unless (match Pat (run Prg 1))
(msg Prg)
(quit "'test' failed" Pat) ) ) )
### Debug ###
`*Dbg
(load "@lib/debug.l" "@lib/led.l" "@lib/edit.l" "@lib/lint.l")
(noLint 'later (loc "@Prg" later))
# vi:et:ts=3:sw=3
|