commit 9ab74bfb577816a945a19c79af0c6fa2cf4ffd09
parent c68e0cc5e8798230d18349e007becffa03396d61
Author: Andres Navarro <canavarro82@gmail.com>
Date: Wed, 16 Mar 2011 02:04:24 -0300
Added encapsulation object definition.
Diffstat:
4 files changed, 83 insertions(+), 1 deletion(-)
diff --git a/src/Makefile b/src/Makefile
@@ -12,7 +12,7 @@ CORE_O= kobject.o ktoken.o kpair.o kstring.o ksymbol.o kread.o \
kcontinuation.o koperative.o kapplicative.o keval.o krepl.o \
kground.o kghelpers.o kgbooleans.o kgeqp.o kgequalp.o \
kgsymbols.o kgcontrol.o kgpairs_lists.o kgpair_mut.o kgenvironments.o \
- kgenv_mut.o kgcombiners.o kgcontinuations.o
+ kgenv_mut.o kgcombiners.o kgcontinuations.o kencapsulation.o
KRN_T= klisp
KRN_O= klisp.o
@@ -63,6 +63,8 @@ koperative.o: koperative.c koperative.h kmem.h kstate.h kobject.h \
klisp.h
kapplicative.o: kapplicative.c kapplicative.h kmem.h kstate.h kobject.h \
klisp.h
+kencapsulation.o: kencapsulation.c kencapsulation.h kmem.h kstate.h kobject.h \
+ klisp.h kpair.h
keval.o: keval.c keval.h kcontinuation.h kenvironment.h kstate.h kobject.h \
kpair.h kerror.h klisp.h
krepl.o: krepl.c krepl.h kcontinuation.h kstate.h kobject.h keval.h klisp.h \
diff --git a/src/kencapsulation.c b/src/kencapsulation.c
@@ -0,0 +1,46 @@
+/*
+** kencapsulation.c
+** Kernel Encapsulation Types
+** See Copyright Notice in klisp.h
+*/
+
+#include "kobject.h"
+#include "kmem.h"
+#include "kstate.h"
+#include "kencapsulation.h"
+#include "kpair.h"
+
+TValue kmake_encapsulation(klisp_State *K, TValue name, TValue si,
+ TValue key, TValue val)
+{
+ Encapsulation *new_enc = klispM_new(K, Encapsulation);
+
+ /* header + gc_fields */
+ new_enc->next = K->root_gc;
+ K->root_gc = (GCObject *)new_enc;
+ new_enc->gct = 0;
+ new_enc->tt = K_TENCAPSULATION;
+ new_enc->flags = 0;
+
+ /* encapsulation specific fields */
+ new_enc->name = name;
+ new_enc->si = si;
+ new_enc->key = key;
+ new_enc->value = val;
+
+ return gc2enc(new_enc);
+}
+
+TValue kmake_encapsulation_key(klisp_State *K)
+{
+ return kcons(K, KINERT, KINERT);
+}
+
+bool kis_encapsulation_type(klisp_State *K, TValue enc, TValue key)
+{
+ return ttisencapsulation(enc) && tv_equal(kget_enc_key(enc), key);
+}
+
+#define kget_enc_val(e_)(tv2enc(e_)->value)
+#define kget_enc_key(e_)(tv2enc(e_)->key)
+
diff --git a/src/kencapsulation.h b/src/kencapsulation.h
@@ -0,0 +1,21 @@
+/*
+** kencapsulation.h
+** Kernel Encapsulation Types
+** See Copyright Notice in klisp.h
+*/
+
+#ifndef kencapsulation_h
+#define kencapsulation_h
+
+#include "kobject.h"
+#include "kstate.h"
+
+TValue kmake_encapsulation(klisp_State *K, TValue name, TValue si,
+ TValue key, TValue val);
+TValue kmake_encapsulation_key(klisp_State *K);
+bool kis_encapsulation_type(klisp_State *K, TValue enc, TValue key);
+
+#define kget_enc_val(e_)(tv2enc(e_)->value)
+#define kget_enc_key(e_)(tv2enc(e_)->key)
+
+#endif
diff --git a/src/kobject.h b/src/kobject.h
@@ -121,6 +121,7 @@ typedef struct __attribute__ ((__packed__)) GCheader {
#define K_TCONTINUATION 34
#define K_TOPERATIVE 35
#define K_TAPPLICATIVE 36
+#define K_TENCAPSULATION 37
#define K_MAKE_VTAG(t) (K_TAG_TAGGED | (t))
@@ -152,6 +153,7 @@ typedef struct __attribute__ ((__packed__)) GCheader {
#define K_TAG_CONTINUATION K_MAKE_VTAG(K_TCONTINUATION)
#define K_TAG_OPERATIVE K_MAKE_VTAG(K_TOPERATIVE)
#define K_TAG_APPLICATIVE K_MAKE_VTAG(K_TAPPLICATIVE)
+#define K_TAG_ENCAPSULATION K_MAKE_VTAG(K_TENCAPSULATION)
/*
@@ -188,6 +190,7 @@ typedef struct __attribute__ ((__packed__)) GCheader {
t_ == K_TAG_OPERATIVE || t_ == K_TAG_APPLICATIVE;})
#define ttisenvironment(o) (tbasetype_(o) == K_TAG_ENVIRONMENT)
#define ttiscontinuation(o) (tbasetype_(o) == K_TAG_CONTINUATION)
+#define ttisencapsulation(o) (tbasetype_(o) == K_TAG_ENCAPSULATION)
/* macros to easily check boolean values */
#define kis_true(o_) (tv_equal((o_), KTRUE))
@@ -276,6 +279,13 @@ typedef struct __attribute__ ((__packed__)) {
TValue underlying; /* underlying operative/applicative */
} Applicative;
+typedef struct __attribute__ ((__packed__)) {
+ CommonHeader;
+ TValue name;
+ TValue si; /* source code info (either () or (filename line col) */
+ TValue key; /* unique pair identifying this type of encapsulation */
+ TValue value; /* encapsulated object */
+} Encapsulation;
/*
** RATIONALE:
@@ -314,6 +324,7 @@ union GCObject {
Continuation cont;
Operative op;
Applicative app;
+ Encapsulation enc;
};
@@ -373,6 +384,7 @@ const TValue keminf;
#define gc2cont(o_) (gc2tv(K_TAG_CONTINUATION, o_))
#define gc2op(o_) (gc2tv(K_TAG_OPERATIVE, o_))
#define gc2app(o_) (gc2tv(K_TAG_APPLICATIVE, o_))
+#define gc2enc(o_) (gc2tv(K_TAG_ENCAPSULATION, o_))
/* Macro to convert a TValue into a specific heap allocated object */
#define tv2pair(v_) ((Pair *) gcvalue(v_))
@@ -382,6 +394,7 @@ const TValue keminf;
#define tv2cont(v_) ((Continuation *) gcvalue(v_))
#define tv2op(v_) ((Operative *) gcvalue(v_))
#define tv2app(v_) ((Applicative *) gcvalue(v_))
+#define tv2enc(v_) ((Encapsulation *) gcvalue(v_))
#define tv2gch(v_) ((GCheader *) gcvalue(v_))
#define tv2mgch(v_) ((MGCheader *) gcvalue(v_))