Obj applyPrimitive(Obj func, Obj arglist) { if (INFO) printf("%s\n", "applying PRIMITIVE..."); List* list = GETLIST(arglist); primType type = func.val.prim.type; if (type == INTPRIM) { int arg1 = list->car.val.num; int arg2 = list->cdr->car.val.num; if (INFO) printf("arg1: %d\narg2: %d\n\n", arg1, arg2); intFunc prim = func.val.prim.func.intfunc; int result = (*prim)(arg1, arg2); return NUMOBJ(result); } else if (type == OBJPRIM) { Obj arg = list->car; if (INFO) ; objFunc prim = func.val.prim.func.objfunc; int result = (*prim)(arg); return NUMOBJ(result); } else { printf("apply_primitive: unknown primitive function type!\n"); return DUMMYOBJ; } }
bool noCompoundArgs(Obj expr) { if (noArgs(expr)) return true; else { List* list = GETLIST(expr); return isSimple(list->car) && noCompoundArgs(LISTOBJ(list->cdr)); } }
/* * 判断传入参数是否为素数 * 0 表示不是素数 * 1 表示是素数 */ int isPrime(long num) { if (num < 0) { return 0; } else { return GETLIST(num); } }
Obj adjoinArg(Obj val, Obj arglist) { List* args = GETLIST(arglist); List* head = malloc(sizeof(List)); head->car = val; head->cdr = args; head = reverse(head); return LISTOBJ(head); }
Obj adjoinArg(Obj val, Obj arglist) { List* args = GETLIST(arglist); if (!args) return LISTOBJ(makeList(val, args)); List* pnr = args; while (pnr->cdr) pnr = pnr->cdr; pnr->cdr = malloc(sizeof(List)); pnr = pnr->cdr; pnr->car = val; pnr->cdr = NULL; return LISTOBJ(args); }
Obj transformOr(Obj expr) { Obj seq = boolExps(expr); if (noExps(seq)) return FALSEOBJ; Obj first = firstExp(seq); List* cdr = GETLIST(seq)->cdr; Obj rest = LISTOBJ(makeList(OROBJ, cdr)); List* ifTrans = makeList( IFOBJ, makeList(first, makeList(TRUEOBJ, makeList(rest, NULL)))); return LISTOBJ(ifTrans); }
/* * checkPrime用于判断单个数num是否是素数 * 这个函数一般在num超过已有list所存储上限的时候调用 * 判断方式根据素数定义, 判断除了1和其自身外, 是否有其他小于它的数能整除它 */ int checkPrime(long num) { long i; /* 这里只需要判断sqrt(num)以内的数即可 */ for (i = 2; i * i< num; i++) { if (i < list_len * bits){ /* * 若除数的范围仍然在list给定的范围之内, 可以先判断这个数是否为素数 * 若是素数, 再判断是否可以整除; 非素数的情况不用考虑 */ if (GETLIST(i) && num % i == 0) break; } else /* 当除数大于list给定范围时, 只能一个个考虑了 */ if (num % i == 0) break; } if (num % i == 0) return 0; else return 1; }
// UGLY HACK bool noExps(Obj seq) { List* list = GETLIST(seq); return GETLIST(seq) == NULL || list->car.tag == ENV; }
Obj quotedText(Obj expr) { return CADR(GETLIST(expr)); }
Obj firstArg(Obj expr) { return CAR(GETLIST(expr)); }
// UGLY HACK bool isLastExp(Obj seq) { List* next = CDR(GETLIST(seq)); return next == NULL || next->car.tag == ENV; //return CDR(GETLIST(seq)) == NULL; }
Obj lambdaParams(Obj expr) { return CADR(GETLIST(expr)); }
bool noExps(Obj seq) { List* list = GETLIST(seq); return list == NULL; }
Obj funcParams(Obj obj) { return CADR(GETLIST(obj)); }
Obj ifThen(Obj expr) { return CADDR(GETLIST(expr)); }
Obj defVal(Obj expr) { return CADDR(GETLIST(expr)); }
char* specialForm(Obj expr) { // return GETLIST(expr)->car.val.name; return GETNAME(CAR(GETLIST(expr))); }
Obj restArgs(Obj expr) { return LISTOBJ(CDR(GETLIST(expr))); }
Obj getFunc(Obj expr) { return CAR(GETLIST(expr)); }
bool isLastArg(Obj expr) { return CDR(GETLIST(expr)) == NULL; }
Obj beginActions(Obj expr) { return LISTOBJ(CDR(GETLIST(expr))); }
Obj assVal(Obj expr) { return CADDR(GETLIST(expr)); }
Obj ifTest(Obj expr) { return CADR(GETLIST(expr)); }
Obj funcEnv(Obj obj) { return CADDDR(GETLIST(obj)); }
Obj ifElse(Obj expr) { return CADDDR(GETLIST(expr)); }
Obj firstExp(Obj seq) { return CAR(GETLIST(seq)); }
Obj lambdaBody(Obj expr) { return CADDR(GETLIST(expr)); }
Obj restExps(Obj seq) { // return MKOBJ(LIST, list, CDR(GETLIST(seq))); return LISTOBJ(CDR(GETLIST(seq))); }
Obj funcBody(Obj obj) { return LISTOBJ(CDDR(GETLIST(obj))); }
bool noArgs(Obj expr) { return GETLIST(expr) == NULL; }