Execute HQ9+/Tcl: Difference between revisions

Added a compiler that is, if anything, shorter than the interpreter...
(Added an implementation in Tcl; see page for why this is interesting)
 
(Added a compiler that is, if anything, shorter than the interpreter...)
Line 2:
 
This implementation has the feature that if you feed itself in as the program, it prints itself out. So the implementation of a language with a quine operation is a quine in the language implemented. This feels rather appropriate...
 
{{works with|Tcl|8.4}}
<br clear=right>
==Interpreter==
<lang tcl>set d [read [expr {$argc?[open [lindex $argv 0]]:"stdin"}]]
for {set i [set a 0]} {$i<[string len $d]} {incr i} {
Line 18 ⟶ 21:
} \x2b {incr a}
}</lang>
==Compiler==
To be fair, this compiler then immediately executes the code. Replace the <code>eval</code> with <code>puts</code> to see what it generates.
<lang tcl>set d [read [expr {[set a $argc]?[open [lindex $argv [set a 0]]]:"stdin"}]]
eval [string map [list \u0068 {puts "\u0048ello, world!";} q \
[list puts -nonewline $d]\n \071 {for {set b [expr 100-1]} 1 {} {
puts "$b bottle[expr {$b-1?{s}:{}}] of beer on t\u0068e wall"
puts "$b bottle[expr {$b-1?{s}:{}}] of beer"
incr b -1
puts "take one down and pass it around"
puts "$b bottle[expr {$b-1?{s}:{}}] of beer on t\u0068e wall"
if {$b} {puts ""} else break
};} \x2b {incr a;}
] [regsub -all {[\u0000-*,-8:-gi-pr-\uffff]*} $d {}]]</lang>
Anonymous user