lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Fri, May 8, 2015 at 1:21 AM, Dirk Laurie <dirk.laurie@gmail.com> wrote:
2015-05-08 9:46 GMT+02:00 Brigham Toskin <brighamtoskin@gmail.com>:

> if you're writing a forth-like language, in lua

Why can't you simply use the C API stack? I.e. write in C a routine that to
Lua looks like

ret1, ret2, ... = forth(program,dictionary,...)

"program" is a list of words from the dictionary, for instance obtained
from split(str) using Microlight's split, "dictionary" has triples
{func,nargin,nargout} as values.


Well, a few reasons.

One, I'd like to keep the code all in Lua for now. I had considered implementing some code in C before; that would be nice and zippy, but then makes building/installation more cumbersome, and the C code that interfaces with Lua is by nature rather ugly and complex. I might consider this kind of thing in the future if it turns out I need more performance than I can squeeze out of thoughtfully-written Lua code, but I'm not convinced it would be a big win for me.

Two, forth doesn't have explicit arg/return counts. Each routine has access to the full stack. If you were careful about defining the stack delta of each of the lowest level primitives, you could calculate a stack delta of every new routine at compile time. But, this could be confounded or at least complicated any time you consumed stack data in a loop, wrote recursive functions, used plain ol' conditionals, etc...

But the main reason is, the code I've been playing with actually manipulates the Lua call stack and varargs from within Lua. There's no need to resort to the C API for this functionality. Unless I'm missing a subtle(?) point about what you're suggesting?

--
Brigham Toskin