Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (ns whitespace
- (:gen-class)
- (:use [clojure.contrib.duck-streams]))
- (defmulti translate class)
- (defmulti read-wsl #(.exists (java.io.File. %)))
- (defn sub
- "Oh god, I hate writing .replaceAll"
- [from to ph]
- (.replaceAll ph from to))
- (defmethod translate Integer [n]
- (->> n
- Math/abs
- Integer/toBinaryString
- seq
- (map #(if (= \0 %) " " "\t"))
- (apply str)
- (format "%s%s\n" (if (pos? n) " " "\t"))))
- (defmethod translate clojure.lang.LazySeq [al]
- (let
- [voc
- ;; Stack Manipulation
- {:push " "
- :dupl " \n "
- :copy " \t "
- :swap " \n\t"
- :disc " \n\n"
- :slid " \t\n"
- ;; Arithmetic
- :add "\t "
- :sub "\t \t"
- :mul "\t \n"
- :div "\t \t "
- :mod "\t \t\t"
- ;; Heap Access
- :store "\t\t "
- :retri "\t\t\t"
- ;; Flow Control
- :mark "\n "
- :call "\n \t"
- :ju "\n \n"
- :jzero "\n\t "
- :jneg "\n\t\t"
- :ends "\n\t\n"
- :end "\n\n\n"
- ;; I/O
- :outc "\t\n "
- :outi "\t\n \t"
- :inc "\t\n\t "
- :ini "\t\n\t\t"
- ;; Etc
- }]
- (apply str (map #(if (integer? %) (translate-int %) (voc %)) al))))
- (defmethod read-wsl false [string]
- (->> string
- (sub "#.*" "")
- (re-seq #"[^ \n\t]+")
- (map #(let [n %] (try (Integer/parseInt n)
- (catch Exception e (keyword n)))))))
- (defmethod read-wsl true [file]
- (read-wsl-string (slurp file)))
- (comment defn -main [& args]
- (->> (first args)
- (read-wsl-file)
- (translate-seq)
- (print)))
- (def programa
- "
- ## Salva a em 0, f em 1
- push 0 push 97 store
- push 1 inc
- ## Ajusta o último argumetno
- push 1
- push 1 retri
- push 1 add
- store
- ## 2 Loops pra processar elementos
- push 1 retri
- push 0 retri
- sub
- ## Loop 1
- mark 1
- dupl jzero 2
- dupl push 1 retri sub push -1 mul
- call 101
- push 1 sub
- ju 1
- mark 2 disc
- push 1 retri
- push 0 retri
- sub
- push 1 sub
- ## Loop 2
- mark 3
- dupl jzero 4
- dupl push 0 retri add push 1 sub
- call 101
- push 1 sub
- ju 3
- mark 4
- end
- ## Rotina pra imprimir caracteres. [caracter]
- mark 101
- dupl dupl dupl dupl
- call 103
- outc
- push 0 retri sub
- jzero 102
- call 104
- outc
- push 1 push 1
- mark 102
- disc disc
- push 10 outc
- ends
- ## Rotina pra calcular espaços anteriores [caracter]
- mark 103
- push 1 retri
- sub
- push -1 mul
- push 1 sub
- call 105
- ends
- ## Rotina pra calcular espaços posteriores [caracter]
- mark 104
- push 0 retri sub
- push 2 mul
- push 1 sub
- call 105
- ends
- ## Rotina pra imprimir espaços
- mark 105
- dupl
- jzero 106
- push 32
- outc
- push 1
- sub
- ju 105
- mark 106
- disc
- ends
- ")
- (spit "/tmp/losango.ws" (->> programa read-wsl translate-seq doall))
- ;; Arquivo gerado abaixo, entre aspas
- "
- "
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement