p2  0.0
 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 "p2.h"
9 #include "internal.h"
10 
11 #ifdef P2
12 /*
16 static PN potion_nil_is_defined(Potion *P, PN closure, PN self) {
17  return PN_FALSE;
18 }
19 PN potion_any_is_defined(Potion *P, PN closure, PN self) {
20  return PN_TRUE;
21 }
22 #else
26 static PN potion_nil_is_nil(Potion *P, PN closure, PN self) {
27  return PN_TRUE;
28 }
29 PN potion_any_is_nil(Potion *P, PN closure, PN self) {
30  return PN_FALSE;
31 }
32 */
33 #endif
34 
39 PN potion_any_cmp(Potion *P, PN cl, PN self, PN value) {
40  return potion_send(self, PN_cmp, value);
41 }
46 static PN potion_nil_cmp(Potion *P, PN cl, PN self, PN value) {
47  switch (potion_type(value)) {
48  case PN_TNIL:
49  return 0;
50  case PN_TNUMBER:
51  return potion_send(PN_ZERO, PN_cmp, value);
52  case PN_TBOOLEAN:
53  return potion_send(PN_FALSE, PN_cmp, value);
54  case PN_TSTRING:
55  return potion_send(PN_STR(""), PN_cmp, value);
56  default:
57  return PN_NUM(-1);
58  }
59 }
60 
62 static PN potion_bool_cmp(Potion *P, PN cl, PN self, PN value) {
63  switch (potion_type(value)) {
64  case PN_TBOOLEAN:
65  return self < value ? -1 : self == value ? 0 : 1;
66  case PN_TNUMBER:
67  return potion_send(PN_NUM(PN_TEST(self)), PN_cmp, value);
68  case PN_TNIL:
69  case PN_TSTRING: // false < ".." < true
70  default:
71  return value == PN_FALSE ? -1 : 1; //false < any < true
72  }
73 }
74 
78 static PN potion_bool_number(Potion *P, PN closure, PN self) {
79  return PN_NUM(PN_TEST(self));
80 }
81 
85 static PN potion_bool_string(Potion *P, PN closure, PN self) {
86  if (PN_TEST(self)) return potion_str(P, "true");
87  return potion_str(P, "false");
88 }
89 
91  PN nil_vt = PN_VTABLE(PN_TNIL);
92  PN boo_vt = PN_VTABLE(PN_TBOOLEAN);
93 #ifdef P2
94  potion_send(nil_vt, PN_def, PN_STR("defined"), PN_FALSE);
95  potion_send(P->lobby, PN_def, PN_STR("defined"), PN_TRUE);
96  //potion_method(P->lobby, "defined", potion_any_is_defined, 0);
97  //potion_method(nil_vt, "defined", potion_nil_is_defined, 0);
98 #else
99  potion_send(nil_vt, PN_def, PN_STR("nil?"), PN_TRUE);
100  potion_send(P->lobby, PN_def, PN_STR("nil?"), PN_FALSE);
101  //potion_method(P->lobby, NIL_NAME"?", potion_any_is_nil, 0);
102  //potion_method(nil_vt, NIL_NAME"?", potion_nil_is_nil, 0);
103 #endif
104  potion_method(nil_vt, "number", potion_bool_number, 0);
106  potion_method(boo_vt, "number", potion_bool_number, 0);
107  potion_method(boo_vt, "string", potion_bool_string, 0);
108  potion_method(P->lobby, "cmp", potion_any_cmp, "value=o");
109  potion_method(nil_vt, "cmp", potion_nil_cmp, "value=o");
110  potion_method(boo_vt, "cmp", potion_bool_cmp, "value=o");
111 }
static PN potion_bool_string(Potion *P, PN closure, PN self)
Definition: primitive.c:85
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:62
#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:78
#define potion_method(RCV, MSG, FN, SIG)
Definition: potion.h:789
#define PN_TNIL
Definition: potion.h:107
PN PN_string
Definition: internal.c:14
#define PN_STR(x)
Definition: potion.h:219
#define PN_ZERO
Definition: potion.h:140
#define PN_NUM(i)
Definition: potion.h:213
PN lobby
root namespace
Definition: potion.h:657
#define PN_TEST(v)
Definition: potion.h:162
#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:39
#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:46
the global interpreter state P. currently singleton (not threads yet)
Definition: potion.h:653
PN PN_def
Definition: internal.c:14
The p2 API.
static PNType potion_type(PN obj)
either immediate (NUM,BOOL,NIL) or a fwd
Definition: potion.h:532
#define potion_send(RCV, MSG, ARGS...)
method caches (more great stuff from ian piumarta)
Definition: potion.h:781
void potion_primitive_init(Potion *P)
Definition: primitive.c:90
volatile _PN PN
Definition: potion.h:81
#define PN_TNUMBER
Definition: potion.h:108
#define PN_VTABLE(t)
Definition: potion.h:136