C-ify a bunch of basic builtin functions
This commit is contained in:
parent
eb383e7663
commit
66533d5c23
94
builtins.c
94
builtins.c
@ -99,61 +99,41 @@ const char _builtins_src[] = {
|
|||||||
0x6c,0x3a,0x0a,0x20,0x20,0x20,0x20,0x72,0x65,0x74,0x75,0x72,0x6e,0x20,0x5f,0x0a,
|
0x6c,0x3a,0x0a,0x20,0x20,0x20,0x20,0x72,0x65,0x74,0x75,0x72,0x6e,0x20,0x5f,0x0a,
|
||||||
0x20,0x20,0x20,0x6c,0x65,0x74,0x20,0x6f,0x3d,0x69,0x0a,0x20,0x20,0x20,0x69,0x2b,
|
0x20,0x20,0x20,0x6c,0x65,0x74,0x20,0x6f,0x3d,0x69,0x0a,0x20,0x20,0x20,0x69,0x2b,
|
||||||
0x2b,0x0a,0x20,0x20,0x20,0x72,0x65,0x74,0x75,0x72,0x6e,0x20,0x6f,0x0a,0x20,0x20,
|
0x2b,0x0a,0x20,0x20,0x20,0x72,0x65,0x74,0x75,0x72,0x6e,0x20,0x6f,0x0a,0x20,0x20,
|
||||||
0x72,0x65,0x74,0x75,0x72,0x6e,0x20,0x5f,0x0a,0x0a,0x64,0x65,0x66,0x20,0x6c,0x65,
|
0x72,0x65,0x74,0x75,0x72,0x6e,0x20,0x5f,0x0a,0x0a,0x63,0x6c,0x61,0x73,0x73,0x20,
|
||||||
0x6e,0x28,0x6f,0x62,0x6a,0x3d,0x4e,0x6f,0x6e,0x65,0x29,0x3a,0x20,0x72,0x65,0x74,
|
0x48,0x65,0x6c,0x70,0x65,0x72,0x28,0x29,0x3a,0x0a,0x20,0x27,0x27,0x27,0x59,0x6f,
|
||||||
0x75,0x72,0x6e,0x20,0x28,0x6f,0x62,0x6a,0x20,0x61,0x6e,0x64,0x20,0x6f,0x62,0x6a,
|
0x75,0x20,0x73,0x65,0x65,0x6d,0x20,0x74,0x6f,0x20,0x61,0x6c,0x72,0x65,0x61,0x64,
|
||||||
0x2e,0x5f,0x5f,0x6c,0x65,0x6e,0x5f,0x5f,0x28,0x29,0x29,0x20,0x6f,0x72,0x20,0x30,
|
0x79,0x20,0x6b,0x6e,0x6f,0x77,0x20,0x68,0x6f,0x77,0x20,0x74,0x6f,0x20,0x75,0x73,
|
||||||
0x0a,0x64,0x65,0x66,0x20,0x73,0x74,0x72,0x28,0x6f,0x62,0x6a,0x3d,0x4e,0x6f,0x6e,
|
0x65,0x20,0x74,0x68,0x69,0x73,0x2e,0x27,0x27,0x27,0x0a,0x20,0x64,0x65,0x66,0x20,
|
||||||
0x65,0x29,0x3a,0x20,0x72,0x65,0x74,0x75,0x72,0x6e,0x20,0x28,0x6f,0x62,0x6a,0x20,
|
0x5f,0x5f,0x63,0x61,0x6c,0x6c,0x5f,0x5f,0x28,0x73,0x65,0x6c,0x66,0x2c,0x6f,0x62,
|
||||||
0x61,0x6e,0x64,0x20,0x6f,0x62,0x6a,0x2e,0x5f,0x5f,0x73,0x74,0x72,0x5f,0x5f,0x28,
|
0x6a,0x3d,0x4e,0x6f,0x6e,0x65,0x29,0x3a,0x0a,0x20,0x20,0x69,0x66,0x20,0x6f,0x62,
|
||||||
0x29,0x29,0x20,0x6f,0x72,0x20,0x22,0x22,0x0a,0x64,0x65,0x66,0x20,0x72,0x65,0x70,
|
0x6a,0x3a,0x0a,0x20,0x20,0x20,0x74,0x72,0x79,0x3a,0x0a,0x20,0x20,0x20,0x20,0x70,
|
||||||
0x72,0x28,0x6f,0x62,0x6a,0x29,0x3a,0x20,0x72,0x65,0x74,0x75,0x72,0x6e,0x20,0x6f,
|
0x72,0x69,0x6e,0x74,0x20,0x6f,0x62,0x6a,0x2e,0x5f,0x5f,0x64,0x6f,0x63,0x5f,0x5f,
|
||||||
0x62,0x6a,0x2e,0x5f,0x5f,0x72,0x65,0x70,0x72,0x5f,0x5f,0x28,0x29,0x0a,0x64,0x65,
|
0x0a,0x20,0x20,0x20,0x65,0x78,0x63,0x65,0x70,0x74,0x3a,0x0a,0x20,0x20,0x20,0x20,
|
||||||
0x66,0x20,0x69,0x6e,0x74,0x28,0x6f,0x62,0x6a,0x3d,0x4e,0x6f,0x6e,0x65,0x29,0x3a,
|
0x74,0x72,0x79,0x3a,0x0a,0x20,0x20,0x20,0x20,0x20,0x70,0x72,0x69,0x6e,0x74,0x20,
|
||||||
0x20,0x72,0x65,0x74,0x75,0x72,0x6e,0x20,0x28,0x6f,0x62,0x6a,0x20,0x61,0x6e,0x64,
|
0x6f,0x62,0x6a,0x2e,0x5f,0x5f,0x63,0x6c,0x61,0x73,0x73,0x5f,0x5f,0x2e,0x5f,0x5f,
|
||||||
0x20,0x6f,0x62,0x6a,0x2e,0x5f,0x5f,0x69,0x6e,0x74,0x5f,0x5f,0x28,0x29,0x29,0x20,
|
0x64,0x6f,0x63,0x5f,0x5f,0x0a,0x20,0x20,0x20,0x20,0x65,0x78,0x63,0x65,0x70,0x74,
|
||||||
0x6f,0x72,0x20,0x30,0x0a,0x64,0x65,0x66,0x20,0x66,0x6c,0x6f,0x61,0x74,0x28,0x6f,
|
0x3a,0x0a,0x20,0x20,0x20,0x20,0x20,0x70,0x72,0x69,0x6e,0x74,0x20,0x22,0x4e,0x6f,
|
||||||
0x62,0x6a,0x3d,0x4e,0x6f,0x6e,0x65,0x29,0x3a,0x20,0x72,0x65,0x74,0x75,0x72,0x6e,
|
0x20,0x64,0x6f,0x63,0x73,0x74,0x72,0x69,0x6e,0x67,0x20,0x61,0x76,0x61,0x69,0x61,
|
||||||
0x20,0x28,0x6f,0x62,0x6a,0x20,0x61,0x6e,0x64,0x20,0x6f,0x62,0x6a,0x2e,0x5f,0x5f,
|
0x6c,0x62,0x6c,0x65,0x20,0x66,0x6f,0x72,0x22,0x2c,0x20,0x6f,0x62,0x6a,0x0a,0x20,
|
||||||
0x66,0x6c,0x6f,0x61,0x74,0x5f,0x5f,0x28,0x29,0x29,0x20,0x6f,0x72,0x20,0x30,0x2e,
|
0x20,0x65,0x6c,0x73,0x65,0x3a,0x0a,0x20,0x20,0x20,0x70,0x72,0x69,0x6e,0x74,0x20,
|
||||||
0x30,0x0a,0x64,0x65,0x66,0x20,0x74,0x79,0x70,0x65,0x28,0x6f,0x62,0x6a,0x29,0x3a,
|
0x22,0x28,0x49,0x6e,0x74,0x65,0x72,0x61,0x63,0x74,0x69,0x76,0x65,0x20,0x68,0x65,
|
||||||
0x20,0x72,0x65,0x74,0x75,0x72,0x6e,0x20,0x6f,0x62,0x6a,0x2e,0x5f,0x5f,0x63,0x6c,
|
0x6c,0x70,0x20,0x69,0x73,0x20,0x6e,0x6f,0x74,0x20,0x79,0x65,0x74,0x20,0x61,0x76,
|
||||||
0x61,0x73,0x73,0x5f,0x5f,0x0a,0x64,0x65,0x66,0x20,0x64,0x69,0x72,0x28,0x6f,0x62,
|
0x61,0x69,0x6c,0x61,0x62,0x6c,0x65,0x29,0x22,0x0a,0x20,0x64,0x65,0x66,0x20,0x5f,
|
||||||
0x6a,0x29,0x3a,0x20,0x72,0x65,0x74,0x75,0x72,0x6e,0x20,0x6f,0x62,0x6a,0x2e,0x5f,
|
0x5f,0x72,0x65,0x70,0x72,0x5f,0x5f,0x28,0x73,0x65,0x6c,0x66,0x29,0x3a,0x0a,0x20,
|
||||||
0x5f,0x64,0x69,0x72,0x5f,0x5f,0x28,0x29,0x0a,0x0a,0x64,0x65,0x66,0x20,0x68,0x65,
|
0x20,0x72,0x65,0x74,0x75,0x72,0x6e,0x20,0x27,0x54,0x79,0x70,0x65,0x20,0x68,0x65,
|
||||||
0x6c,0x70,0x28,0x6f,0x62,0x6a,0x3d,0x4e,0x6f,0x6e,0x65,0x29,0x3a,0x0a,0x20,0x69,
|
0x6c,0x70,0x28,0x29,0x20,0x66,0x6f,0x72,0x20,0x6d,0x6f,0x72,0x65,0x20,0x68,0x65,
|
||||||
0x66,0x20,0x6e,0x6f,0x74,0x20,0x6f,0x62,0x6a,0x3a,0x0a,0x20,0x20,0x70,0x72,0x69,
|
0x6c,0x70,0x2c,0x20,0x6f,0x72,0x20,0x68,0x65,0x6c,0x70,0x28,0x6f,0x62,0x6a,0x29,
|
||||||
0x6e,0x74,0x20,0x22,0x4b,0x75,0x72,0x6f,0x6b,0x6f,0x20,0x2d,0x20,0x49,0x6e,0x74,
|
0x20,0x74,0x6f,0x20,0x64,0x65,0x73,0x63,0x72,0x69,0x62,0x65,0x20,0x61,0x6e,0x20,
|
||||||
0x65,0x72,0x70,0x72,0x65,0x74,0x65,0x64,0x20,0x62,0x79,0x74,0x65,0x63,0x6f,0x64,
|
0x6f,0x62,0x6a,0x65,0x63,0x74,0x2e,0x27,0x0a,0x0a,0x6c,0x65,0x74,0x20,0x68,0x65,
|
||||||
0x65,0x20,0x56,0x4d,0x2e,0x22,0x0a,0x20,0x20,0x70,0x72,0x69,0x6e,0x74,0x20,0x22,
|
0x6c,0x70,0x20,0x3d,0x20,0x48,0x65,0x6c,0x70,0x65,0x72,0x28,0x29,0x0a,0x0a,0x65,
|
||||||
0x20,0x20,0x75,0x73,0x61,0x67,0x65,0x3a,0x22,0x0a,0x20,0x20,0x70,0x72,0x69,0x6e,
|
0x78,0x70,0x6f,0x72,0x74,0x20,0x6c,0x69,0x73,0x74,0x2c,0x64,0x69,0x63,0x74,0x2c,
|
||||||
0x74,0x20,0x22,0x20,0x20,0x20,0x20,0x68,0x65,0x6c,0x70,0x28,0x29,0x20,0x20,0x20,
|
0x72,0x61,0x6e,0x67,0x65,0x2c,0x68,0x65,0x6c,0x70,0x0a,0x0a,0x5f,0x5f,0x62,0x75,
|
||||||
0x20,0x20,0x20,0x2d,0x20,0x44,0x69,0x73,0x70,0x6c,0x61,0x79,0x73,0x20,0x74,0x68,
|
0x69,0x6c,0x74,0x69,0x6e,0x73,0x5f,0x5f,0x2e,0x6d,0x6f,0x64,0x75,0x6c,0x65,0x5f,
|
||||||
0x69,0x73,0x20,0x6d,0x65,0x73,0x73,0x61,0x67,0x65,0x2e,0x22,0x0a,0x20,0x20,0x70,
|
0x70,0x61,0x74,0x68,0x73,0x20,0x3d,0x20,0x5b,0x22,0x2e,0x2f,0x22,0x2c,0x22,0x2e,
|
||||||
0x72,0x69,0x6e,0x74,0x20,0x22,0x20,0x20,0x20,0x20,0x68,0x65,0x6c,0x70,0x28,0x66,
|
0x2f,0x6d,0x6f,0x64,0x75,0x6c,0x65,0x73,0x2f,0x22,0x2c,0x22,0x2f,0x68,0x6f,0x6d,
|
||||||
0x75,0x6e,0x63,0x29,0x20,0x20,0x2d,0x20,0x44,0x69,0x73,0x70,0x6c,0x61,0x79,0x73,
|
0x65,0x2f,0x6b,0x6c,0x61,0x6e,0x67,0x65,0x2f,0x50,0x72,0x6f,0x6a,0x65,0x63,0x74,
|
||||||
0x20,0x64,0x6f,0x63,0x73,0x74,0x72,0x69,0x6e,0x67,0x20,0x66,0x6f,0x72,0x20,0x66,
|
0x73,0x2f,0x6b,0x75,0x72,0x6f,0x6b,0x6f,0x2f,0x6d,0x6f,0x64,0x75,0x6c,0x65,0x73,
|
||||||
0x75,0x6e,0x63,0x74,0x69,0x6f,0x6e,0x2c,0x20,0x69,0x66,0x20,0x70,0x72,0x65,0x73,
|
0x2f,0x22,0x2c,0x22,0x2f,0x75,0x73,0x72,0x2f,0x73,0x68,0x61,0x72,0x65,0x2f,0x6b,
|
||||||
0x65,0x6e,0x74,0x2e,0x22,0x0a,0x20,0x20,0x70,0x72,0x69,0x6e,0x74,0x20,0x22,0x20,
|
0x75,0x72,0x6f,0x6b,0x6f,0x2f,0x22,0x5d,0x0a,0x0a,0x72,0x65,0x74,0x75,0x72,0x6e,
|
||||||
0x20,0x20,0x20,0x68,0x65,0x6c,0x70,0x28,0x63,0x6c,0x61,0x73,0x73,0x29,0x20,0x2d,
|
0x20,0x6f,0x62,0x6a,0x65,0x63,0x74,0x28,0x29,0x0a,
|
||||||
0x20,0x44,0x69,0x73,0x70,0x6c,0x61,0x79,0x73,0x20,0x64,0x6f,0x63,0x73,0x74,0x72,
|
|
||||||
0x69,0x6e,0x67,0x20,0x66,0x6f,0x72,0x20,0x63,0x6c,0x61,0x73,0x73,0x2c,0x20,0x69,
|
|
||||||
0x66,0x20,0x70,0x72,0x65,0x73,0x65,0x6e,0x74,0x2e,0x22,0x0a,0x20,0x65,0x6c,0x73,
|
|
||||||
0x65,0x3a,0x0a,0x20,0x20,0x74,0x72,0x79,0x3a,0x0a,0x20,0x20,0x20,0x70,0x72,0x69,
|
|
||||||
0x6e,0x74,0x20,0x6f,0x62,0x6a,0x2e,0x5f,0x5f,0x64,0x6f,0x63,0x5f,0x5f,0x0a,0x20,
|
|
||||||
0x20,0x65,0x78,0x63,0x65,0x70,0x74,0x3a,0x0a,0x20,0x20,0x20,0x70,0x72,0x69,0x6e,
|
|
||||||
0x74,0x20,0x22,0x4e,0x6f,0x20,0x64,0x6f,0x63,0x73,0x74,0x72,0x69,0x6e,0x67,0x20,
|
|
||||||
0x61,0x76,0x61,0x69,0x6c,0x61,0x62,0x6c,0x65,0x20,0x66,0x6f,0x72,0x22,0x2c,0x20,
|
|
||||||
0x6f,0x62,0x6a,0x0a,0x0a,0x65,0x78,0x70,0x6f,0x72,0x74,0x20,0x6c,0x69,0x73,0x74,
|
|
||||||
0x2c,0x64,0x69,0x63,0x74,0x2c,0x72,0x61,0x6e,0x67,0x65,0x2c,0x6c,0x65,0x6e,0x2c,
|
|
||||||
0x73,0x74,0x72,0x2c,0x72,0x65,0x70,0x72,0x2c,0x69,0x6e,0x74,0x2c,0x66,0x6c,0x6f,
|
|
||||||
0x61,0x74,0x2c,0x64,0x69,0x72,0x2c,0x68,0x65,0x6c,0x70,0x0a,0x0a,0x5f,0x5f,0x62,
|
|
||||||
0x75,0x69,0x6c,0x74,0x69,0x6e,0x73,0x5f,0x5f,0x2e,0x6d,0x6f,0x64,0x75,0x6c,0x65,
|
|
||||||
0x5f,0x70,0x61,0x74,0x68,0x73,0x20,0x3d,0x20,0x5b,0x22,0x2e,0x2f,0x22,0x2c,0x22,
|
|
||||||
0x2e,0x2f,0x6d,0x6f,0x64,0x75,0x6c,0x65,0x73,0x2f,0x22,0x2c,0x22,0x2f,0x68,0x6f,
|
|
||||||
0x6d,0x65,0x2f,0x6b,0x6c,0x61,0x6e,0x67,0x65,0x2f,0x50,0x72,0x6f,0x6a,0x65,0x63,
|
|
||||||
0x74,0x73,0x2f,0x6b,0x75,0x72,0x6f,0x6b,0x6f,0x2f,0x6d,0x6f,0x64,0x75,0x6c,0x65,
|
|
||||||
0x73,0x2f,0x22,0x2c,0x22,0x2f,0x75,0x73,0x72,0x2f,0x73,0x68,0x61,0x72,0x65,0x2f,
|
|
||||||
0x6b,0x75,0x72,0x6f,0x6b,0x6f,0x2f,0x22,0x5d,0x0a,0x0a,0x72,0x65,0x74,0x75,0x72,
|
|
||||||
0x6e,0x20,0x6f,0x62,0x6a,0x65,0x63,0x74,0x28,0x29,0x0a,
|
|
||||||
0x00 };
|
0x00 };
|
||||||
|
37
builtins.krk
37
builtins.krk
@ -79,28 +79,25 @@ class range:
|
|||||||
return o
|
return o
|
||||||
return _
|
return _
|
||||||
|
|
||||||
def len(obj=None): return (obj and obj.__len__()) or 0
|
class Helper():
|
||||||
def str(obj=None): return (obj and obj.__str__()) or ""
|
'''You seem to already know how to use this.'''
|
||||||
def repr(obj): return obj.__repr__()
|
def __call__(self,obj=None):
|
||||||
def int(obj=None): return (obj and obj.__int__()) or 0
|
if obj:
|
||||||
def float(obj=None): return (obj and obj.__float__()) or 0.0
|
try:
|
||||||
def type(obj): return obj.__class__
|
print obj.__doc__
|
||||||
def dir(obj): return obj.__dir__()
|
except:
|
||||||
|
try:
|
||||||
|
print obj.__class__.__doc__
|
||||||
|
except:
|
||||||
|
print "No docstring avaialble for", obj
|
||||||
|
else:
|
||||||
|
print "(Interactive help is not yet available)"
|
||||||
|
def __repr__(self):
|
||||||
|
return 'Type help() for more help, or help(obj) to describe an object.'
|
||||||
|
|
||||||
def help(obj=None):
|
let help = Helper()
|
||||||
if not obj:
|
|
||||||
print "Kuroko - Interpreted bytecode VM."
|
|
||||||
print " usage:"
|
|
||||||
print " help() - Displays this message."
|
|
||||||
print " help(func) - Displays docstring for function, if present."
|
|
||||||
print " help(class) - Displays docstring for class, if present."
|
|
||||||
else:
|
|
||||||
try:
|
|
||||||
print obj.__doc__
|
|
||||||
except:
|
|
||||||
print "No docstring available for", obj
|
|
||||||
|
|
||||||
export list,dict,range,len,str,repr,int,float,dir,help
|
export list,dict,range,help
|
||||||
|
|
||||||
__builtins__.module_paths = ["./","./modules/","/home/klange/Projects/kuroko/modules/","/usr/share/kuroko/"]
|
__builtins__.module_paths = ["./","./modules/","/home/klange/Projects/kuroko/modules/","/usr/share/kuroko/"]
|
||||||
|
|
||||||
|
141
vm.c
141
vm.c
@ -593,6 +593,14 @@ KrkValue krk_typeOf(int argc, KrkValue argv[]) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static KrkValue _type_init(int argc, KrkValue argv[]) {
|
||||||
|
if (argc != 2) {
|
||||||
|
krk_runtimeError(vm.exceptions.argumentError, "type() takes 1 argument");
|
||||||
|
return NONE_VAL();
|
||||||
|
}
|
||||||
|
return krk_typeOf(1,&argv[1]);
|
||||||
|
}
|
||||||
|
|
||||||
/* Class.__base__ */
|
/* Class.__base__ */
|
||||||
static KrkValue krk_baseOfClass(int argc, KrkValue argv[]) {
|
static KrkValue krk_baseOfClass(int argc, KrkValue argv[]) {
|
||||||
return AS_CLASS(argv[0])->base ? OBJECT_VAL(AS_CLASS(argv[0])->base) : NONE_VAL();
|
return AS_CLASS(argv[0])->base ? OBJECT_VAL(AS_CLASS(argv[0])->base) : NONE_VAL();
|
||||||
@ -1145,6 +1153,25 @@ static KrkValue krk_initException(int argc, KrkValue argv[]) {
|
|||||||
return argv[0];
|
return argv[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static KrkValue _string_init(int argc, KrkValue argv[]) {
|
||||||
|
/* Ignore argument which would have been an instance */
|
||||||
|
if (argc < 2) {
|
||||||
|
return OBJECT_VAL(S(""));
|
||||||
|
}
|
||||||
|
if (argc > 2) {
|
||||||
|
krk_runtimeError(vm.exceptions.argumentError, "str() takes 1 argument");
|
||||||
|
return NONE_VAL();
|
||||||
|
}
|
||||||
|
if (IS_STRING(argv[1])) return argv[1]; /* strings are immutable, so we can just return the arg */
|
||||||
|
/* Find the type of arg */
|
||||||
|
krk_push(argv[1]);
|
||||||
|
if (!krk_bindMethod(AS_CLASS(krk_typeOf(1,&argv[1])), AS_STRING(vm.specialMethodNames[METHOD_STR]))) {
|
||||||
|
krk_runtimeError(vm.exceptions.typeError, "Can not convert %s to str", krk_typeName(argv[1]));
|
||||||
|
return NONE_VAL();
|
||||||
|
}
|
||||||
|
return krk_callSimple(krk_peek(0), 0);
|
||||||
|
}
|
||||||
|
|
||||||
#define ADD_BASE_CLASS(obj, name, baseClass) do { \
|
#define ADD_BASE_CLASS(obj, name, baseClass) do { \
|
||||||
obj = krk_newClass(S(name)); \
|
obj = krk_newClass(S(name)); \
|
||||||
krk_attachNamedObject(&vm.builtins->fields, name, (KrkObj*)obj); \
|
krk_attachNamedObject(&vm.builtins->fields, name, (KrkObj*)obj); \
|
||||||
@ -1269,6 +1296,20 @@ static KrkValue _string_to_float(int argc, KrkValue argv[]) {
|
|||||||
return FLOATING_VAL(strtod(AS_CSTRING(argv[0]),NULL));
|
return FLOATING_VAL(strtod(AS_CSTRING(argv[0]),NULL));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static KrkValue _float_init(int argc, KrkValue argv[]) {
|
||||||
|
if (argc < 1) return FLOATING_VAL(0.0);
|
||||||
|
if (argc > 2) {
|
||||||
|
krk_runtimeError(vm.exceptions.argumentError, "float() takes at most 1 argument");
|
||||||
|
return NONE_VAL();
|
||||||
|
}
|
||||||
|
if (IS_STRING(argv[1])) return _string_to_float(1,&argv[1]);
|
||||||
|
if (IS_FLOATING(argv[1])) return argv[1];
|
||||||
|
if (IS_INTEGER(argv[1])) return FLOATING_VAL(AS_INTEGER(argv[1]));
|
||||||
|
if (IS_BOOLEAN(argv[1])) return FLOATING_VAL(AS_BOOLEAN(argv[1]));
|
||||||
|
krk_runtimeError(vm.exceptions.typeError, "float() argument must be a string or a number, not '%s'", krk_typeName(argv[1]));
|
||||||
|
return NONE_VAL();
|
||||||
|
}
|
||||||
|
|
||||||
/* str.__get__(index) */
|
/* str.__get__(index) */
|
||||||
static KrkValue _string_get(int argc, KrkValue argv[]) {
|
static KrkValue _string_get(int argc, KrkValue argv[]) {
|
||||||
if (argc != 2) {
|
if (argc != 2) {
|
||||||
@ -1622,6 +1663,16 @@ static KrkValue _string_split(int argc, KrkValue argv[], int hasKw) {
|
|||||||
}
|
}
|
||||||
#undef PUSH_CHAR
|
#undef PUSH_CHAR
|
||||||
|
|
||||||
|
static KrkValue _int_init(int argc, KrkValue argv[]) {
|
||||||
|
if (argc < 2) return INTEGER_VAL(0);
|
||||||
|
if (IS_INTEGER(argv[1])) return argv[1];
|
||||||
|
if (IS_STRING(argv[1])) return _string_to_int(argc-1,&argv[1]);
|
||||||
|
if (IS_FLOATING(argv[1])) return INTEGER_VAL(AS_FLOATING(argv[1]));
|
||||||
|
if (IS_BOOLEAN(argv[1])) return INTEGER_VAL(AS_BOOLEAN(argv[1]));
|
||||||
|
krk_runtimeError(vm.exceptions.typeError, "int() argument must be a string or a number, not '%s'", krk_typeName(argv[1]));
|
||||||
|
return NONE_VAL();
|
||||||
|
}
|
||||||
|
|
||||||
/* function.__doc__ */
|
/* function.__doc__ */
|
||||||
static KrkValue _closure_get_doc(int argc, KrkValue argv[]) {
|
static KrkValue _closure_get_doc(int argc, KrkValue argv[]) {
|
||||||
if (!IS_CLOSURE(argv[0])) return NONE_VAL();
|
if (!IS_CLOSURE(argv[0])) return NONE_VAL();
|
||||||
@ -1779,6 +1830,31 @@ static KrkValue _bool_to_str(int argc, KrkValue argv[]) {
|
|||||||
return OBJECT_VAL((AS_BOOLEAN(argv[0]) ? S("True") : S("False")));
|
return OBJECT_VAL((AS_BOOLEAN(argv[0]) ? S("True") : S("False")));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Inverse of truthiness.
|
||||||
|
*
|
||||||
|
* None, False, and 0 are all "falsey", meaning they will trip JUMP_IF_FALSE
|
||||||
|
* instructions / not trip JUMP_IF_TRUE instructions.
|
||||||
|
*
|
||||||
|
* Or in more managed code terms, `if None`, `if False`, and `if 0` are all
|
||||||
|
* going to take the else branch.
|
||||||
|
*/
|
||||||
|
static int isFalsey(KrkValue value) {
|
||||||
|
return IS_NONE(value) || (IS_BOOLEAN(value) && !AS_BOOLEAN(value)) ||
|
||||||
|
(IS_INTEGER(value) && !AS_INTEGER(value));
|
||||||
|
/* Objects in the future: */
|
||||||
|
/* IS_STRING && length == 0; IS_ARRAY && length == 0; IS_INSTANCE && __bool__ returns 0... */
|
||||||
|
}
|
||||||
|
|
||||||
|
static KrkValue _bool_init(int argc, KrkValue argv[]) {
|
||||||
|
if (argc < 2) return BOOLEAN_VAL(0);
|
||||||
|
if (argc > 2) {
|
||||||
|
krk_runtimeError(vm.exceptions.argumentError, "bool() takes at most 1 argument");
|
||||||
|
return NONE_VAL();
|
||||||
|
}
|
||||||
|
return BOOLEAN_VAL(isFalsey(argv[1]));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* None.__str__() -> "None"
|
* None.__str__() -> "None"
|
||||||
*/
|
*/
|
||||||
@ -1786,6 +1862,37 @@ static KrkValue _none_to_str(int argc, KrkValue argv[]) {
|
|||||||
return OBJECT_VAL(S("None"));
|
return OBJECT_VAL(S("None"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static KrkValue _len(int argc, KrkValue argv[]) {
|
||||||
|
if (argc != 1) {
|
||||||
|
krk_runtimeError(vm.exceptions.argumentError, "len() takes exactly one argument");
|
||||||
|
return NONE_VAL();
|
||||||
|
}
|
||||||
|
if (!IS_OBJECT(argv[0])) {
|
||||||
|
krk_runtimeError(vm.exceptions.typeError, "object of type '%s' has no len()", krk_typeName(argv[0]));
|
||||||
|
return NONE_VAL();
|
||||||
|
}
|
||||||
|
if (IS_STRING(argv[0])) return INTEGER_VAL(AS_STRING(argv[0])->length);
|
||||||
|
krk_push(argv[0]);
|
||||||
|
if (!krk_bindMethod(AS_CLASS(krk_typeOf(1,&argv[0])), AS_STRING(vm.specialMethodNames[METHOD_LEN]))) {
|
||||||
|
krk_runtimeError(vm.exceptions.typeError, "object of type '%s' has no len()", krk_typeName(argv[0]));
|
||||||
|
return NONE_VAL();
|
||||||
|
}
|
||||||
|
return krk_callSimple(krk_peek(0), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
static KrkValue _repr(int argc, KrkValue argv[]) {
|
||||||
|
if (argc != 1) {
|
||||||
|
krk_runtimeError(vm.exceptions.argumentError, "repr() takes exactly one argument");
|
||||||
|
return NONE_VAL();
|
||||||
|
}
|
||||||
|
krk_push(argv[0]);
|
||||||
|
if (!krk_bindMethod(AS_CLASS(krk_typeOf(1,&argv[0])), AS_STRING(vm.specialMethodNames[METHOD_REPR]))) {
|
||||||
|
krk_runtimeError(vm.exceptions.typeError, "internal error");
|
||||||
|
return NONE_VAL();
|
||||||
|
}
|
||||||
|
return krk_callSimple(krk_peek(0), 0);
|
||||||
|
}
|
||||||
|
|
||||||
void krk_initVM(int flags) {
|
void krk_initVM(int flags) {
|
||||||
vm.flags = flags;
|
vm.flags = flags;
|
||||||
KRK_PAUSE_GC();
|
KRK_PAUSE_GC();
|
||||||
@ -1852,30 +1959,40 @@ void krk_initVM(int flags) {
|
|||||||
|
|
||||||
/* Build classes for basic types */
|
/* Build classes for basic types */
|
||||||
ADD_BASE_CLASS(vm.baseClasses.typeClass, "type", vm.objectClass);
|
ADD_BASE_CLASS(vm.baseClasses.typeClass, "type", vm.objectClass);
|
||||||
|
krk_attachNamedObject(&vm.globals, "type", (KrkObj*)vm.baseClasses.typeClass);
|
||||||
krk_defineNative(&vm.baseClasses.typeClass->methods, ":__base__", krk_baseOfClass);
|
krk_defineNative(&vm.baseClasses.typeClass->methods, ":__base__", krk_baseOfClass);
|
||||||
krk_defineNative(&vm.baseClasses.typeClass->methods, ":__file__", krk_fileOfClass);
|
krk_defineNative(&vm.baseClasses.typeClass->methods, ":__file__", krk_fileOfClass);
|
||||||
krk_defineNative(&vm.baseClasses.typeClass->methods, ":__doc__", krk_docOfClass);
|
krk_defineNative(&vm.baseClasses.typeClass->methods, ":__doc__", krk_docOfClass);
|
||||||
krk_defineNative(&vm.baseClasses.typeClass->methods, ":__name__", krk_nameOfClass);
|
krk_defineNative(&vm.baseClasses.typeClass->methods, ":__name__", krk_nameOfClass);
|
||||||
|
krk_defineNative(&vm.baseClasses.typeClass->methods, ".__init__", _type_init);
|
||||||
krk_defineNative(&vm.baseClasses.typeClass->methods, ".__str__", _class_to_str);
|
krk_defineNative(&vm.baseClasses.typeClass->methods, ".__str__", _class_to_str);
|
||||||
krk_defineNative(&vm.baseClasses.typeClass->methods, ".__repr__", _class_to_str);
|
krk_defineNative(&vm.baseClasses.typeClass->methods, ".__repr__", _class_to_str);
|
||||||
ADD_BASE_CLASS(vm.baseClasses.intClass, "int", vm.objectClass);
|
ADD_BASE_CLASS(vm.baseClasses.intClass, "int", vm.objectClass);
|
||||||
|
krk_attachNamedObject(&vm.globals, "int", (KrkObj*)vm.baseClasses.intClass);
|
||||||
|
krk_defineNative(&vm.baseClasses.intClass->methods, ".__init__", _int_init);
|
||||||
krk_defineNative(&vm.baseClasses.intClass->methods, ".__int__", _noop);
|
krk_defineNative(&vm.baseClasses.intClass->methods, ".__int__", _noop);
|
||||||
krk_defineNative(&vm.baseClasses.intClass->methods, ".__float__", _int_to_floating);
|
krk_defineNative(&vm.baseClasses.intClass->methods, ".__float__", _int_to_floating);
|
||||||
krk_defineNative(&vm.baseClasses.intClass->methods, ".__chr__", _int_to_char);
|
krk_defineNative(&vm.baseClasses.intClass->methods, ".__chr__", _int_to_char);
|
||||||
krk_defineNative(&vm.baseClasses.intClass->methods, ".__str__", _int_to_str);
|
krk_defineNative(&vm.baseClasses.intClass->methods, ".__str__", _int_to_str);
|
||||||
krk_defineNative(&vm.baseClasses.intClass->methods, ".__repr__", _int_to_str);
|
krk_defineNative(&vm.baseClasses.intClass->methods, ".__repr__", _int_to_str);
|
||||||
ADD_BASE_CLASS(vm.baseClasses.floatClass, "float", vm.objectClass);
|
ADD_BASE_CLASS(vm.baseClasses.floatClass, "float", vm.objectClass);
|
||||||
|
krk_attachNamedObject(&vm.globals, "float", (KrkObj*)vm.baseClasses.floatClass);
|
||||||
|
krk_defineNative(&vm.baseClasses.floatClass->methods, ".__init__", _float_init);
|
||||||
krk_defineNative(&vm.baseClasses.floatClass->methods, ".__int__", _floating_to_int);
|
krk_defineNative(&vm.baseClasses.floatClass->methods, ".__int__", _floating_to_int);
|
||||||
krk_defineNative(&vm.baseClasses.floatClass->methods, ".__float__", _noop);
|
krk_defineNative(&vm.baseClasses.floatClass->methods, ".__float__", _noop);
|
||||||
krk_defineNative(&vm.baseClasses.floatClass->methods, ".__str__", _float_to_str);
|
krk_defineNative(&vm.baseClasses.floatClass->methods, ".__str__", _float_to_str);
|
||||||
krk_defineNative(&vm.baseClasses.floatClass->methods, ".__repr__", _float_to_str);
|
krk_defineNative(&vm.baseClasses.floatClass->methods, ".__repr__", _float_to_str);
|
||||||
ADD_BASE_CLASS(vm.baseClasses.boolClass, "bool", vm.objectClass);
|
ADD_BASE_CLASS(vm.baseClasses.boolClass, "bool", vm.objectClass);
|
||||||
|
krk_attachNamedObject(&vm.globals, "bool", (KrkObj*)vm.baseClasses.boolClass);
|
||||||
|
krk_defineNative(&vm.baseClasses.boolClass->methods, ".__init__", _bool_init);
|
||||||
krk_defineNative(&vm.baseClasses.boolClass->methods, ".__str__", _bool_to_str);
|
krk_defineNative(&vm.baseClasses.boolClass->methods, ".__str__", _bool_to_str);
|
||||||
krk_defineNative(&vm.baseClasses.boolClass->methods, ".__repr__", _bool_to_str);
|
krk_defineNative(&vm.baseClasses.boolClass->methods, ".__repr__", _bool_to_str);
|
||||||
ADD_BASE_CLASS(vm.baseClasses.noneTypeClass, "NoneType", vm.objectClass);
|
ADD_BASE_CLASS(vm.baseClasses.noneTypeClass, "NoneType", vm.objectClass);
|
||||||
krk_defineNative(&vm.baseClasses.noneTypeClass->methods, ".__str__", _none_to_str);
|
krk_defineNative(&vm.baseClasses.noneTypeClass->methods, ".__str__", _none_to_str);
|
||||||
krk_defineNative(&vm.baseClasses.noneTypeClass->methods, ".__repr__", _none_to_str);
|
krk_defineNative(&vm.baseClasses.noneTypeClass->methods, ".__repr__", _none_to_str);
|
||||||
ADD_BASE_CLASS(vm.baseClasses.strClass, "str", vm.objectClass);
|
ADD_BASE_CLASS(vm.baseClasses.strClass, "str", vm.objectClass);
|
||||||
|
krk_attachNamedObject(&vm.globals, "str", (KrkObj*)vm.baseClasses.strClass);
|
||||||
|
krk_defineNative(&vm.baseClasses.strClass->methods, ".__init__", _string_init);
|
||||||
krk_defineNative(&vm.baseClasses.strClass->methods, ".__str__", _noop);
|
krk_defineNative(&vm.baseClasses.strClass->methods, ".__str__", _noop);
|
||||||
krk_defineNative(&vm.baseClasses.strClass->methods, ".__repr__", _repr_str);
|
krk_defineNative(&vm.baseClasses.strClass->methods, ".__repr__", _repr_str);
|
||||||
krk_defineNative(&vm.baseClasses.strClass->methods, ".__len__", _string_length);
|
krk_defineNative(&vm.baseClasses.strClass->methods, ".__len__", _string_length);
|
||||||
@ -1903,11 +2020,13 @@ void krk_initVM(int flags) {
|
|||||||
krk_defineNative(&vm.baseClasses.methodClass->methods, ":__file__", _bound_get_file);
|
krk_defineNative(&vm.baseClasses.methodClass->methods, ":__file__", _bound_get_file);
|
||||||
|
|
||||||
/* Build global builtin functions. */
|
/* Build global builtin functions. */
|
||||||
krk_defineNative(&vm.globals, "listOf", krk_list_of);
|
krk_defineNative(&vm.globals, "listOf", krk_list_of); /* Equivalent to list() */
|
||||||
krk_defineNative(&vm.globals, "dictOf", krk_dict_of);
|
krk_defineNative(&vm.globals, "dictOf", krk_dict_of); /* Equivalent to dict() */
|
||||||
krk_defineNative(&vm.globals, "isinstance", krk_isinstance);
|
krk_defineNative(&vm.globals, "isinstance", krk_isinstance);
|
||||||
krk_defineNative(&vm.globals, "globals", krk_globals);
|
krk_defineNative(&vm.globals, "globals", krk_globals);
|
||||||
krk_defineNative(&vm.globals, "type", krk_typeOf);
|
krk_defineNative(&vm.globals, "dir", krk_dirObject);
|
||||||
|
krk_defineNative(&vm.globals, "len", _len);
|
||||||
|
krk_defineNative(&vm.globals, "repr", _repr);
|
||||||
|
|
||||||
/* __builtins__.set_tracing is namespaced */
|
/* __builtins__.set_tracing is namespaced */
|
||||||
krk_defineNative(&vm.builtins->fields, "set_tracing", krk_set_tracing);
|
krk_defineNative(&vm.builtins->fields, "set_tracing", krk_set_tracing);
|
||||||
@ -1968,22 +2087,6 @@ void krk_freeVM() {
|
|||||||
FREE_ARRAY(size_t, vm.stack, vm.stackSize);
|
FREE_ARRAY(size_t, vm.stack, vm.stackSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Inverse of truthiness.
|
|
||||||
*
|
|
||||||
* None, False, and 0 are all "falsey", meaning they will trip JUMP_IF_FALSE
|
|
||||||
* instructions / not trip JUMP_IF_TRUE instructions.
|
|
||||||
*
|
|
||||||
* Or in more managed code terms, `if None`, `if False`, and `if 0` are all
|
|
||||||
* going to take the else branch.
|
|
||||||
*/
|
|
||||||
static int isFalsey(KrkValue value) {
|
|
||||||
return IS_NONE(value) || (IS_BOOLEAN(value) && !AS_BOOLEAN(value)) ||
|
|
||||||
(IS_INTEGER(value) && !AS_INTEGER(value));
|
|
||||||
/* Objects in the future: */
|
|
||||||
/* IS_STRING && length == 0; IS_ARRAY && length == 0; IS_INSTANCE && __bool__ returns 0... */
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Internal type(value).__name__ call for use in debugging methods and
|
* Internal type(value).__name__ call for use in debugging methods and
|
||||||
* creating exception strings.
|
* creating exception strings.
|
||||||
|
Loading…
Reference in New Issue
Block a user