gpt4 book ai didi

arrays - 如何实现字符串数组?

转载 作者:行者123 更新时间:2023-12-04 15:47:25 25 4
gpt4 key购买 nike

我试图实现一个单词,当在 Forth 中的堆栈上给出一个数字时,该单词从数组中生成一个字符串。

我的第一次幼稚尝试是:

create myarray s" Alpha" , s" Beta" , s" Charlie" ,

这被接受了,但它没有按预期工作 — myarray @ type产生不一致的输出(而不是我天真的期望它可能会打印“Alpha”)。

在网上搜索时,我在 Gforth documentation中找到了用 s" 创建的字符串生命有限,这意味着我的 ansatz 从一开始就注定要失败。另一方面,根据 Arrays in Forth,即使是常规对象的数组似乎也没有标准化。 Len 的第四个教程中的部分。

<更新> 显然,这对于 Forth 来说不是一个小问题。网上有一些实现缺失字符串功能的库: FFL ( str module ) 和 String Functions by Bernd Paysan .这是一个很好的起点,尽管它仍然需要工作才能从那里转到字符串数组。

那么如何实现一个从给定数组返回字符串的单词呢?

最佳答案

要解决部分代码,s"叶子addr u在堆栈上,一个地址和字符串的长度。 ,只存储一个值,因此您不会以这种方式获得所需的结果。 2,可能会这样做,因为这将存储表示字符串的两个堆栈项。完成后,您也需要取回这两个值,所以 2@是你想要的。

我的重写看起来像这样:

create myarray s" Alpha" 2, s" Beta" 2, s" Charlie" 2,

\ Test
myarray 2@ type Alpha **ok**

获取数组的其他元素有点棘手。当您输入 myarray 时您获得该字典条目中数据的起始地址,然后您可以使用 2@ 获得前两个地址指向的内容(即“Alpha”的地址和长度)。如果你想要“测试版,你需要下一对地址。所以你可以使用
myarray 2 cells + \ increment the address by two cells

获取指向“Beta”等的地址。因此,为了访问“Beta”,您需要输入
myarray 2 cells + 2@ type Beta **ok**

我已经用 gforth 测试过它,它似乎一切正常,尽管我不确定如何严格测试持久性。

您的单词需要能够根据堆栈上的内容进行地址递增。您可能想了解更多 create does>东西。我可以提供一些建议,但我不想破坏发现的乐趣。

如果我跳过了这实际上意味着什么的太多细节,请说,我会再试一次。

也许这太粗糙了,但不久前我尝试制作了一种“字符串类型”。
: string                                    ( addr u "name" -- )
create 2, \ add address and length to dict entry "name"
does> dup cell+ @ swap @ ; \ push addr u

\ Example
s" Some Words" string words **ok**
words type Some Words **ok**

它定义了一个具有您选择的名称(在本例中为“words”)的单词,该单词将在解释时推送字符串的长度和起始地址(在本例中为“一些单词”)。据我所知,当字符串在这样的定义中时,它是持久的。

这并不能完全回答你的问题,但它可能会有所帮助。

我又尝试了一个持久的字符串,这个绝对是 allot s 存储在字典条目中,只要该词存在,它就是安全的。在字符串“type”之前只存储了 s"的地址和长度创建这只有在其他东西写入该内存区域之前才有用。这现在从 s" 复制字符串将它创建到一个名为“name”的字典项中,保证它与“name”本身一样长。
: string                                    ( addr u "name" -- )
create \ create dict entry called "name"
dup >r here >r \ keep copies of string length and start of "name"'s memory
dup 2 cells + allot \ allot memory for the number of chars/bytes of the string plus 2
\ for the new addr u
r@ 2 cells + \ Get the address two cells from the start the space for "name"
swap cmove \ copy the string at addr u into the alloted space for "name"

\ Now "name" looks like this: "name" -blank1- -blank2- "the text of the string at addr u"
\ blank1 should be the address of the start of the the text = addr2 and blank2 should be u

r@ dup 2 cells + swap ! \ get the address of blank1, copy it, increment by 2 to get addr2
\ and then store that in blank1
r> cell+ r> swap ! \ get address of blank1, increment to get address of blank2, then get u and
\ store it in blank2

\ Now "name" looks like this: "name" addr2 u "the text of the string at addr u"

does> dup @ swap cell+ @ ; \ push addr2 u

为了娱乐,我想我可能会展示如果没有有用的格式,这是多么没有意义
: string-no-comments         ( addr u "name" -- )
create dup >r here >r dup 2 cells + allot r@
2 cells + swap cmove r@ dup 2 cells + swap !
r> cell+ r> swap ! does> dup @ swap cell+ @ ;

关于arrays - 如何实现字符串数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8693341/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com