如何将带有上下文的块传递给例程?
How to pass block with context to routine?
我正在继续学习Red/System。现在我正在尝试了解如何将带有上下文的单词传递给函数。
这是我的代码:
Red [Note: "compile in release mode (-r flag)"]
mycontext: context [
list: []
]
foo: routine [
blk
/local
int [integer!]
str [c-string!]
][
blk: as red-block! _context/get-any <CONTEXTHERE> symbol/make "list" ; ?? <CONTEXTHERE>
int: 123
str: "Hello"
block/rs-append blk as red-value! integer/box int
string/load-in str length? str blk UTF-8
]
foo mycontext ; passing context to foo
probe mycontext
此代码不起作用,因为列表放在 mycontext
中。无上下文传递list
的例子可以参考
我尝试了不同的方法,但每次都出错。
https://github.com/red/red/blob/master/runtime/datatypes/structures.reds#L188
首先,您的例程的类型说明不正确(这是错误消息告诉您的内容):
Compiling to native code...
*** Compilation Error: invalid definition for function exec/foo: [
blk [red-red-block!]
/local
int [integer!]
str [c-string!]
]
编译器自动为例程的参数添加 red-*
前缀,因此正确的规范应该是:
foo: routine [
blk [block!]
/local
int [integer!]
str [c-string!]
][
...
]
其次,这也不正确;你传递的是 object!
,而不是 block!
:
*** Compilation Error: datatype not allowed
这意味着应该写:
foo: routine [
obj [object!]
/local
int [integer!]
str [c-string!]
][
...
]
现在,据我了解,您想获取 list
引用的那个空块并向其附加值。这需要有一个上下文节点
obj/ctx
和符号 ID
symbol/make "list"
有了两个,我们就可以调用 _context/get-any
:
foo: routine [
obj [object!]
/local
blk [red-block!]
int [integer!]
str [c-string!]
][
blk: as red-block! _context/get-any symbol/make "list" obj/ctx
int: 123
str: "Hello"
block/rs-append blk as red-value! integer/box int
string/load-in str length? str blk UTF-8
]
另一种解决方案是使用 word/load
构造一个 word!
值并调用 object/rs-select
:
foo: routine [
obj [object!]
/local
blk [red-block!]
int [integer!]
str [c-string!]
][
blk: as red-block! object/rs-select obj as red-value! word/load "list"
int: 123
str: "Hello"
block/rs-append blk as red-value! integer/box int
string/load-in str length? str blk UTF-8
]
两种方法都会产生预期的结果:
make object! [
list: [123 "Hello"]
]
就个人而言,我不会把事情复杂化,只是将块传递给您的例程:
mycontext: context [
list: []
]
foo: routine [
blk [block!]
/local
int [red-integer!]
str [c-string!]
][
str: "Hello"
int: as red-integer! stack/push*
int/header: TYPE_INTEGER
int/value: 123
block/rs-append blk as red-value! int
string/load-in str length? str blk UTF-8
]
foo mycontext/list
probe mycontext
我正在继续学习Red/System。现在我正在尝试了解如何将带有上下文的单词传递给函数。
这是我的代码:
Red [Note: "compile in release mode (-r flag)"]
mycontext: context [
list: []
]
foo: routine [
blk
/local
int [integer!]
str [c-string!]
][
blk: as red-block! _context/get-any <CONTEXTHERE> symbol/make "list" ; ?? <CONTEXTHERE>
int: 123
str: "Hello"
block/rs-append blk as red-value! integer/box int
string/load-in str length? str blk UTF-8
]
foo mycontext ; passing context to foo
probe mycontext
此代码不起作用,因为列表放在 mycontext
中。无上下文传递list
的例子可以参考
我尝试了不同的方法,但每次都出错。
https://github.com/red/red/blob/master/runtime/datatypes/structures.reds#L188
首先,您的例程的类型说明不正确(这是错误消息告诉您的内容):
Compiling to native code...
*** Compilation Error: invalid definition for function exec/foo: [
blk [red-red-block!]
/local
int [integer!]
str [c-string!]
]
编译器自动为例程的参数添加 red-*
前缀,因此正确的规范应该是:
foo: routine [
blk [block!]
/local
int [integer!]
str [c-string!]
][
...
]
其次,这也不正确;你传递的是 object!
,而不是 block!
:
*** Compilation Error: datatype not allowed
这意味着应该写:
foo: routine [
obj [object!]
/local
int [integer!]
str [c-string!]
][
...
]
现在,据我了解,您想获取 list
引用的那个空块并向其附加值。这需要有一个上下文节点
obj/ctx
和符号 ID
symbol/make "list"
有了两个,我们就可以调用 _context/get-any
:
foo: routine [
obj [object!]
/local
blk [red-block!]
int [integer!]
str [c-string!]
][
blk: as red-block! _context/get-any symbol/make "list" obj/ctx
int: 123
str: "Hello"
block/rs-append blk as red-value! integer/box int
string/load-in str length? str blk UTF-8
]
另一种解决方案是使用 word/load
构造一个 word!
值并调用 object/rs-select
:
foo: routine [
obj [object!]
/local
blk [red-block!]
int [integer!]
str [c-string!]
][
blk: as red-block! object/rs-select obj as red-value! word/load "list"
int: 123
str: "Hello"
block/rs-append blk as red-value! integer/box int
string/load-in str length? str blk UTF-8
]
两种方法都会产生预期的结果:
make object! [
list: [123 "Hello"]
]
就个人而言,我不会把事情复杂化,只是将块传递给您的例程:
mycontext: context [
list: []
]
foo: routine [
blk [block!]
/local
int [red-integer!]
str [c-string!]
][
str: "Hello"
int: as red-integer! stack/push*
int/header: TYPE_INTEGER
int/value: 123
block/rs-append blk as red-value! int
string/load-in str length? str blk UTF-8
]
foo mycontext/list
probe mycontext