Skip to content

Commit

Permalink
adding some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zcaudate committed Jan 7, 2025
1 parent aba1ef5 commit 56644c1
Show file tree
Hide file tree
Showing 13 changed files with 900 additions and 133 deletions.
50 changes: 50 additions & 0 deletions src-build/meta/gen_big_file.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
(ns concat-files
(:require [std.fs :as fs]
[clojure.java.io :as io]))

(def +all-files+
(sort (keys (fs/list "src" {:recursive true
:include [".clj$"]}))))

(defn concat-files
[& [file-filter out-file dir root]]
(let [out-file (or out-file "output.clj")
root (or root ".")
root-path (fs/path root)
dir (or dir "src")
proj-file "project.clj"]
(with-open [writer (io/writer out-file)]
(let [all-paths (cons (str (fs/path root proj-file))
(sort (keys (fs/list (fs/path root dir)
{:recursive true
:include [(or file-filter ".clj$")]}))))]

(doseq [p all-paths]
(let [relative-path (fs/relativize root-path p)]
(.write writer (str "\n\n\n;;; ------- " relative-path "------- \n\n\n"))
(try (with-open [r (io/reader p)]
(doseq [line (line-seq r)]
(.write writer ^String line)
(.write writer "\n")))
(catch Throwable t))))))))

(comment
(concat-files)

(fs/list (fs/path "test")
{:recursive true
:include [".clj$"]})
(concat-files nil
"output-xtalk.clj"
"src/xt")
(concat-files nil
"output-tests.clj"
"test")


(concat-files ".clj$"
"output-xtalk.clj"
"../statstrade-core/src/statsdb"
"../statstrade-core")

)
28 changes: 14 additions & 14 deletions src/std/lang/base/compile.clj
Original file line number Diff line number Diff line change
Expand Up @@ -102,20 +102,20 @@
(defn compile-module-schema
"compiles all namespaces into a single file (for sql)"
{:added "4.0"}
([{:keys [header footer lang main root target] :as opts}]
(let [mopts (last (impl/emit-options opts))
lib (impl/runtime-library)
snapshot (lib/get-snapshot lib)
book (snap/get-book snapshot lang)
deps (h/deps:ordered book [main])
full-arr (map (fn [module-id]
(-> (lifecycle/emit-module-setup module-id
mopts)
(compile/compile-fullbody opts)))
deps)
full (str/join "\n\n" full-arr)
output (compile/compile-out-path opts)]
(compile/compile-write output full))))
([{:keys [header footer lang main root target] :as opts}]
(let [mopts (last (impl/emit-options opts))
lib (impl/runtime-library)
snapshot (lib/get-snapshot lib)
book (snap/get-book snapshot lang)
deps (h/deps:ordered book [main])
full-arr (map (fn [module-id]
(-> (lifecycle/emit-module-setup module-id
mopts)
(compile/compile-fullbody opts)))
deps)
full (str/join "\n\n" full-arr)
output (compile/compile-out-path opts)]
(compile/compile-write output full))))

(def +install-module-schema-fn+
(compile/types-add :module.schema #'compile-module-schema))
2 changes: 1 addition & 1 deletion src/std/lang/model/spec_js.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
[std.lang.base.grammar :as grammar]
[std.lang.base.grammar-spec :as spec]
[std.lang.base.util :as ut]
[std.lang.base.book :as book]
[std.lang.base.book :as book]
[std.lang.base.script :as script]
[std.lang.model.spec-xtalk]
[std.lang.model.spec-xtalk.fn-js :as fn]
Expand Down
79 changes: 79 additions & 0 deletions src/xt/demo/brainfuck.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
(ns xt.demo.brainfuck
(:require [std.lang :as l]
[std.lib :as h]))

(l/script :xtalk
{:require [[xt.lang.base-lib :as k]]
:export [MODULE]})

(defn.xt brainfuck-interpreter
"Interprets and executes Brainfuck code"
{:added "1.0"}
[bfCode]
;; Initialize memory, pointer, and output
(var memory {})
(k/for:index [i [(x:offset 0)
(x:offset 30000)]]
(= (. memory [i]) 0))
(var output "")
(var codeLength (k/len bfCode))
(var i 0) ;; Instruction pointer

;; Execute Brainfuck commands
(while (< i codeLength)
(var command (k/get-idx bfCode i))
(cond
;; Increment the pointer
(== command ">") (:= pointer (+ pointer 1))

;; Decrement the pointer
(== command "<") (:= pointer (- pointer 1))

;; Increment the value at the current cell
(== command "+")
(k/set-idx memory pointer (mod (+ (k/get-idx memory pointer) 1) 256))

;; Decrement the value at the current cell
(== command "-")
(k/set-idx memory pointer (mod (- (+ (k/get-idx memory pointer) 256) 1) 256))

;; Output the character at the current cell
(== command ".")
(:= output (k/cat output (k/char (k/get-idx memory pointer))))

;; Input a character into the current cell
(== command ",")
(do
(var input (k/read "Input a character:"))
(k/set-idx memory pointer (:? input (k/char-code (k/first input)) 0)))

;; Jump forward if the current cell is 0
(== command "[")
(if (== (k/get-idx memory pointer) 0)
(do
(var bracketCounter 1)
(while (> bracketCounter 0)
(:= i (+ i 1))
(if (== (k/get-idx bfCode i) "[")
(:= bracketCounter (+ bracketCounter 1)))
(if (== (k/get-idx bfCode i) "]")
(:= bracketCounter (- bracketCounter 1))))))

;; Jump backward if the current cell is non-zero
(== command "]")
(if (not= (k/get-idx memory pointer) 0)
(do
(var bracketCounter 1)
(while (> bracketCounter 0)
(:= i (- i 1))
(if (== (k/get-idx bfCode i) "]")
(:= bracketCounter (+ bracketCounter 1)))
(if (== (k/get-idx bfCode i) "[")
(:= bracketCounter (- bracketCounter 1))))))))
;; Move to the next instruction
(:= i (+ i 1))

;; Return the output
(return output))

(def.xt MODULE (!:module))
31 changes: 31 additions & 0 deletions src/xt/demo/fizzbuzz.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
(ns xt.demo.fizzbuzz
(:require [std.lang :as l]
[std.lib :as h]))

(l/script :xtalk
{:require [[xt.lang.base-lib :as k]]
:export [MODULE]})

(defn.xt fizzbuzz
"Generates FizzBuzz for numbers from 1 to n"
{:added "1.0"}
[n]
;; Initialize an empty array for results
(var result [])
;; Iterate through numbers 1 to n
(k/for:index [i [1 (+ n 1) 1]]
(var value nil)
;; Check divisibility for FizzBuzz
(if (and (== 0 (mod i 3)) (== 0 (mod i 5)))
(:= value "FizzBuzz")
(if (== 0 (mod i 3))
(:= value "Fizz")
(if (== 0 (mod i 5))
(:= value "Buzz")
(:= value i))))
;; Append the value to the result array
(x:arr-push result value))
;; Return the result array
(return result))

(def.xt MODULE (!:module))
Loading

0 comments on commit 56644c1

Please sign in to comment.