Skip to content

Instantly share code, notes, and snippets.

@retrogradeorbit
Created March 6, 2022 15:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save retrogradeorbit/063f2874eb6155dfe781db9acf1cecad to your computer and use it in GitHub Desktop.
Save retrogradeorbit/063f2874eb6155dfe781db9acf1cecad to your computer and use it in GitHub Desktop.
babashka C?
(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) ")"))
#!/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)))))
$ 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