Advertisement
Guest User

Untitled

a guest
Feb 16th, 2010
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 3.54 KB | None | 0 0
  1. (ns whitespace
  2.   (:gen-class)
  3.   (:use [clojure.contrib.duck-streams]))
  4.  
  5. (defmulti translate class)
  6. (defmulti read-wsl #(.exists (java.io.File. %)))
  7.  
  8. (defn sub
  9.   "Oh god, I hate writing .replaceAll"
  10.   [from to ph]
  11.   (.replaceAll ph from to))
  12.  
  13.  
  14. (defmethod translate Integer [n]
  15.   (->> n
  16.        Math/abs
  17.        Integer/toBinaryString
  18.        seq
  19.        (map #(if (= \0 %) " " "\t"))
  20.        (apply str)
  21.        (format "%s%s\n" (if (pos? n) " " "\t"))))
  22.  
  23. (defmethod translate clojure.lang.LazySeq [al]
  24.   (let
  25.       [voc
  26.        ;; Stack Manipulation
  27.        {:push "  "
  28.         :dupl " \n "
  29.         :copy " \t "
  30.         :swap " \n\t"
  31.         :disc " \n\n"
  32.         :slid " \t\n"
  33.         ;; Arithmetic
  34.         :add "\t   "
  35.         :sub "\t  \t"
  36.         :mul "\t  \n"
  37.         :div "\t \t "
  38.         :mod "\t \t\t"
  39.         ;; Heap Access
  40.         :store "\t\t "
  41.         :retri "\t\t\t"
  42.         ;; Flow Control
  43.         :mark "\n  "
  44.         :call "\n \t"
  45.         :ju "\n \n"
  46.         :jzero "\n\t "
  47.         :jneg "\n\t\t"
  48.         :ends "\n\t\n"
  49.         :end "\n\n\n"
  50.         ;; I/O
  51.         :outc "\t\n  "
  52.         :outi "\t\n \t"
  53.         :inc  "\t\n\t "
  54.         :ini  "\t\n\t\t"
  55.         ;; Etc
  56.         }]
  57.     (apply str (map #(if (integer? %) (translate-int %) (voc %)) al))))
  58.  
  59.  
  60. (defmethod read-wsl false [string]
  61.   (->> string
  62.        (sub "#.*" "")
  63.        (re-seq #"[^ \n\t]+")
  64.        (map #(let [n %] (try (Integer/parseInt n)
  65.                              (catch Exception e (keyword n)))))))
  66.  
  67. (defmethod read-wsl true [file]
  68.   (read-wsl-string (slurp file)))
  69.  
  70.  
  71. (comment defn -main [& args]
  72.   (->> (first args)
  73.        (read-wsl-file)
  74.        (translate-seq)
  75.        (print)))
  76.  
  77.  
  78. (def programa
  79.      "
  80.  
  81. ## Salva a em 0, f em 1
  82. push 0 push 97 store
  83. push 1 inc
  84.  
  85. ## Ajusta o último argumetno
  86. push 1
  87. push 1 retri
  88. push 1 add
  89. store
  90.  
  91. ## 2 Loops pra processar elementos
  92.  
  93. push 1 retri
  94. push 0 retri
  95. sub
  96.  
  97. ## Loop 1
  98. mark 1
  99. dupl jzero 2
  100. dupl push 1 retri sub push -1 mul
  101. call 101
  102. push 1 sub
  103. ju 1
  104.  
  105. mark 2 disc
  106.  
  107. push 1 retri
  108. push 0 retri
  109. sub
  110. push 1 sub
  111.  
  112. ## Loop 2
  113. mark 3
  114. dupl jzero 4
  115. dupl push 0 retri add push 1 sub
  116. call 101
  117. push 1 sub
  118. ju 3
  119.  
  120. mark 4
  121. end
  122.  
  123. ## Rotina pra imprimir caracteres. [caracter]
  124. mark 101
  125. dupl dupl dupl dupl
  126. call 103
  127. outc
  128. push 0 retri sub
  129. jzero 102
  130. call 104
  131. outc
  132. push 1 push 1
  133. mark 102
  134. disc disc
  135. push 10 outc
  136. ends
  137.  
  138. ## Rotina pra calcular espaços anteriores [caracter]
  139. mark 103
  140. push 1 retri
  141. sub
  142. push -1 mul
  143. push 1 sub
  144. call 105
  145. ends
  146.  
  147. ## Rotina pra calcular espaços posteriores [caracter]
  148. mark 104
  149. push 0 retri sub
  150. push 2 mul
  151. push 1 sub
  152. call 105
  153. ends
  154.  
  155. ## Rotina pra imprimir espaços
  156. mark 105
  157. dupl
  158. jzero 106
  159. push 32
  160. outc
  161. push 1
  162. sub
  163. ju 105
  164. mark 106
  165. disc
  166. ends
  167.  
  168. ")
  169.  
  170. (spit "/tmp/losango.ws" (->> programa read-wsl translate-seq doall))
  171.  
  172. ;; Arquivo gerado abaixo, entre aspas
  173.  
  174. "    
  175.                
  176.                
  177.    
  178.            
  179.    
  180.                
  181.                    
  182.                  
  183.                    
  184.    
  185.  
  186.  
  187.          
  188.  
  189.        
  190.                            
  191.      
  192.  
  193.                    
  194.    
  195.        
  196.  
  197.    
  198.  
  199.      
  200.  
  201.  
  202.    
  203.                  
  204.                        
  205.        
  206.        
  207.  
  208.  
  209.          
  210.  
  211.      
  212.                        
  213.        
  214.                    
  215.    
  216.        
  217.  
  218.        
  219.  
  220.      
  221.  
  222.  
  223.  
  224.  
  225.                
  226.  
  227.  
  228.  
  229.  
  230.  
  231.                        
  232.    
  233.          
  234.                    
  235.                      
  236.  
  237.                    
  238.    
  239.        
  240.    
  241.  
  242.                  
  243.  
  244.  
  245.  
  246.  
  247.          
  248.    
  249.  
  250.    
  251.  
  252.                    
  253.    
  254.                            
  255.      
  256.    
  257.        
  258.                    
  259.  
  260.    
  261.  
  262.                
  263.      
  264.                          
  265.      
  266.    
  267.        
  268.                    
  269.  
  270.    
  271.  
  272.                
  273.  
  274.  
  275.                      
  276.          
  277.    
  278.        
  279.        
  280.  
  281.                
  282.  
  283.                  
  284.  
  285.  
  286.  
  287.    
  288. "
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement