txlyre 1 Minggu lalu
induk
melakukan
16f08b249c
3 mengubah file dengan 18 tambahan dan 1 penghapusan
  1. 1 1
      build.sh
  2. 1 0
      help.h
  3. 16 0
      jk.c

+ 1 - 1
build.sh

@@ -1 +1 @@
-cc -lm -ldl -lpcre -lgc -lffi jk.c -o jk
+cc -lm -ldl -lpcre -lgc -lffi jk.c -o jk

+ 1 - 0
help.h

@@ -149,6 +149,7 @@ const char *V2HELP =\
 "R. monadic setrecdepth   set max recursion depth" "\n"\
 "R. dyadic  tackright     append x to y" "\n"\
 "v. monadic value         get value of var x (udf if not defined)" "\n"\
+"h. monadic hex           yield hexadecimal representation of num x"
 "x. monadic show          identity for strings, same as $ repr for other" "\n"\
 "x. dyadic  rematch       match str y with regex (PCRE) x" "\n"\
 "X. dyadic  extract       extract all matches of regex x from y" "\n"\

+ 16 - 0
jk.c

@@ -5487,6 +5487,21 @@ value_t *verb_value(interpreter_t *state, verb_t *self, value_t *x) {
   return r ? r : state->udf;
 }
 
+value_t *verb_hex(interpreter_t *state, verb_t *self, value_t *x) {
+  if (x->tag == NUMBER) {
+    char buf[64];
+    snprintf(buf, sizeof(buf), "%lx", (long)x->val.number);
+
+    list_t *r = list_new();
+    for (size_t i = 0; i < strlen(buf); i++)
+      list_push(r, value_new_char(buf[i]));
+
+    return value_new_array(r);
+  }
+
+  return state->udf;
+}
+
 value_t *verb_lines(interpreter_t *state, verb_t *self, value_t *x) {
   char *s = value_str(x);
 
@@ -5762,6 +5777,7 @@ verb_t VERBS[] = {DEFVERB(":", 0, 0, 0, const, bind),
                   DEFVERBD("L", 0, 0, 0, lines, tackleft),
                   DEFVERBD("R", X, 0, 0, setrecdepth, tackright),
                   DEFVERBD("v", 0, 0, 0, value, udf2),
+                  DEFVERBD("h", X, 0, 0, hex, udf2),
                   DEFVERBD("x", 0, 0, 0, show, rematch),
                   DEFVERBD("X", 0, 0, 0, udf1, extract)};