cstyles.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /* tripl/arch/cstyles.c
  2. *
  3. * This file is part of tripl.
  4. *
  5. * tripl is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * tripl is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with tripl. If not, see <https://www.gnu.org/licenses/>
  17. */
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <sys/types.h>
  21. #include <string.h>
  22. #include <errno.h>
  23. #include <klist.h>
  24. #include <tripl/tripl.h>
  25. #include <tripl/limits.h>
  26. #include <tripl/arch.h>
  27. /* compatible of c-style function defines */
  28. /* arch/arch_templ.c */
  29. int name_defined(char *name);
  30. extern struct klist_node names_list;
  31. extern char *psave;
  32. const static char *c_keywords[] = {
  33. "char", "double", "enum", "float",
  34. "int", "long", "short", "signed",
  35. "struct", "union", "unsigned", "void",
  36. "for", "do", "while", "break",
  37. "continue",
  38. "if", "else", "goto",
  39. "switch", "case", "default",
  40. "return",
  41. "auto", "extern", "register", "static",
  42. "const", "sizeof", "typedef", "volatile",
  43. "inline", "restrict", "_Bool", "_Complex",
  44. "_Imaginary"
  45. };
  46. static int iskeyword(char *p, int l) {
  47. int i;
  48. for (i = 0; i < arrlen(c_keywords); i++)
  49. if (!strncmp(c_keywords[i], p, l))
  50. return 1;
  51. return 0;
  52. }
  53. __arch_templ int cstyle_declare(char *p, struct arch *arch_fn) {
  54. /* return-type function-name ( argument-type-1 argument-name, ...)
  55. *
  56. * and the preprocess is MUST done, so only keywords are before functions.
  57. * and an important thing is, we will do some check with the c-style declares. */
  58. struct klist_node *n;
  59. int type, out, nargs = 0;
  60. char *fname;
  61. /* get function name */
  62. _skip_spaces(p);
  63. for (;;) {
  64. getword(p, &type, &out);
  65. if (type == WORDTYPE_NAME) {
  66. if (iskeyword(p, out)) {
  67. p += out;
  68. _skip_spaces(p);
  69. continue;
  70. }
  71. }
  72. else if (type == WORDTYPE_KWORD)
  73. if (out == '*') {
  74. p++;
  75. _skip_spaces(p);
  76. continue;
  77. }
  78. /* now we found the name */
  79. break;
  80. }
  81. fname = strndup(p, out);
  82. p += out;
  83. _skip_spaces(p);
  84. getword(p, &type, &out);
  85. if (unlikely(type != WORDTYPE_KWORD || out != '('))
  86. /* not a c-style declare */
  87. return 0;
  88. p++;
  89. _skip_spaces(p);
  90. while (getword(p, &type, &out)) {
  91. switch (type) {
  92. case WORDTYPE_NAME:
  93. if (!iskeyword(p, out))
  94. nargs++;
  95. p += out;
  96. _skip_spaces(p);
  97. break;
  98. case WORDTYPE_KWORD:
  99. p++;
  100. _skip_spaces(p);
  101. break;
  102. case WORDTYPE_NR:
  103. default:
  104. exit(generic_error("invalid c-style declare: are you kidding me? ", psave, lno));
  105. }
  106. }
  107. /* join to the function-list */
  108. n = malloc(sizeof(struct klist_node));
  109. klist_init(n);
  110. if (!nargs)
  111. n->value = DECLARE_FN | DECLARE_NOARG;
  112. else
  113. n->value = DECLARE_FN | nargs;
  114. n->payload = fname;
  115. if (name_defined(n->payload)) {
  116. error((char *) n->payload);
  117. exit(generic_error("name already defined", psave, lno));
  118. }
  119. klist_add(&names_list, n);
  120. return 1;
  121. }