Advertisement
Guest User

"Hello World!" EXCON Parser TCL Implementation

a guest
Sep 27th, 2012
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
TCL 2.90 KB | None | 0 0
  1. #!/usr/bin/tclsh
  2.  
  3. # http://esolangs.org/wiki/EXCON
  4.  
  5. # http://www2.tcl.tk/1591
  6. proc bits2int {bits} {
  7.      #returns integer equivalent of a bitlist
  8.      set bits [format %032s [join $bits {}]]
  9.      binary scan [binary format B* $bits] I1 x
  10.      set x
  11. }
  12.  
  13. # http://www.cab.u-szeged.hu/local/doc/tcl/Fragments.html#AscIntConv
  14. proc asc i {
  15.     if { $i<0 || $i>255 } { error "asc:Integer out of range 0-255" }
  16.     return [format %c $i ]
  17. }
  18.  
  19. set printf {}
  20.  
  21. puts "Welcome to the Quick and Dirty \"Hello World!\" EXCON parser."
  22. puts "============================================"
  23. puts "Default output is Binary."
  24. puts "Type in 'int' for integer and 'ascii' for string."
  25.  
  26. if { $argc != 1 } {
  27.     # no parameter
  28.     # default to binary
  29.     puts "Defaulting to binary print."
  30.     set printf {b}
  31. } else {
  32.     set param [lindex $argv 0]
  33.  
  34.     if { [string eq $param {int}] } {
  35.         puts "Printing as integer."
  36.         set printf {i}
  37.     } elseif { [string eq $param {ascii}] } {
  38.         puts "Printing as ASCII."
  39.         set printf {c}
  40.     } else {
  41.         puts "Unknown parameter.  Defaulting to binary."
  42.         set printf {b}
  43.     }
  44. }
  45.  
  46. #H
  47. lappend code {:<<<^<<<^!}
  48. #e
  49. lappend code {:^<<^<<<^<^!}
  50. #l
  51. lappend code {:<<^<^<<^<^!}
  52. #l
  53. lappend code {!}
  54. #o
  55. lappend code {:^<^<^<^<<^<^!}
  56. # space
  57. lappend code {:<<<<<^!}
  58. #W
  59. lappend code {:^<^<^<<^<<^!}
  60. #o
  61. lappend code {:^<^<^<^<<^<^!}
  62. #r
  63. lappend code {:<^<<<^<^<^!}
  64. #l
  65. lappend code {:<<^<^<<^<^!}
  66. #d
  67. lappend code {:<<^<<<^<^!}
  68. # bang
  69. lappend code {:^<<<<<^!}
  70.  
  71. # Clear buffer
  72. for {set i 0} {$i < 8} {incr i} {
  73.     set ar($i) 0
  74. }
  75.  
  76. set llen [llength $code]
  77. # Loop through instructions.
  78. for {set ctr 0} {$ctr < $llen} {incr ctr} {
  79.     set target [lindex $code $ctr]
  80.  
  81.     set len [string length $target]
  82.  
  83.     set needle 7
  84.  
  85.     for {set i 0} {$i < $len} {incr i} {
  86.         set char [string index $target $i]
  87.  
  88.         switch $char {
  89.             {:} {
  90.                 #reset the buffer.  do nothing.
  91.                 set reset 0
  92.                 for {set j 0} {$j < 8} {incr j} {
  93.                     set ar($j) 0
  94.                 }
  95.             }
  96.             {^} {
  97.                 #flips the bit of the needle.
  98.                 set ar($needle) 1
  99.             }
  100.             {!} {
  101.                 set bits {}
  102.                 for {set j 0} {$j < 8} {incr j} {
  103.                     append bits $ar($j)
  104.                 }
  105.  
  106.                 # output.
  107.                 if { [string eq $printf {i}] } {
  108.                     puts [bits2int $bits]
  109.                 } elseif { [string eq $printf {c}] } {
  110.                     set int [bits2int $bits]
  111.                     puts [asc $int]
  112.                 } else {
  113.                     puts $bits
  114.                 }
  115.             }
  116.             {<} {
  117.                  # move needle
  118.                  set needle [expr "$needle - 1"]
  119.             }
  120.         }
  121.     }
  122.    
  123. }
  124.  
  125. puts "Finished processing."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement