expression.c 11 KB

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