summaryrefslogtreecommitdiff
path: root/src/helper.c
blob: 3b02c8133b7b711cfd553ce525531ce83bc5bbeb (plain)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <stdio.h>
#include "helper.h"
#include "big_endian.h"

struct Label
read_label(unsigned char *ptr)
{
  struct Label label = { 0 };

  switch (*ptr) {
  case Plain:
    label.status = Plain;
    break;
  case Packed:
    label.status = Packed;
    break;
  default:
    label.status = Clone;
    label.clone_index = from_big_endian(ptr+1);
    break;
  }

  label.start = from_big_endian(ptr+9);
  label.end = from_big_endian(ptr+17);

  switch (*(ptr+25)) {
  case Terminal:
    label.variant = Terminal;
    break;
  case Nonterminal:
    label.variant = Nonterminal;
    break;
  default:
    label.variant = Rule;
    break;
  }

  label.content = from_big_endian(ptr+26);

  return label;
}

void
print_label(struct Label label)
{
  switch (label.status) {
  case Plain:
    printf("a plain node ");
    break;
  case Packed:
    printf("a packed node ");
    break;
  default:
    printf("a cloned node with index %llu ", label.clone_index);
    break;
  }

  printf("spanning (%llu, %llu) ", label.start, label.end);

  printf("labelled as a ");

  switch (label.variant) {
  case Terminal:
    printf("terminal ");
    break;
  case Nonterminal:
    printf("non-terminal ");
    break;
  default:
    printf("rule ");
    break;
  }

  printf("%llu\n", label.content);
}

void
print_node(struct CForest *forest, uint64_t node)
{
  unsigned char node_ptr[8] = { 0 };

  to_big_endian(node, node_ptr);

  print_forest_node(forest, node_ptr);
}

void print_forest_file(char *filename, char *output_filename)
{
  unsigned char error_vec_len[8] = { 0 };
  unsigned char error_vec_cap[8] = { 0 };

  struct SignedVec error_vec = { 0 };

  error_vec.len = error_vec_len;
  error_vec.capacity = error_vec_cap;

  /* Now read the file into bytes and pack inside a struct UnsignedVec
     and pass to the function. */

  FILE *file = fopen(filename, "r");

  if (file == NULL) {
    fprintf(stderr, "Cannot open file %s\n", filename);

    return;
  }

  fseek(file, 0, SEEK_END);

  uint64_t file_size = ftell(file);

  fseek(file, 0, SEEK_SET);

  unsigned char *file_buffer = malloc(sizeof(unsigned char) * file_size);

  if (file_buffer == NULL) {
    fprintf(stderr, "%s:%d: Cannot allocate %llu memory\n",
            __FILE__, __LINE__,
            file_size);

    return;
  }

  fread(file_buffer, 1, file_size, file);

  fclose(file);

  printf("file size = %llu\n", file_size);

  unsigned char forest_len[8] = { 0 };

  struct UnsignedVec forest = { 0 };

  forest.len = forest_len;

  to_big_endian(file_size, forest.len);

  forest.data = file_buffer;

  print_forest(&forest, &error_vec, output_filename);

  uint64_t error_len = from_big_endian(error_vec.len);

  if (error_len) {
    fprintf(stderr, "error: ");

    for (uint64_t i = 0; i < error_len; i++) {
      fprintf(stderr, "%c", *(error_vec.data+i));
    }

    fprintf(stderr, "\n");

    clean_signed(&error_vec, 4);
  }

  free(file_buffer);
}