listiter.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include "__ofunc.h"
  2. ResultType listiter_init(OFFICAL_FUNCTIONSIG){
  3. ArgumentParser ap[] = {{.type=only_value, .must=1, .long_arg=false},
  4. {.type=name_value, .name="list_", .must=1, .long_arg=false},
  5. {.must=-1}};
  6. LinkValue *index = NULL;
  7. setResultCore(result);
  8. parserArgumentUnion(ap, arg, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  9. if (!CHECK_RESULT(result))
  10. return result->type;
  11. freeResult(result);
  12. if (ap[1].value->value->type != list){
  13. setResultError(E_TypeException, ONLY_ACC(listiter, list), 0, "listiter", true, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  14. return error_return;
  15. }
  16. index = makeLinkValue(makeNumberValue(0, inter), ap[0].value, inter);
  17. addAttributes("__list", false, ap[1].value, ap[0].value, inter);
  18. addAttributes("__index", false, index, ap[0].value, inter);
  19. return result->type;
  20. }
  21. ResultType listiter_next(OFFICAL_FUNCTIONSIG){
  22. ArgumentParser ap[] = {{.type=only_value, .must=1, .long_arg=false},
  23. {.must=-1}};
  24. LinkValue *list_ = NULL;
  25. LinkValue *index = NULL;
  26. setResultCore(result);
  27. parserArgumentUnion(ap, arg, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  28. if (!CHECK_RESULT(result))
  29. return result->type;
  30. list_ = findAttributes("__list", false, ap[0].value, inter);
  31. index = findAttributes("__index", false, ap[0].value, inter);
  32. if (list_->value->type != list){
  33. setResultError(E_TypeException, VALUE_ERROR(listiter.__list, list), 0, "listiter", true, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  34. return error_return;
  35. }
  36. if (index->value->type != number){
  37. setResultError(E_TypeException, VALUE_ERROR(listiter.__index, number), 0, "listiter", true, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  38. return error_return;
  39. }
  40. freeResult(result);
  41. elementDownOne(list_, index, 0, "listiter", CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  42. if (!CHECK_RESULT(result))
  43. setResultError(E_StopIterException, "Stop Iter", 0, "listiter", true, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  44. else {
  45. index->value->data.num.num ++;
  46. addAttributes("__index", false, index, ap[0].value, inter);
  47. }
  48. return result->type;
  49. }
  50. void registeredListIter(REGISTERED_FUNCTIONSIG){
  51. LinkValue *object = makeLinkValue(inter->data.list_iter, inter->base_father, inter);
  52. NameFunc tmp[] = {{inter->data.object_init, listiter_init, object_free_},
  53. {inter->data.object_next, listiter_next, object_free_},
  54. {NULL, NULL}};
  55. gc_addTmpLink(&object->gc_status);
  56. addStrVar("listiter", false, true, object, belong, CALL_INTER_FUNCTIONSIG_CORE(inter->var_list));
  57. iterClassFunc(tmp, object, CALL_INTER_FUNCTIONSIG_CORE(inter->var_list));
  58. gc_freeTmpLink(&object->gc_status);
  59. }
  60. void makeBaseListIter(Inter *inter){
  61. Value *list_iter = makeBaseChildClass(inter->data.vobject, inter);
  62. gc_addStatementLink(&list_iter->gc_status);
  63. inter->data.list_iter = list_iter;
  64. }