用 Rebol 编写的跟踪器函数

问题描述

可以在 Rebol (Red,R3 Ren-c) 中编写一个类似于 TRACE 的函数,产生以下结果:

foo: func [val1 val2 /local temp] [
       temp: val1 + 5
       val2 + temp
]
bar: func [x /ris 'var "blablalba"][
       if ris [set var "fatto"]
       (foo x 2) + 8
]
trace [foo bar]
bar/ris 7 yyy

Enter BAR
   x = 7
   var = yyy
     Enter FOO
       val1 = 7
       val2 = 2
     FOO returned 14
BAR returned 22

解决方法

在用户级别,最直接的方法是编写一个类似闭包的包装器,在调用主代码之前和之后调用您提供的跟踪钩子,然后返回评估结果。

这个想法的粗略草图如下:

notifywait -r .. -q -e modify,delete |
    while read line1; do
        echo ${line1}
    done

有了它,您就可以:

frame-of: function [
    func [function!]
    /local
        match
][
    parse spec-of :func [
        collect any [
            set match [not quote return: all-word!] keep (to word! match)
            | skip
        ]
    ]
]

report: function [frame [block!]][
    environment: construct collect [forall frame [keep to set-word! frame/1]]
    also environment set environment reduce frame
]

trace: function [
    'target [word!]
    enter   [block!]
    leave   [block!]
][
    chain: reduce [
        'do enter
        'set/any quote 'result 'do body-of get target
        'do leave
        quote :result
    ]
    
    new:  func spec-of get target chain
    info: context [
        frame:  bind frame-of get target :new
        name:   target
        result: none
    ]
    
    bind body-of :new info
    set target :new
    exit
]

对于您的示例,给出:

enter: [print ['entering name 'with mold/flat body-of report frame]]
leave: [print [name 'returned result]]

trace foo enter leave
trace bar enter leave

请注意,这只是一个 PoC。基本思想是,您的原始函数被检测版本替换,该版本是通过某个内部命名空间上的闭包创建的,其中包含调试信息。您还可以将一些 Red/System 混合在一起,以获得对运行时信息的细粒度访问,例如评估堆栈。

我将保留带有缩进的漂亮印刷和禁用跟踪作为读者练习;)

,

这个解决方案,不是很优雅,适用于 Red 和 Rebol,应该满足需求要求(我希望我已经考虑了所有情况):

extract-args: func [args-block /local emit result rules w ref] [
    result: copy []
    emit: func [x] [append result x]
    rules: [
        any [
                /local some [word!]
            |   set w [word! | get-word!] (emit w)
            |   set w lit-word! (emit to-get-word w)
            |   [string! | block!]
            |   set ref refinement! set w [word! | lit-word! | get-word!] 
                    (if ref [emit w])
            |   refinement!
        ]
    ]
    parse args-block rules
    result
]

pretty-print-args: func [spc /wrd] [
    foreach wrd extract-args spc [
            print [**indentation** "  " to-string wrd " = " get wrd]
    ]
]

insert-**result**: func [spc] [
    either find spc quote /local 
        [append spc [**result**]]
        [append spc [/local **result**]]
]

**indentation**: copy ""
add-1-ind: func [][append **indentation** copy "   "]
remove-1-ind: func [] [remove/part skip tail **indentation** -3 3]

my-trace: func [wrds /local w spc bdy bdy' uw] [
    foreach w wrds [
        uw: uppercase to-string w
        spc: copy spec-of get w
        insert-**result** spc
        bdy: body-of get w
        bdy': compose/deep copy [
            add-1-ind
            print [**indentation** "Enter" (uw)]
            pretty-print-args [(spc)]
            set/any '**result** do [(bdy)]
            print [**indentation** (uw) "returned" mold :**result**]
            remove-1-ind
            :**result**
        ]
        set w func spc bdy'
    ]
]

那么:

>> my-trace [foo bar]
== func [x /ris 'var "blablalba" /local **result**][
    add-1-ind 
...

>> bar/ris 7 yyy
    Enter  BAR
       x  =  7
       var  =  yyy
       Enter  FOO
          val1  =  7
          val2  =  2
       FOO  returned  14
    BAR  returned  22
== 22
>> source bar
bar: func [x /ris 'var "blablalba" /local **result**][
    add-1-ind 
    print [**indentation** "Enter" "BAR"] 
    pretty-print-args [x /ris 'var "blablalba" /local **result**] 
    set/any '**result** do [
        if ris [set var "fatto"] 
        (foo x 2) + 8
    ] 
    print [**indentation** "BAR" "returned" mold :**result**] 
    remove-1-ind 
    :**result**
]