add method for list

This commit is contained in:
lyon 2021-11-17 10:40:08 +08:00
parent f2b652ca80
commit f3e17a2a75
2 changed files with 27 additions and 8 deletions

View File

@ -30,8 +30,8 @@ class List(TinyObj):
def get(i: int) -> any:
pass
def len() -> int:
def set(i: int, arg: any):
pass
def remove(arg: any):
def len() -> int:
pass

View File

@ -1,16 +1,35 @@
#include "PikaStdLib_List.h"
void PikaStdLib_List_append(PikaObj* self, Arg* arg) {
obj_setArg(self, "", arg);
int top = obj_getInt(self, "top");
char buff[11];
char* topStr = fast_itoa(buff, top);
obj_setArg(self, topStr, arg);
/* top++ */
obj_setInt(self, "top", top + 1);
}
int PikaStdLib_List_len(PikaObj* self) {
return 0;
return obj_getInt(self, "top");
}
Arg* PikaStdLib_List_get(PikaObj* self, int i) {
return NULL;
char buff[11];
char* index = fast_itoa(buff, i);
return obj_getArg(self, index);
}
void PikaStdLib_List_remove(PikaObj* self, Arg* arg) {}
void PikaStdLib_List_init(PikaObj* self) {
/* set top index for append */
obj_setInt(self, "__top", 0);
obj_setInt(self, "top", 0);
}
void PikaStdLib_List_set(PikaObj* self, Arg* arg, int i) {
char buff[11];
char* i_str = fast_itoa(buff, i);
int top = obj_getInt(self, "top");
if (i > top) {
obj_setErrorCode(self, 1);
obj_setSysOut(self, "[error]: index exceeded lengh of list.");
}
obj_setArg(self, i_str, arg);
}