Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
pfeodrippe committed Jan 12, 2025
1 parent b4a3f8f commit 931634b
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 78 deletions.
9 changes: 6 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
## Master

- Don't make things const by default as we don't get `const` info from the jextracts anyway
- Add `vc/comptime`
- Support nested components in VybeC
- VybeC
- Don't make things const by default as we don't get `const` info from the jextracts anyway
- Add `vc/comptime`
- Support nested components in VybeC
- Static calls with no arguments are now called in compile time automatically
-

## v0.7.469

Expand Down
155 changes: 85 additions & 70 deletions src/vybe/c.clj
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,8 @@ signal(SIGSEGV, sighandler);
(= (:type v) :vector)
(format "{%s}"
(->> (:val v)
(mapv #(emit (analyze % (-> (ana/empty-env)
(update :locals merge (:locals (:env v)))))))
(str/join ", ")))

(string? (:val v))
Expand All @@ -842,72 +844,78 @@ signal(SIGSEGV, sighandler);
(str/join ", ")))

:static-call
(let [{:keys [method args] klass :class} v]
(case (->sym klass)
clojure.lang.Util
(case (->sym method)
identical
(format "(%s == %s)"
(emit (first args))
(emit (second args))))

clojure.lang.Numbers
(case (->sym method)
(add multiply divide minus gt gte lt lte)
(->> args
(mapv emit)
(str/join (format " %s "
('{multiply *
divide /
add +
minus -
gt > gte >= lt < lte <=}
(->sym method))))
parens)

inc
(format "(%s + 1)" (emit (first args)))

dec
(format "(%s - 1)" (emit (first args)))

abs
(format "vybe_abs(%s)" (emit (first args)))

;; bit-and
and
(apply format "(%s & %s)" (mapv emit args))

or
(apply format "(%s | %s)" (mapv emit args)))

clojure.lang.RT
(case (->sym method)
aset
(let [[s1 s2 s3] (mapv emit args)]
(-> (->> (format " %s[%s] = %s"
s1 s2 s3)
parens)
(str ";")))

(nth aget)
(let [[s1 s2] (mapv emit args)]
(->> (format " %s[%s] "
s1 s2)
parens))

get
(let [[s1 s2] (mapv emit args)]
(format "%s.%s" s1 s2))

intCast
(format "(int)%s" (emit (first args)))

floatCast
(format "(float)%s" (emit (first args)))

doubleCast
(format "(double)%s" (emit (first args))))))
(let [{:keys [form method args] klass :class} v]
;; If no args, then this is likely a constant.
(if (empty? args)
(emit (analyze (eval form)))
(case (->sym klass)
clojure.lang.Util
(case (->sym method)
identical
(format "(%s == %s)"
(emit (first args))
(emit (second args))))

clojure.lang.Numbers
(case (->sym method)
(add multiply divide minus gt gte lt lte)
(->> args
(mapv emit)
(str/join (format " %s "
('{multiply *
divide /
add +
minus -
gt > gte >= lt < lte <=}
(->sym method))))
parens)

inc
(format "(%s + 1)" (emit (first args)))

dec
(format "(%s - 1)" (emit (first args)))

abs
(format "vybe_abs(%s)" (emit (first args)))

;; bit-and
and
(apply format "(%s & %s)" (mapv emit args))

or
(apply format "(%s | %s)" (mapv emit args)))

clojure.lang.RT
(case (->sym method)
aset
(let [[s1 s2 s3] (mapv emit args)]
(-> (->> (format " %s[%s] = %s"
s1 s2 s3)
parens)
(str ";")))

(nth aget)
(let [[s1 s2] (mapv emit args)]
(->> (format " %s[%s] "
s1 s2)
parens))

get
(let [[s1 s2] (mapv emit args)]
(format "%s.%s" s1 s2))

intCast
(format "(int)%s" (emit (first args)))

longCast
(format "(long)%s" (emit (first args)))

floatCast
(format "(float)%s" (emit (first args)))

doubleCast
(format "(double)%s" (emit (first args)))))))

:keyword-invoke
(let [{:keys [keyword target]} v]
Expand Down Expand Up @@ -1832,6 +1840,10 @@ long long int: \"long long int\", unsigned long long int: \"unsigned long long i
[{:keys [args]}]
(format "(*%s)" (emit (first args))))

(defmethod c-macroexpand #'name
[{:keys [form]}]
(eval form))

(defmethod c-invoke #'merge
[{:keys [args]}]
(let [[target] args]
Expand Down Expand Up @@ -1867,11 +1879,14 @@ long long int: \"long long int\", unsigned long long int: \"unsigned long long i

(defmethod c-invoke #'vp/as
[{:keys [args]}]
(def args args)
(format "(%s)%s"
(-adapt-type (:val (second args)))
(emit (first args))))

(defmethod c-macroexpand #'vp/arr
[{:keys [args]}]
`(vp/as ~(first args) [:vec ~(second args)]))

(defmethod c-invoke #'vp/sizeof
[{:keys [args]}]
(if (= (count args) 2)
Expand Down Expand Up @@ -1904,9 +1919,9 @@ _my_v;
c-sym
c-sym
(emit (analyze `(merge ~'@_my_v
~(:form (first args)))
(-> (:env (first args))
(update :locals assoc '_my_v {})))))
~(:form (first args)))
(-> (:env (first args))
(update :locals assoc '_my_v {})))))
(format "((%s*)malloc(sizeof (%s)))" c-sym c-sym))))

(declare ^:private ^:no-ns memset)
Expand Down
25 changes: 20 additions & 5 deletions test/vybe/flecs_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,33 @@
["alice" {:x 10.0, :y 20.0}]}
(set @*acc))))))

#_(vc/defn* vybe-transform :- :void
[it :- vf/iter_t])

(vc/defn* ^:debug default-systems :- :void
[^:mut w :- :*]
(let [e (vf.c/ecs-entity-init w #vp/& (vf/entity_desc_t
{:name "vybe_transform"
:add (-> [(vc/comptime
(vf.c/vybe-pair (flecs/EcsDependsOn)
(flecs/EcsOnUpdate)))]
(vp/as [:vec :long]))}))]
:add (-> [(vf.c/vybe-pair (flecs/EcsDependsOn)
(flecs/EcsOnUpdate))]
(vp/arr :long))}))]
(vf.c/ecs-system-init
w #vp/& (vf/system_desc_t
{:entity e
:query {}
#_:callback
:query {:terms [{:first {:name (name vt/Translation)} :inout (flecs/EcsIn)}
{:first {:name (name vt/Rotation)} :inout (flecs/EcsIn)}
{:first {:name (name vt/Scale)} :inout (flecs/EcsIn)}
{:first {:name (name vt/Transform)}
:second {:name "global"}
:inout (flecs/EcsOut)}
{:first {:name (name vt/Transform)}
:inout (flecs/EcsOut)}
{:first {:name (name vt/Transform)}
:second {:name "global"}
:src {:id (bit-or (flecs/EcsCascade) (flecs/EcsUp))}
:inout (flecs/EcsOut)
:oper (flecs/EcsOptional)}]}
#_ #_:callback (-system-callback
(fn [it-p]
(let [it (vp/jx-p->map it-p ecs_iter_t)
Expand Down

0 comments on commit 931634b

Please sign in to comment.