gmol has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I am trying to make a very simple list oriented language (well more like preprocessor ala m4), I think there is a very "natural" solution (something that basically writes itself) to what I am looking for, but I was wondering if I could get some help fleshing out the idea. Basically, I would just want to define list assignment, functions and function application over lists that is very \n delimiter oriented, with the default interpolation behaviour to take cross-products. This would be pseudo example, the idea being that the syntax would be very lightweight over a normal text file...
As->{ 1 2 3 4 5 } Bs->{ 6 7 8 9 } AsBs >>16 27 38 49 5 x-> is some number 6x >>6 is some number 6{1,2,3,4} >>61 62 63 64 level1a->{ A AA } level1b->{ B BB } level2->{ level1a level1a level1a level1b } level2 >>A A A AA AA A AA AA A B A BB AA B AA BB
I am not sure exactly how well I've thought this out, but it gives you an idea...it's basically just subsitition with and emphasis on cross products that is sacrifices some flexibility for simplicity by imposing things like new line delimiters. I am just a little flustered in terms of where to start doing this "the right way". Am I supposed to do this in recdescent? Is there a way to extend/modify perl's syntax so I can get things like regex's in my function definitions? Is there an obvious way to do this without perl?

Replies are listed 'Best First'.
Re: A simple list oriented language in Perl
by kvale (Monsignor) on May 10, 2006 at 23:26 UTC
    When designing and implementing a new language, it is best to start by creating a grammar for the elements in your language, keeping in mind the semantics of each element as you go along.

    For instance, your assignment might take on the form

    <assign> =: <assign-token> "->{\n" <assign-list> "}\n" <assign-token> =: "\w+" <assign-list> =: <assign-element> <assign-list> | (null) <assign-element> =: "[0-9]+\n"
    where nonterminal elements are in <> and literals and regexes are in "".

    Once you have created a grammar you can implement it using Parse::RecDescent or your own recursive desecnt set of routines. Once you have the parser working, you can begin to create the translation routines.

    Programming macro languages can be a bit tricky due to expansion rules. What gets expanded when? How do I quote expressions to prevent expansions? And so on. Two good examples of macro languages that deal with these issues are m4 and TeX. Both have good documentation.

    -Mark

      Programming macro languages can be a bit tricky due to expansion rules. What gets expanded when? How do I quote expressions to prevent expansions? And so on. Two good examples of macro languages that deal with these issues are m4 and TeX. Both have good documentation.

      In fact TeX itself is even in the opinion of very knowledgeable TeXperts, a real mess. See for example David Kastrup's posts in this thread (also available from Google Groups).

Re: A simple list oriented language in Perl
by pajout (Curate) on May 11, 2006 at 12:47 UTC
    Just an idea: Is it possible to use Perl? You can eval the script with some compartments, for instance...

    http://search.cpan.org/~rgarcia/Safe-2.11/Safe.pm