开发者

quasiquote/quote in lua?

开发者 https://www.devze.com 2022-12-29 06:41 出处:网络
In Lisp, I can have: (a b c d e f g开发者_运维问答) which means look up b, c, d, e, f, g look up a; apply value of a to above

In Lisp, I can have:

(a b c d e f g开发者_运维问答)

which means

look up b, c, d, e, f, g
look up a; apply value of a to above

Then, I can also have:

`(a b c d e f g)

which is the equiv to

 (list 'a 'b 'c 'd 'e 'f 'g)

Now, in lua, I can have:

[snipplet 1]
foo = {
  Foo,
  {Cat, cat},
  {Dog, dog}
};

Which ends up most likely expanding into: { nil, { nil, nil}, {nil, nil}}

Whereas what I really want is something like:

[snipplet 2]
{ "Foo", {"Cat", "cat"}, {"Dog", "dog"}}

Is there some backquote-like method in lua?

I find [snipplet 1] to be more visually pleasing than [snipplet 2], but what I mean is snipplet 2.

Thanks!


There's no syntax for that but you can try this run-time solution:

setmetatable(_G,{__index=function (t,k) return k end})

This makes all undefined variables behave as if they contained strings with their names.


Consider a function that parses a string containing the Lisp syntax and constructs a table from that.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号