Question

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 Perl 6 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

So, question obviously is: can it be done in present specification (or even implementation) of Perl 6?

Was it helpful?

Solution

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.

OTHER TIPS

[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.)

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top