/* return 0 for equal, 1 for great, -1 for less, 2 for non equal(cannot compare size) -2 for type error */ int atom_compare(const Atom* a1, const Atom* a2) { { /* string */ if (a1->type == ATOM_TYPE_STRING && a2->type == ATOM_TYPE_STRING) { return strcmp(a1->string, a2->string); } } { /* list */ List* lst1 = NULL, *lst2 = NULL; if (atom_get_list((Atom*)a1, &lst1) == 0 && atom_get_list((Atom*)a2, &lst1) == 0) { return list_compare(lst1, lst2); } } { /* function */ Function* func1, *func2; if (atom_get_function((Atom*)a1, &func1) == 0 && atom_get_function((Atom*)a1, &func2) == 0) { if (func1->type != func2->type) { return 0; } if (func1->type == FUNC_TYPE_INTERNAL && func1->internal == func2->internal) { return 0; } if (func1->type == FUNC_TYPE_USERDEFINED && list_compare(func1->parameter, func2->parameter) == 0 && list_compare(func1->user_defined, func2->user_defined) == 0) { return 0; } return 2; } } { /* token */ if (a1->type == ATOM_TYPE_TOKEN && a2->type == ATOM_TYPE_TOKEN) { return strcmp(a1->token, a2->token); } } { /* signed/unsigned int */ long intval1, intval2; if (atom_get_int((Atom*)a1, &intval1) == 0 && atom_get_int((Atom*)a2, &intval2) == 0) { return (intval1 == intval2 ? 0 : (intval1 < intval2 ? -1 : 1)); } } { /* boolean */ char boolval1, boolval2; if (atom_get_boolean((Atom*)a1, &boolval1) == 0 && atom_get_boolean((Atom*)a2, &boolval2) == 0) { if (boolval1 == boolval2) return 0; return 2; } } return -2; }
int value_compare (int * a) { if (a) list_compare (a); }