Skip to content

Instantly share code, notes, and snippets.

@AHaliq
Created March 31, 2021 10:56
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 AHaliq/79786831c4f834025ff94ad404a575fc to your computer and use it in GitHub Desktop.
Save AHaliq/79786831c4f834025ff94ad404a575fc to your computer and use it in GitHub Desktop.
export ocaml function to call from js
let global_nctx = ref Kernel.Context.empty
let global_ectx = ref Kernel.Context.empty
let js_run_repl str =
match String.(compare str "quit") with
| 0 -> "bye"
| _ ->
match String.(compare str "reset") with
| 0 ->
global_nctx := Kernel.Context.empty;
global_ectx := Kernel.Context.empty;
"reset success"
| _ ->
try
let stmts, naming_ctx =
str
|> Parsing.Parser.parse_string
|> Fun.flip Ast_conv.parser_to_internal_stmts
!global_nctx
in
try
let stmts' , ctx' =
stmts
|> Fun.flip Kernel.Eval_statements.eval_stmts !global_ectx
in
let res = stmts'
|> Pretty_print.unparse_internal_expr naming_ctx in
global_nctx := naming_ctx;
global_ectx := ctx';
res
with exc ->
Error_reporting.fmt_eval_err_str naming_ctx exc
with exc ->
Error_reporting.fmt_parse_err_str exc
let () =
Js.export_all
(object%js
method js_run_repl = Js.wrap_callback js_run_repl
end)
Welcome to Node.js v15.4.0.
Type ".help" for more information.
> var mod = require('./main.bc');
undefined
> mod.js_run("constant P : Prop");
MlBytes { t: 0, c: 'bye', l: 3 }
> mod.js_run_repl
undefined
>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment