解压数组堆栈在运行时 [英] Unpack an array to the stack at runtime

查看:157
本文介绍了解压数组堆栈在运行时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组:

{ 1 2 3 4 }

我想它的内容推到堆栈中。

I want to push its contents to the stack.

我试过:

(sc) { 1 2 3 4 } dup length firstn
1
2
3
4

大!

里面一个字,虽然:

: explode ( a -- * ) dup length firstn ; inline

抛出一个错误无法适用firstn来运行时计算值,因为 firstn 通话呼叫

字调用其中的输入参数必须内联声明,这样它通过在字面报价主叫方可以有一个静态烟囱效应。

Words which call an input parameter must be declared inline so that a caller which passes in a literal quotation can have a static stack effect.

...也因为呼叫
的语义很难计算在运行时的报价。

... and because of call 's semantics it's hard to compute quotations at runtime.

必须的是实现这一目标的方法。这是什么?

There must be a way to accomplish this. What is it?

推荐答案

由于比约恩·林奎斯特说,因子不会让你这样做 1 ,但也有一些解决办法。

As Björn Lindqvist said, Factor won't let you do that1, but there are some workarounds.

最直接的办法也许是使用向量作为堆栈

The most direct way is perhaps using a vector as a stack:

: explode ( v s -- v ) dupd [ swap push ] with each ;
V{ } "somestring" explode
==> V{ 115 111 109 101 115 116 114 105 110 103 }

当然,这意味着重新设计所有的相关功能与这个工作。

Of course, this means redesigning all related functions to work with this.

也许比载体更好,也有串缓冲器 ,可以像一个堆栈了。

Maybe better than vectors, there are also string buffers, that can behave like a stack too.

"somestring" >sbuf
==> SBUF" somestring"
"-abcd" [ over push ] each
==> "somestring-abcd"

(或者 SBUFsomesbuf

如果你需要保持与内容的工作就好像它是输入到堆栈传递一些其他的话,你可以使用与-datastack

If you need to keep working with the content as if it were the input to some other words passed on the stack, you can use with-datastack:

"somestring-abcd" [ 5 ndrop 3dup ] with-datastack >string
==> "somestringing"

不过想到这,就好像它在那里经常非基于堆栈的语言。每个字需要的参数的固定号码,并输出结果的一个固定数。

But think of this as if it where a regular non-stack-based language. Each word takes a fixed number of parameters and outputs a fixed number of results.

[1]。必须有一些元编程的方式来做到这一点。我无法想象有一个简单实用的方法,虽然。

[1]. There must be some metaprogramming ways to do this. I don't imagine there's an easy practical way, though.

这篇关于解压数组堆栈在运行时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆