211 lines · 2.9 KB · C
RawDownload
#include <u.h>#include <libc.h>#include <luft.h>#include "impl.h" voidlenventer(LEnv *e, char *s, LVal *v){	u32int h;	char *p;	LEnvHash *hsh; 	h = 0; 	for(p = s; *p; p++)		h = h*3 + *p; 	h %= LUFTHASHSZ; 	hsh = mallocz(sizeof(*hsh), 1);	hsh->next = e->hash[h];	e->hash[h] = hsh;	snprint(hsh->name, sizeof(hsh->name), "%s", s);	hsh->val = v;} /* find which env a symbol is in */LEnv*lenvfind(LEnv *e, char *s){	u32int h;	char *p;	LEnvHash *hsh; 	h = 0; 	for(p = s; *p; p++)		h = h*3 + *p; 	h %= LUFTHASHSZ; 	for(hsh = e->hash[h]; hsh != nil; hsh = hsh->next){		/* check symbol exists in this environment */		if(strcmp(s, hsh->name) == 0)			return e;	} 	if(e->parent != nil)		return lenvfind(e->parent, s); 	return nil;} /* return the value of a symbol in an env */LVal*lenvlookup(LEnv *e, char *s){	u32int h;	char *p;	LEnvHash *hsh;	LVal *v; 	h = 0;	v = nil; 	for(p = s; *p; p++)		h = h*3 + *p; 	h %= LUFTHASHSZ; 	for(hsh = e->hash[h]; hsh != nil; hsh = hsh->next){		if(strcmp(s, hsh->name) == 0){			v = hsh->val;			break;		}	} 	return v;} LEnv*lenv(LuftVM *L, LEnv *parent){	LEnv *env; 	env = mallocz(sizeof(*env), 1);	env->gctype = LGCENV;	env->gclink = L->gcl;	L->gcl = env;	env->L = L;	env->parent = parent; 	return env;} LEnv*lenvset(LuftVM *L, LVal *params, LVal *args, LEnv *outer){	int i, min;	LEnv *env;	LVal *param, *arg; 	assert(params != nil && params->type == TLIST);	assert(args != nil && args->type == TLIST); 	min = params->len < args->len ? params->len : args->len; 	env = lenv(L, outer); 	for(i = 0; i < min; i++){		param = params->list[i];		if(param->type != TSYMBOL)			lufterr(L, "parameter %V not a symbol", param);		arg = args->list[i]; 		lenventer(env, param->s, arg);	} 	return env;} static voidlenvdel(LuftVM*, LEnv *env){	int i;	LEnvHash *hsh, *next; 	for(i = 0; i < LUFTHASHSZ; i++){		for(hsh = env->hash[i]; hsh != nil; hsh = next){			next = hsh->next;			free(hsh);		}	} 	free(env);} static void gcmarkval(LVal *v); static voidgcmarkenv(LEnv *env){	int i;	LEnvHash *hsh;	LVal *v; 	if(env->gcmark > 0)		return; 	env->gcmark = 1; 	for(i = 0; i < LUFTHASHSZ; i++){		for(hsh = env->hash[i]; hsh != nil; hsh = hsh->next){			v = hsh->val;			gcmarkval(v);		}	}} static voidgcmarkval(LVal *v){	int i; 	if(v->gcmark > 0)		return; 	v->gcmark = 1; 	switch(v->type){	case TLIST:	case TLAMBDA:		for(i = 0; i < v->len; i++){			gcmarkval(v->list[i]);		}	} 	if(v->env != nil)		gcmarkenv(v->env); 	return;} voidlenvgc(LuftVM *L){	LGc *m, **p, *next; 	for(m = L->gcl; m != nil; m = m->gclink)		m->gcmark = 0; 	gcmarkenv(L->env); 	p = &L->gcl;	for(m = L->gcl; m != nil; m = next){		next = m->gclink;		if(m->gcmark == 0){			*p = next;			switch(m->gctype){			case LGCVAL:				ldel(L, (LVal*)m);				break;			case LGCENV:				lenvdel(L, (LEnv*)m);				break;			}		} else			p = &m->gclink;	}} 

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t