Skip to content

Commit

Permalink
fix lambda
Browse files Browse the repository at this point in the history
  • Loading branch information
y2k committed Dec 15, 2024
1 parent 7290be9 commit 8f1a7af
Show file tree
Hide file tree
Showing 14 changed files with 911 additions and 1,592 deletions.
18 changes: 17 additions & 1 deletion clj2js/lib/backend_java.ml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ let generate_class (compile_exp : cljexp -> string) prefix params clsName
%s%s}"
clsName superCls state ms

let unwrap_do = function RBList (Atom (_, "do*") :: xs) -> xs | x -> [ x ]

let rec compile_ (context : context) (node : cljexp) : context * string =
let compile node = compile_ context node |> snd in
let with_context node = (context, node) in
Expand Down Expand Up @@ -108,10 +110,20 @@ let rec compile_ (context : context) (node : cljexp) : context * string =
| RBList [ Atom (_, "bind-update*"); Atom (_, name); value ] ->
let js_code = Printf.sprintf "%s = %s;" name (compile value) in
with_context js_code
| RBList [ Atom (_, "set!"); Atom (_, name); value ] ->
let js_code = Printf.sprintf "%s = %s;" name (compile value) in
with_context js_code
| RBList [ Atom (_, "set!"); name; value ] ->
let js_code = Printf.sprintf "%s = %s;" (compile name) (compile value) in
with_context js_code
| RBList [ Atom (_, "if*"); (Atom _ as cond); then_; else_ ] ->
Printf.sprintf "if (%s) {\n%s\n} else {\n%s\n}" (compile cond)
(compile then_) (compile else_)
|> with_context
| RBList [ Atom (_, "if*"); cond; then_; else_ ] ->
Printf.sprintf "if (%s) {\n%s\n} else {\n%s\n}" (compile cond)
(compile then_) (compile else_)
|> with_context
| RBList [ Atom (_, "spread"); Atom (_, value) ] ->
Printf.sprintf "...%s" value |> with_context
(* /Version 2.0 *)
Expand Down Expand Up @@ -215,7 +227,9 @@ let rec compile_ (context : context) (node : cljexp) : context * string =
|> List.reduce_opt (Printf.sprintf "%s,%s")
|> Option.value ~default:""
in
let body = body |> List.concat_map unwrap_do in
let sbody =
let body = body |> List.concat_map unwrap_do in
let length = List.length body in
body
|> List.filteri (fun i _ -> i < length - 1)
Expand Down Expand Up @@ -255,6 +269,7 @@ let rec compile_ (context : context) (node : cljexp) : context * string =
|> List.reduce_opt (Printf.sprintf "%s,%s")
|> Option.value ~default:""
in
let body = body |> List.concat_map unwrap_do in
let sbody =
let length = List.length body in
body
Expand Down Expand Up @@ -361,6 +376,7 @@ let main (log : bool) (filename : string) prelude_macros code =
|> Stage_normalize_bracket.invoke
|> try_log "Stage_normalize_bracket ->" log
|> Stage_linter.invoke _macro_sexp
|> Stage_a_normal_form.convert
(* |> Stage_a_normal_form.convert *)
|> Stage_convert_if_to_statment.invoke
|> try_log "Stage_a_normal_form ->" log
|> compile_ ctx |> snd |> String.trim
19 changes: 17 additions & 2 deletions clj2js/lib/backend_js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ open Common

let unpack_string x = String.sub x 1 (String.length x - 2)
let unpack_symbol x = String.sub x 1 (String.length x - 1)
let unwrap_do = function RBList (Atom (_, "do*") :: xs) -> xs | x -> [ x ]

let rec compile_ (context : context) (node : cljexp) : context * string =
(* log_sexp "js: " node |> ignore; *)
Expand Down Expand Up @@ -32,12 +33,24 @@ let rec compile_ (context : context) (node : cljexp) : context * string =
| RBList [ Atom (_, "bind-update*"); Atom (_, name); value ] ->
let js_code = Printf.sprintf "%s = %s;" name (compile value) in
with_context js_code
| RBList [ Atom (_, "set!"); Atom (_, name); value ] ->
let js_code = Printf.sprintf "%s = %s;" name (compile value) in
with_context js_code
| RBList [ Atom (_, "set!"); name; value ] ->
let js_code = Printf.sprintf "%s = %s;" (compile name) (compile value) in
with_context js_code
| RBList [ Atom (_, "if*"); (Atom _ as cond); then_; else_ ] ->
Printf.sprintf "if (%s) {\n%s\n} else {\n%s\n}" (compile cond)
(compile then_) (compile else_)
|> with_context
| RBList [ Atom (_, "if*"); cond; then_; else_ ] ->
Printf.sprintf "if (%s) {\n%s\n} else {\n%s\n}" (compile cond)
(compile then_) (compile else_)
|> with_context
| RBList [ Atom (_, "spread"); Atom (_, value) ] ->
Printf.sprintf "...%s" value |> with_context
| RBList [ Atom (_, "spread"); value ] ->
Printf.sprintf "...%s" (compile value) |> with_context
(* Version 2.0 *)
(* Expressions *)
(* | RBList (Atom (_, "do*") :: body) ->
Expand Down Expand Up @@ -198,6 +211,7 @@ let rec compile_ (context : context) (node : cljexp) : context * string =
in
let sargs = loop_args args in
let sbody =
let body = body |> List.concat_map unwrap_do in
body |> List.map compile |> List.rev
|> List.mapi (fun i x -> if i = 0 then "return " ^ x else x)
|> List.rev
Expand Down Expand Up @@ -264,6 +278,7 @@ let main (log : bool) (filename : string) prelude_macros code =
|> Stage_normalize_bracket.invoke
|> try_log "Stage_normalize_bracket ->" log
|> Stage_linter.invoke _macro_sexp
|> Stage_a_normal_form.convert
|> try_log "Stage_a_normal_form ->" log
(* |> Stage_a_normal_form_2.invoke *)
|> Stage_convert_if_to_statment.invoke
|> try_log "Stage_normalize_if ->" log
|> compile_ ctx |> snd |> String.trim
2 changes: 2 additions & 0 deletions clj2js/lib/common.ml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
let last xs = List.nth xs (List.length xs - 1)
let butlast xs = List.rev (List.tl (List.rev xs))
let loc (a, b, c, _) = Printf.sprintf "%s:%i:%i" a b c

type meta = { line : int; pos : int; symbol : string } [@@deriving show]
Expand Down
Loading

0 comments on commit 8f1a7af

Please sign in to comment.