Ejemplo n.º 1
0
inline char* obj2str(struct a2_obj* obj, char* buf, size_t len){
	assert(obj);
	switch(obj_t(obj)){
		case A2_TNUMBER:
			snprintf(buf, len, "%.14g", obj_vNum(obj));
			return buf;
		case A2_TSTRING:
			return a2_gcobj2string(obj_vX(obj, obj));
		case A2_TBOOL:
			snprintf(buf, len, "%s", (obj_vX(obj, uinteger))?("true"):("false"));
			return buf;
		case A2_TNIL:
			return "nil";
		case _A2_TADDR:
			_sf(buf, len, "[%zd]", obj_vX(obj, addr));
			return buf;
		case A2_TCLOSURE:
			snprintf(buf, len, "closure:%p", a2_gcobj2closure(obj_vX(obj, obj)));
			return buf;
		case A2_TARRAY:
			snprintf(buf, len, "array:%p", a2_gcobj2array(obj_vX(obj, obj)));
			return buf;
		case A2_TMAP:
			snprintf(buf, len, "map:%p", a2_gcobj2map(obj_vX(obj, obj)));
			return buf;
		default:
			assert(0);
	}
	return NULL;
}
Ejemplo n.º 2
0
Archivo: a2_vm.c Proyecto: eagles125/A2
// foreachloop
static inline void _vm_foreachloop(struct a2_vm* vm_p){
	struct a2_obj* _k = callinfo_sfreg(curr_ci, ir_ga(curr_ir));
	struct a2_obj* _v = callinfo_sfreg(curr_ci, ir_ga(curr_ir)+1);
	struct a2_obj* _c = callinfo_sfreg(curr_ci, ir_ga(curr_ir)+2);
	struct a2_obj* __v = NULL;

	if(obj_t(_c)!=A2_TMAP && obj_t(_c)!=A2_TARRAY)
		vm_error("the varable is not map or array.");

	// dump next
	switch(obj_t(_c)){
		case A2_TMAP:
			__v = a2_map_next(a2_gcobj2map(obj_vX(_c, obj)), _k);
			if(__v==NULL)
				curr_pc++;
			else{
				*_v = *__v;
				jump(ir_gbx(curr_ir));
			}
			break;
		case A2_TARRAY:
			__v = a2_array_next(a2_gcobj2array(obj_vX(_c, obj)), _k);
			if(__v==NULL)  // dump is end
				curr_pc++;
			else{
				*_v = *__v;
				jump(ir_gbx(curr_ir));
			}
			break;
		default:
			assert(0);
	}
}
Ejemplo n.º 3
0
Archivo: a2_vm.c Proyecto: eagles125/A2
// set value
static inline void _vm_setvalue(struct a2_vm* vm_p){
	struct a2_obj* _c = callinfo_sfreg(curr_ci, ir_ga(curr_ir));
	struct a2_obj* _k = _getvalue(vm_p, ir_gb(curr_ir));
	struct a2_obj* _v = _getvalue(vm_p, ir_gc(curr_ir));
	struct a2_obj* __d = NULL;
	switch(obj_t(_c)){
		case A2_TARRAY:
	 		if(obj_t(_k)!=A2_TNUMBER)
	 			vm_error("the key is must number at set array.");
			__d = a2_array_get(a2_gcobj2array(obj_vX(_c, obj)), _k);
			if(!__d)  goto SVALUE_ERROR;
			*__d = *_v;
			break;
		case A2_TMAP:
			if(obj_t(_k)!=A2_TNUMBER && obj_t(_k)!=A2_TSTRING)
				vm_error("the key is must number or string at set map.");
			__d = a2_map_query(a2_gcobj2map(obj_vX(_c, obj)), _k);
			if(!__d) goto SVALUE_ERROR;
			*__d = *_v;
			break;
		default:
			vm_error("the varable is not map or array.");
	}

 	curr_pc++;
 	return;
 SVALUE_ERROR:
 	vm_error("the key is overfllow.");
}
Ejemplo n.º 4
0
// add array
A2_API inline void a2_addarray(struct a2_state* state){
	int top = a2_top(state)-1;
	struct a2_obj* v = a2_getcstack(state->env_p, top);
	struct a2_obj* array = a2_getcstack(state->env_p, top-1);

	check_array(array);
	a2_array_add(a2_gcobj2array(obj_vX(array, obj)), v);
}
Ejemplo n.º 5
0
Archivo: a2_vm.c Proyecto: eagles125/A2
//set list
static inline void _vm_setlist(struct a2_vm* vm_p){
	int i, end=ir_gb(curr_ir)+ir_gc(curr_ir);
	struct a2_obj* _d = callinfo_sfreg(curr_ci, ir_ga(curr_ir));	
	assert(obj_t(_d)==A2_TARRAY);
	for(i=ir_gb(curr_ir); i<end; i++){
		a2_array_add(a2_gcobj2array(obj_vX(_d, obj)), callinfo_sfreg(curr_ci, i));
	}
	curr_pc++;
}
Ejemplo n.º 6
0
A2_API inline void* a2_topoint(struct a2_state* state, int idx){
	struct a2_obj* obj = a2_getcstack(state->env_p, idx);
	switch(obj_t(obj)){
		case A2_TARRAY:
			return a2_gcobj2array(obj_vX(obj, obj));
		case A2_TMAP:
			return a2_gcobj2map(obj_vX(obj, obj));
		case A2_TCLOSURE:
			return a2_gcobj2closure(obj_vX(obj, obj));
		default:
			return NULL;			
	}
}
Ejemplo n.º 7
0
Archivo: a2_gc.c Proyecto: xebecnan/A2
// mark array
static inline void _gc_mark_array(struct a2_gc* gc_p, struct a2_gcobj* gcobj, enum gc_mark m){
	assert(gcobj->type == A2_TARRAY);
	if(mask(gcobj->mark) == mask(m))
		return;

	gcobj->mark = m;
	struct a2_obj k = a2_nil2obj();
	struct a2_obj* vp = NULL;
	struct a2_array* array_p = a2_gcobj2array(gcobj);

	while(NULL != (vp=a2_array_next(array_p, &k))){
		a2_gc_markit(gc_p, vp, m);
	}
}
Ejemplo n.º 8
0
// get array
A2_API inline void a2_getarray(struct a2_state* state){
	int top = a2_top(state)-1;
	struct a2_obj* k = a2_getcstack(state->env_p, top);
	struct a2_obj* array = a2_getcstack(state->env_p, top-1);
	struct a2_obj* _v = NULL;

	check_array(array);
	check_num(k);
	_v = a2_array_get(a2_gcobj2array(obj_vX(array, obj)), k);
	if(_v==NULL)
		obj_setX(k, A2_TNIL, point, NULL);
	else
		*k = *_v;
}
Ejemplo n.º 9
0
// set array
A2_API inline void a2_setarray(struct a2_state* state){
	int top = a2_top(state)-1;
	struct a2_obj* v = a2_getcstack(state->env_p, top);
	struct a2_obj* k = a2_getcstack(state->env_p, top-1);
	struct a2_obj* array = a2_getcstack(state->env_p, top-2);
	struct a2_obj* _v = NULL;

	check_array(array);
	check_num(k);
	_v  = a2_array_get(a2_gcobj2array(obj_vX(array, obj)), k);
	if(_v==NULL)
		a2_error(state->env_p, e_run_error, 
			"the error index at array.\n");
	else
		*_v = *v;
	a2_topset(state, top-1);
}
Ejemplo n.º 10
0
A2_API inline void a2_len(struct a2_state* state, int idx){
	struct a2_obj* obj = a2_getcstack(state->env_p, idx);
	size_t len =0;
	struct a2_obj len_obj;
	switch(obj_t(obj)){
		case A2_TMAP:
			len = a2_map_len(a2_gcobj2map(obj_vX(obj, obj)));
			break;
		case A2_TARRAY:
			len = a2_array_len(a2_gcobj2array(obj_vX(obj, obj)));
			break;
		case A2_TSTRING:
			len = a2_string_len(a2_gcobj2string(obj_vX(obj, obj)));
			break;
		default:
			a2_error(state->env_p, e_run_error,
			 "the type is not map or array at len function.");
	}
	len_obj = a2_number2obj((a2_number)len);
	a2_pushstack(state->env_p, &len_obj);
}