4 ms·
"The programmer is free to reinvent the language, write new control structures, and so on." I don't remember that about Tcl.. How far can you do that? And how
by yesenadam 7y ago
"The programmer is free to reinvent the language, write new control structures, and so on."
I don't remember that about Tcl.. How far can you do that? And how do you do that? (an example would be great). Thanks.
I've been getting into Forth lately, where you can redefine absolutely everything[0], and make your own anything, to a ridiculous degree. Not sure what this quote means. I'll be pleasantly surprised if it's true.
[0] Well, you are stuck with the stack(s) I guess. But that's about it.
edit: Thank you all for the great answers!
- davidw 7y agoHere's a starting point: https://wiki.tcl-lang.org/page/New+Control+Structures https://wiki.tcl-lang.org/page/New+Control+Structures
- blacksqr 7y agotl;dr Tcl has an "uplevel" command that lets you execute code in the variable scope level of the procedure that called it. The classic example is a "do" loop, which Tcl doesn't include out of the box: proc do {body condition} { while 1 { uplevel $body if {![uplevel [list expr $condition]]} break } } set i 10 do {puts $i ; incr i -1} {$i > 0} output: 10 9 8 7 6 5 4 3 2 1
- Uhhrrr 7y agoIt is pretty true. There are not many rules in Tcl. One of them is that the first word is a procedure, and the following words are arguments. Procedures are defined using "proc", which is also a procedure: proc sum {x y} { return [expr $x + $y] } Here proc is taking 3 args: name, args, and body (curly braces wind up enclosing one arg). But you can also redefine proc, using proc. Here is a neat function which uses this to keep other procs from being given new definitions: rename proc _proc _proc proc {name args body} { if {[info commands $name]!=""} { puts stderr "warning: [info script] redefines $name" } _proc $name $args $body } The above is taken from here: https://wiki.tcl-lang.org/page/Guarded+proc https://wiki.tcl-lang.org/page/Guarded+proc List of overloaded procs here: https://wiki.tcl-lang.org/page/Overloading+Proc https://wiki.tcl-lang.org/page/Overloading+Proc
- kbr2000 7y agoYou can use proc command in Tcl to replace existing commands and procedures, which gives you the possibility to implement different behaviour. An example that shows this: ~$ tclsh % rename while origwhile % proc while {args} { puts stderr "Debug: invoked while with arguments $args" # do anything you want here uplevel 1 origwhile $args } % set i 0 % while {$i < 5} {puts $i; incr i} Debug: invoked while with arguments {$i < 5} {puts $i; incr i} 0 1 2 3 4 % And in Forth the dictionary is searched backwards for a word (starting with the most recent definitions), so you can similarly override words. Powerful!
- yesenadam 7y agoIn Forth, not just new words - everything (except the stacks, I think) - as if in C you could redefine not only every reserved word but every symbol - (, ), +, *, ->, %, &, [,],{,},# = etc etc. Also there are 'defining words' (akin to typedef, struct, enum etc) and you can define new defining words... And so turn Forth into any other language you wanted, if you were crazy enough..
- pjmlp 7y agoPutting on words instead of code, since my last Tcl was about 20 years ago. Semantically Tcl is a bit like Lisp with Algol syntax, and you get to execute expressions on upper stack levels, and can also evaluate parameter blocks (aka lambdas). So you just need to create a function for your control structure, that will take the required parameters for the control structure body, and then eval it on the upper call level. It is very rough as description, but maybe I was still able to convey the idea. For our in-house app server, all our configuration DSLs were just Tcl scripts that we sourced (loaded) on application start.
- kragen 7y agoYou can't invent new syntax — the curly braces gotta curly-brace — but you can do whatever else you want.