00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 #include "soarkernel.h"
00045 #include <math.h>
00046 #include <errno.h>
00047 #include "rhsfun.h"
00048
00049
00050
00051
00052
00053
00054
00055
00056 Symbol *plus_rhs_function_code(list * args)
00057 {
00058 bool float_found;
00059 long i;
00060 float f = 0;
00061 Symbol *arg;
00062 cons *c;
00063
00064 for (c = args; c != NIL; c = c->rest) {
00065 arg = c->first;
00066 if ((arg->common.symbol_type != INT_CONSTANT_SYMBOL_TYPE) &&
00067 (arg->common.symbol_type != FLOAT_CONSTANT_SYMBOL_TYPE)) {
00068 print_with_symbols("Error: non-number (%y) passed to + function\n", arg);
00069 return NIL;
00070 }
00071 }
00072
00073 i = 0;
00074 float_found = FALSE;
00075 while (args) {
00076 arg = args->first;
00077 if (arg->common.symbol_type == INT_CONSTANT_SYMBOL_TYPE) {
00078 if (float_found)
00079 f += arg->ic.value;
00080 else
00081 i += arg->ic.value;
00082 } else {
00083 if (float_found)
00084 f += arg->fc.value;
00085 else {
00086 float_found = TRUE;
00087 f = arg->fc.value + i;
00088 }
00089 }
00090 args = args->rest;
00091 }
00092 if (float_found)
00093 return make_float_constant(f);
00094 return make_int_constant(i);
00095 }
00096
00097
00098
00099
00100
00101
00102
00103
00104 Symbol *times_rhs_function_code(list * args)
00105 {
00106 bool float_found;
00107 long i;
00108 float f = 0;
00109 Symbol *arg;
00110 cons *c;
00111
00112 for (c = args; c != NIL; c = c->rest) {
00113 arg = c->first;
00114 if ((arg->common.symbol_type != INT_CONSTANT_SYMBOL_TYPE) &&
00115 (arg->common.symbol_type != FLOAT_CONSTANT_SYMBOL_TYPE)) {
00116 print_with_symbols("Error: non-number (%y) passed to * function\n", arg);
00117 return NIL;
00118 }
00119 }
00120
00121 i = 1;
00122 float_found = FALSE;
00123 while (args) {
00124 arg = args->first;
00125 if (arg->common.symbol_type == INT_CONSTANT_SYMBOL_TYPE) {
00126 if (float_found)
00127 f *= arg->ic.value;
00128 else
00129 i *= arg->ic.value;
00130 } else {
00131 if (float_found)
00132 f *= arg->fc.value;
00133 else {
00134 float_found = TRUE;
00135 f = arg->fc.value * i;
00136 }
00137 }
00138 args = args->rest;
00139 }
00140 if (float_found)
00141 return make_float_constant(f);
00142 return make_int_constant(i);
00143 }
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154 Symbol *minus_rhs_function_code(list * args)
00155 {
00156 Symbol *arg;
00157 float f = 0;
00158 long i = 0;
00159 cons *c;
00160 bool float_found;
00161
00162 if (!args) {
00163 print("Error: '-' function called with no arguments\n");
00164 return NIL;
00165 }
00166
00167 for (c = args; c != NIL; c = c->rest) {
00168 arg = c->first;
00169 if ((arg->common.symbol_type != INT_CONSTANT_SYMBOL_TYPE) &&
00170 (arg->common.symbol_type != FLOAT_CONSTANT_SYMBOL_TYPE)) {
00171 print_with_symbols("Error: non-number (%y) passed to - function\n", arg);
00172 return NIL;
00173 }
00174 }
00175
00176 if (!args->rest) {
00177
00178 arg = args->first;
00179 if (arg->common.symbol_type == INT_CONSTANT_SYMBOL_TYPE)
00180 return make_int_constant(-arg->ic.value);
00181 return make_float_constant(-arg->fc.value);
00182 }
00183
00184
00185 arg = args->first;
00186 float_found = FALSE;
00187 if (arg->common.symbol_type == INT_CONSTANT_SYMBOL_TYPE)
00188 i = arg->ic.value;
00189 else {
00190 float_found = TRUE;
00191 f = arg->fc.value;
00192 }
00193 for (c = args->rest; c != NIL; c = c->rest) {
00194 arg = c->first;
00195 if (arg->common.symbol_type == INT_CONSTANT_SYMBOL_TYPE) {
00196 if (float_found)
00197 f -= arg->ic.value;
00198 else
00199 i -= arg->ic.value;
00200 } else {
00201 if (float_found)
00202 f -= arg->fc.value;
00203 else {
00204 float_found = TRUE;
00205 f = i - arg->fc.value;
00206 }
00207 }
00208 }
00209
00210 if (float_found)
00211 return make_float_constant(f);
00212 return make_int_constant(i);
00213 }
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224 Symbol *fp_divide_rhs_function_code(list * args)
00225 {
00226 Symbol *arg;
00227 float f;
00228 cons *c;
00229
00230 if (!args) {
00231 print("Error: '/' function called with no arguments\n");
00232 return NIL;
00233 }
00234
00235 for (c = args; c != NIL; c = c->rest) {
00236 arg = c->first;
00237 if ((arg->common.symbol_type != INT_CONSTANT_SYMBOL_TYPE) &&
00238 (arg->common.symbol_type != FLOAT_CONSTANT_SYMBOL_TYPE)) {
00239 print_with_symbols("Error: non-number (%y) passed to / function\n", arg);
00240 return NIL;
00241 }
00242 }
00243
00244 if (!args->rest) {
00245
00246 arg = args->first;
00247 if (arg->common.symbol_type == INT_CONSTANT_SYMBOL_TYPE)
00248 f = (float) arg->ic.value;
00249 else
00250 f = arg->fc.value;
00251 if (f != 0.0)
00252 return make_float_constant((float) (1.0 / f));
00253 print("Error: attempt to divide ('/') by zero.\n");
00254 return NIL;
00255 }
00256
00257
00258 arg = args->first;
00259 if (arg->common.symbol_type == INT_CONSTANT_SYMBOL_TYPE)
00260 f = (float) arg->ic.value;
00261 else
00262 f = arg->fc.value;
00263 for (c = args->rest; c != NIL; c = c->rest) {
00264 arg = c->first;
00265 if (arg->common.symbol_type == INT_CONSTANT_SYMBOL_TYPE) {
00266 if (arg->ic.value)
00267 f /= arg->ic.value;
00268 else {
00269 print("Error: attempt to divide ('/') by zero.\n");
00270 return NIL;
00271 }
00272 } else {
00273 if (arg->fc.value != 0.0)
00274 f /= arg->fc.value;
00275 else {
00276 print("Error: attempt to divide ('/') by zero.\n");
00277 return NIL;
00278 }
00279 }
00280 }
00281 return make_float_constant(f);
00282 }
00283
00284
00285
00286
00287
00288
00289
00290 Symbol *div_rhs_function_code(list * args)
00291 {
00292 Symbol *arg1, *arg2;
00293
00294 arg1 = args->first;
00295 arg2 = args->rest->first;
00296
00297 if (arg1->common.symbol_type != INT_CONSTANT_SYMBOL_TYPE) {
00298 print_with_symbols("Error: non-integer (%y) passed to div function\n", arg1);
00299 return NIL;
00300 }
00301 if (arg2->common.symbol_type != INT_CONSTANT_SYMBOL_TYPE) {
00302 print_with_symbols("Error: non-integer (%y) passed to div function\n", arg2);
00303 return NIL;
00304 }
00305
00306 if (arg2->ic.value == 0) {
00307 print("Error: attempt to divide ('div') by zero.\n");
00308 return NIL;
00309 }
00310
00311 return make_int_constant(arg1->ic.value / arg2->ic.value);
00312
00313
00314 }
00315
00316
00317
00318
00319
00320
00321
00322
00323 Symbol *mod_rhs_function_code(list * args)
00324 {
00325 Symbol *arg1, *arg2;
00326
00327 arg1 = args->first;
00328 arg2 = args->rest->first;
00329
00330 if (arg1->common.symbol_type != INT_CONSTANT_SYMBOL_TYPE) {
00331 print_with_symbols("Error: non-integer (%y) passed to mod function\n", arg1);
00332 return NIL;
00333 }
00334 if (arg2->common.symbol_type != INT_CONSTANT_SYMBOL_TYPE) {
00335 print_with_symbols("Error: non-integer (%y) passed to mod function\n", arg2);
00336 return NIL;
00337 }
00338
00339 if (arg2->ic.value == 0) {
00340 print("Error: attempt to divide ('mod') by zero.\n");
00341 return NIL;
00342 }
00343
00344 return make_int_constant(arg1->ic.value % arg2->ic.value);
00345
00346
00347
00348 }
00349
00350
00351
00352
00353
00354
00355 Symbol *sin_rhs_function_code(list * args)
00356 {
00357 Symbol *arg;
00358 float arg_value;
00359
00360 if (!args) {
00361 print("Error: 'sin' function called with no arguments\n");
00362 return NIL;
00363 }
00364
00365 arg = args->first;
00366 if (arg->common.symbol_type == FLOAT_CONSTANT_SYMBOL_TYPE)
00367 arg_value = arg->fc.value;
00368 else if (arg->common.symbol_type == INT_CONSTANT_SYMBOL_TYPE)
00369 arg_value = (float) arg->ic.value;
00370 else {
00371 print_with_symbols("Error: 'sin' function called with non-numeric argument %y\n", arg);
00372 return NIL;
00373 }
00374
00375 return make_float_constant((float) sin(arg_value));
00376 }
00377
00378
00379
00380
00381
00382
00383 Symbol *cos_rhs_function_code(list * args)
00384 {
00385 Symbol *arg;
00386 float arg_value;
00387
00388 if (!args) {
00389 print("Error: 'cos' function called with no arguments\n");
00390 return NIL;
00391 }
00392
00393 arg = args->first;
00394 if (arg->common.symbol_type == FLOAT_CONSTANT_SYMBOL_TYPE)
00395 arg_value = arg->fc.value;
00396 else if (arg->common.symbol_type == INT_CONSTANT_SYMBOL_TYPE)
00397 arg_value = (float) arg->ic.value;
00398 else {
00399 print_with_symbols("Error: 'cos' function called with non-numeric argument %y\n", arg);
00400 return NIL;
00401 }
00402 return make_float_constant((float) cos(arg_value));
00403 }
00404
00405
00406
00407
00408
00409
00410 Symbol *sqrt_rhs_function_code(list * args)
00411 {
00412 Symbol *arg;
00413 float arg_value;
00414
00415 if (!args) {
00416 print("Error: 'sqrt' function called with no arguments\n");
00417 return NIL;
00418 }
00419
00420 arg = args->first;
00421 if (arg->common.symbol_type == FLOAT_CONSTANT_SYMBOL_TYPE)
00422 arg_value = arg->fc.value;
00423 else if (arg->common.symbol_type == INT_CONSTANT_SYMBOL_TYPE)
00424 arg_value = (float) arg->ic.value;
00425 else {
00426 print_with_symbols("Error: 'sqrt' function called with non-numeric argument %y\n", arg);
00427 return NIL;
00428 }
00429 return make_float_constant((float) sqrt(arg_value));
00430 }
00431
00432
00433
00434
00435
00436
00437
00438 Symbol *atan2_rhs_function_code(list * args)
00439 {
00440 Symbol *arg;
00441 cons *c;
00442 float numer_value, denom_value;
00443
00444 if (!args) {
00445 print("Error: 'atan2' function called with no arguments\n");
00446 return NIL;
00447 }
00448
00449 for (c = args; c != NIL; c = c->rest) {
00450 arg = c->first;
00451 if ((arg->common.symbol_type != INT_CONSTANT_SYMBOL_TYPE)
00452 && (arg->common.symbol_type != FLOAT_CONSTANT_SYMBOL_TYPE)) {
00453 print_with_symbols("Error: non-number (%y) passed to atan2\n", arg);
00454 return NIL;
00455 }
00456 }
00457
00458 if (!args->rest) {
00459 print("Error: 'atan2' function called with only one argument\n");
00460 return NIL;
00461 }
00462
00463 arg = args->first;
00464 if (arg->common.symbol_type == FLOAT_CONSTANT_SYMBOL_TYPE)
00465 numer_value = arg->fc.value;
00466 else
00467 numer_value = (float) arg->ic.value;
00468
00469 c = args->rest;
00470 if (c->rest) {
00471 print("Error: 'atan2' function called with more than two arguments.\n");
00472 return NIL;
00473 }
00474 arg = c->first;
00475 if (arg->common.symbol_type == FLOAT_CONSTANT_SYMBOL_TYPE)
00476 denom_value = arg->fc.value;
00477 else
00478 denom_value = (float) arg->ic.value;
00479
00480 return make_float_constant((float) atan2(numer_value, denom_value));
00481 }
00482
00483
00484
00485
00486
00487
00488 Symbol *abs_rhs_function_code(list * args)
00489 {
00490 Symbol *arg, *return_value;
00491
00492 if (!args) {
00493 print("Error: 'abs' function called with no arguments\n");
00494 return NIL;
00495 }
00496
00497 arg = args->first;
00498 if (arg->common.symbol_type == FLOAT_CONSTANT_SYMBOL_TYPE)
00499 return_value = make_float_constant((float) fabs(arg->fc.value));
00500 else if (arg->common.symbol_type == INT_CONSTANT_SYMBOL_TYPE)
00501 return_value = make_int_constant((arg->ic.value < 0) ? -arg->ic.value : arg->ic.value);
00502 else {
00503 print_with_symbols("Error: 'abs' function called with non-numeric argument %y\n", arg);
00504 return NIL;
00505 }
00506 return return_value;
00507 }
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517 Symbol *int_rhs_function_code(list * args)
00518 {
00519 Symbol *sym;
00520
00521 if (!args) {
00522 print("Error: 'int' function called with no arguments.\n");
00523 return NIL;
00524 }
00525
00526 if (args->rest) {
00527 print("Error: 'int' takes exactly 1 argument.\n");
00528 return NIL;
00529 }
00530
00531 sym = (Symbol *) args->first;
00532 if (sym->common.symbol_type == VARIABLE_SYMBOL_TYPE) {
00533 print_with_symbols("Error: variable (%y) passed to 'int' RHS function.\n", sym);
00534 return NIL;
00535 } else if (sym->common.symbol_type == IDENTIFIER_SYMBOL_TYPE) {
00536 print_with_symbols("Error: identifier (%y) passed to 'int' RHS function.\n", sym);
00537 return NIL;
00538 } else if (sym->common.symbol_type == SYM_CONSTANT_SYMBOL_TYPE) {
00539 long int_val;
00540
00541 errno = 0;
00542 int_val = strtol(symbol_to_string(sym, FALSE, NIL, 0), NULL, 10);
00543 if (errno) {
00544 print("Error: bad integer (%y) given to 'int' RHS function\n", sym);
00545 return NIL;
00546 }
00547 return make_int_constant(int_val);
00548 } else if (sym->common.symbol_type == INT_CONSTANT_SYMBOL_TYPE) {
00549 return sym;
00550 } else if (sym->common.symbol_type == FLOAT_CONSTANT_SYMBOL_TYPE) {
00551 double int_part;
00552 modf((double) sym->fc.value, &int_part);
00553 return make_int_constant((long) int_part);
00554 }
00555
00556 print("Error: unknown symbol type (%y) given to 'int' RHS function\n", sym);
00557 return NIL;
00558 }
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568 Symbol *float_rhs_function_code(list * args)
00569 {
00570 Symbol *sym;
00571
00572 if (!args) {
00573 print("Error: 'float' function called with no arguments.\n");
00574 return NIL;
00575 }
00576
00577 if (args->rest) {
00578 print("Error: 'float' takes exactly 1 argument.\n");
00579 return NIL;
00580 }
00581
00582 sym = (Symbol *) args->first;
00583 if (sym->common.symbol_type == VARIABLE_SYMBOL_TYPE) {
00584 print_with_symbols("Error: variable (%y) passed to 'float' RHS function.\n", sym);
00585 return NIL;
00586 } else if (sym->common.symbol_type == IDENTIFIER_SYMBOL_TYPE) {
00587 print_with_symbols("Error: identifier (%y) passed to 'float' RHS function.\n", sym);
00588 return NIL;
00589 } else if (sym->common.symbol_type == SYM_CONSTANT_SYMBOL_TYPE) {
00590 double float_val;
00591
00592 errno = 0;
00593
00594 float_val = strtod(symbol_to_string(sym, FALSE, NIL, 0), NULL);
00595 if (errno) {
00596 print("Error: bad float (%y) given to 'float' RHS function\n", sym);
00597 return NIL;
00598 }
00599 return make_float_constant((float) float_val);
00600 } else if (sym->common.symbol_type == FLOAT_CONSTANT_SYMBOL_TYPE) {
00601 return sym;
00602 } else if (sym->common.symbol_type == INT_CONSTANT_SYMBOL_TYPE) {
00603 return make_float_constant((float) sym->ic.value);
00604 }
00605
00606 print("Error: unknown symbol type (%y) given to 'float' RHS function\n", sym);
00607 return NIL;
00608 }
00609
00610
00611
00612
00613
00614
00615
00616
00617 void init_built_in_rhs_math_functions(void)
00618 {
00619 add_rhs_function(make_sym_constant("+"), plus_rhs_function_code, -1, TRUE, FALSE);
00620 add_rhs_function(make_sym_constant("*"), times_rhs_function_code, -1, TRUE, FALSE);
00621 add_rhs_function(make_sym_constant("-"), minus_rhs_function_code, -1, TRUE, FALSE);
00622 add_rhs_function(make_sym_constant("/"), fp_divide_rhs_function_code, -1, TRUE, FALSE);
00623 add_rhs_function(make_sym_constant("div"), div_rhs_function_code, 2, TRUE, FALSE);
00624 add_rhs_function(make_sym_constant("mod"), mod_rhs_function_code, 2, TRUE, FALSE);
00625
00626 add_rhs_function(make_sym_constant("sin"), sin_rhs_function_code, 1, TRUE, FALSE);
00627 add_rhs_function(make_sym_constant("cos"), cos_rhs_function_code, 1, TRUE, FALSE);
00628 add_rhs_function(make_sym_constant("atan2"), atan2_rhs_function_code, 2, TRUE, FALSE);
00629 add_rhs_function(make_sym_constant("sqrt"), sqrt_rhs_function_code, 1, TRUE, FALSE);
00630 add_rhs_function(make_sym_constant("abs"), abs_rhs_function_code, 1, TRUE, FALSE);
00631 add_rhs_function(make_sym_constant("int"), int_rhs_function_code, 1, TRUE, FALSE);
00632 add_rhs_function(make_sym_constant("float"), float_rhs_function_code, 1, TRUE, FALSE);
00633
00634 }
00635
00636 void remove_built_in_rhs_math_functions(void)
00637 {
00638 remove_rhs_function(make_sym_constant("+"));
00639 remove_rhs_function(make_sym_constant("*"));
00640 remove_rhs_function(make_sym_constant("-"));
00641 remove_rhs_function(make_sym_constant("/"));
00642 remove_rhs_function(make_sym_constant("div"));
00643 remove_rhs_function(make_sym_constant("mod"));
00644 remove_rhs_function(make_sym_constant("sin"));
00645 remove_rhs_function(make_sym_constant("cos"));
00646 remove_rhs_function(make_sym_constant("atan2"));
00647 remove_rhs_function(make_sym_constant("sqrt"));
00648 remove_rhs_function(make_sym_constant("abs"));
00649 remove_rhs_function(make_sym_constant("int"));
00650 remove_rhs_function(make_sym_constant("float"));
00651 }