Skip to content

Commit

Permalink
remove js prelude
Browse files Browse the repository at this point in the history
  • Loading branch information
y2k committed May 1, 2024
1 parent 539b2c8 commit 9abb1d9
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 29 deletions.
5 changes: 1 addition & 4 deletions clj2js/bin/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ let () =
| "json" -> Clj2js.main_json filename
| "js" ->
let prelude_macros = read_code_file Sys.argv.(3) in
let prelude_path = Filename.chop_extension Sys.argv.(3) ^ ".js" in
fun code ->
Lib__Linter.run_resolve
(fun name ->
Expand All @@ -22,9 +21,7 @@ let () =
in
(* prerr_endline @@ Sys.getenv "PWD" ^ " | " ^ filename; *)
In_channel.with_open_bin path In_channel.input_all)
(fun _ ->
Clj2js.main_js prelude_path filename prelude_macros code
|> snd)
(fun _ -> Clj2js.main_js filename prelude_macros code |> snd)
| "sh" ->
fun str ->
let shebang = "#!/usr/bin/env clj2sh\n" in
Expand Down
13 changes: 7 additions & 6 deletions clj2js/lib/backend_js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ let prelude =
(defn deref [a] (get a 0))
|}

let prelude_imports prelude_path = "import * as RT from '" ^ prelude_path ^ "';"
(* let prelude_imports = "import { atom, reset, deref } from './prelude.js';" *)

let unpack_string x = String.sub x 1 (String.length x - 2)
let unpack_symbol x = String.sub x 1 (String.length x - 1)

Expand Down Expand Up @@ -328,14 +325,18 @@ let rec compile_ (context : context) (node : cljexp) : context * string =
else args |> List.map compile |> List.reduce (Printf.sprintf "%s, %s"))
|> Printf.sprintf "new %s(%s)" (unpack_string cnst_name)
|> with_context
(* Functino call *)
(* Function call *)
| RBList (head :: args) ->
(let sargs =
if List.length args = 0 then ""
else args |> List.map compile |> List.reduce (Printf.sprintf "%s, %s")
in
String.map (function '/' -> '.' | x -> x) (compile head)
^ "(" ^ sargs ^ ")")
let fname =
match head with
| RBList (Atom (_, "fn*") :: _) -> "(" ^ compile head ^ ")"
| _ -> compile head
in
String.map (function '/' -> '.' | x -> x) fname ^ "(" ^ sargs ^ ")")
|> with_context
| x -> fail_node [ x ]

Expand Down
3 changes: 0 additions & 3 deletions clj2js/lib/frontend.ml
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,6 @@ let failnode prefix es =
failwith ("Invalid node [" ^ prefix ^ "]")

module NameGenerator = struct
let reset () = () (* FIXME: *)

type _ Effect.t += CreateVal : string Effect.t

let with_scope f =
Expand Down Expand Up @@ -463,7 +461,6 @@ let rec expand_core_macro (context : context) node : context * cljexp =
| x -> fail_node [ x ]

let parse_and_simplify (macros : cljexp StringMap.t) start_line filename code =
NameGenerator.reset ();
(* if filename <> "prelude" then
print_endline "==| DEBUG |==============================================\n"; *)
let sexp =
Expand Down
1 change: 0 additions & 1 deletion clj2js/lib/frontend.mli
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ type context = {

module NameGenerator : sig
val get_new_var : unit -> string
val reset : unit -> unit
val with_scope : (unit -> 'a) -> 'a
end

Expand Down
8 changes: 2 additions & 6 deletions clj2js/lib/lib.ml
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
let main_js prelude_path (filename : string) (prelude_macros : string) code =
let main_js (filename : string) (prelude_macros : string) code =
(match filename with
| "prelude" ->
(Backend_js.prelude |> Backend_js.main "prelude" prelude_macros, "")
| filename ->
( code |> Backend_js.main filename prelude_macros,
Backend_js.prelude_imports prelude_path ))
| filename -> (code |> Backend_js.main filename prelude_macros, ""))
|> fun ((ctx, code), imports) ->
(ctx, Printf.sprintf "\"use strict\";\n%s\n%s" imports code)

Expand Down
2 changes: 1 addition & 1 deletion clj2js/test/samples/prelude
17 changes: 9 additions & 8 deletions clj2js/test/test_js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ let assert1 code expected =
in
let _ctx, actual =
Lib__.Frontend.NameGenerator.with_scope (fun _ ->
Clj2js.main_js "" "main.clj" prelude code)
Clj2js.main_js "main.clj" prelude code)
in
let start = 38 in
let start = 15 in
let actual = String.sub actual start (String.length actual - start) in
Alcotest.(check string) "1" expected actual
in
Expand All @@ -33,9 +33,9 @@ let assert2 files code expected =
let _ctx, actual =
with_extenal_files files (fun () ->
Lib__.Frontend.NameGenerator.with_scope (fun _ ->
Clj2js.main_js "" "main.clj" prelude code))
Clj2js.main_js "main.clj" prelude code))
in
let start = 38 in
let start = 15 in
let actual = String.sub actual start (String.length actual - start) in
Alcotest.(check string) "1" expected actual
in
Expand Down Expand Up @@ -302,10 +302,11 @@ bar(2)|};
"export const a = 1;\nexport const b = 2;";
assert1 {|(fn [{a :url b :props}] [a b])|}
{|(p__1) => { return (function () { const a = p__1["url"]; const b = p__1["props"]; return [a, b] })() }|};
assert1 {|(atom 1)|} {|RT.atom(1)|};
assert1 {|(deref 'x)|} {|RT.deref(x)|};
assert1 {|(reset! 'x 2)|} {|RT.reset(x, 2)|};
assert1 {|(swap! 'a (fn [x] x))|} {|RT.swap(a, (x) => { return x })|};
assert1 {|(atom 1)|} {|Array.of(1)|};
assert1 {|(deref 'x)|} {|x[0]|};
assert1 {|(reset! 'x 2)|} {|(function () { x.fill(2); return 2 })()|};
assert1 {|(swap! 'a (fn [x] x))|}
{|a.splice(0, 1, ((x) => { return x })(a[0]))[0]|};
assert1 {|(fn [a {b :b} c] (a b c))|}
{|(a, p__1, c) => { return (function () { const b = p__1["b"]; return a(b, c) })() }|};
assert1 {|(fn [a [b c] d] (a b c d))|}
Expand Down

0 comments on commit 9abb1d9

Please sign in to comment.