Created
March 6, 2022 15:21
-
-
Save retrogradeorbit/063f2874eb6155dfe781db9acf1cecad to your computer and use it in GitHub Desktop.
babashka C?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns c99 | |
(:require [clojure.core :as core] | |
[clojure.string :as string])) | |
(def e' clojure.walk/macroexpand-all) | |
(core/defmacro defn [var bindings & body] | |
(str (:tag (meta var)) " " var "(" | |
(->> bindings | |
(map (fn [v] | |
(let [{:keys [line column tag]} (meta v)] | |
(str tag " " v) | |
))) | |
(string/join ", ")) | |
")\n" | |
"{\n" | |
(apply str | |
(map #(str "\t" %) | |
(e' body))) | |
"}")) | |
(core/defmacro return [body] | |
(str "return(" | |
(e' body) | |
");\n")) | |
(core/defmacro + [a b] | |
(str "(" (e' a) "+" (e' b) ")")) | |
(core/defmacro * [a b] | |
(str "(" (e' a) "*" (e' b) ")")) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bb | |
(require '[c99 :as c]) | |
(println | |
(c/defn ^int calculate [^int x ^int y ^int p ^int q] | |
(c/return | |
(c/* | |
(c/+ x y) | |
(c/+ p q))))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ BABASHKA_CLASSPATH=. bb gen.clj | |
int calculate(int x, int y, int p, int q) | |
{ | |
return(((x+y)*(p+q))); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment