txlyre 6 jam lalu
induk
melakukan
ceeefc1450
1 mengubah file dengan 94 tambahan dan 2 penghapusan
  1. 94 2
      qic.c

+ 94 - 2
qic.c

@@ -2352,6 +2352,49 @@ const char *STD[][2] = {
     "  println(msg)\n"
     "  exit(c)\n"
     "}\n"
+    "func min(x, y): x < y? x: y\n"
+    "func max(x, y): x > y? x: y\n"
+    "func reverse(x) {\n"
+    "  if type(x) !in (\"list\", \"string\", \"bytes\")\n"
+    "    throw \"expected first argument to be: list, string or bytes, but got: \" + type(x)\n"
+    "  var r = []\n"
+    "  for var i = len(x)-1; i >= 0; i--\n"
+    "    list_push(r, x[i])\n"
+    "  if type(x) == \"string\"\n"
+    "    return list_join(r)\n"
+    "  elif type(x) == \"bytes\"\n"
+    "    return bytes(r)\n"
+    "  return r\n"
+    "}\n"
+    "set_pseudomethod(\"list.reverse\", reverse)\n"
+    "set_pseudomethod(\"string.reverse\", reverse)\n"
+    "set_pseudomethod(\"bytes.reverse\", reverse)\n"
+    "func range(f) {\n"
+    "  var t, s\n"
+    "  if len(arguments) >= 3 {\n"
+    "    t = arguments[1]\n"
+    "    s = arguments[2]\n"
+    "  } elif len(arguments) >= 2 {\n"
+    "    t = arguments[1]\n"
+    "    s = 1\n"
+    "  } else {\n"
+    "    t = f\n"
+    "    f = 0\n"
+    "    s = 1\n"
+    "  }\n"
+    "  if type(f) != \"number\"\n"
+    "    throw \"expected first argument to be: number, but got: \" + type(f)\n"
+    "  if type(t) != \"number\"\n"
+    "    throw \"expected second argument to be: number, but got: \" + type(t)\n"
+    "  if type(s) != \"number\"\n"
+    "    throw \"expected third argument to be: number, but got: \" + type(s)\n"
+    "  if f > t\n"
+    "    return reverse(range(t, f, s))\n"
+    "  var r = []\n"
+    "  for var i = f; i < t; i += s\n"
+    "    list_push(r, i)\n"
+    "  return r\n"
+    "}\n"
     "const SEEK_SET = 0, SEEK_CUR = 1, SEEK_END = 2\n"
     "func frewind(file)\n"
     "  return file.rewind()\n"
@@ -2407,6 +2450,16 @@ const char *STD[][2] = {
     "  return r\n"
     "}\n"
     "set_pseudomethod(\"list.join\", list_join)\n"
+    "func list_pop_at(l, i) {\n"
+    "  if type(l) != \"list\"\n"
+    "    throw \"expected first argument to be: list, but got: \" + type(l)\n"
+    "  if type(i) != \"number\"\n"
+    "    throw \"expected second argument to be: number, but got: \" + type(i)\n"
+    "  var x = l[i]\n"
+    "  list_delete(l, i)\n"
+    "  return x\n"
+    "}\n"
+    "set_pseudomethod(\"list.popAt\", list_pop_at)\n"
     "func slice(l) {\n"
     "  if type(l) !in (\"list\", \"string\", \"bytes\")\n"
     "    throw \"expected first argument to be: list, string or bytes, but got: \" + type(l)\n"
@@ -2462,7 +2515,7 @@ const char *STD[][2] = {
     "        return obj\n"
     "    }\n"
     "})\n"
-    "inline `srand(time(NULL))`\n"
+    "func open(path, mode=\"r\"): fopen(path, mode)\n"
   },
 
   {"utf8",
@@ -2507,6 +2560,46 @@ const char *STD[][2] = {
     "func is_char(c): return type(c) == \"string\" && len(c) == 1\n"
   },
 
+  {"time",
+    "func time() {\n"
+    "  inline `unsigned long long ts = time(NULL)`\n"
+    "  inline `return qi_make_number(state, ts)`\n"
+    "}\n"
+    "func strftime(f, t=time()) {\n"
+    "  if type(f) != \"string\"\n"
+    "    throw \"expected first argument to be: string, but got: \" + type(f)\n"
+    "  if type(t) != \"number\"\n"
+    "    throw \"expected second argument to be: number, but got: \" + type(t)\n"
+    "  inline `time_t ts = qi_get(state, \"t\")->value.number`\n"
+    "  inline `struct tm lt`\n"
+    "  inline `lt = *localtime(&ts)`\n"
+    "  inline `char buffer[512]`\n"
+    "  inline `strftime(buffer, sizeof(buffer), qi_get(state, \"f\")->value.string, &lt)`\n"
+    "  inline `return qi_make_string_copy(state, buffer)`\n"
+    "}\n"
+  },
+
+  {"random",
+    "func rand() {\n"
+    "  inline `return qi_make_number(state, rand())`\n"
+    "}\n"
+    "func random_int(min, max): return rand() % (max + 1 - min) + min\n"
+    "func random_choice(l, k=1) {\n"
+    "  l = list_copy(list(l))\n"
+    "  var r = []\n"
+    "  for _ of range(k)\n"
+    "    list_push(r, list_pop_at(l, random_int(0, len(l)-1)))\n"
+    "  return r\n"
+    "}\n"
+    "func random_pick(l, k=1) {\n"
+    "  var r = []\n"
+    "  for _ of range(k)\n"
+    "    list_push(r, l[random_int(0, len(l)-1)])\n"
+    " return r\n"
+    "}\n"
+    "inline `srand(time(NULL))`\n"
+  },
+
   {"math", NULL},
 
   {NULL, NULL}
@@ -2538,7 +2631,6 @@ const struct {
   { "atan", NULL, 1 },
   { "atan2", NULL, 2 },
   { "hypot", NULL, 2 },
-  { "random", "rand", 0 },
 
   { NULL, 0 }
 };