| 1 |
(load "string_stuff.lisp") |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
;;;;;;;;;;;;;;;;;;; |
|---|
| 5 |
;; build a test where the outer term is "known" |
|---|
| 6 |
;; |
|---|
| 7 |
;; base string |
|---|
| 8 |
;; side expression |
|---|
| 9 |
(defun Test_Know_Type_Is (Root Type) |
|---|
| 10 |
; (print (list 'type_K type)) |
|---|
| 11 |
; (if (atom type) t (print (list 'type_K 'op (car type)))) |
|---|
| 12 |
|
|---|
| 13 |
(concatenate 'string |
|---|
| 14 |
(if (listp type) |
|---|
| 15 |
(concatenate 'string "is(" root ".Op == " (Op_name (car type)) ") && ") |
|---|
| 16 |
"" |
|---|
| 17 |
) |
|---|
| 18 |
(if |
|---|
| 19 |
(atom type) |
|---|
| 20 |
(concatenate 'string "is(" root ".DefP == Defined)") |
|---|
| 21 |
(concatenate 'string |
|---|
| 22 |
(Test_Know_Type_Is (concatenate 'string root ".LHS") (cadr type)) |
|---|
| 23 |
" && " |
|---|
| 24 |
(let ((op (car type))) |
|---|
| 25 |
(cond |
|---|
| 26 |
( |
|---|
| 27 |
(or |
|---|
| 28 |
(equal op '+) |
|---|
| 29 |
(equal op '-) |
|---|
| 30 |
(equal op '*) |
|---|
| 31 |
(equal op '/) |
|---|
| 32 |
) |
|---|
| 33 |
(Test_Know_Type_Is (concatenate 'string root ".RHS") (caddr type)) |
|---|
| 34 |
) |
|---|
| 35 |
( |
|---|
| 36 |
(or |
|---|
| 37 |
(equal op '+>) |
|---|
| 38 |
(equal op '->) |
|---|
| 39 |
(equal op '*>) |
|---|
| 40 |
(equal op '/>) |
|---|
| 41 |
(equal op '-r>) |
|---|
| 42 |
(equal op '/r>) |
|---|
| 43 |
) |
|---|
| 44 |
(Test_UnKnow_Type_Is (concatenate 'string root ".RHS") (caddr type)) |
|---|
| 45 |
) |
|---|
| 46 |
( t (format nil "%%UnKnown(~s)%%" type)) |
|---|
| 47 |
) |
|---|
| 48 |
) |
|---|
| 49 |
) |
|---|
| 50 |
) |
|---|
| 51 |
) |
|---|
| 52 |
) |
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
;;;;;;;;;;;;;;;;;;; |
|---|
| 59 |
;; build a test where the outer term is "unknown" |
|---|
| 60 |
;; |
|---|
| 61 |
;; base string |
|---|
| 62 |
;; side expression |
|---|
| 63 |
(defun Test_unKnow_Type_Is (Root Type) |
|---|
| 64 |
; (print (list 'type_U type)) |
|---|
| 65 |
; (if (atom type) t (print (list 'type_U 'op (car type)))) |
|---|
| 66 |
|
|---|
| 67 |
(concatenate 'string |
|---|
| 68 |
(if (listp type) |
|---|
| 69 |
(concatenate 'string "is(" root ".Op == " (Op_name (car type)) ") && ") |
|---|
| 70 |
"" |
|---|
| 71 |
) |
|---|
| 72 |
(if |
|---|
| 73 |
(atom type) |
|---|
| 74 |
(concatenate 'string "is(" root ".DefP == UnDefined)") |
|---|
| 75 |
(concatenate 'string |
|---|
| 76 |
(Test_Know_Type_Is (concatenate 'string root ".LHS") (cadr type)) |
|---|
| 77 |
" && " |
|---|
| 78 |
(let ((op (car type))) |
|---|
| 79 |
(cond |
|---|
| 80 |
( |
|---|
| 81 |
(or |
|---|
| 82 |
(equal op '+) |
|---|
| 83 |
(equal op '-) |
|---|
| 84 |
(equal op '*) |
|---|
| 85 |
(equal op '/) |
|---|
| 86 |
) |
|---|
| 87 |
(Test_Know_Type_Is (concatenate 'string root ".RHS") (caddr type)) |
|---|
| 88 |
) |
|---|
| 89 |
( |
|---|
| 90 |
(or |
|---|
| 91 |
(equal op '+>) |
|---|
| 92 |
(equal op '->) |
|---|
| 93 |
(equal op '*>) |
|---|
| 94 |
(equal op '/>) |
|---|
| 95 |
(equal op '-r>) |
|---|
| 96 |
(equal op '/r>) |
|---|
| 97 |
) |
|---|
| 98 |
(Test_UnKnow_Type_Is (concatenate 'string root ".RHS") (caddr type)) |
|---|
| 99 |
) |
|---|
| 100 |
( t (format nil "%%UnKnown(~s)%%" type)) |
|---|
| 101 |
) |
|---|
| 102 |
) |
|---|
| 103 |
) |
|---|
| 104 |
) |
|---|
| 105 |
) |
|---|
| 106 |
) |
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 |
|
|---|
| 112 |
;;;;;;;;;;;;;;;;;;;;;; |
|---|
| 113 |
;; test for existance of an atom in a list |
|---|
| 114 |
;; |
|---|
| 115 |
(defun has (v l) |
|---|
| 116 |
(if (equal l nil) |
|---|
| 117 |
nil |
|---|
| 118 |
(if (atom l) |
|---|
| 119 |
(equal v l) |
|---|
| 120 |
(or |
|---|
| 121 |
(equal v (car l)) |
|---|
| 122 |
(has v (cdr l)) |
|---|
| 123 |
) |
|---|
| 124 |
) |
|---|
| 125 |
) |
|---|
| 126 |
) |
|---|
| 127 |
#| |
|---|
| 128 |
"==== has ut ====" |
|---|
| 129 |
'( nil t nil t nil t) |
|---|
| 130 |
(list (has 'a 'b) (has 'a 'a) (has 'a '(b)) (has 'a '(a)) (has 'a '(b c)) (has 'a '(b a)) ) |
|---|
| 131 |
|# |
|---|
| 132 |
|
|---|
| 133 |
|
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
|---|
| 137 |
;; The list of implicity undefined terms |
|---|
| 138 |
;; |
|---|
| 139 |
(defconstant undefineds_set '(x)) |
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 142 |
|
|---|
| 143 |
|
|---|
| 144 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
|---|
| 145 |
;; Root case for type test |
|---|
| 146 |
;; |
|---|
| 147 |
;; base string |
|---|
| 148 |
;; side expression |
|---|
| 149 |
(defun Test_Type_Is_h1 (base side) |
|---|
| 150 |
; (print (list 'side_h1 side)) |
|---|
| 151 |
; (if (atom side) t (print (list 'side_h1 'LHS (cadr side)))) |
|---|
| 152 |
; (if (atom side) t (print (list 'side_h1 'RHS (caddr side)))) |
|---|
| 153 |
(if (atom side) |
|---|
| 154 |
;; for the atom as root case, test if this ID is defined as undefined |
|---|
| 155 |
(concatenate 'string "is(" base ".DefP == " (if (has side undefineds_set) "UnDefined" "Defined") ")") |
|---|
| 156 |
;; the compound root case |
|---|
| 157 |
(concatenate 'string |
|---|
| 158 |
;; test the type og op |
|---|
| 159 |
"is(" base ".Op == " (Op_name (car side)) ") && " |
|---|
| 160 |
;; LHS is always known ************************* |
|---|
| 161 |
(Test_Know_Type_Is (concatenate 'string base ".LHS") (cadr side)) |
|---|
| 162 |
" && " |
|---|
| 163 |
;; RHS might not be known |
|---|
| 164 |
(cond |
|---|
| 165 |
( |
|---|
| 166 |
(or |
|---|
| 167 |
(equal (cadr side) '+) |
|---|
| 168 |
(equal (cadr side) '-) |
|---|
| 169 |
(equal (cadr side) '*) |
|---|
| 170 |
(equal (cadr side) '/) |
|---|
| 171 |
) |
|---|
| 172 |
(Test_Know_Type_Is (concatenate 'string base ".RHS") (caddr side)) |
|---|
| 173 |
) |
|---|
| 174 |
( |
|---|
| 175 |
(or |
|---|
| 176 |
(equal (car side) '+>) |
|---|
| 177 |
(equal (car side) '->) |
|---|
| 178 |
(equal (car side) '*>) |
|---|
| 179 |
(equal (car side) '/>) |
|---|
| 180 |
(equal (car side) '-r>) |
|---|
| 181 |
(equal (car side) '/r>) |
|---|
| 182 |
) |
|---|
| 183 |
(Test_UnKnow_Type_Is (concatenate 'string base ".RHS") (caddr side)) |
|---|
| 184 |
) |
|---|
| 185 |
( t "%%UnKnown%%") |
|---|
| 186 |
) |
|---|
| 187 |
) |
|---|
| 188 |
) |
|---|
| 189 |
) |
|---|
| 190 |
|
|---|
| 191 |
|
|---|
| 192 |
|
|---|
| 193 |
;;;;;;;;;;;;;;;;;; |
|---|
| 194 |
;; build a test to match a type pettern |
|---|
| 195 |
;; |
|---|
| 196 |
(defun Test_Type_Is (Type) |
|---|
| 197 |
; (print (list 'Test_Type_Is Type)) |
|---|
| 198 |
; (print (list 'Test_Type_Is 'lhs (cadr Type))) |
|---|
| 199 |
; (print (list 'Test_Type_Is 'rhs (caddr Type))) |
|---|
| 200 |
(if (atom type) |
|---|
| 201 |
"/+ This is somewhat erroneous +/" |
|---|
| 202 |
(concatenate 'string |
|---|
| 203 |
nl |
|---|
| 204 |
" " |
|---|
| 205 |
(Test_Type_Is_h1 "T" (cadr type)) |
|---|
| 206 |
" &&" nl |
|---|
| 207 |
" " |
|---|
| 208 |
(Test_Type_Is_h1 "V" (caddr type)) |
|---|
| 209 |
) |
|---|
| 210 |
) |
|---|
| 211 |
) |
|---|
| 212 |
|
|---|
| 213 |
#| |
|---|
| 214 |
"===UNITTEST===" |
|---|
| 215 |
(and |
|---|
| 216 |
(print (Test_Type_Is '(+ a b))) |
|---|
| 217 |
(print (Test_Type_Is '(+ (-> a c) b))) |
|---|
| 218 |
'() |
|---|
| 219 |
) |
|---|
| 220 |
|# |
|---|
| 221 |
|
|---|
| 222 |
|
|---|
| 223 |
|
|---|
| 224 |
;;;;;;;;;;;;;;;;;;;;;;;; |
|---|
| 225 |
;; Find an instance of Exp in Source |
|---|
| 226 |
;; |
|---|
| 227 |
;; Exp the thing to find |
|---|
| 228 |
;; Source the expression to search |
|---|
| 229 |
;; LHS/RHS the strings to add for the LHS and RHS branches |
|---|
| 230 |
(defun Find_In (Exp Source LHS RHS) |
|---|
| 231 |
(cond |
|---|
| 232 |
((equal Exp Source) "") |
|---|
| 233 |
((not (listp Source)) nil) |
|---|
| 234 |
( |
|---|
| 235 |
t |
|---|
| 236 |
; (print `(source ,source Exp ,Exp)) |
|---|
| 237 |
(let ((ret-l (Find_In Exp (cadr Source) ".LHS" ".RHS"))) |
|---|
| 238 |
(if ret-l |
|---|
| 239 |
(concatenate 'string LHS ret-l) |
|---|
| 240 |
(let ((ret-r (Find_In Exp (caddr Source) ".LHS" ".RHS"))) |
|---|
| 241 |
(if ret-r |
|---|
| 242 |
(concatenate 'string RHS ret-r) |
|---|
| 243 |
'nil |
|---|
| 244 |
) |
|---|
| 245 |
) |
|---|
| 246 |
) |
|---|
| 247 |
) |
|---|
| 248 |
) |
|---|
| 249 |
) |
|---|
| 250 |
) |
|---|
| 251 |
#| |
|---|
| 252 |
(find_in 'a 'a "L" "R") |
|---|
| 253 |
(find_in 'a '(+ a b) "L" "R") |
|---|
| 254 |
(find_in 'a '(+ (/ c a) b) "L" "R") |
|---|
| 255 |
(find_in 'a '(+ (/ c (* a d)) b) "L" "R") |
|---|
| 256 |
|# |
|---|
| 257 |
|
|---|
| 258 |
|
|---|
| 259 |
;;;;;;;;;;;;;;;;;;;; |
|---|
| 260 |
;; find name of all instance of Exp |
|---|
| 261 |
(defun Find_In_all (Exp Source) |
|---|
| 262 |
(cond |
|---|
| 263 |
((not (listp Source)) nil) |
|---|
| 264 |
(t |
|---|
| 265 |
(append |
|---|
| 266 |
(Find_In_all_ Exp (cadr Source) "T") |
|---|
| 267 |
(Find_In_all_ Exp (caddr Source) "V") |
|---|
| 268 |
) |
|---|
| 269 |
) |
|---|
| 270 |
) |
|---|
| 271 |
) |
|---|
| 272 |
#| |
|---|
| 273 |
(find_in_all 'a 'a "B") |
|---|
| 274 |
(find_in_all 'a '(+ a b) "B") |
|---|
| 275 |
(find_in_all 'a '(+ (/ c a) b) "B") |
|---|
| 276 |
(find_in_all 'a '(+ (/ c (* a d)) b) "B") |
|---|
| 277 |
(find_in_all 'c '(+ (/ c (* a (-> d c))) (- b c)) "B") |
|---|
| 278 |
(Find_In_all 'c '(+ (+> a c) (+> b c)) "R") |
|---|
| 279 |
|# |
|---|
| 280 |
|
|---|
| 281 |
;;;;;;;;;;;;;;;;;;; |
|---|
| 282 |
;; helper for Find_In_all |
|---|
| 283 |
(defun Find_In_all_ (Exp Source Path) |
|---|
| 284 |
(cond |
|---|
| 285 |
((equal Exp Source) (list (concatenate 'string path ""))) |
|---|
| 286 |
((not (listp Source)) nil) |
|---|
| 287 |
(t |
|---|
| 288 |
(append |
|---|
| 289 |
(Find_In_all_ Exp (cadr Source) (concatenate 'string path ".LHS")) |
|---|
| 290 |
(Find_In_all_ Exp (caddr Source) (concatenate 'string path ".RHS")) |
|---|
| 291 |
) |
|---|
| 292 |
) |
|---|
| 293 |
) |
|---|
| 294 |
) |
|---|
| 295 |
|
|---|
| 296 |
|
|---|
| 297 |
|
|---|
| 298 |
|
|---|
| 299 |
;;;;;;;;;;;;;;;;;;; |
|---|
| 300 |
;; merge two lists, droping duplicates |
|---|
| 301 |
(defun join (L1 L2) |
|---|
| 302 |
(cond |
|---|
| 303 |
((equal nil L1) L2) |
|---|
| 304 |
((equal nil L2) L1) |
|---|
| 305 |
((equal (car L1) (car L2)) (join (cdr L1) L2)) |
|---|
| 306 |
(t |
|---|
| 307 |
(join |
|---|
| 308 |
(cdr L1) |
|---|
| 309 |
(cons (car L2) (join `(,(car L1)) (cdr L2))) |
|---|
| 310 |
) |
|---|
| 311 |
) |
|---|
| 312 |
) |
|---|
| 313 |
) |
|---|
| 314 |
|
|---|
| 315 |
#| |
|---|
| 316 |
(join '(b c) '()) |
|---|
| 317 |
(join '() '(b c)) |
|---|
| 318 |
(join '(a) '(b c)) |
|---|
| 319 |
(join '(a b) '(b c)) |
|---|
| 320 |
(join '(b a) '(b c)) |
|---|
| 321 |
(join '(b a) '(c b)) |
|---|
| 322 |
(join '(a b) '(c b)) |
|---|
| 323 |
(join '(a b j i k l) '(c b d e f g h)) |
|---|
| 324 |
|# |
|---|
| 325 |
|
|---|
| 326 |
;;;;;;;;;;;;;;;;;;;;; |
|---|
| 327 |
;; get list of used IDs in an expression |
|---|
| 328 |
;; |
|---|
| 329 |
(defun used_ids (Exp) |
|---|
| 330 |
(if (atom Exp) |
|---|
| 331 |
`(,Exp) |
|---|
| 332 |
(Join |
|---|
| 333 |
(used_ids (cadr exp)) |
|---|
| 334 |
(used_ids (caddr exp)) |
|---|
| 335 |
) |
|---|
| 336 |
) |
|---|
| 337 |
) |
|---|
| 338 |
;(used_ids '(+ (+> a c) (+> b c))) |
|---|
| 339 |
|
|---|
| 340 |
|
|---|
| 341 |
;;;;;;;;;;;;;;;;;;;; |
|---|
| 342 |
;; extract duplicate terms in an pattern expression |
|---|
| 343 |
;; |
|---|
| 344 |
(defun reuse (exp) |
|---|
| 345 |
(let ((ret (reuse_h1 exp (used_ids exp)))) |
|---|
| 346 |
(if (equal ret "") " /+ no repeats +/" ret) |
|---|
| 347 |
) |
|---|
| 348 |
) |
|---|
| 349 |
|
|---|
| 350 |
;;;;;;;;;;;;;;;;;;;; |
|---|
| 351 |
;; string together same-type checks |
|---|
| 352 |
;; |
|---|
| 353 |
(defun reuse_h1 (exp names) |
|---|
| 354 |
; (print `(this ,names ,(car names) ,(cdr names))) |
|---|
| 355 |
(if (equal nil names) |
|---|
| 356 |
"" |
|---|
| 357 |
(concatenate |
|---|
| 358 |
'string |
|---|
| 359 |
(reuse_h2 (Find_In_all (car names) exp)) |
|---|
| 360 |
(reuse_h1 exp (cdr names)) |
|---|
| 361 |
) |
|---|
| 362 |
) |
|---|
| 363 |
) |
|---|
| 364 |
|
|---|
| 365 |
;;;;;;;;;;;;;;;;;;;; |
|---|
| 366 |
;; single instance same-type check |
|---|
| 367 |
;; |
|---|
| 368 |
;; test first two members of names |
|---|
| 369 |
(defun reuse_h2 (names) |
|---|
| 370 |
(if (or (equal nil names) (equal nil (cdr names))) |
|---|
| 371 |
"" |
|---|
| 372 |
(concatenate |
|---|
| 373 |
'string |
|---|
| 374 |
" && is(" |
|---|
| 375 |
(car names) |
|---|
| 376 |
" == " |
|---|
| 377 |
(cadr names) |
|---|
| 378 |
")" |
|---|
| 379 |
) |
|---|
| 380 |
) |
|---|
| 381 |
) |
|---|
| 382 |
|
|---|
| 383 |
;(reuse_h1 '(+ (+> a c) (+> b c)) (used_ids '(+ (+> a c) (+> b c)) )) |
|---|
| 384 |
|
|---|
| 385 |
|
|---|
| 386 |
;;;;;;;;;;;;;;;;;;;;;; |
|---|
| 387 |
;; Build a type based on the result side of a rule |
|---|
| 388 |
;; |
|---|
| 389 |
(defun Type_Construct (LHS RHS Exp Source) |
|---|
| 390 |
; (print `(,exp ,(Op_name (car Exp)))) |
|---|
| 391 |
(if (atom exp) |
|---|
| 392 |
(if (numberp exp) |
|---|
| 393 |
(format nil "Value!(~s)" exp) |
|---|
| 394 |
(Find_In Exp Source LHS RHS) |
|---|
| 395 |
) |
|---|
| 396 |
(concatenate |
|---|
| 397 |
'string |
|---|
| 398 |
"Op" (Op_name (car Exp)) "!(" |
|---|
| 399 |
(type_construct LHS RHS (cadr exp) Source) ", " |
|---|
| 400 |
(type_construct LHS RHS (caddr exp) Source) ")" |
|---|
| 401 |
) |
|---|
| 402 |
) |
|---|
| 403 |
) |
|---|
| 404 |
|
|---|
| 405 |
;;;;;;;;;;;;;;;;;;;;;;;; |
|---|
| 406 |
;; Generate the code for a List of rewright rules |
|---|
| 407 |
;; this incues the pattern match and the result generation |
|---|
| 408 |
(defun Test_op_and_build (O L) |
|---|
| 409 |
(if (endp L) |
|---|
| 410 |
"" |
|---|
| 411 |
(let* |
|---|
| 412 |
( ;; extrat parts of rule |
|---|
| 413 |
(rule (car L)) |
|---|
| 414 |
(from (car rule)) |
|---|
| 415 |
(to (cadr rule)) |
|---|
| 416 |
) |
|---|
| 417 |
(concatenate |
|---|
| 418 |
'string |
|---|
| 419 |
|
|---|
| 420 |
;; the conditional |
|---|
| 421 |
" static if(" |
|---|
| 422 |
|
|---|
| 423 |
;; comment/output trace |
|---|
| 424 |
" /+ " (format nil "~s" from) " -> " (format nil "~s" to) " +/" |
|---|
| 425 |
|
|---|
| 426 |
;; the form test |
|---|
| 427 |
(Test_Type_Is from) nl |
|---|
| 428 |
|
|---|
| 429 |
;; test repeated terms |
|---|
| 430 |
" " (reuse from) nl |
|---|
| 431 |
|
|---|
| 432 |
;; end of test expreession |
|---|
| 433 |
" ) " nl |
|---|
| 434 |
|
|---|
| 435 |
;; action |
|---|
| 436 |
" alias " |
|---|
| 437 |
|
|---|
| 438 |
;; build type |
|---|
| 439 |
(Type_Construct "T" "V" to from) |
|---|
| 440 |
|
|---|
| 441 |
;; what to alias as |
|---|
| 442 |
" TypeOf" (op_name O) ";" nl |
|---|
| 443 |
|
|---|
| 444 |
;; otherwise |
|---|
| 445 |
" else" nl |
|---|
| 446 |
|
|---|
| 447 |
;; try next |
|---|
| 448 |
(Test_op_and_build O (cdr L)) |
|---|
| 449 |
) |
|---|
| 450 |
) |
|---|
| 451 |
) |
|---|
| 452 |
) |
|---|
| 453 |
|
|---|
| 454 |
|
|---|
| 455 |
;(Test_op_and_build '/ (cadr (car (sort_Rewrite_By_Top_Op (BM::Meta_Rules))))) |
|---|