klisp

an open source interpreter for the Kernel Programming Language.
git clone http://git.hanabi.in/repos/klisp.git
Log | Files | Refs | README

commit 504ffa97f0f1545ee54a2891b53b90b885c3ece0
parent 3038df214b3f593a1023f86797df6bce29a0a5f7
Author: Andres Navarro <canavarro82@gmail.com>
Date:   Mon, 12 Dec 2011 01:12:13 -0300

Added module type definition, gc, constructor and field getters

Diffstat:
MTODO | 3++-
Msrc/Makefile | 5++++-
Msrc/kgc.c | 10++++++++++
Msrc/klisp.c | 2+-
Asrc/kmodule.c | 27+++++++++++++++++++++++++++
Asrc/kmodule.h | 20++++++++++++++++++++
Msrc/kobject.c | 4+++-
Msrc/kobject.h | 12++++++++++++
8 files changed, 79 insertions(+), 4 deletions(-)

diff --git a/TODO b/TODO @@ -5,7 +5,8 @@ ** documentation: *** update the manual with the current features *** add a section to the manual with the interpreter usage - +** refactor +*** convert all tabs to spaces (use sed) * Release 0.4+ ** refactor: diff --git a/src/Makefile b/src/Makefile @@ -36,7 +36,8 @@ CORE_O= kobject.o ktoken.o kpair.o kstring.o ksymbol.o kread.o \ kcontinuation.o koperative.o kapplicative.o keval.o krepl.o \ kencapsulation.o kpromise.o kport.o kinteger.o krational.o ksystem.o \ kreal.o ktable.o kgc.o imath.o imrat.o kbytevector.o kvector.o \ - kchar.o kkeyword.o kground.o kghelpers.o kgbooleans.o kgeqp.o \ + kchar.o kkeyword.o kmodule.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 \ kgencapsulations.o kgpromises.o kgkd_vars.o kgks_vars.o kgports.o \ @@ -266,6 +267,8 @@ kport.o: kport.c kport.h kobject.h klimits.h klisp.h klispconf.h kstate.h \ ktoken.h kmem.h kerror.h kpair.h kgc.h kstring.h kbytevector.h kpromise.o: kpromise.c kobject.h klimits.h klisp.h klispconf.h kstate.h \ ktoken.h kmem.h kpromise.h kpair.h kgc.h +kmodule.o: kmodule.c kobject.h klimits.h klisp.h klispconf.h kstate.h \ + ktoken.h kmem.h kmodule.h kgc.h krational.o: krational.c krational.h kobject.h klimits.h klisp.h \ klispconf.h kstate.h ktoken.h kmem.h kinteger.h imath.h imrat.h kgc.h kread.o: kread.c kread.h kobject.h klimits.h klisp.h klispconf.h kstate.h \ diff --git a/src/kgc.c b/src/kgc.c @@ -114,6 +114,7 @@ static void reallymarkobject (klisp_State *K, GCObject *o) case K_TVECTOR: case K_TFPORT: case K_TMPORT: + case K_TMODULE: o->gch.gclist = K->gray; K->gray = o; break; @@ -347,6 +348,12 @@ static int32_t propagatemark (klisp_State *K) { markvaluearray(K, v->array, v->sizearray); return sizeof(Vector) + v->sizearray * sizeof(TValue); } + case K_TMODULE: { + Module *m = cast(Module *, o); + markvalue(K, m->env); + markvalue(K, m->exp_list); + return sizeof(Module); + } default: fprintf(stderr, "Unknown GCObject type (in GC propagate): %d\n", type); @@ -499,6 +506,9 @@ static void freeobj (klisp_State *K, GCObject *o) { case K_TVECTOR: klispM_freemem(K, o, sizeof(Vector) + sizeof(TValue) * o->vector.sizearray); break; + case K_TMODULE: + klispM_free(K, (Module *)o); + break; default: /* shouldn't happen */ fprintf(stderr, "Unknown GCObject type (in GC free): %d\n", diff --git a/src/klisp.c b/src/klisp.c @@ -10,7 +10,7 @@ ** ** For starters: ** - Split dofile in dofile & dostdin -** - Merge dofile and dorfile with a boolean flat (load/require) +** - Merge dofile and dorfile with a boolean flag (load/require) ** (use dorfile as a model) ** - Add get_ground_binding somewhere (probably kstate) and use it. */ diff --git a/src/kmodule.c b/src/kmodule.c @@ -0,0 +1,27 @@ +/* +** kmodule.c +** Kernel Modules +** See Copyright Notice in klisp.h +*/ + +#include "kobject.h" +#include "kstate.h" +#include "kmodule.h" +#include "kmem.h" +#include "kgc.h" + +/* GC: Assumes env & ext_list are roooted */ +/* ext_list should be immutable */ +TValue kmake_module(klisp_State *K, TValue env, TValue exp_list) +{ + klisp_assert(kis_immutable(exp_list)); + Module *new_mod = klispM_new(K, Module); + + /* header + gc_fields */ + klispC_link(K, (GCObject *) new_mod, K_TMODULE, 0); + + /* module specific fields */ + new_mod->env = env; + new_mod->exp_list = exp_list; + return gc2mod(new_mod); +} diff --git a/src/kmodule.h b/src/kmodule.h @@ -0,0 +1,20 @@ +/* +** kmodule.h +** Kernel Modules +** See Copyright Notice in klisp.h +*/ + +#ifndef kmodule_h +#define kmodule_h + +#include "kobject.h" +#include "kstate.h" + +/* GC: Assumes env & ext_list are roooted */ +/* ext_list should be immutable */ +TValue kmake_module(klisp_State *K, TValue env, TValue exp_list); + +#define kmodule_env(p_) (tv2mod(p_)->env) +#define kmodule_exp_list(p_) (tv2mod(p_)->exp_list) + +#endif diff --git a/src/kobject.c b/src/kobject.c @@ -81,7 +81,9 @@ char *ktv_names[] = { [K_TERROR] = "error", [K_TBYTEVECTOR] = "bytevector", [K_TFPORT] = "file port", - [K_TMPORT] = "mem port" + [K_TMPORT] = "mem port", + [K_TKEYWORD] = "keyword", + [K_TMODULE] = "module" }; int32_t klispO_log2 (uint32_t x) { diff --git a/src/kobject.h b/src/kobject.h @@ -169,6 +169,7 @@ typedef struct __attribute__ ((__packed__)) GCheader { #define K_TMPORT 43 #define K_TVECTOR 44 #define K_TKEYWORD 45 +#define K_TMODULE 46 /* for tables */ #define K_TDEADKEY 60 @@ -225,6 +226,7 @@ typedef struct __attribute__ ((__packed__)) GCheader { #define K_TAG_MPORT K_MAKE_VTAG(K_TMPORT) #define K_TAG_VECTOR K_MAKE_VTAG(K_TVECTOR) #define K_TAG_KEYWORD K_MAKE_VTAG(K_TKEYWORD) +#define K_TAG_MODULE K_MAKE_VTAG(K_TMODULE) /* ** Macros to test types @@ -325,6 +327,7 @@ typedef struct __attribute__ ((__packed__)) GCheader { t_ == K_TAG_FPORT || t_ == K_TAG_MPORT;}) #define ttisvector(o) (tbasetype_(o) == K_TAG_VECTOR) #define ttiskeyword(o) (tbasetype_(o) == K_TAG_KEYWORD) +#define ttismodule(o) (tbasetype_(o) == K_TAG_MODULE) /* macros to easily check boolean values */ #define kis_true(o_) (tv_equal((o_), KTRUE)) @@ -550,6 +553,12 @@ typedef struct __attribute__ ((__packed__)) { keyword always falling in the same bucket */ } Keyword; +typedef struct __attribute__ ((__packed__)) { + CommonHeader; /* symbols are marked via their strings */ + TValue env; /* this is inherited and a child is returned */ + TValue exp_list; /* this is an immutable list of symbols */ +} Module; + /* ** `module' operation for hashing (size is always a power of 2) */ @@ -613,6 +622,7 @@ union GCObject { MPort mport; Vector vector; Keyword keyw; + Module mod; }; @@ -728,6 +738,7 @@ const TValue kfree; #define gc2bytevector(o_) (gc2tv(K_TAG_BYTEVECTOR, o_)) #define gc2vector(o_) (gc2tv(K_TAG_VECTOR, o_)) #define gc2keyw(o_) (gc2tv(K_TAG_KEYWORD, o_)) +#define gc2mod(o_) (gc2tv(K_TAG_MODULE, o_)) #define gc2deadkey(o_) (gc2tv(K_TAG_DEADKEY, o_)) /* Macro to convert a TValue into a specific heap allocated object */ @@ -750,6 +761,7 @@ const TValue kfree; #define tv2mport(v_) ((MPort *) gcvalue(v_)) #define tv2port(v_) ((Port *) gcvalue(v_)) #define tv2keyw(v_) ((Keyword *) gcvalue(v_)) +#define tv2mod(v_) ((Module *) gcvalue(v_)) #define tv2gch(v_) ((GCheader *) gcvalue(v_)) #define tv2mgch(v_) ((MGCheader *) gcvalue(v_))