示例#1
0
文件: int.c 项目: rendau/rose
obj_t
int_new_wc(obj_t obj) {
  ASSERT(!obj || (obj->type != OBJ_INT), "bad object");
  return OBJ(int_new(INT(obj)->v));
 error:
  return NULL;
}
示例#2
0
文件: obj.c 项目: Nonpython/projects
PmReturn_t
obj_loadFromImg(PmMemSpace_t memspace,
                uint8_t const **paddr, pPmObj_t *r_pobj)
{
    PmReturn_t retval = PM_RET_OK;
    PmObj_t obj;


    /* Get the object descriptor */
    obj.od = (PmObjDesc_t)0x0000;
    OBJ_SET_TYPE(&obj, mem_getByte(memspace, paddr));

    switch (OBJ_GET_TYPE(&obj))
    {
        case OBJ_TYPE_NON:
            /* If it's the None object, return global None */
            *r_pobj = PM_NONE;
            break;

        case OBJ_TYPE_INT:
            /* Read an integer and create an integer object with the value */
            retval = int_new(mem_getInt(memspace, paddr), r_pobj);
            break;

#ifdef HAVE_FLOAT
        case OBJ_TYPE_FLT:
            /* Read a float and create an float object with the value */
            retval = float_new(mem_getFloat(memspace, paddr), r_pobj);
            break;
#endif /* HAVE_FLOAT */

        case OBJ_TYPE_STR:
            retval = string_loadFromImg(memspace, paddr, r_pobj);
            break;

        case OBJ_TYPE_TUP:
            retval = tuple_loadFromImg(memspace, paddr, r_pobj);
            break;

        case OBJ_TYPE_NIM:
            /* If it's a native code img, load into a code obj */
            retval = no_loadFromImg(memspace, paddr, r_pobj);
            break;

        case OBJ_TYPE_CIM:
            /* If it's a code img, load into a code obj */
            retval = co_loadFromImg(memspace, paddr, r_pobj);
            break;

        default:
            /* All other types should not be in an img obj */
            PM_RAISE(retval, PM_RET_EX_SYS);
            break;
    }
    return retval;
}
示例#3
0
/*----------------------------------------------------------------------------*/
PmReturn_t
tres_pm_get_state(pPmFrame_t *ppframe)
{
  PmReturn_t retv = PM_RET_OK;
  pPmObj_t pobj;
  pPmObj_t pobj_new;
  pPmInstance_t pcli;
  pPmDict_t pdict;
  int16_t index;
  float fval;
  int32_t ival;

  if(NATIVE_GET_NUM_ARGS() != 1) {
    PM_RAISE(retv, PM_RET_EX_TYPE);
    return retv;
  }
  pobj = NATIVE_GET_LOCAL(0);
  if(OBJ_GET_TYPE(pobj) != OBJ_TYPE_CLI) {
    PM_RAISE(retv, PM_RET_EX_TYPE);
    return retv;
  }
  pcli = (pPmInstance_t)pobj;
  pdict = pcli->cli_attrs;

  if(*tres_pm_io.state_len > 0) {
    // restore each attribute of the object
    for(index = pdict->length - 1; index >= 0; index--) {
      seglist_getItem(pdict->d_keys, index, &pobj);
      retv = seglist_getItem(pdict->d_vals, index, &pobj);
      PM_RETURN_IF_ERROR(retv);
      switch (OBJ_GET_TYPE(pobj)) {
      case OBJ_TYPE_INT:
        //pop int
        pop_int(&ival);
        retv = int_new(ival, &pobj_new);
        break;
      case OBJ_TYPE_FLT:
        //pop float
        pop_float(&fval);
        retv = float_new(fval, &pobj_new);
        break;
      default:
        /* Raise TypeError */
        PM_RAISE(retv, PM_RET_EX_TYPE);
      }
      if (retv == PM_RET_OK) {
        seglist_setItem(pdict->d_vals, pobj_new, index);
      }
    }
  }
  NATIVE_SET_TOS((pPmObj_t)pcli);
  return retv;
}
示例#4
0
/**
 * Unit Test ported from SNARF
 * This tests if it correctly creates the dictionary object
 * This also tests if dict_setItem correctly replaces the existing value
 * if the new val has the same key as the exisiting one.
 */
void
ut_dict_getItem_001(CuTest *tc)
{
    uint8_t heap[HEAP_SIZE];
    p_test_str1 = test_str1;
    p_test_str2 = test_str2;
    p_test_str3 = test_str3;
    p_test_strnew = test_strnew;
    PmReturn_t retval;
    
    retval = pm_init(heap, HEAP_SIZE, MEMSPACE_RAM, C_NULL);
    
    retval = string_new((uint8_t const **)&test_str1, &p_firstval);
    retval = string_new((uint8_t const **)&test_str2, &p_secondval);
    retval = string_new((uint8_t const **)&test_str3, &p_thirdval);
    retval = string_new((uint8_t const **)&test_strnew, &p_newval);
    
    retval = int_new(1, (pPmObj_t *)&p_firstkey);
    retval = int_new(2, (pPmObj_t *)&p_secondkey);
    retval = int_new(3, (pPmObj_t *)&p_thirdkey);
    retval = int_new(2, (pPmObj_t *)&p_newkey);
    
    dict_new(&p_dict);
    
    dict_setItem(p_dict, (pPmObj_t)p_firstkey, p_firstval);
    dict_setItem(p_dict, (pPmObj_t)p_secondkey, p_secondval);
    dict_setItem(p_dict, (pPmObj_t)p_thirdkey, p_thirdval);
    dict_setItem(p_dict, (pPmObj_t)p_newkey, p_newval);
    dict_setItem(p_dict, (pPmObj_t)p_secondkey, p_secondval);
    
    dict_getItem(p_dict, (pPmObj_t)p_firstkey, &p_tempobj);
    CuAssertTrue(tc, obj_compare(p_tempobj, p_firstval) == C_SAME);
    
    dict_getItem(p_dict, (pPmObj_t)p_secondkey, &p_tempobj);
    CuAssertTrue(tc, obj_compare(p_tempobj, p_secondval) == C_SAME);
    
    dict_getItem(p_dict, (pPmObj_t)p_thirdkey, &p_tempobj);
    CuAssertTrue(tc, obj_compare(p_tempobj, p_thirdval) == C_SAME);
}
示例#5
0
PmReturn_t
nat_t002_push42(pPmFrame_t *ppframe)
{

    pPmObj_t pint = C_NULL;
    PmReturn_t retval;

    retval = int_new((int32_t)42, &pint);
    NATIVE_SET_TOS(pint);

    return retval;
    
}
示例#6
0
// FIXME: this function will be be removed when JSON will be used 
PmReturn_t
tres_pm_get_int_input(pPmFrame_t *ppframe)
{
  PmReturn_t retv = PM_RET_OK;
  pPmObj_t r_pint;
  int32_t val;

  val = (int32_t)strtol(tres_pm_io.in, NULL, 10);
  retv = int_new(val, &r_pint);
  NATIVE_SET_TOS(r_pint);

  return retv;
}
示例#7
0
/*----------------------------------------------------------------------------*/
PmReturn_t
tres_pm_state_pop(pPmFrame_t *ppframe)
{
  PmReturn_t retv = PM_RET_OK;
  pPmObj_t r_pflt;
  pPmObj_t pa;
  float fval;
  int32_t ival;
  int pop_retv;

  /* Raise TypeError if wrong number of args */
  pa = NATIVE_GET_LOCAL(0);
  if(NATIVE_GET_NUM_ARGS() != 1) {
    PM_RAISE(retv, PM_RET_EX_TYPE);
    return retv;
  }
  switch (OBJ_GET_TYPE(pa)) {
    //case OBJ_TYPE_STR:
    //  ptr = (char const *)&(((pPmString_t)pa)->val);
    //  // TODO: unimplemented
    //  break;
  case OBJ_TYPE_INT:
    pop_retv = pop_int(&ival);
    if(pop_retv != TRES_ERR_NONE) {
      ival = ((pPmInt_t) pa)->val;
    }
    retv = int_new(ival, &r_pflt);
    break;
  case OBJ_TYPE_FLT:
    pop_retv = pop_float(&fval);
    if(pop_retv != TRES_ERR_NONE) {
      fval = ((pPmFloat_t) pa)->val;
    }
    retv = float_new(fval, &r_pflt);
    break;
  default:
    /* Raise TypeError */
    PM_RAISE(retv, PM_RET_EX_TYPE);
  }
  NATIVE_SET_TOS(r_pflt);

  return retv;
}
示例#8
0
文件: lab2.c 项目: gaibo/C
/* note: argc, argv not used, but included per convention */
int main(int argc, char *argv[])
{
  printf("/*** fib ***/\n");
  unsigned int f0 = fib(0);
  unsigned int f1 = fib(1);
  unsigned int f2 = fib(2);
  printf("fib(0)\t%d\n",f0);
  printf("fib(1)\t%d\n",f1);
  printf("fib(2)\t%d\n",f2);

  printf("/*** fact ***/\n");
  unsigned int t6 = fact(6);
  unsigned int t7 = fact(7);
  unsigned int t8 = fact(8);
  printf("fact(6)\t%9d\n",t6);
  printf("fact(7)\t%9d\n",t7);
  printf("fact(8)\t%9d\n",t8);

  printf("/*** int_new ***/\n");
  int* h = int_new(99);
  printf("h\t%p\n",h);
  printf("h+1\t%p\n",h+1);
  printf("*h\t%d\n",*h);
  free(h);

  printf("/*** upto ***/\n");
  unsigned int* arr10 = upto(10);
  int i;
  for (i=0; i<=10; i++)
    printf("arr10[%d]\t%2d\n",i,arr10[i]);
  free(arr10);

  printf("/*** num_evens ***/\n");
  unsigned int e = num_evens(upto(10),11);
  printf("num_evens(upto(10),11)\t%d\n",e);
  
  return 0;
}
示例#9
0
			"int() can't convert non-string with explicit base");
	return NULL;
}

/* Wimpy, slow approach to tp_new calls for subtypes of int:
   first create a regular int from whatever arguments we got,
   then allocate a subtype instance and initialize its ob_ival
   from the regular int.  The regular int is then thrown away.
*/
static PyObject *
int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
	PyObject *tmp, *new;

	assert(PyType_IsSubtype(type, &PyInt_Type));
	tmp = int_new(&PyInt_Type, args, kwds);
	if (tmp == NULL)
		return NULL;
	assert(PyInt_Check(tmp));
	new = type->tp_alloc(type, 0);
	if (new == NULL)
		return NULL;
	((PyIntObject *)new)->ob_ival = ((PyIntObject *)tmp)->ob_ival;
	Py_DECREF(tmp);
	return new;
}

static char int_doc[] =
"int(x[, base]) -> integer\n\
\n\
Convert a string or number to an integer, if possible.  A floating point\n\