|
| 1 | +(* ::Package:: *) |
| 2 | + |
| 3 | + BeginPackage[ "Atomizer`"] |
| 4 | + |
| 5 | + gridTree::usage = |
| 6 | + "gridTree[e] displays a compact tree representation of an expression." |
| 7 | + lispify::usage = "lispify[e] converts a Mathematica expression to a list in Polish Prefix Notation.a" |
| 8 | + |
| 9 | + Begin["`Private`"] |
| 10 | + |
| 11 | +SetAttributes[dpyNullary,HoldAll]; |
| 12 | +dpyNullary[ex_] := |
| 13 | + Grid[{{ex, ""}}, |
| 14 | + Frame -> {All,False}, |
| 15 | + Alignment -> Left, |
| 16 | + Background -> {{LightOrange,{LightYellow}}}]; |
| 17 | + |
| 18 | +SetAttributes[dpyMultiary,HoldAll]; |
| 19 | +dpyMultiary[key_, vals_] := |
| 20 | + With[{c = Length @ vals}, |
| 21 | + Module[{ |
| 22 | + spans = Table["", {c}], |
| 23 | + slot = Floor[(1+c)/2]}, |
| 24 | + spans[[slot]] = key; |
| 25 | + Grid[MapThread[List,{spans, vals}], |
| 26 | + Frame -> {All,False}, |
| 27 | + Alignment -> Left, |
| 28 | + Background -> {{LightOrange,{LightGreen}}}] |
| 29 | + ]]; |
| 30 | + |
| 31 | +SetAttributes[dpyAtom,HoldAll]; |
| 32 | +dpyAtom["String"[e_]]:= |
| 33 | + Grid[{{Style[e,Bold]}}, |
| 34 | + Frame -> All, |
| 35 | + Alignment -> Left, |
| 36 | + Background-> LightYellow]; |
| 37 | +dpyAtom[ex_] := |
| 38 | + Grid[{{Style[ex,Bold]}}, |
| 39 | + Frame -> All, |
| 40 | + Alignment -> Left, |
| 41 | + Background -> |
| 42 | + Switch[Head @ ex, |
| 43 | + String, LightYellow, |
| 44 | + Symbol, LightPurple, |
| 45 | + Integer, LightBlue, |
| 46 | + Real, LightBlue, |
| 47 | + Rational, LightBlue, |
| 48 | + Complex, LightBlue, |
| 49 | + _, Red]]; |
| 50 | + |
| 51 | +SetAttributes[lispify,HoldAll]; |
| 52 | +lispify[c:Complex[_,_]]:=c; |
| 53 | +lispify[r:Rational[_,_]]:=r; |
| 54 | +lispify[h_[args___]]:=Prepend[lispify/@Unevaluated@{args},lispify[Unevaluated@h]]; |
| 55 | +lispify[s_(*?AtomQ*)]/;AtomQ[Unevaluated[s]]:=s;(* LEAK!!! todo *) |
| 56 | + |
| 57 | +SetAttributes[stringulateLisp,HoldAll]; |
| 58 | +stringulateLisp[l_List]:=stringulateLisp/@l; |
| 59 | +stringulateLisp[e_Symbol]:=ToString[Unevaluated[e]]; |
| 60 | +stringulateLisp[e_String]:="String"[Unevaluated[e]]; |
| 61 | +stringulateLisp[e_]:=e;(* LEAK!!! todo *) |
| 62 | + |
| 63 | +SetAttributes[gridTree,HoldAll]; |
| 64 | +gridTree2[l_List]/;Length[l]===1&&AtomQ[First[l]]:=dpyNullary[First[l]]; |
| 65 | +gridTree2[l_List]/;Length[l]===1:=dpyNullary[gridTree2[First[l]]]; |
| 66 | +gridTree2[l_List]/;AtomQ[First[l]]:=dpyMultiary[First[l],gridTree2/@Rest[l]]; |
| 67 | +gridTree2[l_List]:=dpyMultiary[gridTree2[First[l]],gridTree2/@Rest[l]]; |
| 68 | +gridTree2[e_String]:=dpyAtom[ToExpression[e]]; |
| 69 | +gridTree2[e_]:=dpyAtom[e]; |
| 70 | +gridTree2[___]:=Throw["GRIDTREE: CATASTROPHE"]; |
| 71 | +gridTree[e_]:=gridTree2[stringulateLisp@lispify[e]]; |
| 72 | + |
| 73 | + End[] |
| 74 | + |
| 75 | + EndPackage[] |
| 76 | + |
| 77 | + |
0 commit comments