potion  0.2
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
primitive.c
Go to the documentation of this file.
1 //
4 // (c) 2008 why the lucky stiff, the freelance professor
5 //
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include "potion.h"
9 #include "internal.h"
10 
14 static PN potion_nil_is_nil(Potion *P, PN closure, PN self) {
15  return PN_TRUE;
16 }
17 
18 static PN potion_any_is_nil(Potion *P, PN closure, PN self) {
19  return PN_FALSE;
20 }
21 
26 PN potion_any_cmp(Potion *P, PN cl, PN self, PN value) {
27  return potion_send(self, PN_cmp, value);
28 }
33 static PN potion_nil_cmp(Potion *P, PN cl, PN self, PN value) {
34  switch (potion_type(value)) {
35  case PN_TNIL:
36  return 0;
37  case PN_TNUMBER:
38  return potion_send(PN_ZERO, PN_cmp, value);
39  case PN_TBOOLEAN:
40  return potion_send(PN_FALSE, PN_cmp, value);
41  case PN_TSTRING:
42  return potion_send(PN_STR(""), PN_cmp, value);
43  default:
44  return PN_NUM(-1);
45  }
46 }
47 
49 static PN potion_bool_cmp(Potion *P, PN cl, PN self, PN value) {
50  switch (potion_type(value)) {
51  case PN_TBOOLEAN:
52  return self < value ? -1 : self == value ? 0 : 1;
53  case PN_TNUMBER:
54  return potion_send(PN_NUM(PN_TEST(self)), PN_cmp, value);
55  case PN_TNIL:
56  case PN_TSTRING: // false < ".." < true
57  default:
58  return value == PN_FALSE ? -1 : 1; //false < any < true
59  }
60 }
61 
65 static PN potion_bool_number(Potion *P, PN closure, PN self) {
66  return PN_NUM(PN_TEST(self));
67 }
68 
72 static PN potion_bool_string(Potion *P, PN closure, PN self) {
73  if (PN_TEST(self)) return potion_str(P, "true");
74  return potion_str(P, "false");
75 }
76 
78  PN nil_vt = PN_VTABLE(PN_TNIL);
79  PN boo_vt = PN_VTABLE(PN_TBOOLEAN);
80  potion_method(nil_vt, "nil?", potion_nil_is_nil, 0);
81  potion_method(P->lobby, "nil?", potion_any_is_nil, 0);
82  potion_method(nil_vt, "number", potion_bool_number, 0);
84  potion_method(boo_vt, "number", potion_bool_number, 0);
85  potion_method(boo_vt, "string", potion_bool_string, 0);
86  potion_method(P->lobby, "cmp", potion_any_cmp, "value=o");
87  potion_method(nil_vt, "cmp", potion_nil_cmp, "value=o");
88  potion_method(boo_vt, "cmp", potion_bool_cmp, "value=o");
89 }
static PN potion_bool_string(Potion *P, PN closure, PN self)
Definition: primitive.c:72
static PN potion_nil_is_nil(Potion *P, PN closure, PN self)
memberof PN_NIL "nil?" method (non-p2)
Definition: primitive.c:14
PN potion_str(Potion *, const char *)
Definition: string.c:33
static PN potion_bool_cmp(Potion *P, PN cl, PN self, PN value)
fw to num
Definition: primitive.c:49
static PN potion_any_is_nil(Potion *P, PN closure, PN self)
Definition: primitive.c:18
#define PN_TSTRING
Definition: potion.h:112
#define NIL_NAME
Definition: potion.h:149
PN PN_cmp
Definition: internal.c:18
static PN potion_bool_number(Potion *P, PN closure, PN self)
Definition: primitive.c:65
#define potion_method(RCV, MSG, FN, SIG)
Definition: potion.h:780
#define PN_TNIL
Definition: potion.h:107
PN PN_string
Definition: internal.c:14
#define PN_STR(x)
Definition: potion.h:210
#define PN_ZERO
Definition: potion.h:140
#define PN_NUM(i)
Definition: potion.h:204
PN lobby
root namespace
Definition: potion.h:648
#define PN_TEST(v)
Definition: potion.h:155
#define PN_FALSE
Definition: potion.h:141
#define PN_TBOOLEAN
Definition: potion.h:109
PN potion_any_cmp(Potion *P, PN cl, PN self, PN value)
Definition: primitive.c:26
#define PN_TRUE
Definition: potion.h:142
non-API internal parts
static PN potion_nil_cmp(Potion *P, PN cl, PN self, PN value)
memberof NilKind "cmp" method.
Definition: primitive.c:33
the global interpreter state P. currently singleton (not threads yet)
Definition: potion.h:644
The potion API.
PN PN_def
Definition: internal.c:14
static PNType potion_type(PN obj)
either immediate (NUM,BOOL,NIL) or a fwd
Definition: potion.h:523
#define potion_send(RCV, MSG, ARGS...)
method caches (more great stuff from ian piumarta)
Definition: potion.h:772
void potion_primitive_init(Potion *P)
Definition: primitive.c:77
volatile _PN PN
Definition: potion.h:81
#define PN_TNUMBER
Definition: potion.h:108
#define PN_VTABLE(t)
Definition: potion.h:136