示例#1
0
文件: ops.c 项目: xupingmao-old/venus
tm_obj tm_get(tm_obj self, tm_obj k){
	tm_obj v;
	switch( self.type){
		case TM_STR:{
			if( k.type == TM_NUM ){
				int n = get_num(k);		
				if (n < 0) n += get_str_len(self);	
				if( n >= get_str_len(self) || n < 0)
					tm_raise("tm_get: index overflow");
				//tm_printf("str = @, len = @, index = @\n", self,number_new(get_str_len(self)),  k);
				unsigned char c = get_str(self)[n];
                //printf("c = %d\n", c);
				return __chars__[c];
			}else if( dict_iget( get_dict(str_class), k , &v) ){
				return method_new(v, self);
			}
		}
		case TM_LST: {
			if( k.type == TM_NUM ){
				return list_get( self.value.list, get_num(k));
			}else if( dict_iget(get_dict(list_class), k, &v) ){
				return method_new( v, self);
			}
			break;
		}
		case TM_DCT:
			if( dict_iget( get_dict(self), k, &v)){
				return v;
			}else if(dict_iget(get_dict(dict_class), k, &v)){
				return method_new( v, self);
			}
			break;
		case TM_FNC:
            /*
			if( k.type == TM_STR && strequals(get_str(k), "code")){
				return get_func( self )->code;
			}*/
            break;
	}
    // tm_printf_only_type("@", self);
	// cprintln(self);
	tm_raise("tm_get: keyError @, self = @ ", _tm_type(k), _tm_type(self) );
	return obj_none;
}
示例#2
0
文件: ops.c 项目: xupingmao-old/venus
int _tm_has( tm_obj a, tm_obj b ){
	switch( a.type ){
		case TM_LST:{
			return ( list_index( get_list(a), b) != -1 );
		}
		case TM_STR:{
			if( b.type != TM_STR) return 0;
			return _str_find( a.value.str, b.value.str, 0) != -1;
		}
		case TM_DCT:{
			tm_obj v;
			return ( dict_iget(get_dict(a), b, &v) );
		}
	}
	return 0;
}
示例#3
0
文件: dict.c 项目: ktosiu/C-Snip
/* Get val by NULL-terminated key from dict, NULL on not found. */
void *
dict_get(struct dict *dict, char *key)
{
    return dict_iget(dict, key, strlen(key));
}