A dynamic tracer for Linux

node.h 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #ifndef _PLY_NODE_H
  2. #define _PLY_NODE_H
  3. #include <stdint.h>
  4. #include <stdio.h>
  5. #include "sym.h"
  6. #include "type.h"
  7. typedef struct node node_t;
  8. typedef enum kwclass {
  9. KW_SUBSCRIPT = '[',
  10. KW_ASSIGN = '=',
  11. KW_BINOP = 'b',
  12. } kwclass_t;
  13. typedef enum kwarith {
  14. KW_ADD = '+',
  15. KW_SUB = '-',
  16. KW_MUL = '*',
  17. KW_DIV = '/',
  18. } kwarith_t;
  19. typedef struct keyword {
  20. kwclass_t class;
  21. int op;
  22. } keyword_t;
  23. typedef enum ntype {
  24. N_LIST,
  25. N_KEYWORD,
  26. N_IDENT,
  27. N_NUM,
  28. N_STRING,
  29. } ntype_t;
  30. struct node {
  31. node_t *next, *prev, *up;
  32. ntype_t ntype;
  33. union {
  34. node_t *list;
  35. keyword_t keyword;
  36. const char *ident;
  37. int64_t num;
  38. const char *string;
  39. };
  40. type_t *type;
  41. sym_t *sym;
  42. };
  43. /* debug */
  44. void node_print(node_t *n, FILE *fp);
  45. void node_dump (node_t *n, FILE *fp);
  46. typedef int (*walk_fn)(node_t *, void *);
  47. int node_walk(node_t *n, walk_fn pre, walk_fn post, void *ctx);
  48. int node_replace(node_t *n, node_t *new);
  49. /* constructors */
  50. node_t *node_list (node_t *head);
  51. node_t *node_vlist (node_t *head, ...);
  52. node_t *node_keyword(kwclass_t class, int op);
  53. node_t *node_ident (const char *name);
  54. node_t *node_num (int64_t num);
  55. node_t *node_string (const char *string);
  56. static inline node_t *node_head(node_t *n)
  57. {
  58. if (!n)
  59. return NULL;
  60. for (; n->prev; n = n->prev);
  61. return n;
  62. }
  63. static inline node_t *node_prev(node_t *n)
  64. {
  65. return n ? n->prev : NULL;
  66. }
  67. static inline node_t *node_next(node_t *n)
  68. {
  69. return n ? n->next : NULL;
  70. }
  71. static inline node_t *node_up(node_t *n)
  72. {
  73. return n ? n->up : NULL;
  74. }
  75. #endif /* _PLY_NODE_H */