diff --git a/src/rustint/core.clj b/src/rustint/core.clj index 5f78f62..52acc5c 100644 --- a/src/rustint/core.clj +++ b/src/rustint/core.clj @@ -1900,10 +1900,35 @@ ; ; nil ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;TODO -;(defn listar -; -;) +(defn flistar [ilevel t nxt] + (cond + (string? t) (format "\"%s\" " t) + (= t (symbol "{")) (clojure.string/join + (cons "\n{\n" (repeat (* ilevel 2) " ")) + ) + (= t (symbol "}")) "\n}" + (and (not= nxt (symbol "{")) (not= nxt (symbol "}"))) (format "%s " t) + :else (format "%s" t) + ) +) + +(defn listar [l] + (let [contadores (hash-map (symbol "{") 1, (symbol "}") -1), + ident_levels (reductions + (map #(get contadores %1 0) l)), + ] + (dorun + ( + map + print + (map + #(apply flistar (cons %1 %2)) + ident_levels + (partition 2 1 (concat l '(nil))) + ) + ) + ) + ) +) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; AGREGAR-PTOCOMA: Recibe una lista con los tokens de un programa en Rust y la devuelve con un token ; insertado a continuacion de ciertas } (llaves de cierre, pero no a continuacion de todas ellas). diff --git a/test/rustint/core_test.clj b/test/rustint/core_test.clj index c132f1f..e74aa1c 100644 --- a/test/rustint/core_test.clj +++ b/test/rustint/core_test.clj @@ -446,3 +446,17 @@ ) ) ) + +(deftest test-listar-01 + (testing "Test 01 listar" + (let [ + printed (with-out-str (listar (list 'fn 'main (symbol "(") (symbol ")") (symbol "{") 'println! (symbol "(") "Hola, mundo!" (symbol ")") (symbol "}")))) + ] + (is (= + printed + "fn main ( )\n{\n println! ( \"Hola, mundo!\" )\n}" + ) + ) + ) + ) +)