Top Level Namespace

Defined Under Namespace

Modules: CodeTools

Instance Method Summary collapse

Instance Method Details

#write_node_types(list) ⇒ Object



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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'ext/rubinius/code/melbourne/node_types.rb', line 122

def write_node_types(list)
  File.open("node_types.cpp", "wb") do |f|
    f.puts <<EOF
/* This file is generated by node_types.rb. Do not edit. */

#include "namespace.h"
#include "node_types.hpp"

#include <stdio.h>

namespace MELBOURNE {
  static const char node_types[] = {
EOF

    indexes = [0]
    list.each do |type|
      f.puts("    \"#{type}\\0\"")
      indexes.push indexes.last + type.size + 1
    end

    f.puts("  };")
    f.puts

    f.puts("  static const unsigned short node_types_offsets[] = {")

    f.puts indexes.map { |i| "    #{i}" }.join(",\n")

    f.puts <<EOF
  };

  const char *get_node_type_string(enum node_type node) {
    if(node < #{list.size}) {
      return node_types + node_types_offsets[node];
    } else {
#define NODE_STRING_MESSAGE_LEN 20
      static char msg[NODE_STRING_MESSAGE_LEN];
      snprintf(msg, NODE_STRING_MESSAGE_LEN, "unknown node type: %d", node);
      return msg;
    }
  }
};
EOF
  end

  File.open("node_types.hpp", "wb") do |f|
    f.puts <<EOF
#ifndef MEL_NODE_TYPES_HPP
#define MEL_NODE_TYPES_HPP
/* This file is generated by node_types.rb. Do not edit. */

namespace MELBOURNE {
  enum node_type {
EOF

  f.puts list.map { |x| "    NODE_#{x.upcase}" }.join(",\n")

  f.puts <<EOF
  };

  const char *get_node_type_string(enum node_type nt);

};

#endif
EOF
  end
end