3 ms·
If I understood correctly, Tcl's metaprogramming capabilities are based on manipulating strings and not a syntax tree (e.g. lisp). Is that a key component of ma
by gugagore 7y ago
If I understood correctly, Tcl's metaprogramming capabilities are based on manipulating strings and not a syntax tree (e.g. lisp). Is that a key component of making it usable by "mere mortals"?
I think ideally I'd like both capabilities, which seems possible if you just expose the language's parser as a function.
- kragen 7y agoYes. And Tcl does expose list-parsing functions, so you can pretend the strings are lists. Also, current versions of Tcl have optimizations around that.
- mst 7y agoTcl's syntax is sufficiently simple that you can write something that parses it in Tcl, and there's examples of doing that to do more complex metaprogramming all over the wiki. Plus things like uplevel let you jam a lot of meta trickery into commands without needing AST manipulation. So, yeah, you do get both capabilities, it's just done atop the very simple baseline system, if that makes sense. (look at e.g. 'snit', a pure Tcl OO system, as an example of what can be done with just Tcl itself)
- geoelectric 7y agoThe other comment mentions it in passing, but the ability to uplevel (operate within the scope of your caller instead of your own) means the difference between statements and function calls is blurred. That, along with the ability to pass code block literals, lets you redefine the language into your own DSL with new control flow statements. You can define a “while loop” function, for example. Before heavy use of closures and callbacks in dynamic languages (Array.forEach, for example) became the thing this was pretty huge, and it’s still nicer syntax sugar than you get defining control structures with that route. I used TCL with Squish for Qt-based UI test automation for a couple of years. For stuff like that where you really do want a set of domain-specific imperatives you can string together, it was really cool to work with. Biggest downside was you could get too fancy and find yourself alone off in the weeds of your own DSL.
- lmm 7y agoI think the keys are a) uplevel (which might be very difficult to do in a compilable language?) and b) having a syntax that's a bit more traditional. It's not the fact that you're manipulating strings - indeed I'd very much like a TCL that allowed you to metaprogram with a more structured representation.