This source file includes following definitions.
- potion_any_cmp
- potion_nil_cmp
- potion_bool_cmp
- potion_bool_number
- potion_bool_string
- potion_primitive_init
#include <stdio.h>
#include <stdlib.h>
#include "p2.h"
#include "internal.h"
#ifdef P2
#endif
PN potion_any_cmp(Potion *P, PN cl, PN self, PN value) {
return potion_send(self, PN_cmp, value);
}
static PN potion_nil_cmp(Potion *P, PN cl, PN self, PN value) {
switch (potion_type(value)) {
case PN_TNIL:
return 0;
case PN_TNUMBER:
return potion_send(PN_ZERO, PN_cmp, value);
case PN_TBOOLEAN:
return potion_send(PN_FALSE, PN_cmp, value);
case PN_TSTRING:
return potion_send(PN_STR(""), PN_cmp, value);
default:
return PN_NUM(-1);
}
}
static PN potion_bool_cmp(Potion *P, PN cl, PN self, PN value) {
switch (potion_type(value)) {
case PN_TBOOLEAN:
return self < value ? -1 : self == value ? 0 : 1;
case PN_TNUMBER:
return potion_send(PN_NUM(PN_TEST1(self)), PN_cmp, value);
case PN_TNIL:
case PN_TSTRING:
default:
return value == PN_FALSE ? -1 : 1;
}
}
static PN potion_bool_number(Potion *P, PN closure, PN self) {
return PN_NUM(PN_TEST1(self));
}
static PN potion_bool_string(Potion *P, PN closure, PN self) {
if (PN_TEST1(self)) return potion_str(P, "true");
return potion_str(P, "false");
}
void potion_primitive_init(Potion *P) {
PN nil_vt = PN_VTABLE(PN_TNIL);
PN boo_vt = PN_VTABLE(PN_TBOOLEAN);
#ifdef P2
potion_send(nil_vt, PN_def, PN_STR("defined"), PN_FALSE);
potion_send(P->lobby, PN_def, PN_STR("defined"), PN_TRUE);
#else
potion_send(nil_vt, PN_def, PN_STR("nil?"), PN_TRUE);
potion_send(P->lobby, PN_def, PN_STR("nil?"), PN_FALSE);
#endif
potion_method(nil_vt, "number", potion_bool_number, 0);
potion_send(nil_vt, PN_def, PN_string, potion_str(P, NIL_NAME));
potion_method(boo_vt, "number", potion_bool_number, 0);
potion_method(boo_vt, "string", potion_bool_string, 0);
potion_method(P->lobby, "cmp", potion_any_cmp, "value=o");
potion_method(nil_vt, "cmp", potion_nil_cmp, "value=o");
potion_method(boo_vt, "cmp", potion_bool_cmp, "value=o");
}