kuroko/builtins.c
2021-01-19 22:27:05 +09:00

117 lines
3.0 KiB
C

const char krk_builtinsSrc[] =
"# Please avoid using double quotes or escape sequences\n"
"# in this file to allow it to be easily converted to C.\n"
"class dict():\n"
" 'Hashmap of arbitrary keys to arbitrary values.'\n"
" 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"
" o=self.t._key_at_index(i)\n"
" i++\n"
" if not o[0]:\n"
" return _\n"
" return o[1]\n"
" return _\n"
" return KeyIterator(self)\n"
" def items(self):\n"
" return [(k,self[k]) for k in self.keys()]\n"
"\n"
"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"
" def __repr__(self):\n"
" if self.__inrepr: return '{}'\n"
" 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"
" def __iter__(self):\n"
" return self._dict.keys().__iter__()\n"
" 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"
"\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"
"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"
" def __repr__(self):\n"
" return 'Type help() for more help, or help(obj) to describe an object.'\n"
"\n"
"let help = Helper()\n"
"\n"
"class LicenseReader():\n"
" def __call__(self):\n"
" from help import __licenseText\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__.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"
"\n"
"return object()\n"
;