是否可以覆盖 rebol 路径运算符?

发布于 2024-08-09 09:20:10 字数 67 浏览 3 评论 0原文

可以覆盖 rebol 系统单词,如 print、make 等,那么是否可以对路径运算符执行相同的操作?那么语法是什么呢?

It is possible to overide rebol system words like print, make etc., so is it possible to do the same with the path operator ? Then what's the syntax ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

始于初秋 2024-08-16 09:20:10

另一种可能的方法是使用 REBOL 元编程功能并预处理您自己的代码以捕获路径访问并添加处理程序代码。这里有一个例子:

apply-my-rule: func [spec [block!] /local value][
    print [
        "-- path access --" newline
        "object:" mold spec/1 newline
        "member:" mold spec/2 newline
        "value:" mold set/any 'value get in get spec/1 spec/2 newline
        "--"
    ]
    :value
]

my-do: func [code [block!] /local rule pos][
    parse code rule: [
        any [
            pos: path! (
                pos: either object? get pos/1/1 [
                    change/part pos reduce ['apply-my-rule to-block pos/1] 1
                ][
                    next pos
                ]
            ) :pos
            | into rule    ;-- dive into nested blocks
            | skip         ;-- skip every other values
        ]
    ]
    do code
]

;-- example usage -- 

obj: make object! [
    a: 5
]

my-do [
    print mold obj/a
]

这将为您提供:

-- path access --
object: obj
member: a
value: 5
--
5

另一种(较慢但更灵活)的方法也可以是将字符串模式的代码传递给预处理器,从而使您摆脱任何 REBOL 特定语法规则,如: 然后,

my-alternative-do {
    print mold obj..a
}

预处理器代码将发现所有 .放置并更改代码以正确插入对“apply-my-rule”的调用,并最终使用以下命令运行代码:

do load code

在运行时可以处理和更改整个代码的程度没有真正的限制(所谓的)第一个示例的“块模式”是最有效的方式)。

Another possible approach is to use REBOL meta-programming capabilities and preprocess your own code to catch path accesses and add your handler code. Here's an example :

apply-my-rule: func [spec [block!] /local value][
    print [
        "-- path access --" newline
        "object:" mold spec/1 newline
        "member:" mold spec/2 newline
        "value:" mold set/any 'value get in get spec/1 spec/2 newline
        "--"
    ]
    :value
]

my-do: func [code [block!] /local rule pos][
    parse code rule: [
        any [
            pos: path! (
                pos: either object? get pos/1/1 [
                    change/part pos reduce ['apply-my-rule to-block pos/1] 1
                ][
                    next pos
                ]
            ) :pos
            | into rule    ;-- dive into nested blocks
            | skip         ;-- skip every other values
        ]
    ]
    do code
]

;-- example usage -- 

obj: make object! [
    a: 5
]

my-do [
    print mold obj/a
]

This will give you :

-- path access --
object: obj
member: a
value: 5
--
5

Another (slower but more flexible) approach could also be to pass your code in string mode to the preprocessor allowing freeing yourself from any REBOL specific syntax rule like in :

my-alternative-do {
    print mold obj..a
}

The preprocessor code would then spot all .. places and change the code to properly insert calls to 'apply-my-rule, and would in the end, run the code with :

do load code

There's no real limits on how far you can process and change your whole code at runtime (the so-called "block mode" of the first example being the most efficient way).

缺⑴份安定 2024-08-16 09:20:10

你的意思是用(比如说)....替换

print mold system/options

(比如说)....

print mold system..options

....我用点点语法替换了REBOL的正斜杠

简短的回答:。有些东西是硬连线到解析器中的。

You mean replace (say)....

print mold system/options

with (say)....

print mold system..options

....where I've replaced REBOL's forward slash with dot dot syntax?

Short answer: no. Some things are hardwired into the parser.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文