问题描述
我试图执行鉴于第四栈上的数字时,产生从数组字符串的话。
I tried to implement a word that produces a string from an array when given a number on the stack in Forth.
我的第一天真的尝试是:
My first naive attempt was:
create myarray s" Alpha", s" Beta", s" Charlie",
这被接受,但预计它没有工作 - myArray的@类型
产生不一致的输出(而不是我的天真的期望,它可能会打印出阿尔法)。
This was accepted, but it did not work as expected -- myarray @ type
produces inconsistent output (instead of my naive expectation that it might print "Alpha").
在网上搜索,我在本教程中以的字符串寿命有限,这意味着我的拟设势必从一开始就失败。在另一方面,即使是普通对象的数组似乎根据和的。这是一个很好的起点,但它仍然需要工作,从那里到字符串数组。 < /更新>
<Update> Apparently, this is not a trivial problem with Forth. There are libraries on the web that implement missing string functionality: here and here. This is a good starting point, although it still requires work to go from there to an array of strings. </Update>
那么,如何可以实现返回从给定的数组中的字符串字?
So how can I implement a word that returns a string from a given array?
推荐答案
要解决您的code,的
叶地址的部分U
堆栈,地址和字符串的长度上。,
只存储一个值,所以你不会得到想要的结果的方式 2
可能做得一样,将同时存储了重新present字符串栈中的项目。一旦你这样做,你需要得到这两个值回过所以 2 @
是你想要的。
To address parts of your code, s"
leaves addr u
on the stack, an address and the length of the string. ,
only stores one value so you won't get the desired results that way. 2,
might do it as that would store both of the stack items that represent the string. Once you have done that you need to get both values back too so 2@
is what you want.
我的改写是这样的:
create myarray s" Alpha" 2, s" Beta" 2, s" Charlie" 2,
\ Test
myarray 2@ type Alpha **ok**
在获取你的数组中的其它元素是有点棘手。当键入 myarray中
你在字典条目数据的起始地址,然后你可以使用2 @拿到前两个地址指向的东西向(即阿尔法的地址和长度)。如果你想测试你需要下一对地址的,因此你可以使用
Getting at the other elements of your array is a bit trickier. When you type myarray
you get the address of the start of the data in that dictionary entry, and you can then use 2@ to get the the things that the first two addresses point to (which are the address and length of "Alpha"). If you want "Beta you need the next pair of addresses. So you can use
myarray 2 cells + \ increment the address by two cells
要获得指向测试版等地址。因此,为了获得测试版,你会进入
To get the addresses that point to "Beta" and so on. So in order to access "Beta" you would enter
myarray 2 cells + 2@ type Beta **ok**
我跟gforth测试这一点,似乎所有的工作,虽然我不知道如何严格测试的持久性。
I have tested this with gforth and it seems to all work, although I am not sure how to rigorously test for persistence.
你的话将需要能够做的地址递增基于什么是堆栈开始对。你可能想进入一些 create将&GT;
的东西。我可以给一些指点,但我不想破坏发现的乐趣。
Your word would need to be able to do the address incrementing based on what is on the stack to start with. You might want to get into some more create does>
stuff. I can give some pointers but I don't want to spoil the fun of discovery.
如果我跳的是什么这实际上意味着只是说太多的细节,我会再试一次。
If I am skipping too many details of what this actually means just say, and I will try again.
这也许是过于粗糙,但我在做各种各样的字符串类型前一阵子有一展身手。
Maybe this is too crude, but I had a go at making a "string type" of sorts a while ago.
: 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**
word type Some Words **ok**
它定义与您所选择的名称的字(在这种情况下言),这将推动长度和启动字符串的地址(在这种情况下,有些话),当它是PTED间$ P $。据我知道什么时候该字符串是在定义这个样子是永久性的。
It defines a word with a name of your choosing (in this case "words") that will push length and start address of your string (in this case "some words") when it is interpreted. As far as I know when the string is in a definition like this it is persistent.
这不回答你充分的问题,但它可能会有所帮助。
This doesn't answer you question fully, but it might help.
我有另一个去一个持久的字符串,这其中肯定配发
本词典条目中的内存,将是安全的,只要这个词的存在。字符串类型以前只存储创建地址和长度的
这是唯一的好,直到别的东西写了的内存。现在这的字符串复制来自该区域的其中,的
创建它变成称为字典项名在这里可以保证,只要持续的名称本身。
I have had another go at a persistent string, this one definitely allot
s memory within a dictionary entry and will be safe as long as that word exists. Before the string "type" only stored the address and length that s"
created which is only any good until something else writes over that region of memory. This now copies the string from where s"
creates it into a dictionary item called "name" where it is guaranteed to last as long as "name" itself.
: 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
有关娱乐,我想也许我可以带感怎么一点这使得没有有用的格式
For amusement, I thought I might show how little sense this makes without helpful formatting
: 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+ @ ;
这篇关于如何实现一个字符串数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!