/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-â
âvi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8                                :viâ
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ¡
â Copyright 2022 Justine Alexandra Roberts Tunney                              â
â                                                                              â
â Permission to use, copy, modify, and/or distribute this software for         â
â any purpose with or without fee is hereby granted, provided that the         â
â above copyright notice and this permission notice appear in all copies.      â
â                                                                              â
â THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL                â
â WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED                â
â WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE             â
â AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL         â
â DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR        â
â PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER               â
â TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR             â
â PERFORMANCE OF THIS SOFTWARE.                                                â
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ*/
#include "tool/plinko/lib/plinko.h"

/**
 * Compares LISP data structures
 *
 *     (â· ð¥ ð¦) â¹ (â¥) | â¥ | â¤
 *
 * Where
 *
 *     (â¥) means less than       a.k.a. -1
 *      â¥  means equal           a.k.a.  0
 *      â¤  means greater than    a.k.a. +1
 *
 * The comparison is performed as follows:
 *
 *     (â· ð¥ ð¥) â¹ â¥                   everything's equal to itself
 *     (â¡ ð¥ ð¦) â¶ (â¡ (â· ð¥ ð¦) â¥)       (eq) and (cmp) agree if (eq) returns t
 *     (â¡ (â· ð¥ ð¦) â¥) âº (equal ð¥ ð¦)   (cmp) returns eq iff (equal) returns t
 *     (â· (â¶ x ð¦) (â¶ x ð¦)) â¹ â¥       i.e. this does deep comparisons
 *     (â· â¥ ð¥) â¹ (â¥)                 nil is less than everything non-nil
 *     (â· ð¥ â¥) â¹ â¤                   comparisons are always symmetric
 *     (â· ð ð) â¹ (â¥)                 atom vs. atom compares unicodes
 *     (â· ðð ðð) âº (â· (ð ð) (ð ð))   atom characters treated like lists
 *     (â· ð (x . ð¦)) â¹ (â¥)           atom vs. cons is always less than
 *     (â· (x . ð¦) (x . ð¦)) â¹ â¥       cons vs. cons just recurses
 *     (â· (ð¥) (â¥ ð¦)) â¹ â¤             e.g. cmp returns gt because ð¥ > â¥
 *     (â· (ð¥) (ð§ ð¦)) â¹ (â¥)           e.g. cmp returns lt because â¥ < (ð¦)
 *     (â· (x . ð¦) (x ð¦)) â¹ (â¥)       e.g. cmp returns lt because ð¦ < (ð¦)
 *
 * @return -1, 0, +1
 */
int Cmp(int x, int y) {
  int c;
  dword t, u;
  if (x == y) return 0;
  if (x > 1 && y > 1) {
    if (LO(Get(x)) < LO(Get(x))) return -1;
    if (LO(Get(x)) > LO(Get(x))) return +1;
  }
  for (;; x = Cdr(x), y = Cdr(y)) {
    if (x == y) return 0;
    if (!x) return -1;
    if (!y) return +1;
    if (x < 0) {
      if (y >= 0) return +1;
      if ((c = Cmp(Car(x), Car(y)))) return c;
    } else {
      if (y < 0) return -1;
      for (;;) {
        t = x != 1 ? Get(x) : MAKE(L'T', TERM);
        u = y != 1 ? Get(y) : MAKE(L'T', TERM);
        if (LO(t) != LO(u)) {
          return LO(t) < LO(u) ? -1 : +1;
        }
        x = HI(t);
        y = HI(u);
        if (x == y) return 0;
        if (x == TERM) return -1;
        if (y == TERM) return +1;
      }
      if (Car(x) != Car(y)) {
        return Car(x) < Car(y) ? -1 : +1;
      }
    }
  }
}
