expression.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <grass/gis.h>
  5. #include <grass/glocale.h>
  6. #include "mapcalc.h"
  7. #include "func_proto.h"
  8. /****************************************************************************/
  9. static expr_list *variables;
  10. /****************************************************************************/
  11. static func_desc *find_func(const char *name)
  12. {
  13. int i;
  14. for (i = 0; local_func_descs[i].name; i++) {
  15. if (strcmp(name, local_func_descs[i].name) == 0)
  16. return &local_func_descs[i];
  17. }
  18. for (i = 0; calc_func_descs[i].name; i++) {
  19. if (strcmp(name, calc_func_descs[i].name) == 0)
  20. return &calc_func_descs[i];
  21. }
  22. return NULL;
  23. }
  24. static expression *find_variable(const char *name)
  25. {
  26. expr_list *l;
  27. for (l = variables; l; l = l->next)
  28. if (strcmp(name, l->exp->data.bind.var) == 0)
  29. return l->exp;
  30. return NULL;
  31. }
  32. static expression *allocate(int type, int res_type)
  33. {
  34. expression *e = G_malloc(sizeof(expression));
  35. e->type = type;
  36. e->res_type = res_type;
  37. e->buf = NULL;
  38. e->worker = NULL;
  39. return e;
  40. }
  41. /****************************************************************************/
  42. static expression *to_int(expression * e1)
  43. {
  44. expression *e = allocate(expr_type_function, CELL_TYPE);
  45. expression **args = G_malloc(2 * sizeof(expression *));
  46. int *argt = G_malloc(2 * sizeof(int));
  47. argt[0] = CELL_TYPE;
  48. args[1] = e1;
  49. argt[1] = e1->res_type;
  50. e->data.func.name = "";
  51. e->data.func.oper = NULL;
  52. e->data.func.func = f_int;
  53. e->data.func.argc = 1;
  54. e->data.func.args = args;
  55. e->data.func.argt = argt;
  56. e->data.func.argv = NULL;
  57. return e;
  58. }
  59. static expression *to_float(expression * e1)
  60. {
  61. expression *e = allocate(expr_type_function, FCELL_TYPE);
  62. expression **args = G_malloc(2 * sizeof(expression *));
  63. int *argt = G_malloc(2 * sizeof(int));
  64. argt[0] = FCELL_TYPE;
  65. args[1] = e1;
  66. argt[1] = e1->res_type;
  67. e->data.func.name = "";
  68. e->data.func.oper = NULL;
  69. e->data.func.func = f_float;
  70. e->data.func.argc = 1;
  71. e->data.func.args = args;
  72. e->data.func.argt = argt;
  73. e->data.func.argv = NULL;
  74. return e;
  75. }
  76. static expression *to_double(expression * e1)
  77. {
  78. expression *e = allocate(expr_type_function, DCELL_TYPE);
  79. expression **args = G_malloc(2 * sizeof(expression *));
  80. int *argt = G_malloc(2 * sizeof(int));
  81. argt[0] = DCELL_TYPE;
  82. args[1] = e1;
  83. argt[1] = e1->res_type;
  84. e->data.func.name = "";
  85. e->data.func.oper = NULL;
  86. e->data.func.func = f_double;
  87. e->data.func.argc = 1;
  88. e->data.func.args = args;
  89. e->data.func.argt = argt;
  90. e->data.func.argv = NULL;
  91. return e;
  92. }
  93. /****************************************************************************/
  94. int is_var(const char *name)
  95. {
  96. return find_variable(name) ? 1 : 0;
  97. }
  98. /****************************************************************************/
  99. void define_variable(expression * e)
  100. {
  101. variables = list(e, variables);
  102. }
  103. char *composite(const char *name, const char *mapset)
  104. {
  105. char *buf = G_malloc(strlen(name) + strlen(mapset) + 2);
  106. strcpy(buf, name);
  107. strcat(buf, "@");
  108. strcat(buf, mapset);
  109. return buf;
  110. }
  111. expr_list *list(expression * exp, expr_list * next)
  112. {
  113. expr_list *l = G_malloc(sizeof(struct expr_list));
  114. l->exp = exp;
  115. l->next = next;
  116. return l;
  117. }
  118. int list_length(expr_list * l)
  119. {
  120. int n = 0;
  121. for (; l; l = l->next)
  122. n++;
  123. return n;
  124. }
  125. expr_list *singleton(expression * e1)
  126. {
  127. return list(e1, NULL);
  128. }
  129. expr_list *pair(expression * e1, expression * e2)
  130. {
  131. return list(e1, list(e2, NULL));
  132. }
  133. expr_list *triple(expression * e1, expression * e2, expression * e3)
  134. {
  135. return list(e1, list(e2, list(e3, NULL)));
  136. }
  137. expression *constant_int(int x)
  138. {
  139. expression *e = allocate(expr_type_constant, CELL_TYPE);
  140. e->data.con.ival = x;
  141. return e;
  142. }
  143. expression *constant_float(float x)
  144. {
  145. expression *e = allocate(expr_type_constant, FCELL_TYPE);
  146. e->data.con.fval = x;
  147. return e;
  148. }
  149. expression *constant_double(double x)
  150. {
  151. expression *e = allocate(expr_type_constant, DCELL_TYPE);
  152. e->data.con.fval = x;
  153. return e;
  154. }
  155. expression *variable(const char *name)
  156. {
  157. expression *var = find_variable(name);
  158. expression *e;
  159. if (!var)
  160. syntax_error(_("Undefined variable '%s'"), name);
  161. e = allocate(expr_type_variable, var ? var->res_type : CELL_TYPE);
  162. e->data.var.name = name;
  163. e->data.var.bind = var;
  164. return e;
  165. }
  166. expression *mapname(const char *name, int mod, int row, int col, int depth)
  167. {
  168. int res_type = map_type(name, mod);
  169. expression *e = allocate(expr_type_map,
  170. res_type >= 0 ? res_type : CELL_TYPE);
  171. if (res_type < 0)
  172. syntax_error(_("Invalid map <%s>"), name);
  173. e->data.map.name = name;
  174. e->data.map.mod = mod;
  175. e->data.map.row = row;
  176. e->data.map.col = col;
  177. e->data.map.depth = depth;
  178. return e;
  179. }
  180. expression *binding(const char *var, expression * val)
  181. {
  182. expression *e = allocate(expr_type_binding, val->res_type);
  183. e->data.bind.var = var;
  184. e->data.bind.val = val;
  185. e->data.bind.fd = -1;
  186. return e;
  187. }
  188. expression *operator(const char *name, const char *oper, int prec,
  189. expr_list * arglist)
  190. {
  191. func_desc *d = find_func(name);
  192. int argc = list_length(arglist);
  193. expression **args = G_malloc((argc + 1) * sizeof(expression *));
  194. int *argt = G_malloc((argc + 1) * sizeof(int));
  195. expression *e;
  196. expr_list *l;
  197. int i;
  198. for (l = arglist, i = 1; l; l = l->next, i++)
  199. args[i] = l->exp;
  200. for (i = 1; i <= argc; i++)
  201. argt[i] = args[i]->res_type;
  202. argt[0] = CELL_TYPE;
  203. switch (!d ? -1 : d->check_args(argc, argt)) {
  204. case -1:
  205. syntax_error(_("Undefined function '%s'"), name);
  206. break;
  207. case 0:
  208. break;
  209. case E_ARG_LO:
  210. syntax_error(_("Too few arguments (%d) to function %s()"),
  211. argc, name);
  212. break;
  213. case E_ARG_HI:
  214. syntax_error(_("Too many arguments (%d) to function %s()"),
  215. argc, name);
  216. break;
  217. case E_ARG_TYPE:
  218. syntax_error(_("Incorrect argument types to function %s()"), name);
  219. break;
  220. default:
  221. G_fatal_error(_("Internal error for function %s()"), name);
  222. break;
  223. }
  224. for (i = 1; i <= argc; i++)
  225. if (argt[i] != args[i]->res_type) {
  226. if (argt[i] == CELL_TYPE)
  227. args[i] = to_int(args[i]);
  228. if (argt[i] == FCELL_TYPE)
  229. args[i] = to_float(args[i]);
  230. if (argt[i] == DCELL_TYPE)
  231. args[i] = to_double(args[i]);
  232. }
  233. e = allocate(expr_type_function, argt[0]);
  234. e->data.func.name = name;
  235. e->data.func.oper = oper;
  236. e->data.func.prec = prec;
  237. e->data.func.func = d ? d->func : NULL;
  238. e->data.func.argc = argc;
  239. e->data.func.args = args;
  240. e->data.func.argt = argt;
  241. e->data.func.argv = NULL;
  242. return e;
  243. }
  244. expression *function(const char *name, expr_list * arglist)
  245. {
  246. return operator(name, NULL, 0, arglist);
  247. }
  248. /****************************************************************************/
  249. static char *format_expression_prec(const expression * e, int prec);
  250. /****************************************************************************/
  251. static char *format_constant(const expression * e)
  252. {
  253. char buff[64];
  254. if (e->res_type == CELL_TYPE)
  255. sprintf(buff, "%d", e->data.con.ival);
  256. else
  257. sprintf(buff, "%.8g", e->data.con.fval);
  258. return strdup(buff);
  259. }
  260. static char *format_variable(const expression * e)
  261. {
  262. return strdup(e->data.var.name);
  263. }
  264. static char *format_map(const expression * e)
  265. {
  266. char buff[1024];
  267. const char *mod;
  268. switch (e->data.map.mod) {
  269. case 'r':
  270. mod = "r#";
  271. break;
  272. case 'g':
  273. mod = "g#";
  274. break;
  275. case 'b':
  276. mod = "b#";
  277. break;
  278. case '#':
  279. mod = "#";
  280. break;
  281. case '@':
  282. mod = "@";
  283. break;
  284. case 'M':
  285. mod = "";
  286. break;
  287. default:
  288. G_warning(_("Invalid map modifier: '%c'"), e->data.map.mod);
  289. mod = "?";
  290. break;
  291. }
  292. if (e->data.map.depth)
  293. sprintf(buff, "%s%s[%d,%d,%d]",
  294. mod, e->data.map.name,
  295. e->data.map.row, e->data.map.col, e->data.map.depth);
  296. else if (e->data.map.row || e->data.map.col)
  297. sprintf(buff, "%s%s[%d,%d]",
  298. mod, e->data.map.name, e->data.map.row, e->data.map.col);
  299. else
  300. sprintf(buff, "%s%s", mod, e->data.map.name);
  301. return strdup(buff);
  302. }
  303. static char *format_function(const expression * e, int prec)
  304. {
  305. char **args = NULL;
  306. int num_args = 0;
  307. char *result;
  308. int len;
  309. int i;
  310. if (e->data.func.argc == 1 && !*e->data.func.name)
  311. return format_expression_prec(e->data.func.args[1], prec);
  312. len = strlen(e->data.func.name) + 3;
  313. for (i = 1; i <= e->data.func.argc; i++) {
  314. if (i >= num_args) {
  315. num_args = i + 1000;
  316. args = G_realloc(args, num_args * sizeof(char *));
  317. }
  318. args[i] = format_expression_prec(e->data.func.args[i], 9);
  319. if (i > 1)
  320. len += 2;
  321. len += strlen(args[i]);
  322. }
  323. result = G_malloc(len);
  324. strcpy(result, e->data.func.name);
  325. strcat(result, "(");
  326. for (i = 1; i <= e->data.func.argc; i++) {
  327. if (i > 1)
  328. strcat(result, ", ");
  329. strcat(result, args[i]);
  330. G_free(args[i]);
  331. }
  332. strcat(result, ")");
  333. return result;
  334. }
  335. static char *format_operator(const expression * e, int prec)
  336. {
  337. int prec2 = e->data.func.prec;
  338. char *arg1, *arg2, *arg3;
  339. char *result;
  340. switch (e->data.func.argc) {
  341. case 1:
  342. arg1 = format_expression_prec(e->data.func.args[1], prec2);
  343. result = G_malloc(strlen(e->data.func.oper) + strlen(arg1) + 3);
  344. sprintf(result, "%s%s%s%s",
  345. prec <= prec2 ? "(" : "",
  346. e->data.func.oper, arg1, prec <= prec2 ? ")" : "");
  347. G_free(arg1);
  348. return result;
  349. case 2:
  350. arg1 = format_expression_prec(e->data.func.args[1], (prec2 + 1));
  351. arg2 = format_expression_prec(e->data.func.args[2], prec2);
  352. result =
  353. G_malloc(strlen(e->data.func.oper) + strlen(arg1) + strlen(arg2) +
  354. 5);
  355. sprintf(result, "%s%s %s %s%s", prec <= prec2 ? "(" : "", arg1,
  356. e->data.func.oper, arg2, prec <= prec2 ? ")" : "");
  357. G_free(arg1);
  358. G_free(arg2);
  359. return result;
  360. case 3:
  361. arg1 = format_expression_prec(e->data.func.args[1], prec2);
  362. arg2 = format_expression_prec(e->data.func.args[2], prec2);
  363. arg3 = format_expression_prec(e->data.func.args[3], (prec2 + 1));
  364. result = G_malloc(strlen(arg1) + strlen(arg2) + strlen(arg3) + 9);
  365. sprintf(result, "%s%s ? %s : %s%s",
  366. prec <= prec2 ? "(" : "",
  367. arg1, arg2, arg3, prec <= prec2 ? ")" : "");
  368. G_free(arg1);
  369. G_free(arg2);
  370. G_free(arg3);
  371. return result;
  372. default:
  373. G_warning(_("Illegal number of arguments (%d) for operator '%s'"),
  374. e->data.func.argc, e->data.func.oper);
  375. return format_function(e, prec);
  376. }
  377. }
  378. static char *format_func_op(const expression * e, int prec)
  379. {
  380. return (e->data.func.oper ? format_operator : format_function) (e, prec);
  381. }
  382. static char *format_binding(const expression * e, int prec)
  383. {
  384. char *result;
  385. char *expr = format_expression_prec(e->data.bind.val, 8);
  386. result = G_malloc(strlen(e->data.bind.var) + strlen(expr) + 6);
  387. sprintf(result, "%s%s = %s%s",
  388. prec < 8 ? "(" : "", e->data.bind.var, expr, prec < 8 ? ")" : "");
  389. G_free(expr);
  390. return result;
  391. }
  392. static char *format_expression_prec(const expression * e, int prec)
  393. {
  394. switch (e->type) {
  395. case expr_type_constant:
  396. return format_constant(e);
  397. case expr_type_variable:
  398. return format_variable(e);
  399. case expr_type_map:
  400. return format_map(e);
  401. case expr_type_function:
  402. return format_func_op(e, prec);
  403. case expr_type_binding:
  404. return format_binding(e, prec);
  405. default:
  406. G_warning(_("Format_expression_prec: unknown type: %d"), e->type);
  407. return strdup("??");
  408. }
  409. }
  410. char *format_expression(const expression * e)
  411. {
  412. return format_expression_prec(e, 9);
  413. }
  414. /****************************************************************************/