aboutsummaryrefslogtreecommitdiff
path: root/discocaml/util.ml
diff options
context:
space:
mode:
authorNathan Ringo <nathan@remexre.com>2024-01-24 00:37:06 -0600
committerNathan Ringo <nathan@remexre.com>2024-01-24 00:37:21 -0600
commitf290fca1afab9bf8aeb58ce0789b4d810abc9f66 (patch)
tree1e6defb96d80e88520d2b3c85547572d13c98871 /discocaml/util.ml
parent9b22d5b3358ae62c06d40b2ef78b42b1e45f8ab4 (diff)
Adds space for letrec, some capture avoidance, remove binder arrows.
Diffstat (limited to 'discocaml/util.ml')
-rw-r--r--discocaml/util.ml19
1 files changed, 19 insertions, 0 deletions
diff --git a/discocaml/util.ml b/discocaml/util.ml
new file mode 100644
index 0000000..9c70a1a
--- /dev/null
+++ b/discocaml/util.ml
@@ -0,0 +1,19 @@
+let break_to_subscript (s : string) : (string * int) option =
+ let rec loop (i : int) : (string * int) option =
+ if i < 0 then None
+ else
+ let ch = String.get s i in
+ if '0' <= ch && ch <= '9' then loop (i - 1)
+ else
+ let name = String.sub s 0 (i + 1)
+ and sub = String.sub s (i + 1) (String.length s - i - 1) in
+ if sub = "" then None
+ else int_of_string_opt sub |> Option.map (fun i -> (name, i))
+ in
+ loop (String.length s - 1)
+
+let%test "break_to_subscript fail" = break_to_subscript "x1y" = None
+let%test "break_to_subscript simple" = break_to_subscript "x1" = Some ("x", 1)
+let%test "break_to_subscript empty" = break_to_subscript "" = None
+let%test "break_to_subscript number" = break_to_subscript "123" = None
+let%test "break_to_subscript multi" = break_to_subscript "x12" = Some ("x", 12)