yylex.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include <grass/gis.h>
  6. #include "list.h"
  7. #include "mapcalc.h"
  8. #include "yylex.h"
  9. #include "v.mapcalc.tab.h"
  10. /*
  11. * missing features (some might go to bison):
  12. * - quit/exit/bye
  13. * - load script
  14. * - set plugin path
  15. * - describe a function/variable
  16. * - get description for function/variable (help)
  17. * - save current state in a script to be reproducible (vars, descr...)
  18. */
  19. int yylex(void)
  20. {
  21. int c;
  22. while ((c = getchar()) == ' ' || c == '\t' || c == '\n') ;
  23. if (c == EOF)
  24. return 0;
  25. if (c == '.' || isdigit(c)) {
  26. ungetc(c, stdin);
  27. scanf("%lf", &yylval.dbl);
  28. return NUM;
  29. }
  30. if (isalpha(c) || c == '_') {
  31. SYMBOL *s, *sym;
  32. static char *symbuf = 0;
  33. static int length = 0;
  34. int i;
  35. if (length == 0) {
  36. length = 40;
  37. symbuf = (char *)G_malloc(length + 1);
  38. }
  39. i = 0;
  40. do {
  41. if (i == length) {
  42. length *= 2;
  43. symbuf = (char *)G_realloc(symbuf, length + 1);
  44. }
  45. symbuf[i++] = c;
  46. c = getchar();
  47. } while (c != EOF && (isalnum(c) || c == '_'));
  48. ungetc(c, stdin);
  49. symbuf[i] = '\0';
  50. /*
  51. * 1. Check, if it's a known symbol. If so, we know the type.
  52. * 2. Check, if it might be an external function. If so load
  53. * 3. Check, if there is a map with this name
  54. * 4. return as string.
  55. */
  56. sym = (SYMBOL *) listitem(sizeof(SYMBOL));
  57. s = getsym(symbuf);
  58. if (!s) {
  59. sym->v.p = strdup(symbuf);
  60. sym->type = sym->itype = st_str;
  61. yylval.ptr = sym;
  62. return STRING;
  63. }
  64. else {
  65. symcpy(sym, s);
  66. s = sym;
  67. }
  68. yylval.ptr = s;
  69. switch (s->type) {
  70. case st_map:
  71. return MAPVAR;
  72. case st_mfunc:
  73. return MAPFUNC;
  74. case st_pnt:
  75. return PNTVAR;
  76. case st_any:
  77. return ANYVAR;
  78. case st_num:
  79. return NUMVAR;
  80. case st_nfunc:
  81. return NUMFUNC;
  82. case st_afunc:
  83. return ANYFUNC;
  84. case st_pfunc:
  85. return PNTFUNC;
  86. default:
  87. G_fatal_error("Insert more translations here (%d)", s->type);
  88. }
  89. }
  90. return c;
  91. }
  92. void yyerror(const char *msg)
  93. {
  94. fprintf(stdout, "%s\n", msg);
  95. }