lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Tue, May 20, 2014 at 7:47 AM, Thierry@spoludo <thierry@spoludo.com> wrote:
‎Hi,

Just a question: why the DSL has side effects on the upper scope?

‎I understand that the idea is to use the keywords without scoping. I tend not to do this anymore rather using scope: that allows having a local context. This is not so unusual with XML schemas, xs:...

If I don't need these contexts, I still scope the language ‎and use local var to unscope rather than using env.

local mydsl= require'my.dsl.grammar'
‎local kw1, kw2 = mydsl.kw1, mydsl.kw2 -- easy copy/paste

How your grammar ‎looks like?


From: Marc Lepage
Sent: mardi 20 mai 2014 07:32
To: Lua mailing list
Reply To: Lua mailing list
Subject: Loading DSL Data

Hi, looking for some thoughts on loading DSL style data.

I have it working but would like to swap out the environment when doing so, so it doesn't trample on the global environment.



Hi Thierry, that is a good question.

Firstly, I've been experimenting lately with injecting stuff into the global table from loaded data, in both this and another project. So far it's looking promising, it simplifies a lot. Basically, things have IDs, and for convenience I make a global using that ID. Since these are small projects, it's no big deal.

In this case, though, some of the "keywords" have different meaning (or at least must do the same thing) depending on their form. Here's an example:

object 'A'
    foo 'bar'
object 'B'
    function foo() ... end

This allows me to have a simple foo on one object, but to make it a routine in another object, for more complex behaviour. In practical terms, these are just four function calls (regardless of indentation). I don't have any nesting so this is fine.

Function foo (in the DSL) handles the simple string case. So I can't have the second object's definition of function foo overwrite it as a new global. That's the thing I trap, instead to set the function as a property of the object being built.

Where it gets complicated is, some of my DSL keywords that build objects while loading data, are also global functions in the engine outside the DSL and its data. But they need to be accessible when the functions in the DSL-loaded objects are run. Something like this:

object 'C'
    function foo() -- this is object C's foo
        foo() -- it should be able to call global foo
   end

I know it doesn't make a lot of sense when it's named "foo" but trust me with real names it becomes quite sensible. :-)

I also know I could scope the DSL's keywords but I am trying to avoid that. I just want a really clean easy syntax, in part because I would like children to be able to produce the data files. The less distraction the better.

I have it all working except for some environment swapping issues when loading the data, which I've now figured out, so it all should be fine. And again, part of this is experimentation, I may later decide that I don't like all the globals. I will let everything play out and see what conclusion I come to after I have built more of this project.

Thanks for asking,
Marc