aboutsummaryrefslogtreecommitdiff
path: root/src/ast_dump.c
blob: 0224c099dcce848f8450ceaadc137813bf66c7bc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "all.h"
#include <stddef.h>
#include <stdio.h>

static const char *type_name(ast_type_e type) {
  switch (type) {
  case AST_INC:
    return "INC";
  case AST_DEC:
    return "DEC";
  case AST_PTR_INC:
    return "PTR_INC";
  case AST_PTR_DEC:
    return "PTR_DEC";
  case AST_OUT:
    return "OUT";
  case AST_IN:
    return "IN";
  case AST_LOOP:
    return "LOOP";
  case AST_CLEAR:
    return "CLEAR";
  default:
    return "?";
  }
}

void ast_dump(ast_node_t *node, int depth) {
  while (node) {

    for (int i = 0; i < depth; i++)
      fprintf(stderr, "  ");

    fprintf(stderr, "%s(%dx / %zu len)", type_name(node->type), node->val,
            node->children.len);

    switch (node->type) {
    case AST_INC:
    case AST_DEC:
    case AST_PTR_INC:
    case AST_PTR_DEC:
      break;

    default:
      break;
    }

    fprintf(stderr, "\n");

    if (node->type == AST_LOOP) {

      for (int i = 0; i < depth; i++)
        fprintf(stderr, "  ");

      fprintf(stderr, "{\n");

      for (int i = 0; i < node->children.len; i++)
        ast_dump(node->children._data[i], depth + 1);

      for (size_t i = 0; i < depth; i++)
        fprintf(stderr, "  ");

      fprintf(stderr, "}\n");
    }

    node = node->next;
  }
}