gapsf
gapsf

Reputation: 644

compile and execute program written in subset of perl6 with rakudo

Suppose I would like to use simple language that is only a subset of Perl6 as a extension/embeddable language to "script" my own Perl 6 programs. For example let this language has only:

with Perl6 syntax and may be very limited subset of built in functions.

Anything outside of this should cause a compilation error and should not executed.

Is it possible to (re)use Rakudo compiler for this or it can be done only by hand-written interpreter/compiler?

Let me clarify my motivation for this.

  1. Using (subset of) host language (Perl 6 in this case) as DSL-language for configuration files for scripts/apps written in host language.
    This can be done with EVAL (perl6 'do(file)' equivalent), but it's not safe at all since there is no control what EVAL can do.
  2. Using (subset of) host language as extension/scripting language for apps written in host language. Much like scripting Blender with Python or WoW with Lua. I guess app core with some API is needed in this case? But how exactly it should/can be done?

But again, why host language for configuration/scripting? In case of conf files I don't like using "foreign languages" like YAML or JSON because:

In case of extension/scripting: again, I don't see any reason to use Lua or Python for Perl 6 apps, but again I don't like idea about inventing my own extension/scripting language and writing interpreter/compiler for it in Perl 6 if I already have Perl 6/Rakudo.

Upvotes: 4

Views: 235

Answers (2)

Christopher Bottoms
Christopher Bottoms

Reputation: 11158

I know that this isn't the answer you were looking for, but I really think most configuration can be handled well by JSON. JSON is well accepted outside of the JavaScript community. Many languages use it. In fact, JSON::Fast comes with Rakudo-Star (as evidenced by it's json_fast submodule). You can convert JSON files to Perl 6 data structures with this one-liner (okay, two-liner including use JSON::Fast):

use JSON::Fast;
my %json = from-json(slurp($filename));

Also, JSON is a pretty decent data structure. It can be simple if you need simple, but you can use it for very complex configurations using nested hashes and arrays in just about any combination.

Upvotes: 1

jjmerelo
jjmerelo

Reputation: 23487

Perl 6 code can be abstracted into Blocks. You can declare Block-type variables, and you can also declare subset of blocks using where. If you are able to express the restrictions to your Perl 6 blocks into Perl 6 expressions, you can easily create subsets of Perl 6 combining those constraints. Your DSL will then be valid objects of the (sub)type you have declared.

Upvotes: 0

Related Questions