Skip to content

Commit

Permalink
adding rust functions
Browse files Browse the repository at this point in the history
  • Loading branch information
zcaudate committed Jul 7, 2024
1 parent 12dbe16 commit 0fb17cc
Show file tree
Hide file tree
Showing 17 changed files with 606 additions and 171 deletions.
2 changes: 1 addition & 1 deletion src/js/react.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
{:macro-only true
:bundle {:default [["react" :as React]]
:nil [["react-nil" :as ReactNIL]]
:dom [["react-dom" :as ReactDOM]]}
:dom [["react-dom/client" :as ReactDOM]]}
:import [["react" :as React]]
:require [[js.core :as j]
[xt.lang.base-lib :as k]]
Expand Down
5 changes: 1 addition & 4 deletions src/rt/solidity/grammar.clj
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@
(let [block (emit-fn/emit-fn-block :defn grammar)
typestr (emit-fn/emit-fn-type sym nil grammar mopts)
preamble (emit-fn/emit-fn-preamble [:defn sym args]
block
grammar
mopts)
codebody (emit-block/emit-block-body nil block body grammar mopts)]
Expand Down Expand Up @@ -125,9 +124,7 @@
"creates an event"
{:added "4.0"}
[[_ sym & args] grammar mopts]
(let [block (emit-fn/emit-fn-block :defn grammar)
preamble (emit-fn/emit-fn-preamble [:defn sym (vec (mapcat identity args))]
block
(let [preamble (emit-fn/emit-fn-preamble [:defn sym (vec (mapcat identity args))]
grammar
mopts)]
(str "event " preamble ";")))
Expand Down
15 changes: 8 additions & 7 deletions src/std/lang/base/emit_assign.clj
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,18 @@
([_ {:keys [raw] :as props} [_ & args] grammar mopts]
(let [{:keys [sep space assign]} (helper/get-options grammar [:default :define])
args (helper/emit-typed-args args grammar)

argstrs (map (fn [{:keys [value symbol] :as arg}]
(let [{:assign/keys [inline template] :as aopts} (meta value)
custom (cond (:assign/fn aopts) [:raw ((:assign/fn aopts) symbol)]
template [:template (h/prewalk-replace {template symbol} value)]
inline [:inline (emit-def-assign-inline symbol
value
grammar
mopts)])]
template [:template (h/prewalk-replace {template symbol} value)]
inline [:inline (emit-def-assign-inline symbol
value
grammar
mopts)])]
(if custom
(common/*emit-fn* (second custom) grammar mopts)
(fn/emit-input-default arg assign grammar mopts))))
(common/*emit-fn* (second custom) grammar mopts)
(fn/emit-input-default arg assign grammar mopts))))
args)
vstr (str/join (str sep space) argstrs)
rawstr (if (not-empty raw) (str raw space))]
Expand Down
4 changes: 3 additions & 1 deletion src/std/lang/base/emit_block.clj
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@
(emit-block-controls key block control ctl-args grammar mopts)))
(common/newline-indent)
end
(if (and wrap (not (:append wrap))) (common/newline-indent))
(if (and wrap (not (:append wrap)))
(common/newline-indent))

(:end wrap)))))

Expand All @@ -204,6 +205,7 @@
([key {:keys [main control] :as block} form grammar mopts]
(let [[raw params args ctl-args wrap] (emit-block-setup key block form grammar mopts)
{:keys [start end append]} wrap]
(h/prn (emit-block-setup key block form grammar mopts))
(str start
raw
(if (:parameter main)
Expand Down
31 changes: 19 additions & 12 deletions src/std/lang/base/emit_common.clj
Original file line number Diff line number Diff line change
Expand Up @@ -721,9 +721,13 @@
(defn emit-class-static-invoke
"invokes a constructor"
{:added "3.0"}
([raw [sym new & args] grammar mopts]
([raw [sym field & args] grammar mopts]
(let [{:keys [static]} (helper/get-options grammar [:default :invoke])]
(emit-invoke-raw (str sym (or raw static) new) args grammar mopts))))
(cond (keyword? field)
(str sym (or raw static) (name field))

:else
(emit-invoke-raw (str sym (or raw static) field) args grammar mopts)))))

;;
;; INDEX
Expand All @@ -738,16 +742,19 @@
(str "." (emit-symbol v grammar mopts)))

(h/form? v)
(do (assert (symbol? (first v)))
(let [{:keys [apply]} (helper/get-options grammar [:default :invoke])
braces (meta (first v))]
(emit-invoke-raw (str apply
(emit-symbol (first v) grammar mopts)
#_(*emit-fn* (first v) grammar mopts)
(if (not-empty braces)
(*emit-fn* braces grammar mopts)
""))
(rest v) grammar mopts)))
(let [sym (first v)
sym (if (string? sym)
(symbol sym)
sym)
_ (assert (symbol? sym))
{:keys [apply]} (helper/get-options grammar [:default :invoke])
braces (meta sym)]
(emit-invoke-raw (str apply
(emit-symbol sym grammar mopts)
(if (not-empty braces)
(*emit-fn* braces grammar mopts)
""))
(rest v) grammar mopts))

(vector? v)
(do (assert (= 1 (count v)) "Only one index")
Expand Down
1 change: 0 additions & 1 deletion src/std/lang/base/emit_data.clj
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@
(custom form grammar mopts)
(emit-coll key form grammar mopts)))))


(defn emit-data
"main function for data forms"
{:added "3.0"}
Expand Down
89 changes: 61 additions & 28 deletions src/std/lang/base/emit_fn.clj
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,27 @@

tmodarr (mapv arr-fn tmod)
kmodarr (mapv arr-fn kmod)
mod-rev? (and (or (empty tmodarr)
mod-rev? (and (or (empty tmodarr)
(not-empty kmodarr))
reversed)
reversed)
mod-has? (or (not-empty tmodarr)
(not-empty kmodarr))
mod-sym (str (if symbol
(common/*emit-fn* symbol grammar mopts))
(if (and mod-rev?
mod-has?)
hint))
mixarr (concat (if (or (not mod-rev?)
(not-empty tmodarr))
kmodarr)
[(str (if symbol
(common/*emit-fn* symbol grammar mopts))
(if mod-rev? hint))]
(if mod-rev?
(or (not-empty tmodarr)
kmodarr)))
(filter not-empty [mod-sym])
(if mod-rev? mod-has?))
mixstr (str/join " "
(filter (fn [x]
(if (seq x)
(not-empty x)
x)) mixarr))]
x))
mixarr))]
(str mixstr
(if (not-empty vmod)
(str/join
Expand All @@ -64,10 +68,12 @@
(let [arr (cond-> (:- (meta name))
:then vec
(not-empty suffix) (conj suffix))]

(str/join " " (map (fn [v]
(cond (or (keyword? v)
(vector? v)
(string? v))
(string? v)
(and (vector? v)
(empty? v)))
(cond-> (h/strn v)
(:uppercase type) (str/upper-case))

Expand Down Expand Up @@ -95,20 +101,31 @@
(h/merge-nested (get-in grammar [:default :function])
(get-in grammar [:function key])))

(defn emit-fn-preamble
(defn emit-fn-preamble-args
"constructs the function preamble"
{:added "4.0"}
([[key name args] block grammar mopts]
([key args grammar mopts]
(let [args (helper/emit-typed-args args grammar)
{:keys [sep space assign start end multiline]}
(h/merge-nested (helper/get-options grammar [:default :function :args])
(get-in grammar [:function key :args]))
iargs (map #(emit-input-default % assign grammar mopts)
args)]
(str (if name (common/*emit-fn* name grammar mopts))
(get-in grammar [:function key :args]))]
(map #(emit-input-default % assign grammar mopts)
args))))

(defn emit-fn-preamble
"constructs the function preamble"
{:added "4.0"}
([[key name args] grammar mopts]
(let [iargs (emit-fn-preamble-args key args grammar mopts)
{:keys [prefix]} (helper/get-options grammar [:default :function])
{:keys [sep space assign start end multiline]}
(h/merge-nested (helper/get-options grammar [:default :function :args])
(get-in grammar [:function key :args]))]
(str (if prefix (str prefix " "))
(if name (common/*emit-fn* name grammar mopts))
space
(cond (empty? iargs) (str start end)

multiline
(str start
(common/with-indent [2]
Expand All @@ -131,16 +148,32 @@
[args & body] body
header-only? (:header (meta name))
{:keys [compressed] :as block} (emit-fn-block key grammar)
typestr (emit-fn-type name (or (:raw block) (h/strn tag)) grammar mopts)
prestr (emit-fn-preamble [key name args] block grammar mopts)]

(str (if (not-empty typestr)
(str typestr " "))
prestr
(if header-only?
(get-in grammar [:default :common :statement])
(binding [common/*compressed* compressed]
(block/emit-block-body key block body grammar mopts)))))))
{:keys [enabled assign space after]} (helper/get-options grammar [:default :typehint])
typestr (emit-fn-type name (or (:raw block) (h/strn tag)) grammar mopts)
prestr (emit-fn-preamble [key name args] grammar mopts)
hintstr (cond (or (not enabled)
(empty? typestr)) ""

after (str assign space typestr)

:else (str typestr space assign))
blockstr (if header-only?
(get-in grammar [:default :common :statement])
(binding [common/*compressed* compressed]
(block/emit-block-body key block body grammar mopts)))]
(cond enabled
(cond after
(str/join space (filter not-empty [prestr hintstr blockstr]))


:else
(str/join space (filter not-empty [hintstr prestr blockstr])))

(not-empty typestr)
(str typestr " " prestr blockstr)

:else
(str prestr " " blockstr)))))

;;
;;
Expand Down
1 change: 1 addition & 0 deletions src/std/lang/base/emit_helper.clj
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
:block {:statement ""
:parameter {:start "(" :end ")"}
:body {:start "{" :end "}"}}
:typehint {:enabled false :assign "->" :space " " :after false}
:function {:raw "function"
:args {:start "(" :end ")" :space ""}
:body {:start "{" :end "}"}}
Expand Down
5 changes: 2 additions & 3 deletions src/std/lang/base/emit_preprocess.clj
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,11 @@
(h/error "Var not found" {:input (second input)}))))
(list '!:eval input))

#_#_
(and (symbol? tag)
(str/includes? (str tag) "$$"))
(let [[cls func] (str/split (str tag) #"\$\$")]
(concat '(static-invoke)
[(symbol cls) func]
(rest x)))))
(concat '($) [(symbol cls) func] (rest x)))))

(defn to-input
"converts a form to input (extracting deref forms)"
Expand Down
4 changes: 2 additions & 2 deletions src/std/lang/base/grammar_spec.clj
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,10 @@
;;

(def +op-class+
[{:op :new :symbol #{'new} :emit :new :raw "new"}
[{:op :new :symbol #{'new} :emit :new :value true :raw "new"}
{:op :defclass :symbol #{'defclass} :spec :defclass
:type :def :section :code :abstract true}
{:op :static-invoke :symbol #{'static-invoke} :emit :static-invoke}
{:op :static-invoke :symbol #{'$} :emit :static-invoke}
{:op :this :symbol #{'this} :emit :throw :raw "this" :value true}
{:op :super :symbol #{'super} :emit :invoke :raw "super" :value true}
{:op :fn.inner :symbol #{'fn.inner} :type :fn :block {:raw "" :main #{:body}}}
Expand Down
5 changes: 4 additions & 1 deletion src/std/lang/base/impl_entry.clj
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@
form-input (preprocess/to-input form)
entry (book/book-entry (merge {:op op
:op-key (:op reserved)
:id sym
:id (cond (vector? sym)
(first (filter symbol? sym))

:else sym)
:form-input form-input
:section (or section :code)
:priority priority}
Expand Down
9 changes: 7 additions & 2 deletions src/std/lang/base/script_macro.clj
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@
(let [{:keys [format section priority]} reserved
[module fmeta] (intern-prep lang form-raw)
form (apply list op sym body)

[tmeta entry] (binding [preprocess/*macro-splice* (:static/template smeta)]
(entry/create-code-raw form
reserved
Expand All @@ -198,6 +199,10 @@
{:lang lang
:module module})))
lib (impl/runtime-library)
sym (cond (vector? sym)
(first (filter symbol? sym))

:else sym)
var (ptr/ptr-intern (:namespace entry)
(with-meta sym (merge tmeta
(if (:fn reserved)
Expand All @@ -216,7 +221,7 @@
module (lib/get-module lib
(:lang entry)
(:module entry))

;;
;; LINT HACK FOR JS
;;
Expand Down Expand Up @@ -262,7 +267,7 @@
`(intern-top-level-fn ~lang
(quote [~op ~reserved])
(quote ~&form)
(merge ~(meta sym)
(merge (quote ~(meta sym))
{:time (h/time-ns)})))))))

(defn intern-macros
Expand Down
Loading

0 comments on commit 0fb17cc

Please sign in to comment.