lua-users home
lua-l archive

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


On 05/05/2011 22.53, E. Toernig wrote:
| lib.Interpreter( function(_ENV)
|    AddItem "hello!"
|    AddItem(AN_UPVALUE)
|    AddItem "Hope You Enjoy!"
|
|    PrintResult()  -->  hello! This is a silly DSL! Hope You Enjoy!
| end)

It is still too boilerplate IMO when explaining how to write a DSL
snippet to a non-programmer

Just an idea: How about misusing the for statement?

| for _ENV in lib.Interpreter do
|    AddItem "hello!"
|    AddItem(AN_UPVALUE)
|    AddItem "Hope You Enjoy!"
|
|    PrintResult()  -->  hello! This is a silly DSL! Hope You Enjoy!
| end

Still the _ENV but it's readable ;-)

Really smart code! But probably too smart :-D ! As I see it my internal parser triggers the "what's being iterated here?" exception! :-D As the old say goes: "Try not to outsmart the language/compiler/interpreter!" ;-)
But you told me that: "for" is being misused! :-)
Nevertheless, very smart! I wonder if there is a less academic use for that pattern.

P.S.: (for future reference in the archive) I think now AN_UPVALUE inside the "for" body is no more an upvalue, but just a local. From the manual (emphasis mine):

*****
Because of the lexical scoping rules, local variables can be freely accessed by functions defined inside their scope. A *local variable used by an inner function* is called an upvalue, or external local variable, *inside the inner function*.
*****

Since the body of the "for" is not a closure, technically AN_UPVALUE is not an upvalue there (debug.getupvalue doesn't list it, while debug.getlocal does).


Full code below:

[...]


Ciao, ET.




Cheers!
--
Lorenzo