// (expt x y) Cell* op_expt(Scheme *sc) { Cell* x = first(sc->args); Cell* y = second(sc->args); double result; int real_result = TRUE; if (x->_num.isFix && y->_num.isFix) real_result = FALSE; /* This 'if' is an R5RS compatibility fix. */ /* NOTE: Remove this 'if' fix for R6RS. */ if (double_value(x) == 0 && double_value(y) < 0) { result = 0.0; } else { result = pow(double_value(x), double_value(y)); } /* Before returning integer result make sure we can. */ /* If the test fails, result is too big for integer. */ if (!real_result) { long result_as_long = (long) result; //如果result有小数位,必然导致result_as_long和result不相等 if (result != (double) result_as_long) real_result = TRUE; } if (real_result) { return s_return_helper(sc, make_real(sc, result)); } else { return s_return_helper(sc, make_integer(sc, (long) result)); } }
// (atan x) // (atan x y) Cell* op_atan(Scheme *sc) { Cell* x = first(sc->args); if (rest(sc->args) == &g_nil) { return s_return_helper(sc, make_real(sc, atan(double_value(x)))); } else { Cell* y = second(sc->args); return s_return_helper(sc, make_real(sc, atan2(double_value(x), double_value(y)))); } }
// (round x) Cell* op_round(Scheme *sc) { Cell* num = first(sc->args); if (num->_num.isFix) return s_return_helper(sc, num); return s_return_helper(sc, make_real(sc, round_per_r5rs(double_value(num)))); }
/* :nodoc: */ static mrb_value Cache_init(mrb_state *mrb, mrb_value self) { mrb_value o; mrb_get_args(mrb, "o", &o); lmc_check_dict(mrb, o); lmc_error_t e; rb_lmc_handle_t *h; local_memcache_t *l = local_memcache_create(rstring_ptr_null(mrb_hash_get(mrb, o, lmc_rb_sym_namespace(mrb))), rstring_ptr_null(mrb_hash_get(mrb, o, lmc_rb_sym_filename(mrb))), double_value(mrb, mrb_hash_get(mrb, o, lmc_rb_sym_size_mb(mrb))), size_t_value(mrb_hash_get(mrb, o, lmc_rb_sym_min_alloc_size(mrb))), &e); if (!l) rb_lmc_raise_exception(mrb, &e); h = (rb_lmc_handle_t *)DATA_PTR(self); if (h) { mrb_free(mrb, h); } DATA_TYPE(self) = &lmc_cache_type; DATA_PTR(self) = NULL; h = (rb_lmc_handle_t *)mrb_malloc(mrb, sizeof(rb_lmc_handle_t)); if (!h) { mrb_raise(mrb, E_RUNTIME_ERROR, "memory allocation error"); } h->lmc = l; h->open = 1; DATA_PTR(self) = h; return self; }
// (truncate x) Cell* op_truncate(Scheme *sc) { Cell* num = first(sc->args); double doubleValue = double_value(num); if (doubleValue > 0) { return s_return_helper(sc, make_real(sc, floor(doubleValue))); } else { return s_return_helper(sc, make_real(sc, ceil(doubleValue))); } }
int main(void) { int arr[SIZE1][SIZE2] = {{1,1,1,1,1}, {2,2,2,2,2}, {3,3,3,3,3}}; show_arr(arr,SIZE1); double_value(arr,SIZE1); show_arr(arr,SIZE1); return 0; }
/* :nodoc: */ static VALUE LocalMemCache__new2(VALUE klass, VALUE o) { lmc_check_dict(o); lmc_error_t e; local_memcache_t *l = local_memcache_create( rstring_ptr_null(rb_hash_aref(o, lmc_rb_sym_namespace)), rstring_ptr_null(rb_hash_aref(o, lmc_rb_sym_filename)), double_value(rb_hash_aref(o, lmc_rb_sym_size_mb)), long_value(rb_hash_aref(o, lmc_rb_sym_min_alloc_size)), &e); if (!l) rb_lmc_raise_exception(&e); rb_lmc_handle_t *h = calloc(1, sizeof(rb_lmc_handle_t)); if (!h) rb_raise(rb_eRuntimeError, "memory allocation error"); h->lmc = l; h->open = 1; return Data_Wrap_Struct(klass, NULL, rb_lmc_free_handle, h); }
// (/ x y ...) Cell* op_div(Scheme *sc) { Num value; Cell* x; if (cdr(sc->args) == &g_nil) { x = sc->args; value = g_one; } else { x = cdr(sc->args); value = num_value(car(sc->args)); } for (; x != &g_nil; x = cdr(x)) { if (!is_zero_double(double_value(car(x)))) value = num_div(value, num_value(car(x))); else { return error_helper(sc, "/: division by zero", NULL); } } return s_return_helper(sc, make_number(sc, value)); }
TEST(EventToStringTest, ScalarType) { IntValue int_value(-42); std::string int_str; EXPECT_TRUE(ToString(&int_value, &int_str)); EXPECT_STREQ("-42", int_str.c_str()); UIntValue uint_value(42); std::string uint_str; EXPECT_TRUE(ToString(&uint_value, &uint_str)); EXPECT_STREQ("42", uint_str.c_str()); LongValue long_value(-42); std::string long_str; EXPECT_TRUE(ToString(&long_value, &long_str)); EXPECT_STREQ("-42", long_str.c_str()); ULongValue ulong_value(42); std::string ulong_str; EXPECT_TRUE(ToString(&ulong_value, &ulong_str)); EXPECT_STREQ("42", ulong_str.c_str()); FloatValue float_value(.5); std::string float_str; EXPECT_TRUE(ToString(&float_value, &float_str)); EXPECT_STREQ("0.5", float_str.c_str()); DoubleValue double_value(.25); std::string double_str; EXPECT_TRUE(ToString(&double_value, &double_str)); EXPECT_STREQ("0.25", double_str.c_str()); StringValue string_value("dummy"); std::string string_str; EXPECT_TRUE(ToString(&string_value, &string_str)); EXPECT_STREQ("\"dummy\"", string_str.c_str()); }
// (ceiling x) Cell* op_ceiling(Scheme *sc) { Cell* num = first(sc->args); return s_return_helper(sc, make_real(sc, ceil(double_value(num)))); }
// (floor x) Cell* op_floor(Scheme *sc) { Cell* num = first(sc->args); return s_return_helper(sc, make_real(sc, floor(double_value(num)))); }
static bool assign_xml_value_to_protobuf_field(const std::string &value, xmltype xtype, ::google::protobuf::Message* msg, const ::google::protobuf::FieldDescriptor* field) { if (xtype==duration) return assign_xml_duration_value_to_protobuf_field(value,msg,field); // handle xmltype 'automatic' const ::google::protobuf::Reflection* reflection = msg->GetReflection(); switch (field->cpp_type()) { case ::google::protobuf::FieldDescriptor::CPPTYPE_INT32: { long longval; if (long_value(value,longval)) reflection->SetInt32(msg, field, longval); else printf("ERROR:\n"); break; } case ::google::protobuf::FieldDescriptor::CPPTYPE_INT64: { long longval; if (long_value(value,longval)) reflection->SetInt64(msg, field, longval); else printf("ERROR:\n"); break; } case ::google::protobuf::FieldDescriptor::CPPTYPE_UINT32: { unsigned long ulongval; if (ulong_value(value,ulongval)) reflection->SetUInt32(msg, field, ulongval); else printf("ERROR:\n"); break; } case ::google::protobuf::FieldDescriptor::CPPTYPE_UINT64: { unsigned long ulongval; if (ulong_value(value,ulongval)) reflection->SetUInt64(msg, field, ulongval); else printf("ERROR:\n"); break; } case ::google::protobuf::FieldDescriptor::CPPTYPE_DOUBLE: { double doubleval; if (double_value(value,doubleval)) reflection->SetDouble(msg, field, doubleval); else printf("ERROR:\n"); break; } case ::google::protobuf::FieldDescriptor::CPPTYPE_FLOAT: { double floatval; if (float_value(value,floatval)) reflection->SetFloat(msg, field, floatval); else printf("ERROR:\n"); break; } case ::google::protobuf::FieldDescriptor::CPPTYPE_BOOL: { bool boolval; if (bool_value(value,boolval)) reflection->SetBool(msg, field, boolval); else printf("ERROR:\n"); break; } case ::google::protobuf::FieldDescriptor::CPPTYPE_ENUM: { const ::google::protobuf::EnumDescriptor *enu = field->enum_type(); const ::google::protobuf::EnumValueDescriptor *enuval = enu->FindValueByName(value); if (!enuval) { printf("ERROR: '%s' not a valid value for %s !\n",value.c_str(),enu->name().c_str()); } else { //printf("SUCCESS: '%s' is a valid value for %s !\n",value.c_str(),enu->name().c_str()); reflection->SetEnum(msg, field, enuval); } break; } case ::google::protobuf::FieldDescriptor::CPPTYPE_STRING: reflection->SetString(msg, field, value); break; case ::google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE: printf("ERROR: element should not contain text data but we got '%s' !\n",value.c_str()); return false; default: printf("ERROR: Unsupported field type '%d' !\n", field->cpp_type()); return false; } return true; }
CS_IMPLEMENT_APPLICATION int main (int argc, char *argv[]) { iObjectRegistry* objreg = csInitializer::CreateEnvironment (argc, argv); if (! objreg) { csFPrintf (stderr, "Failed to create environment!\n"); return 1; } bool ok = csInitializer::RequestPlugins (objreg, CS_REQUEST_REPORTER, CS_REQUEST_REPORTERLISTENER, CS_REQUEST_PLUGIN ("crystalspace.script.perl5", iScript), CS_REQUEST_END); if (! ok) { csFPrintf (stderr, "Failed to load plugins!\n"); return 2; } if (csCommandLineHelper::CheckHelp (objreg)) { csCommandLineHelper::Help (objreg); return 0; } { csRef<iScript> script = csQueryRegistry<iScript> (objreg); if (! script) { csFPrintf (stderr, "Failed to find perl5 plugin!\n"); return 3; } ok = script->LoadModule ("cspace"); //ok = script->LoadModule ("scripts/perl5", "cspace.pm"); if (! ok) { csFPrintf (stderr, "Failed to load perl5 cspace module!\n"); return 4; } csInitializer::OpenApplication (objreg); //====================================================================// csPrintf ("Testing RValue/Store/Retrieve:\n"); int test_int = 3; float test_float = 3.0; double test_double = 3.0; bool test_bool = true; const char *test_str = "hello"; csPrintf (" Int: "); csRef<iScriptValue> int_value (script->RValue (test_int)); ok = script->Store("i", int_value); int_value.AttachNew (script->Retrieve ("i")); csPrintf ("%d == %d\n", test_int, int_value->GetInt ()); csPrintf (" Float: "); csRef<iScriptValue> float_value (script->RValue (test_float)); ok = script->Store("f", float_value); float_value.AttachNew (script->Retrieve ("f")); csPrintf ("%f == %f\n", test_float, float_value->GetFloat ()); csPrintf (" Double: "); csRef<iScriptValue> double_value (script->RValue (test_double)); ok = script->Store("d", double_value); double_value.AttachNew (script->Retrieve ("d")); csPrintf ("%lf == %lf\n", test_double, double_value->GetDouble ()); csPrintf (" String: "); csRef<iScriptValue> str_value (script->RValue (test_str)); ok = script->Store("s", str_value); str_value.AttachNew (script->Retrieve ("s")); csPrintf ("%s == %s\n", test_str, str_value->GetString ()->GetData ()); csPrintf (" Bool: "); csRef<iScriptValue> bool_value (script->RValue (test_bool)); ok = script->Store("b", bool_value); bool_value.AttachNew (script->Retrieve ("b")); csPrintf ("%s == %s\n\n", test_bool ? "true" : "false", bool_value->GetBool () ? "true" : "false"); //====================================================================// csPrintf ("Testing Remove:\n"); ok = script->Remove ("i") && script->Remove ("f") && script->Remove ("d") && script->Remove ("s") && script->Remove ("b"); csPrintf (" %s\n", ok ? "ok" : "failed"); int_value.AttachNew (script->Retrieve ("i")); csPrintf (" %s\n", int_value.IsValid () ? "failed" : "ok"); //====================================================================// csPrintf ("Testing New(csVector3):\n"); csRef<iScriptObject> obj (script->New ("csVector3")); csPrintf (" %s\n", obj.IsValid () ? "ok" : "failed"); //====================================================================// csPrintf ("Testing GetClass/IsA:\n"); csRef<iString> classname (obj->GetClass ()); csPrintf (" %s\n", classname->GetData ()); csPrintf (" %s\n", obj->IsA ("csVector3") ? "ok" : "failed"); //====================================================================// csPrintf ("Testing Set/Get:\n"); csPrintf (" %f == ", float_value->GetFloat ()); ok = obj->Set ("x", float_value); float_value.AttachNew (obj->Get ("x")); csPrintf ("%f\n", float_value->GetFloat ()); //====================================================================// csPrintf ("Testing Call(csVector3::Set):\n"); csRefArray<iScriptValue> args; args.Push (float_value); csRef<iScriptValue> ret (obj->Call ("Set", args)); csPrintf (" %f\n", float_value->GetFloat ()); //====================================================================// csPrintf ("Testing Call(csVector3::Norm):\n"); ret.AttachNew (obj->Call ("Norm")); csPrintf (" %f\n", ret->GetFloat ()); //====================================================================// csPrintf ("Testing GetPointer:\n"); csVector3 &vector = * (csVector3 *) obj->GetPointer (); vector.Normalize (); csPrintf (" %f %f %f\n", vector[0], vector[1], vector[2]); csPrintf (" ok\n"); csPrintf ("All Done!\n"); } csInitializer::DestroyApplication (objreg); return 0; }
void print_ast(struct ast* node) { if (node != NULL) { switch(node->node_type) { case N_NIL : { printf("nil"); break; }; case N_BOOL : { if (bool_value(node) == 1) { printf("true"); } else { printf("false"); }; break; }; case N_INTEGER : { printf("%d", int_value(node)); break; }; case N_DOUBLE : { printf("%f", double_value(node)); break; }; case N_STRING_1: { printf("%s", string_value(node)); break; }; case N_STRING_2: { printf("%s", string_value(node)); break; }; case N_IDENTIFIER : { struct identifier_node* i = (struct identifier_node*)node; printf("%s", i->name); break; }; case N_OBJECT : { struct object_node* o = (struct object_node*)node; printf("Class: %s\n", o->class_ptr->name); printf("Inst Vars:\n"); print_sym_list(o->sym_list); break; }; case N_OP_EQUAL : { print_ast(node->left); printf(" = "); print_ast(node->right); break; }; case N_OP_PLUS_EQ : { print_ast(node->left); printf(" += "); print_ast(node->right); break; }; case N_OP_MINUS_EQ : { print_ast(node->left); printf(" -= "); print_ast(node->right); break; }; case N_OP_MUL_EQ : { print_ast(node->left); printf(" *= "); print_ast(node->right); break; }; case N_OP_DIV_EQ : { print_ast(node->left); printf(" /= "); print_ast(node->right); break; }; case N_OP_MODULO_EQ : { print_ast(node->left); printf(" mod= "); print_ast(node->right); break; }; case N_ARG_LIST : { struct list_node* l = (struct list_node*)node; print_list(l); break; }; case N_OP_MUL : { print_ast(node->left); printf(" * "); print_ast(node->right); break; }; case N_OP_DIV : { print_ast(node->left); printf(" / "); print_ast(node->right); break; }; case N_OP_MODULO : { print_ast(node->left); printf(" mod "); print_ast(node->right); break; }; case N_OP_PLUS : { print_ast(node->left); printf(" + "); print_ast(node->right); break; }; case N_OP_MINUS : { print_ast(node->left); printf(" - "); print_ast(node->right); break; }; case N_OP_CMP_GT : { print_ast(node->left); printf(" > "); print_ast(node->right); break; }; case N_OP_CMP_GT_EQ : { print_ast(node->left); printf(" >= "); print_ast(node->right); break; }; case N_OP_CMP_LE : { print_ast(node->left); printf(" < "); print_ast(node->right); break; }; case N_OP_CMP_LE_EQ : { print_ast(node->left); printf(" <= "); print_ast(node->right); break; }; case N_OP_CMP_EQ : { print_ast(node->left); printf(" == "); print_ast(node->right); break; }; case N_OP_CMP_EQ_EQ : { print_ast(node->left); printf(" === "); print_ast(node->right); break; }; case N_OP_CMP_INEQ : { print_ast(node->left); printf(" <=> "); print_ast(node->right); break; }; case N_OP_CMP_NEG : { print_ast(node->left); printf(" != "); print_ast(node->right); break; }; case N_OP_CMP_AND : { print_ast(node->left); printf(" && "); print_ast(node->right); break; }; case N_OP_CMP_OR : { print_ast(node->left); printf(" || "); print_ast(node->right); break; }; case N_OP_PLUS_UN : { printf("+"); print_ast(node->left); break; }; case N_OP_MINUS_UN : { printf("+"); print_ast(node->left); break; }; case N_OP_NOT : { printf("+"); print_ast(node->left); break; }; case N_STMT_LIST : { print_ast(node->right); print_ast(node->left); break; }; case N_FUNCTION : { struct function_node* f = (struct function_node*)node; printf("def %s", f->name); // def function name print_list(f->args); // function parameters print_ast(f->stmts); // comp_statements printf("end"); break; }; case N_RETURN : { printf("return "); print_ast(node->left); // expression printf("\n"); break; }; case N_WHILE : { printf("while "); print_ast(node->left); // expression printf("\n"); print_ast(node->right); // comp_statements printf("\nend"); break; }; case N_CLASS : { struct class_node* c = (struct class_node*)node; printf("class %s\n", c->name); // class name print_list(c->stmts); // comp_statements printf("end"); break; }; case N_METHOD_CALL_2 : { struct method_call_node* m = (struct method_call_node*) node; printf("%s ", m->method_name); print_list(m->args); break; }; default : { printf("%i\n", node->node_type); printf("ERROR: when printing %c.\n", node->node_type); }; }; }; };
struct ast* rputs(struct ast* a){ if (a->node_type == N_METHOD_CALL_0 || a->node_type == N_METHOD_CALL_1 || a->node_type == N_METHOD_CALL_2) { struct method_call_node* m = (struct method_call_node*)a; struct list_node* arg_node = m->args; // caso especial: puts() sin parámetros imprime salto de línea if (arg_node == NULL) { printf("\n"); // comportamiento normal } else { while(arg_node != NULL){ struct ast* evaluated = eval_ast(arg_node->arg); rputs(evaluated); arg_node = arg_node->next; }; }; } else if (a->node_type == N_ARRAY) { int arr_size = array_tree_size(a->left); struct ast* result[arr_size]; struct ast* ptr = a->left; int i; for (i = 0; i < arr_size; i++) { result[i] = ptr; ptr = ptr->right; }; ptr = result[arr_size-1]; for (i = arr_size-1; i >= 0; i--) { rputs(eval_ast(result[i])); }; } else if (a->node_type == N_ARRAY_CONTENT) { rputs(eval_ast(a->left)); } else if (a->node_type == N_STRING_1) { printf("%s\n", string_value(a)); } else if (a->node_type == N_STRING_2) { char * str = malloc(sizeof( strlen(string_value(a)) )); strcpy(str, string_value(a)); str = build_end_of_lines(str); printf("%s\n", str); } else if (a->node_type == N_INTEGER) { printf("%d\n", int_value(a)); } else if (a->node_type == N_DOUBLE) { double d = double_value(a); if ( d - floor(d) == 0.0 ) { printf( "%g.0\n", d ); } else { printf( "%g\n", d ); }; } else if (a->node_type == N_BOOL) { printf("%s\n", bool_value(a) ? "true" : "false"); } else if (a->node_type == N_NIL) { printf("\n"); } else if (a->node_type == N_OBJECT) { struct object_node * object = (struct object_node *) a; printf("<#%s:%p>\n", object->class_ptr->name, (void *)object); } else { printf("Puts doesn't support %s type, sorry :D\n", type_name(a->node_type)); }; return new_nil_node(); };