5

I would like to add scripting support to an applications and with plenty scripting languages available I am a bit overwhelmed.

At first I thought about Python but I guess Python is a little too big for my taste and the application (although I like it very much, it gets a lot of things right in my opinion while the syntax is clean and simple especially if you are not doing heavy OOP).

I am also very fond of Lisps. I like scheme and it is the perfect extension language - at least in some ways, but I would like to stay syntactically more conservative.

I remember having heard a lot of positive things about Lua and indeed Lua seems a good possibility. Unfortunately there is this one thing that really "turns me off": Its the local keyword for local binding. This is really ugly to my taste and I frankly do not quite grok why languages are still designed this way and in essence knowing about local. This is also a major drawback of JavaScript in my opinion and I feel like it all boils down to bad language design. My major concern is, that a user might just override a function from just within her/his function just by omitting local.

But then Lua gets a lot of things right, size, speed, and it is surprisingly scheme-esque. Yet I am really afraid that it is not suitable for non-programmers who have no sense of scope.

  • Any alternatives on the horizon?
  • Might hacking the Luacore to make local the default be an option?

    Lua sources appear to be quite decent and modular, so this would be an interesting project ;)

Joachim Sauer
  • 10,956
  • 3
  • 52
  • 45
wirrbel
  • 3,018
  • 2
  • 21
  • 33
  • 3
    Have you looked at Tcl and Forth? – yannis Mar 21 '13 at 07:23
  • 1
    Forth is probably a little to obscure for the purpose (although I like the idea) Tcl might be worthwhile a look but I think the grammar also suffers a lot of baroque features. I really would opt for Scheme if people would not resent brackets :) – wirrbel Mar 21 '13 at 07:30
  • If you are feeling particularly adventurous, you might also want to check out small C interpreters like [picoc](https://code.google.com/p/picoc/). – yannis Mar 21 '13 at 08:01
  • 3
    Lua and lisp have very similar scoping rules, so if you dislike one you will probably also dislike the other. – finnw Mar 21 '13 at 13:30
  • @finnw In a way this is true, I think you are talking about let and other expressions for local variable binding. Yet: let has a role similar to the assignment operator in these languages and is taught early on. Using set! etc. without let would behave like variables without "local" in lua I guess but then you usually use let for assignments. – wirrbel Mar 21 '13 at 14:18
  • Worth mentioning that the global table can be "locked" by giving it `__index` and `__newindex` metamethods that throw errors, this way you won't be able to inadvertently leak things into the global table or reference things that don't exist. http://lua-users.org/wiki/DetectingUndefinedVariables – Hey Aug 23 '14 at 21:18

2 Answers2

8

local is mandatory attribute of full lexical scoping:

local x = 0
function f(inc)
    x = x + inc -- if you auto-localize it, this is broken
end

On the other hand, we probably could remove local from the language, but add complementing outer (bound, upvalue, you name it):

x, y = 0, 0
function f(inc)
    outer x
    x = x + inc -- uses outer slot: "0 + inc"
    y = y + inc -- creates local (and raises "nil + number" error)
end

If you do not have either (including regular globals), your scoping is just useless.

Lua parser is written well, although I don't think it will be trivial fix.

  • I actually had this in mind (kind of). I think Python has the global keyword for this. – wirrbel Mar 21 '13 at 14:13
  • 1
    +1 for "`local` is mandatory"; but making it default and creating an `outer` keyword opens a _huge_ can of worms. There would be several common cases where you find the wrong scoping unless you add many initializations. In the end you get more verbose and "to fix _this_, write that _over there_". almost like replacing GOTO with [COMEFROM](http://en.wikipedia.org/wiki/COMEFROM) – Javier Mar 21 '13 at 16:18
  • Forgot to mention, every for-loop and do-end block defines own local scope. This `outer` opens huge can of worms that is indeed under insane pressure :) Bad idea. –  Mar 22 '13 at 05:41
  • You just made me read up on python scoping again, and it seems that python's way of doing things is to not create new scopes for every new block (if statement etc.) which means that you hardly have to write `outer`. Indeed it seems that lua chose the simplest and straightforward way to handle scoping. I am rather concerned that a user might write a loop or something and have a temporary variable `next=5` for example forgetting `local` declaration which will in turn break other code. – wirrbel Mar 22 '13 at 07:02
2

I'd suggest TCL, its designed to be an embedded scripting language that can define DSLs for non-programmers.

The key thing with TCL, scheme or any solution here though is designing a DSL that the user can understand, with a good enough DSL the user should rarely need general programming constructs (which could be harder to comprehend)

WRT your comment TCL doesn't really have a grammar so I wonder if you are misunderstanding something about it? Or rather its grammar is about as complex as schemes.

jk.
  • 10,216
  • 1
  • 33
  • 43