def field_accessor(owning_type, type, name, nullable, plural):
  preface =  '  struct %s *node;\n' % struct_name(owning_type)
  preface += '  TypedData_Get_Struct(self, struct %s, &%s_type, node);\n' % (
            struct_name(owning_type), snake(owning_type))

  call_get = "%s_get_%s" % (struct_name(owning_type), snake(name))
  func = '%s_get_%s' % (snake(owning_type), snake(name))
  ret = '  return '

  if plural:
    func += '_size'
    call_get += '_size(node)'
    ret += 'INT2FIX(%s)' % call_get
  else:
    call_get += '(node)'
    if nullable:
      preface += '  if (%s == NULL) return Qnil;\n' % call_get
    if type in ['string', 'OperationKind']:
      ret += 'rb_str_new_cstr(%s)' % call_get
    elif type in ['boolean']:
      ret += 'INT2BOOL(%s)' % call_get
    else:
      ret += 'TypedData_Wrap_Struct(%s_class, &%s_type, (void*)%s)' % (
          snake(type), snake(type), call_get)

  return 'static VALUE ' + func + '(VALUE self) {\n' + preface + ret + ';\n}\n'
def field_accessor(owning_type, type, name, nullable, plural):
    preface = '  struct %s *node;\n' % struct_name(owning_type)
    preface += '  TypedData_Get_Struct(self, struct %s, &%s_type, node);\n' % (
        struct_name(owning_type), snake(owning_type))

    call_get = "%s_get_%s" % (struct_name(owning_type), snake(name))
    func = '%s_get_%s' % (snake(owning_type), snake(name))
    ret = '  return '

    if plural:
        func += '_size'
        call_get += '_size(node)'
        ret += 'INT2FIX(%s)' % call_get
    else:
        call_get += '(node)'
        if nullable:
            preface += '  if (%s == NULL) return Qnil;\n' % call_get
        if type in ['string', 'OperationKind']:
            ret += 'rb_str_new_cstr(%s)' % call_get
        elif type in ['boolean']:
            ret += 'INT2BOOL(%s)' % call_get
        else:
            ret += 'TypedData_Wrap_Struct(%s_class, &%s_type, (void*)%s)' % (
                snake(type), snake(type), call_get)

    return 'static VALUE ' + func + '(VALUE self) {\n' + preface + ret + ';\n}\n'
  def start_type(self, name):
    # define the values declared 'extern' in the header
    print 'const rb_data_type_t %s_type = { "%s", {}, };' % (
            snake(name), struct_name(name))
    print 'VALUE %s_class;' % snake(name)
    print 'ID visit_%s_id;' % snake(name)
    print 'ID end_visit_%s_id;' % snake(name)

    # build the two visitor methods
    print type_visitor(name)
    print type_end_visitor(name)

    # init the class
    self._class_init_buffer += '  %s_class = rb_define_class_under(module, "%s", node_class);\n' % (
            snake(name), name)

    # init the symbols
    self._sym_init_buffer += '  visit_%s_id = rb_intern("visit_%s");\n' % (
        snake(name), snake(name))
    self._sym_init_buffer += '  end_visit_%s_id = rb_intern("end_visit_%s");\n' % (
        snake(name), snake(name))

    # init the visitor struct
    self._cbs_init_buffer += '  cbs.visit_%s = visit_%s;\n' % (
            snake(name), snake(name))
    self._cbs_init_buffer += '  cbs.end_visit_%s = end_visit_%s;\n' % (
            snake(name), snake(name))
    self._current_type = name
    def start_type(self, name):
        # define the values declared 'extern' in the header
        print 'const rb_data_type_t %s_type = { "%s", {}, };' % (
            snake(name), struct_name(name))
        print 'VALUE %s_class;' % snake(name)
        print 'ID visit_%s_id;' % snake(name)
        print 'ID end_visit_%s_id;' % snake(name)

        # build the two visitor methods
        print type_visitor(name)
        print type_end_visitor(name)

        # init the class
        self._class_init_buffer += '  %s_class = rb_define_class_under(module, "%s", node_class);\n' % (
            snake(name), name)

        # init the symbols
        self._sym_init_buffer += '  visit_%s_id = rb_intern("visit_%s");\n' % (
            snake(name), snake(name))
        self._sym_init_buffer += '  end_visit_%s_id = rb_intern("end_visit_%s");\n' % (
            snake(name), snake(name))

        # init the visitor struct
        self._cbs_init_buffer += '  cbs.visit_%s = visit_%s;\n' % (snake(name),
                                                                   snake(name))
        self._cbs_init_buffer += '  cbs.end_visit_%s = end_visit_%s;\n' % (
            snake(name), snake(name))
        self._current_type = name
Beispiel #5
0
  def field(self, type, name, nullable, plural):
    print field_prototype(self._current_type, type, name, nullable, plural) + ' {'
    print '  const auto *realNode = reinterpret_cast<const %s *>(node);' % self._current_type
    title_name = title(name)
    call_get = 'realNode->get%s()' % title_name
    if plural:
      if nullable:
        print '  return %s ? %s->size() : 0;' % (call_get, call_get)
      else:
        print '  return %s.size();' % call_get
    else:
      if type in ['string', 'OperationKind', 'boolean']:
        print '  return %s;' % call_get
      else:
        fmt = '  return reinterpret_cast<const struct %s *>(%s%s);'
        print fmt % (struct_name(type), '' if nullable else '&', call_get)

    print '}'
Beispiel #6
0
  def field(self, type, name, nullable, plural):
    print field_prototype(self._current_type, type, name, nullable, plural) + ' {'
    print '  const auto *realNode = (const %s *)node;' % self._current_type
    title_name = title(name)
    call_get = 'realNode->get%s()' % title_name
    if plural:
      if nullable:
        print '  return %s ? %s->size() : 0;' % (call_get, call_get)
      else:
        print '  return %s.size();' % call_get
    else:
      if type in ['string', 'OperationKind', 'boolean']:
        print '  return %s;' % call_get
      else:
        fmt = '  return (const struct %s *)%s%s;'
        print fmt % (struct_name(type), '' if nullable else '&', call_get)

    print '}'
 def start_union(self, name):
   print 'struct ' + struct_name(name) + ';'
   print 'extern const rb_data_type_t %s_type;' % snake(name)
   print 'extern VALUE %s_class;' % snake(name)
 def start_type(self, name):
   print 'struct ' + struct_name(name) + ';'
   print 'extern const rb_data_type_t %s_type;' % snake(name)
   print 'extern VALUE %s_class;' % snake(name)
   print 'extern ID visit_%s_id;' % snake(name)
   print 'extern ID end_visit_%s_id;' % snake(name)
 def start_union(self, name):
   print 'const rb_data_type_t %s_type = { "%s", {}, };' % (snake(name), struct_name(name))
   print 'VALUE %s_class;' % snake(name)
 def start_union(self, name):
     print 'const rb_data_type_t %s_type = { "%s", {}, };' % (
         snake(name), struct_name(name))
     print 'VALUE %s_class;' % snake(name)
Beispiel #11
0
 def start_union(self, name):
     print 'struct ' + struct_name(name) + ';'
     print 'extern const rb_data_type_t %s_type;' % snake(name)
     print 'extern VALUE %s_class;' % snake(name)
Beispiel #12
0
 def start_type(self, name):
     print 'struct ' + struct_name(name) + ';'
     print 'extern const rb_data_type_t %s_type;' % snake(name)
     print 'extern VALUE %s_class;' % snake(name)
     print 'extern ID visit_%s_id;' % snake(name)
     print 'extern ID end_visit_%s_id;' % snake(name)