bite the bullet and rename length() to __len__()

This commit is contained in:
K. Lange 2020-12-30 15:50:26 +09:00
parent 9fd9d81afe
commit a5928b18c9
5 changed files with 14 additions and 13 deletions

View File

@ -7,15 +7,15 @@ class list():
return __builtins__.list_set(self._list,i,v)
def append(v):
return __builtins__.list_append(self._list,v)
def length():
def __len__():
return __builtins__.list_length(self._list)
def extend(i):
for v in i:
self.append(v)
return self.length()
return self.__len__()
def __str__(self):
let b = "["
let l = self.length()
let l = self.__len__()
for i=0,i<l,i=i+1:
if i>0:
b=b+", "
@ -25,7 +25,7 @@ class list():
let m = self
def I(i):
let e=m
let l=e.length()
let l=e.__len__()
let x=i
def _():
if x>=l:
@ -53,7 +53,7 @@ class dict():
out = out + v + ": " + self[v]
out = out + "}"
return out
def length(self):
def __len__(self):
return __builtins__.hash_count(self._map)
def capacity(self):
return __builtins__.hash_capacity(self._map)
@ -98,7 +98,7 @@ class range:
return iter
return makeIter(self.min)
def len(obj): return obj.length()
def len(obj=None): return (obj and obj.__len__()) or 0
def str(obj=None): return (obj and ("" + obj)) or ""
def int(obj=None): return (obj and obj.__int__()) or 0
def float(obj=None): return (obj and obj.__float__()) or 0.0

View File

@ -12,7 +12,7 @@ def __main__():
ordering.append(1) # 9 -> 1
ordering.extend(range(11,1000001))
ordering.append(5) # 1,000,000 -> 5
print "Done, ordering is " + ordering.length()
print "Done, ordering is", len(ordering)
print "Starting loop..."
#__builtins__.set_tracing("tracing=1")

View File

@ -164,14 +164,14 @@ print hash["hello"]
print "Let's make some lists:"
let l = list()
print "Length before: " + l.length()
print "Length before: " + len(l)
l.append(1)
l.append(2)
l.append(3)
print "Length after: " + l.length()
for j = 0, j < l.length(), j = j + 1:
print "Length after: " + len(l)
for j = 0, j < len(l), j = j + 1:
print "j=" + j + ", list[j]=" + l[j]
print "Can we call properties of strings?".length() # Of course we can.
print "Can we call properties of strings?".__len__() # Of course we can.
return 0

4
vm.c
View File

@ -548,6 +548,7 @@ void krk_initVM(int flags) {
vm.specialMethodNames[METHOD_INT] = OBJECT_VAL(S("__int__"));
vm.specialMethodNames[METHOD_CHR] = OBJECT_VAL(S("__chr__"));
vm.specialMethodNames[METHOD_FLOAT]= OBJECT_VAL(S("__float__"));
vm.specialMethodNames[METHOD_LEN] = OBJECT_VAL(S("__len__"));
/* Create built-in class `object` */
vm.object_class = krk_newClass(S("object"));
@ -1153,8 +1154,7 @@ static KrkValue run() {
break;
}
case OBJ_STRING: {
/* vm.specialMethodNames[NAME_LEN] ? */
if (!strcmp(name->chars,"length")) {
if (krk_valuesEqual(OBJECT_VAL(name), vm.specialMethodNames[METHOD_LEN])) {
bindSpecialMethod(_string_length,0);
} else if (krk_valuesEqual(OBJECT_VAL(name), vm.specialMethodNames[METHOD_GET])) {
bindSpecialMethod(_string_get,1);

1
vm.h
View File

@ -26,6 +26,7 @@ typedef enum {
METHOD_INT,
METHOD_FLOAT,
METHOD_CHR,
METHOD_LEN,
METHOD__MAX,
} KrkSpecialMethods;