Comment detail

α置換 (Nested Flatten)

Squeak Smalltalk で。

ふつう、こういうことはしないのですが、リフレクション機能を試す問題として捉え、システム組み込みのパーサに委譲するかたちで実現してみました。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
| code tree old new |
code := 'sampleCode
    | a |
    a := ''This is a pen''.
    ^a'.
old := 'a'. new := 'b'.
tree := Parser new parse: code class: UndefinedObject.
(tree instVarNamed: #temporaries) do: [:each |
    each name = old ifTrue: [each key: new; name: new]].
^tree decompileString  "=> 'sampleCode
    | b |
    b := ''This is a pen''.
    ^ b' "

Index

Feed

Other

Link

Pathtraq

loading...