kuroko/builtins.krk
2021-01-11 21:46:47 +09:00

155 lines
3.9 KiB
Plaintext

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