Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP oCaml language support #155

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions resources/public/ocaml-dbg.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>KLIPSE: a simple and elegant online cljs compiler and evaluator</title>
<link rel='shortcut icon' type='image/x-icon' href='img/klipse.png' />
<link rel="stylesheet" type="text/css" href="css/codemirror.css">
</head>
<body>
<pre>
<code class="eval-ocaml">
let rec fib (n: int): int =
match n with
| 0 -> 1
| 1 -> 1
| _ -> (fib (n - 1)) + (fib (n - 2))

let () =
Js.log (fib 9)
</code>
</pre>
<p> Transpilation </p>
<pre>
<code class="transpile-ocaml">
let rec fib (n: int): int =
match n with
| 0 -> 0
| 1 -> 1
| _ -> (fib (n - 1)) + (fib (n - 2))

let () =
Js.log (fib 6)
</code>
</pre>
<br/>
<script>
window.klipse_settings = {
selector_transpile_ocaml: '.transpile-ocaml',
selector_eval_ocaml: '.eval-ocaml',
};
</script>
<script src="/lib/mirror_extensions.js"></script>
<script src="/fig/js/klipse.fig.js"></script>
</body>
</html>
59 changes: 59 additions & 0 deletions src/klipse/lang/ocaml.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
(ns klipse.lang.ocaml
(:require-macros
[gadjett.core :refer [dbg]]
[klipse.macros :refer [my-with-redefs]]
[purnam.core :refer [!>]]
[cljs.core.async.macros :refer [go]])
(:require
[clojure.string :as string]
[cljs.core.async :refer [chan put!]]
[klipse.common.registry :refer [codemirror-mode-src register-mode]]))

(def eval-in-global-scope js/eval); this is the trick to make `eval` work in the global scope: http://perfectionkills.com/global-eval-what-are-the-options/

(defn ocaml-to-js [src]
(let [{:keys [js_error_msg js_code]} (js->clj (-> (js/ocaml.compile src)
js/JSON.parse) :keywordize-keys true)];TODO - use transit JSON reader
(if js_error_msg [:error js_error_msg]
[:ok js_code])))

(defn eval-ocaml [exp _]
(let [c (chan)]
(my-with-redefs [js/console.log (fn[& args]
(put! c (string/join " " args))
(put! c "\n"))]

(try
(set! js/exports #js {})
(let [[status res] (ocaml-to-js exp)]
(if (= :error status) (put! c res)
(put! c (-> res
eval-in-global-scope
js/prettyFormat))))
(catch :default o
(str o))))
c))

(defn transpile-ocaml [exp _]
(go
(let [[_ res] (ocaml-to-js exp)]
res)))

(def eval-opts {:editor-in-mode "text/x-ocaml"
:editor-out-mode "javascript"
:beautify? false
:eval-fn eval-ocaml
:external-scripts [(codemirror-mode-src "mllike") "https://viebel.github.io/klipse/repo/js/ocaml-compiler.js" "https://viebel.github.io/klipse/repo/js/pretty_format.js"]
; oCaml does not know a single line comment
})

(def transpile-opts {:editor-in-mode "text/x-ocaml"
:editor-out-mode "javascript"
:beautify? false
:eval-fn transpile-ocaml
:external-scripts [(codemirror-mode-src "mllike") "https://viebel.github.io/klipse/repo/js/ocaml-compiler.js"]
; oCaml does not know a single line comment
})

(register-mode "eval-ocaml" "selector_eval_ocaml" eval-opts)
(register-mode "transpile-ocaml" "selector_transpile_ocaml" transpile-opts)
1 change: 1 addition & 0 deletions src/klipse/run/all.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
klipse.lang.cpp
klipse.lang.server-eval
klipse.lang.php
klipse.lang.ocaml
klipse.run.app.app))
1 change: 1 addition & 0 deletions src/klipse/run/plugin/plugin.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
klipse.lang.html
klipse.lang.server-eval
klipse.lang.clojure
klipse.lang.ocaml
[klipse.plugin :as plugin]))

(js/console.info "settings: " (aget js/window "klipse_settings"))
Expand Down
3 changes: 2 additions & 1 deletion src/klipse/run/plugin_prod/plugin.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
klipse.lang.server-eval
klipse.lang.markdown
klipse.lang.lambdaway
; DO NOT include clojure it inflates the build sive and anyway it doesn't work with advanced compilation klipse.lang.clojure
; DO NOT include clojure it inflates the build size and anyway it doesn't work with advanced compilation klipse.lang.clojure
klipse.lang.html
klipse.lang.ocaml
[klipse.plugin :as plugin]))

(print "settings: " (aget js/window "klipse_settings"))
Expand Down