kuroko/builtins.c

157 lines
4.6 KiB
C
Raw Normal View History

const char krk_builtinsSrc[] =
2021-01-07 02:39:46 +03:00
"# Please avoid using double quotes or escape sequences\n"
2021-01-07 14:42:25 +03:00
"# in this file to allow it to be easily converted to C.\n"
2021-01-07 02:39:46 +03:00
"class list():\n"
" 'Resizable array with direct constant-time indexing.'\n"
" def extend(i):\n"
" 'Add all entries from an iterable to the end of this list.'\n"
2021-01-10 14:49:01 +03:00
" if isinstance(i,list):\n"
" return self._extend_fast(i)\n"
2021-01-07 02:39:46 +03:00
" for v in i:\n"
" self.append(v)\n"
" return self.__len__()\n"
" def __str__(self): return self.__repr__()\n"
" def __repr__(self):\n"
" if self.__inrepr: return '[...]'\n"
" self.__inrepr=1\n"
" let b='['+', '.join([repr(i) for i in self])+']'\n"
2021-01-07 02:39:46 +03:00
" self.__inrepr=0\n"
" return b\n"
2021-01-07 02:39:46 +03:00
"\n"
"class dict():\n"
" 'Hashmap of arbitrary keys to arbitrary values.'\n"
" def __str__(self): return self.__repr__()\n"
" def __repr__(self):\n"
" if self.__inrepr: return '{...}'\n"
" self.__inrepr = 1\n"
" let b='{'+', '.join([repr(k)+': '+repr(self[k]) for k in self.keys()])+'}'\n"
2021-01-07 02:39:46 +03:00
" self.__inrepr = 0\n"
" return b\n"
2021-01-07 02:39:46 +03:00
" def keys(self):\n"
" 'Returns an iterable of the keys in this dictionary.'\n"
" class KeyIterator():\n"
" def __init__(self,t):\n"
" self.t=t\n"
" def __iter__(self):\n"
" let i=0\n"
" let c=self.t.capacity()\n"
" def _():\n"
" let o=(False,None)\n"
" while not o[0] and i<c:\n"
2021-01-07 02:39:46 +03:00
" o=self.t._key_at_index(i)\n"
" i++\n"
" if not o[0]:\n"
2021-01-07 02:39:46 +03:00
" return _\n"
" return o[1]\n"
2021-01-07 02:39:46 +03:00
" return _\n"
" return KeyIterator(self)\n"
" def items(self):\n"
" return [(k,self[k]) for k in self.keys()]\n"
2021-01-07 02:39:46 +03:00
"\n"
2021-01-09 04:12:50 +03:00
"class set():\n"
" def __init__(self, iter=[]):\n"
" self._dict = {}\n"
" self.__inrepr = 0\n"
" for v in iter:\n"
" self._dict[v] = 1\n"
" def __contains__(self, v):\n"
" return v in self._dict\n"
" def __str__(self): return self.__repr__()\n"
2021-01-09 04:12:50 +03:00
" def __repr__(self):\n"
2021-01-11 15:46:47 +03:00
" if self.__inrepr: return '{}'\n"
2021-01-09 04:12:50 +03:00
" let b='{'+', '.join([repr(k) for k in self._dict.keys()])+'}'\n"
" self.__inrepr = 0\n"
" return b\n"
" def add(self,v):\n"
" self._dict[v] = 1\n"
" def __len__(self):\n"
" return self._dict.__len__()\n"
2021-01-09 04:12:50 +03:00
" def __iter__(self):\n"
" return self._dict.keys().__iter__()\n"
2021-01-11 15:46:47 +03:00
" def __or__(self, o):\n"
" if not isinstance(o, set):\n"
" raise TypeError()\n"
" let b = set()\n"
" for k in self:\n"
" b.add(k)\n"
" for k in o:\n"
" b.add(k)\n"
" return b\n"
" def __and__(self, o):\n"
" if not isinstance(o, set):\n"
" raise TypeError()\n"
" let b = set()\n"
" for k in self:\n"
" if k in o:\n"
" b.add(k)\n"
" return b\n"
2021-01-09 04:12:50 +03:00
"\n"
"__builtins__.set = set\n"
"\n"
"def any(iter):\n"
" for v in iter:\n"
" if v: return True\n"
" return False\n"
"__builtins__.any = any\n"
"\n"
"def all(iter):\n"
" for v in iter:\n"
" if not v: return False\n"
" return True\n"
"__builtins__.all = all\n"
"\n"
2021-01-07 02:39:46 +03:00
"class Helper():\n"
" '''You seem to already know how to use this.'''\n"
" def __call__(self,obj=None):\n"
" if obj:\n"
" try:\n"
" print(obj.__doc__)\n"
" except:\n"
" try:\n"
" print(obj.__class__.__doc__)\n"
" except:\n"
" print('No docstring avaialble for', obj)\n"
" else:\n"
" from help import interactive\n"
" interactive()\n"
2021-01-07 02:39:46 +03:00
" def __repr__(self):\n"
" return 'Type help() for more help, or help(obj) to describe an object.'\n"
"\n"
"let help = Helper()\n"
"\n"
"let _licenseText = '''\n"
"Copyright (c) 2020-2021 K. Lange <klange@toaruos.org>\n"
"\n"
"Permission to use, copy, modify, and/or distribute this software for any\n"
"purpose with or without fee is hereby granted, provided that the above\n"
"copyright notice and this permission notice appear in all copies.\n"
"\n"
"THE SOFTWARE IS PROVIDED 'AS IS' AND THE AUTHOR DISCLAIMS ALL WARRANTIES\n"
"WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF\n"
"MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR\n"
"ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES\n"
"WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN\n"
"ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF\n"
"OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.\n"
"'''\n"
"\n"
"class LicenseReader():\n"
" def __call__(self):\n"
" print(_licenseText)\n"
" def __repr__(self):\n"
" return 'Copyright 2020-2021 K. Lange <klange@toaruos.org>. Type `license()` for more information.'\n"
"\n"
"let license = LicenseReader()\n"
"\n"
"__builtins__.list = list\n"
"__builtins__.dict = dict\n"
"__builtins__.help = help\n"
"__builtins__.license = license\n"
"\n"
"# this works because `kuroko` is always a built-in\n"
"import kuroko\n"
"kuroko.module_paths = ['./','./modules/','/usr/local/lib/kuroko/']\n"
2021-01-07 02:39:46 +03:00
"\n"
"return object()\n"
;