Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
y2k committed Mar 6, 2024
1 parent 8b2e506 commit 3785314
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
14 changes: 12 additions & 2 deletions clj2js/lib/kt_target.ml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ let rec compile_ (context : context) (node : cljexp) : context * string =
|> Printf.sprintf "(%s)" |> withContext
| RBList [ Atom (_, "="); a; b ] ->
compile a ^ " == " ^ compile b |> withContext
| RBList [ Atom (_, "not="); a; b ] ->
compile a ^ " != " ^ compile b |> withContext
| RBList [ Atom (_, "get"); target; index ] ->
Printf.sprintf "geta(%s, %s)" (compile target) (compile index)
|> withContext
Expand Down Expand Up @@ -98,9 +100,17 @@ let rec compile_ (context : context) (node : cljexp) : context * string =
| n -> fail_node [ n ])
|> List.reduce (Printf.sprintf "%s\n%s")
| n -> fail_node [ n ])
|> List.reduce (Printf.sprintf "%s\n%s")
|> List.fold_left (Printf.sprintf "%s\n%s") ""
in
Printf.sprintf "package %s\n%s\n" name imports |> withContext
Printf.sprintf "package %s;%s\n" name imports |> withContext
| RBList (Atom (_, "and") :: xs) ->
xs |> List.map compile
|> List.reduce (Printf.sprintf "%s && %s")
|> Printf.sprintf "(%s)" |> withContext
| RBList (Atom (_, "or") :: xs) ->
xs |> List.map compile
|> List.reduce (Printf.sprintf "%s || %s")
|> Printf.sprintf "(%s)" |> withContext
| RBList (Atom (_, "+") :: args) ->
args |> List.map compile
|> List.reduce (Printf.sprintf "%s, %s")
Expand Down
10 changes: 10 additions & 0 deletions clj2js/test/test_kt.ml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ prelude.minus(a, b) };|};
assert_ {|(defn foo [xs] (let [[a b] (foo 1 2)] (bar a b)))|}
{|fun foo(xs:Any?) = run { val p__4 = foo(1, 2); val a = geta(p__4, 0); val b = geta(p__4, 1); bar(a, b) };|};
assert_ {|(= a b)|} {|a == b|};
assert_ {|(not= a b)|} {|a != b|};
assert_ {|(get xs 1)|} {|geta(xs, 1)|};
assert_ {|(:foo bar)|} {|getm(bar, "foo")|};
assert_ {|(def foo 1)|} {|val foo = 1;|};
Expand All @@ -33,4 +34,13 @@ prelude.minus(a, b) };|};
assert_ {|(spread (.toTypedArray a))|} {|*a.toTypedArray()|};
assert_ {|(class android.app.AlarmManager)|}
{|android.app.AlarmManager::class.java|};
assert_
{|(ns im.y2k.chargetimer (:import [android.app Activity NotificationChannel]))|}
{|package im.y2k.chargetimer;
import android.app.Activity;
import android.app.NotificationChannel;|};
assert_ {|(ns prelude)|} {|package prelude;|};
assert_ "(and (= a 1) b)" "(a == 1 && b)";
assert_ "(or (= a 1) b)" "(a == 1 || b)";
assert_ "(.play r)" "r.play()";
()

0 comments on commit 3785314

Please sign in to comment.