/** * Finds the minimum value in a list. * * @param args list of args * @param c number of args * @returns min number in list */ LuciObject *luci_min(LuciObject **args, unsigned int c) { if (c < 1) { LUCI_DIE("%s", "Missing parameter to min()\n"); } LuciObject *list = args[0]; if (!list || (!ISTYPE(list, obj_list_t))) { LUCI_DIE("%s", "Must specify a list to calculate min\n"); } LuciObject *item; double min = 0; unsigned int i, found_float = 0; for (i = 0; i < AS_LIST(list)->count; i ++) { item = AS_LIST(list)->items[i]; if (!item) { LUCI_DIE("%s", "Can't calulate max of list containing NULL value\n"); } if (ISTYPE(item, obj_int_t)) { if (i == 0) { min = (double)AS_INT(item)->i; } else if ( (double)AS_INT(item)->i < min) { min = (double)AS_INT(item)->i; } } else if (ISTYPE(item, obj_float_t)) { found_float = 1; if (i == 0) { min = AS_FLOAT(item)->f; } else if (AS_FLOAT(item)->f < min) { min = AS_FLOAT(item)->f; } } else { LUCI_DIE("Can't find min of list containing an object of type %s\n", item->type->type_name); } } LuciObject *ret; if (!found_float) { ret = LuciInt_new((long)min); } else { ret = LuciFloat_new(min); } return ret; }
bool condition_op_const::eval(record_type& rt, record& r){ if (offset==-1){ offset = offset_of_column(rt, lhs_table_name, lhs_column_name); if (offset==-1) throw string("Undefined column"); } if (index==-1){ index = index_of_column(rt, lhs_table_name, lhs_column_name); if (index==-1) throw string("Undefined column"); } if (rt[index].type == column_type::INT){ if (op=="<"){ return AS_INT(r[offset]) < value.vInt; } else if (op=="="){ return AS_INT(r[offset]) == value.vInt; } else if (op==">"){ return AS_INT(r[offset]) > value.vInt; } throw string("unimplemented operators in condition_op_const"); } else if(rt[index].type == column_type::FLOAT){ if (op=="<"){ return AS_FLOAT(r[offset]) < value.vFloat; } else if (op=="="){ return AS_FLOAT(r[offset]) == value.vFloat; } else if (op==">"){ return AS_FLOAT(r[offset]) > value.vFloat; } throw string("unimplemented operators in condition_op_const"); } else if(rt[index].type == column_type::STRING){ if (op=="<"){ return (strncmp(AS_STRING(r[offset]), value.vString, 500) < 0); } else if (op=="="){ return (strncmp(AS_STRING(r[offset]), value.vString, 500) == 0); } else if (op==">"){ return (strncmp(AS_STRING(r[offset]), value.vString, 500) > 0); } throw string("unimplemented operators in condition_op_const"); } //throw string("unimplemented datatypes in condition_op_const"); }
/** * Casts a LuciObject to a LuciFloatObj if possible, then returns * the new object. * * @param args list of args * @param c number of args * @returns LuciFloatObj cast of the first arg */ LuciObject *luci_cast_float(LuciObject **args, unsigned int c) { LuciObject *ret = LuciNilObj; if (c < 1) { LUCI_DIE("%s", "Missing parameter to int()\n"); } LuciObject *item = args[0]; if (!item) { LUCI_DIE("%s", "Can't cast NULL to int\n"); } if (ISTYPE(item, obj_int_t)) { ret = LuciFloat_new((double)AS_INT(item)->i); } else if (ISTYPE(item, obj_float_t)) { ret = LuciFloat_new(AS_FLOAT(item)->f); } else if (ISTYPE(item, obj_string_t)) { double f; int scanned = sscanf(AS_STRING(item)->s, "%f", (float *)&f); if (scanned <= 0 || scanned == EOF) { LUCI_DIE("%s", "Could not cast to float\n"); } ret = LuciFloat_new(f); } else { LUCI_DIE("Cannot cast type %s to type float\n", item->type->type_name); } return ret; }
/** * Asserts that a given LuciObject is equivalent to a boolean True * * Currently uses C @code assert @endcode , which will exit a program * mid-execution if the assertion fails. * * @param args list of args * @param c number of args * @returns LuciNilObj */ LuciObject *luci_assert(LuciObject **args, unsigned int c) { if (c < 1) { LUCI_DIE("%s", "Missing condition parameter to assert()\n"); } LuciObject *item = args[0]; if (ISTYPE(item, obj_int_t) && !AS_INT(item)->i) { LUCI_DIE("%s\n", "Assertion failed"); } else if (ISTYPE(item, obj_float_t) && !((long)AS_FLOAT(item)->f)) { LUCI_DIE("%s\n", "Float assertion failed"); } else if (ISTYPE(item, obj_string_t)) { if (strcmp("", AS_STRING(item)->s) == 0) { LUCI_DIE("%s\n", "String assertion failed"); } } else if (ISTYPE(item, obj_list_t) && (AS_LIST(item)->count == 0)) { LUCI_DIE("%s\n", "List assertion failed"); } else if (ISTYPE(item, obj_map_t) && (AS_MAP(item)->count == 0)) { LUCI_DIE("%s\n", "Map assertion failed"); } else if (ISTYPE(item, obj_file_t) && (AS_FILE(item)->ptr)) { LUCI_DIE("%s\n", "File assertion failed"); } return LuciNilObj; }
constant_t node_add_eval(node_t*n, environment_t* env) { double sum = 0; int t; for(t=0;t<n->num_children;t++) { sum += AS_FLOAT(EVAL_CHILD(t)); } return float_constant(sum); }
bool ffi_pointer_write(VMState *state, Object *type, void *ptr, Value val) { ValueCache *vcache = &state->shared->vcache; Object *string_base = vcache->string_base; FFIObject *ffi = (FFIObject*) vcache->ffi_obj; if (type == ffi->float_obj) { if (IS_FLOAT(val)) *(float*) ptr = AS_FLOAT(val); else if (IS_INT(val)) *(float*) ptr = AS_INT(val); else { VM_ASSERT(false, "invalid value for float type") false; } return true; } else { Object *c_type_obj = AS_OBJ(OBJECT_LOOKUP(type, c_type)); StringObject *c_type = (StringObject*) obj_instance_of(c_type_obj, string_base); assert(c_type); VM_ASSERT(false, "unhandled pointer write type: %s", c_type->value) false; } }
/** * Computes the sum of a range of numbers. * * @param args list of args * @param c number of args * @returns sum of numbers */ LuciObject * luci_sum(LuciObject **args, unsigned int c) { if (c < 1) { LUCI_DIE("%s", "Missing parameter to sum()\n"); } LuciObject *list = args[0]; if (!list || (!ISTYPE(list, obj_list_t))) { LUCI_DIE("%s", "Must specify a list to calculate sum\n"); } LuciObject *item; double sum = 0; unsigned int i, found_float = 0; for (i = 0; i < AS_LIST(list)->count; i++) { item = AS_LIST(list)->items[i]; if (!item) { LUCI_DIE("%s", "Can't calulate sum of list containing NULL value\n"); } if (ISTYPE(item, obj_int_t)) { sum += (double)AS_INT(item)->i; } else if (ISTYPE(item, obj_float_t)) { found_float = 1; sum += AS_FLOAT(item)->f; } else { LUCI_DIE("%s", "Can't calculate sum of list containing non-numeric value\n"); } } LuciObject *ret; if (!found_float) { ret = LuciInt_new((long)sum); } else { ret = LuciFloat_new(sum); } return ret; }
constant_t node_sub_eval(node_t*n, environment_t* env) { constant_t left = EVAL_CHILD(0); constant_t right = EVAL_CHILD(1); return float_constant(AS_FLOAT(left) - AS_FLOAT(right)); }