是否有可能实现 lisp“语言”?在拉库?

发布于 2024-12-14 08:48:23 字数 882 浏览 2 评论 0原文

在这里,每个引用符号对 Hello $world 的解释是指语言

 $world = "WΩrlδ"
"(Hell)*o $world\n"           # <--- plain (Hell)*o, resolve $world, escape \n
'(Hell)*o $world\n'           # <--- plain (Hell)*o, plain $world, escape \n
/(Hell)*o $world\n/           # <--- regexp (Hell)*, resolve $world, interpret \n
<(Hell)*o $world\n>           # <--- make list ["(Hello*o", "$world\n"]
{(Hell)*o $world\n}           # <--- syntax error, this language cant' parse it

那么 Raku 是否足够强大,能够存在于未来的语言中,例如

my $emacs_func = (defun perl-backward-to-start-of-continued-exp (lim)
      (if (= (preceding-char) ?\))
          (forward-sexp -1))
      (beginning-of-line)
      (if (<= (point) lim)
          (goto-char (1+ lim)))
      (skip-chars-forward " \t\f"))


$  typeof($emacs_func)
> Emacs Lisp list

In here, interpretation of Hello $world per each quoting symbol I mean language.

 $world = "WΩrlδ"
"(Hell)*o $world\n"           # <--- plain (Hell)*o, resolve $world, escape \n
'(Hell)*o $world\n'           # <--- plain (Hell)*o, plain $world, escape \n
/(Hell)*o $world\n/           # <--- regexp (Hell)*, resolve $world, interpret \n
<(Hell)*o $world\n>           # <--- make list ["(Hello*o", "$world\n"]
{(Hell)*o $world\n}           # <--- syntax error, this language cant' parse it

So is Raku powerful enough to be able to exist in future language for something like

my $emacs_func = (defun perl-backward-to-start-of-continued-exp (lim)
      (if (= (preceding-char) ?\))
          (forward-sexp -1))
      (beginning-of-line)
      (if (<= (point) lim)
          (goto-char (1+ lim)))
      (skip-chars-forward " \t\f"))


$  typeof($emacs_func)
> Emacs Lisp list

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

百变从容 2024-12-21 08:48:24

据我所知,Perl6 允许使用强大的、类似 Lisp 的宏。例如,解析器在该语言中可用。这允许您将任何语言实现为 Perl 的子语言,而不仅仅是作为解释器。

在 Ikegami 的示例中,他引入了关键字 lisp(xxx),这会更简单。 lispxxx 可以是一个宏,用于解析 Lisp 并将其转换为 Perl6 并返回结果,在他的示例中,是一个带有一个参数的已编译的本机子程序。尽管我认为您实际上也需要实现您使用的 emacs Lisp 函数。实际上,很容易做的是用 Lisp 语法来处理 Perl6 原语……例如。 <代码>(+ abcd) =>; {quasi + abcd} 这样你的 Lisp 语法就拥有 Perl6 的所有功能。

请参阅http://strangelyconcient.org/blog/macros-what-are-they-真的 简单介绍了 perl6 和 AST。

As far as I can see, Perl6 allows for powerful, Lisp-like macros. E.g. the parser is available in the language. That allows you to implement ANY language as a sublanguage of Perl and not just as an interpreter.

In Ikegami's example he introduced the keyword lisp(xxx) and that would be even simpler. The lispxxx could be one single macro that parses and translates Lisp to Perl6 and returns the result, and in his example, a compiled native sub taking one argument. Though I reckon you actually need to implement the emacs Lisp functions that you use as well. Actually what would be easy to do is to address Perl6 primitives with Lisp syntax... eg. (+ a b c d) => {quasi + a b c d} so that your Lisp syntax has all the power of Perl6.

See http://strangelyconsistent.org/blog/macros-what-are-they-really for a light introduction to perl6 and AST.

孤凫 2024-12-21 08:48:24

不是完整的答案...

如果您唯一感兴趣的是 Lisp 格式,那么您可能需要查看 Raku 的 Format::Lisp< /code> 模块:

https://raku.land/zef:raku-community-modules/Format::Lisp
https://github.com/raku-community-modules/Format-Lisp

自述文件说:

为 Perl 6 实现 Common Lisp(格式)语言 - 主要是因为这是一个有趣的挑战,但部分原因是它提供了比任何 sprintf 风格的格式化程序更强大的选项,包括 Python 和 Ruby。

Not a complete answer...

If the only thing you're interested in is Lisp Formatting, then you might want to check out Raku's Format::Lisp module:

https://raku.land/zef:raku-community-modules/Format::Lisp
https://github.com/raku-community-modules/Format-Lisp

The ReadMe says:

Implementing Common Lisp's (format) language for Perl 6 - Mostly because it's an interesting challenge, but partially because it offers much more powerful options than any sprintf-style formatter, including Python and Ruby's.

花开柳相依 2024-12-21 08:48:23

Perl 6 的语法只是用 Perl 6 编写的语法,并且具有很强的可塑性(尽管当前的实现并没有完全提供所有指定的灵活性)。

所以你的要求原则上是可能的,但可能需要更多的小心。特别是圆括号完全有效的 Perl 6 语法,甚至 (defun ab) 解析为有效的 Perl 6。因此,您需要/真正/小心消除歧义规则,这将是一个巨大的蠕虫罐头。

将 Lisp 语法限制为某些特殊分隔的语法结构(例如 lisp(...)q:lisp{...})可能更有意义,尽管有些混合量可能是可以实施的。

我确信,一旦编译器中提供了这些功能,我们将看到大量有趣的实验,并且只有这些实验才能表明哪种语言混合既可行又有用。

Perl 6's grammar is just a grammar written in Perl 6, and very malleable (though current implementations don't quite provide all of the specced flexibility).

So what you ask is possible in principle, but might need more care. In particular are round parenthesis perfectly valid Perl 6 syntax, and even (defun a b) parses as valid Perl 6. So you'd need to be /really/ careful with disambiguation rules, and it would be a huge can of worms.

It probably makes more sense to restrict Lisp syntax to some specially delimited syntactic construct (like lisp(...) or q:lisp{...}), though some amount of mixing would probably be implementable.

I'm sure that once such features are available in a compiler, we'll see a whole lot of interesting experiments, and only those experiments will show what kind of language mixing is both feasible and useful.

梦晓ヶ微光ヅ倾城 2024-12-21 08:48:23

[我似乎错过了这个问题是关于 Perl6,而不是 Perl5。无论如何,留下我的答案,因为它可能会引起一些人的兴趣。]

据我了解,Scheme 和 Lisp 是相关的。如果您同意使用Scheme,Inline::MzScheme 允许您拥有Perl 中的方案代码块。

即使您对Scheme 不满意,您也肯定可以分叉该模块来编辑它以使用您最喜欢的Lisp 引擎,而不会遇到太多麻烦。

这并不完全是你所描述的,但正如 moritz 解释的那样,你所描述的是不可能的,因为无法知道代码的哪些部分应该被视为 Perl 代码,哪些部分应该被视为 Lisp 代码。

在另一个句柄上,通过使用 5.14 的可插入令牌处理程序(由 feature:: 使用)例如,qw_comments 来覆盖 qw),执行以下操作应该相对容易:(

my $emacs_func = lisp(defun perl-backward-to-start-of-continued-exp (lim)
   (if (= (preceding-char) ?\))
      (forward-sexp -1))
   (beginning-of-line)
   (if (<= (point) lim)
      (goto-char (1+ lim)))
   (skip-chars-forward " \t\f"));

请注意在代码中添加 lisp。)

[I seem to have missed the question is about Perl6, not Perl5. Leaving my answer up anyway since it might be of interest to some.]

It's my understanding that Scheme and Lisp are related. If you're ok with Scheme instead, Inline::MzScheme allows one to have blocks of Scheme code in Perl.

Even if you're not ok with Scheme, you could surely fork the module to edit it to use your favorite Lisp engine without too much trouble.

It's not quite what you described, but as moritz explained, what you described is impossible because there's no way to know what parts of the code should be treated as Perl code and which parts should be treated as Lisp code.

On the other handle, through the use of 5.14's pluggable token handler (used by feature::qw_comments to override qw, for example), it should be relatively easy to do the following:

my $emacs_func = lisp(defun perl-backward-to-start-of-continued-exp (lim)
   (if (= (preceding-char) ?\))
      (forward-sexp -1))
   (beginning-of-line)
   (if (<= (point) lim)
      (goto-char (1+ lim)))
   (skip-chars-forward " \t\f"));

(Note the addition of lisp to your code.)

黑凤梨 2024-12-21 08:48:23

Carl Masak 最近(2014 年底)创建了 ipso,“Perl 6 中的元循环 Lisp”,适用于当前乐道。

它可以通过多种方式与 P6 内联结合;请探索“俚语”以了解更多相关信息,例如最近的一篇关于宏/俚语支持任意语言的递归内联

另请参阅 Damian Conway 的 (P6ish) Lisp 中的快速排序

Carl Masak recently (late 2014) created ipso, "A metacircular Lisp in Perl 6" that works on current Rakudo.

There will be ways it can be combined with P6 inline; please explore "slangs" for more about that, eg a recent blog post about macros/slangs enabling recursive inlining of arbitrary langs.

See also Damian Conway's Quicksort in (P6ish) Lisp

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文