Skip to content

Commit 07e6d7a

Browse files
y2kclaude
andcommitted
add atom support to interpreter and JS runtime
- Add OAtom type and atom/swap!/deref functions to eval backend - Add swap_BANG_ function to JS runtime - Simplify swap! macro to delegate to prelude/swap! - Add _QMARK_ decoding in JS backend for ? in names - Simplify def* name handling in JS backend - Add @SuppressWarnings("unchecked") to Java RT 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 391c443 commit 07e6d7a

8 files changed

Lines changed: 40 additions & 11 deletions

File tree

backend/backend_eval_functions.ml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ let attach reg_val reg_fun ctx =
9999
| OFloat (m, f) -> OFloat ({ m with symbol }, f)
100100
| OBool (m, b) -> OBool ({ m with symbol }, b)
101101
| OLambda (m, f) -> OLambda ({ m with symbol }, f)
102-
| OQuote (m, n) -> OQuote ({ m with symbol }, n))
102+
| OQuote (m, n) -> OQuote ({ m with symbol }, n)
103+
| OAtom (m, r) -> OAtom ({ m with symbol }, r))
103104
| x -> Obj.failobj __LOC__ x)
104105
|> reg_fun "boolean" (function
105106
| [ OBool (_, x) ] -> OBool (meta_empty, x)
@@ -268,3 +269,15 @@ let attach reg_val reg_fun ctx =
268269
| [ OLambda (_, f); OVector (_, xs) ] ->
269270
OVector (meta_empty, List.map (fun x -> f [ x ]) xs)
270271
| x -> Obj.failobj __LOC__ x)
272+
|> reg_fun "atom" (function
273+
| [ x ] -> OAtom (meta_empty, ref x)
274+
| x -> Obj.failobj __LOC__ x)
275+
|> reg_fun "swap!" (function
276+
| [ OAtom (_, r); OLambda (_, f) ] ->
277+
let old_val = !r in
278+
r := f [ old_val ];
279+
old_val
280+
| x -> Obj.failobj __LOC__ x)
281+
|> reg_fun "deref" (function
282+
| [ OAtom (_, r) ] -> !r
283+
| x -> Obj.failobj __LOC__ x)

backend/backend_js.ml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ let rec do_compile (ctx : context) = function
3535
| SAtom (_, x) when String.starts_with ~prefix:":" x ->
3636
"\"" ^ unpack_symbol x ^ "\""
3737
| SAtom (_, x) when not (String.starts_with ~prefix:"\"" x) ->
38-
x |> String.map (fun x -> if x = '/' then '.' else x)
38+
x
39+
|> String.map (fun x -> if x = '/' then '.' else x)
40+
|> Re.replace (Re.Pcre.re "_QMARK_" |> Re.compile) ~f:(Fun.const "?")
3941
| SAtom (_, x) -> x
4042
(* TODO: move to macro *)
4143
| SList (_, [ SAtom (_, "<="); a; b ]) ->
@@ -114,10 +116,9 @@ let rec do_compile (ctx : context) = function
114116
| SList (_, [ SAtom (_, "assoc"); map; key; value ]) ->
115117
Printf.sprintf "{ ...%s, [%s]: %s }" (do_compile ctx map)
116118
(do_compile ctx key) (do_compile ctx value)
117-
| SList (_, [ SAtom (m, "def*"); name; value ]) ->
119+
| SList (_, [ SAtom (m, "def*"); SAtom (_, name); value ]) ->
118120
let export = if m.symbol = "private" then "" else "export " in
119-
Printf.sprintf "%sconst %s=%s" export (do_compile ctx name)
120-
(do_compile ctx value)
121+
Printf.sprintf "%sconst %s=%s" export name (do_compile ctx value)
121122
(* let *)
122123
| SList (_, [ SAtom (_, "let*"); name; value ]) ->
123124
Printf.sprintf "const %s=%s" (do_compile ctx name) (do_compile ctx value)

core/common.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ type obj =
125125
| ONil of meta
126126
| OLambda of meta * (obj list -> obj)
127127
| OQuote of meta * sexp
128+
| OAtom of meta * obj ref
128129
[@@deriving show]
129130

130131
module Obj = struct
@@ -294,6 +295,7 @@ module OUtils = struct
294295
| OQuote (_, m) -> "(quote" ^ debug_show_sexp_for_error [ m ] ^ ")"
295296
| OFloat _ -> failwith __LOC__
296297
| OLambda _ -> failwith __LOC__
298+
| OAtom (_, r) -> Printf.sprintf "(atom %s)" (obj_to_string !r)
297299

298300
let failobj loc x = Printf.sprintf "%s %s" loc (obj_to_string x) |> failwith
299301

@@ -329,6 +331,7 @@ module OUtils = struct
329331
| OQuote (_, m) -> "(quote '" ^ debug_show_sexp_for_error [ m ] ^ "')"
330332
| OMap _ -> failwith "OMap"
331333
| OLambda _ -> failwith "OLambda"
334+
| OAtom (_, r) -> Printf.sprintf "(atom %s)" (debug_obj_to_string !r)
332335
end
333336

334337
module NamespaceUtils = struct

core/prelude.ml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package y2k;
55

66
import java.util.*;
77

8+
@SuppressWarnings("unchecked")
89
public class RT {
910

1011
public static Object hash_map_from(Object xs) {
@@ -350,6 +351,12 @@ export const re_find = (p, i) => {
350351
}
351352
return result[0];
352353
}
354+
355+
export const swap_BANG_ = (atom, f) => {
356+
const result = atom[0];
357+
atom[0] = (result);
358+
return result;
359+
}
353360
|}
354361

355362
let java_runtime2 = {|
@@ -883,9 +890,7 @@ let prelude_js_macro = {|
883890
;; Atoms
884891

885892
(defn macro_swap! [a f]
886-
(list 'do
887-
(list 'assoc! a 0 (list f (list 'get a 0)))
888-
(list 'get a 0)))
893+
(list 'prelude/swap! a f))
889894

890895
(defn macro_reset! [a x]
891896
(list 'assoc! a 0 x))

prelude/data/prelude_js_macro.clj

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,7 @@
156156
;; Atoms
157157

158158
(defn macro_swap! [a f]
159-
(list 'do
160-
(list 'assoc! a 0 (list f (list 'get a 0)))
161-
(list 'get a 0)))
159+
(list 'prelude/swap! a f))
162160

163161
(defn macro_reset! [a x]
164162
(list 'assoc! a 0 x))

prelude/data/y2k/RT.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.*;
44

5+
@SuppressWarnings("unchecked")
56
public class RT {
67

78
public static Object hash_map_from(Object xs) {

prelude/data/y2k/rt.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,10 @@ export const re_find = (p, i) => {
1616
return null;
1717
}
1818
return result[0];
19+
}
20+
21+
export const swap_BANG_ = (atom, f) => {
22+
const result = atom[0];
23+
atom[0] = (result);
24+
return result;
1925
}

test/test_common.clj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
(defn- ?!- [x] x)
1212

1313
(defn test []
14+
(let [value_atom (atom 1)]
15+
(assert_ __LOC__ 1 (swap! value_atom (fn [^int x] (+ x 2)))))
1416
(assert_ __LOC__ 1 (?!- 1))
1517
(assert_ __LOC__ [] (filter (fn [_] false) [1 2]))
1618
(assert_ __LOC__ ["1" "2"] (map (fn [x] (str x)) [1 2]))

0 commit comments

Comments
 (0)