spearl.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /* spearl/include/spearl/spearl.h
  2. *
  3. * This file is part of spearl.
  4. *
  5. * Spearl 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. * Spearl 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 spearl. If not, see <https://www.gnu.org/licenses/>
  17. */
  18. #ifndef __SPEARL_SPEARL_H
  19. #define __SPEARL_SPEARL_H
  20. #include <spearl/sys.h>
  21. #include <spearl/compiler.h>
  22. #include <spearl/klist.h>
  23. /* user should never use a variable or function that marks with __locally.
  24. * oppositely, they can / should / must use __visible things.
  25. *
  26. * for __locally variables and functions, we use the prefix 'spl' and for
  27. * __visibles, we use 'spearl' and give each of them will be well-commented.
  28. *
  29. * for unmarked symbols (exempli grandia est spearl_object, id didn't marked
  30. * at all), users can use them carefully, but shouldn't modify or depends on
  31. * its behavior. */
  32. #define __locally
  33. #define __visible
  34. extern char __locally *spl_input;
  35. extern int __locally spl_input_size;
  36. extern struct spearl_object __visible root_object;
  37. extern char __visible lbracket, rbracket;
  38. static inline __visible void spearl_setbracket_chars(char l, char r) {
  39. lbracket = l, rbracket = r;
  40. }
  41. static inline __visible void spearl_setbracket(char *s) {
  42. spearl_setbracket_chars(s[0], s[1]);
  43. }
  44. #define MCALL_MNAME '#'
  45. #define MCALL_MARGS '~'
  46. #define MCALL_MDEST '@'
  47. struct spearl_word {
  48. /* pointer to somewhere in spl_input, doesn't terminate with zero */
  49. char *p;
  50. /* length of string, by strlen without + 1 */
  51. int len;
  52. /* the head is in spearl_list */
  53. struct spl_klist_node node;
  54. };
  55. extern struct spl_klist_node __visible wordlist;
  56. #define word_of_node(n) struct_of_node(n, struct spearl_word)
  57. /* the ';' last is not required, just to make it more beautiful. */
  58. spl_make_alloc(struct spearl_word, word);
  59. struct spearl_list {
  60. struct spl_klist_node node;
  61. struct spl_klist_node objs;
  62. int nobjs;
  63. };
  64. #define list_of_node(n) struct_of_node(n, struct spearl_list)
  65. spl_make_alloc(struct spearl_list, list);
  66. enum spl_object_type {
  67. WORD,
  68. LIST,
  69. };
  70. struct spearl_object {
  71. enum spl_object_type type;
  72. struct spl_klist_node node;
  73. union {
  74. struct spearl_word *word;
  75. struct spearl_list *list;
  76. };
  77. };
  78. #define object_of_node(n) struct_of_node(n, struct spearl_object)
  79. spl_make_alloc(struct spearl_object, object);
  80. /* status of scanner, describes the current status. IN if is in
  81. * a quote and OUT if it's OUT of a quote. */
  82. enum spl_state {
  83. IN,
  84. OUT
  85. };
  86. #define spl_state_name() __state_tmp
  87. #define spl_state_level_name() __state_level__tmp
  88. #define spl_make_state() enum spl_state spl_state_name() __unused = OUT; \
  89. int spl_state_level_name() __unused = 0;
  90. #define spl_current_state() spl_state_name()
  91. #define spl_set_state(s) spl_current_state() = s
  92. #define spl_current_state_level() spl_state_level_name()
  93. #define spl_state_in() (spl_current_state() == IN)
  94. #define spl_state_out() (spl_current_state() == OUT)
  95. #define spl_state_left() spl_current_state_level()++
  96. #define spl_state_right() if (!--spl_current_state_level()) spl_set_state(OUT)
  97. struct spearl_mcall_arg {
  98. enum spl_object_type type;
  99. struct spearl_object *object;
  100. union {
  101. /* zero-terminated */
  102. char *p;
  103. struct spearl_list *list;
  104. };
  105. struct spl_klist_node node;
  106. };
  107. spl_make_alloc(struct spearl_mcall_arg, arg);
  108. struct spearl_mcall {
  109. struct spearl_list *list;
  110. struct spearl_object *dest;
  111. struct spearl_object *mname;
  112. struct spl_klist_node args;
  113. };
  114. spl_make_alloc(struct spearl_mcall, mcall);
  115. enum spl_macro_type {
  116. USER,
  117. BUILTIN,
  118. };
  119. typedef struct spearl_list *spearl_builtin_fn_t (struct spearl_mcall *);
  120. struct spearl_builtin {
  121. char *name;
  122. spearl_builtin_fn_t *do_builtin;
  123. };
  124. struct spearl_macro {
  125. enum spl_macro_type type;
  126. char *name;
  127. union {
  128. struct {
  129. struct spearl_object
  130. *eval,
  131. *ins;
  132. };
  133. struct spearl_builtin builtin;
  134. };
  135. struct spl_klist_node node;
  136. };
  137. #define macro_of_node(n) struct_of_node(n, struct spearl_macro)
  138. spl_make_alloc(struct spearl_macro, macro);
  139. extern struct spl_klist_node __visible spl_macros;
  140. /* initialize the library, or reset */
  141. int __visible spearl_init(void);
  142. /* join a string to spl_input and scan, join into spearl_list */
  143. int __visible spearl_join(char *s);
  144. /* define a new builtin */
  145. struct spearl_macro *spearl_define_builtin(char *name, spearl_builtin_fn_t fn);
  146. int __locally spl_builtin_init(void);
  147. int expand_builtin_macro(struct spearl_macro *, struct spearl_mcall *, struct spl_klist_node *);
  148. #endif