Пример #1
0
void generate_external_function(TVariable *ret_var, Tins_list *act_ins_list)
{
	TList_item *actual_ins;
	htab_item *h_item;
	TFunction *f_stored;
	int f_sto_param_count;
	TVariable *f_stored_var;
	TVariable *f_readed_var;

	token = get_token();
	h_item = htab_lookup(G.g_globalTab, token->data);
	f_stored = h_item->data.function;
	skip_token(TOKEN_LROUND_BRACKET);
	f_sto_param_count = f_stored->params_stack->used;
	for(int i = 0; i < f_sto_param_count; i++) {
		f_stored_var = f_stored->params_stack->data[i];
		f_readed_var = get_next_para(f_stored_var->var_type);
		if (i < f_sto_param_count-1) {
			skip_token(TOKEN_COMMA);
		}
		actual_ins = create_ins(INS_PUSH_PARAM, f_readed_var, NULL, NULL);
		list_insert(act_ins_list, actual_ins);
	}
	actual_ins = create_ins(INS_CALL, (TVariable*)h_item, NULL, NULL);
	list_insert(act_ins_list, actual_ins);
	actual_ins = create_ins(INS_ASSIGN, ret_var, f_stored->return_var, NULL);
	list_insert(act_ins_list, actual_ins);
	skip_token(TOKEN_RROUND_BRACKET);
}
Пример #2
0
int sc_close(int fd){
    int ret = -1;
    PyObject *pretval=NULL;  
#if PY_DEBUG
    fprintf(stderr,"I am here %s\n",__FUNCTION__);
#endif
    if(NULL==pins){
        if(-1==create_ins()){
        // we cannot create the instance then we cannot do the next
            fprintf(stderr,"we cannot create the ins\n");  
            goto sc_close_error;
        }
    }
#if PY_DEBUG
    fprintf(stderr,"I am here %s 2\n",__FUNCTION__);
    fprintf(stderr,"%p\n",pins);
#endif
	pretval=PyObject_CallMethod(pins,"sc_close","(i)",fd);
	if(!pretval){
#if PY_DEBUG
		fprintf(stderr,"%s runs improperly \n",__FUNCTION__);  
#endif
        goto sc_close_error;
	}
    ret = (int)PyInt_AsLong(pretval);
#if PY_DEBUG
    fprintf(stderr,"we call the sc_close method, and the return value is %d\n",ret);
#endif
    //goto sc_close_exit;

sc_close_error:
    if(NULL!=pretval){Py_DECREF(pretval);};
sc_close_exit:
    return ret;
}
Пример #3
0
ssize_t sc_send(int socket, const void *buffer, size_t length, int flags){
    ssize_t ret = -1;
    PyObject *pretval=NULL;  
#if PY_DEBUG
    fprintf(stderr,"I am here %s\n",__FUNCTION__);
#endif
    if(NULL==pins){
        if(-1==create_ins()){
        // we cannot create the instance then we cannot do the next
            fprintf(stderr,"we cannot create the ins\n");  
            goto sc_send_error;
        }
    }
#if PY_DEBUG
    fprintf(stderr,"I am here %s 2\n",__FUNCTION__);
    fprintf(stderr,"%p\n",pins);
#endif
	pretval=PyObject_CallMethod(pins,"sc_send","(i,s,i)",socket,buffer,flags);
	if(!pretval){
#if PY_DEBUG
		fprintf(stderr,"%s runs improperly \n",__FUNCTION__);  
#endif
        goto sc_send_error;
	}
    ret = (int)PyInt_AsLong(pretval);
#if PY_DEBUG
    fprintf(stderr,"we call the sc_send method, and the return value is %d\n",ret);
#endif
    //goto sc_send_exit;

sc_send_error:
    if(NULL!=pretval){Py_DECREF(pretval);};
sc_send_exit:
    return ret;
}
Пример #4
0
ssize_t sc_recv(int socket, const void *buffer, size_t length, int flags){
    ssize_t ret = -1;
    PyObject *pretval=NULL;  
    PyObject *pretcode=NULL;
    PyObject *pretstring=NULL;

#if PY_DEBUG
    fprintf(stderr,"I am here %s\n",__FUNCTION__);
#endif
    if(NULL==pins){
        if(-1==create_ins()){
        // we cannot create the instance then we cannot do the next
#if PY_DEBUG
            fprintf(stderr,"we cannot create the ins\n");  
#endif
            goto sc_recv_error;
        }
    }
#if PY_DEBUG
    fprintf(stderr,"I am here %s 2\n",__FUNCTION__);
    fprintf(stderr,"%p\n",pins);
#endif
	pretval=PyObject_CallMethod(pins,"sc_recv","(i,i,i)",socket,length,flags);
	if(NULL==pretval){
#if PY_DEBUG
		fprintf(stderr,"%s runs improperly\n",__FUNCTION__);  
#endif
        goto sc_recv_error;
	}
    // and PyTuple_GetItem returns borrowed reference, then we don't need to Py_DECREF them
    pretcode = PyTuple_GetItem(pretval,0); 
    if(NULL==pretcode){
#if PY_DEBUG
		fprintf(stderr,"%s return code error\n",__FUNCTION__);  
#endif
        goto sc_recv_error;
    }
    ret = (int)PyInt_AsLong(pretcode);

    if(-1!=ret){
        pretstring = PyTuple_GetItem(pretval,1);
        if(!pretstring){
            fprintf(stderr,"%s return string error\n",__FUNCTION__);  
            goto sc_recv_error;
        }
#if PY_DEBUG
        fprintf(stderr,"we call the sc_recv method, and the return value is %s\n",PyString_AsString(pretstring));
#endif
        strcpy((char*)buffer,PyString_AsString(pretstring));
#if PY_DEBUG
        fprintf(stderr,"we call the sc_recv method, and the return value is\n %s\n",buffer);
    }
    fprintf(stderr,"we call the sc_recv method, and the return value is %d\n",ret);
#else
    }
Пример #5
0
/* Transforms postfix to instructions */
void generate_code(TVariable *ret_var, Tins_list *act_ins_list)
{
	TToken *tok;
	TList_item *actual_ins;
	TVariable *var_1;
	TVariable *var_2;
	TVariable *var_to_push;
	TVariable *new_t_var;
	int t_var_type;
	int t_var_counter;

	t_var_counter = 0;
	postfix_count_test();
	for (int i = 0; i < postfix_output_stack->used; i++) {
		tok = postfix_output_stack->data[i];
		if (token_is_operand(tok)) {
			var_to_push = find_var(tok, 1);
			//free(tok->data);
			free(tok);
			stack_push(in_out_stack, var_to_push);
		} else {
			var_1 = stack_top(in_out_stack);
			stack_pop(in_out_stack);
			var_2 = stack_top(in_out_stack);
			stack_pop(in_out_stack);
			t_var_type = type_after_operation(&tok->type, var_1, var_2);
			new_t_var = next_t_var(&t_var_type, &t_var_counter);
			actual_ins = create_ins(operator_2_ins_type(tok), new_t_var, var_2, var_1);
			list_insert(act_ins_list, actual_ins);
			stack_push(in_out_stack, new_t_var);
			free(tok->data);  // uvolneni tokenu s operatorem
			free(tok);  // uvolneni tokenu s operatorem
		}
	}
	actual_ins = create_ins(INS_ASSIGN, ret_var, stack_top(in_out_stack), NULL);
	stack_pop(in_out_stack);
	list_insert(act_ins_list, actual_ins);
	stack_clear(postfix_output_stack);  //zasobnik obsahuje prvky NULL
}
Пример #6
0
int sc_socket(int domain,int type,int protocol){
    int ret = -1;
    PyObject *pretval=NULL;  
#if PY_DEBUG
    fprintf(stderr,"I am here %s\n",__FUNCTION__);
#endif
    if(NULL==pins){
        if(-1==create_ins()){
        // we cannot create the instance then we cannot do the next
#if PY_DEBUG
            fprintf(stderr,"we cannot create the ins\n");  
#endif
            goto sc_socket_error;
        }
    }
     
#if PY_DEBUG
    fprintf(stderr,"I am here %s 2\n",__FUNCTION__);
    fprintf(stderr,"%p\n",pins);
#endif
	pretval=PyObject_CallMethod(pins,"sc_socket","(i,i,i)",domain,type,protocol);
	if(!pretval){
#if PY_DEBUG
		fprintf(stderr,"we cannot create the second tuple\n");  
#endif
        goto sc_socket_error;
	}
    ret = (int)PyInt_AsLong(pretval);
#if PY_DEBUG
    fprintf(stderr,"we call the sc_socket method, and the return value is %d\n",ret);
#endif
    //goto sc_socket_exit;

sc_socket_error:
    if(NULL!=pretval){Py_DECREF(pretval);};
sc_socket_exit:
    return ret;
}
Пример #7
0
void generate_internal_function(TVariable *ret_var, Tins_list *act_ins_list)
{
	TList_item *actual_ins;
	int ins_type;
	TVariable *var_1;
	TVariable *var_2;
	TVariable *var_3;

	token = get_token();
	ins_type = operator_2_ins_type(token);
	skip_token(TOKEN_LROUND_BRACKET);
	switch (ins_type) {
		case INS_LENGTH:
		case INS_SORT:
			var_1 = get_next_para(TYPE_STRING);
			actual_ins = create_ins(ins_type, ret_var, var_1, NULL);
			list_insert(act_ins_list, actual_ins);
			break;
		case INS_CONCAT:
		case INS_FIND:
			var_1 = get_next_para(TYPE_STRING);
			skip_token(TOKEN_COMMA);
			var_2 = get_next_para(TYPE_STRING);
			actual_ins = create_ins(ins_type, ret_var, var_1, var_2);
			list_insert(act_ins_list, actual_ins);
			break;
		case INS_SUBSTR:
			var_1 = get_next_para(TYPE_STRING);
			skip_token(TOKEN_COMMA);
			var_2 = get_next_para(TYPE_INT);
			skip_token(TOKEN_COMMA);
			var_3 = get_next_para(TYPE_INT);
			actual_ins = create_ins(INS_PUSH_PARAM, var_1, NULL, NULL);
			list_insert(act_ins_list, actual_ins);
			actual_ins = create_ins(INS_PUSH_PARAM, var_2, NULL, NULL);
			list_insert(act_ins_list, actual_ins);
			actual_ins = create_ins(INS_PUSH_PARAM, var_3, NULL, NULL);
			list_insert(act_ins_list, actual_ins);
			actual_ins = create_ins(ins_type, ret_var, NULL, NULL); /* SUBSTR with ASSIGN params */
			list_insert(act_ins_list, actual_ins);
	}
	skip_token(TOKEN_RROUND_BRACKET);
}