Krishna,

It's still a work in progess (it doesn't produce gst code that will cleanly 
fileIn yet,) but I've attached what I've got so far.  I just followed the 
instructions on the swig site on how to incorporate new language to integrate 
it into the swig development code.

Regards,

Brad Watson

----- Original Message ----
From: Krishna <[EMAIL PROTECTED]>
To: Brad Watson <[EMAIL PROTECTED]>
Cc: [email protected]
Sent: Friday, December 1, 2006 9:25:28 AM
Subject: Re: [Help-smalltalk] glade2

Brad,

Tried wrapping FLTK? It has a much simpler and uniform callback
interface. You can find the SWIG interface files here:

http://pyfltk.svn.sourceforge.net/viewvc/pyfltk/trunk/pyfltk/swig/

Right now I'm stuck in windoze land otherwise I'll try it myself (is
your swigging code available somewhere?)

Cheers,
Krishna
-- 
I long to accomplish a great and noble task, but it is my chief duty
to accomplish small tasks as if they were great and noble !
- Helen Keller





 
____________________________________________________________________________________
Want to start your own business?
Learn how on Yahoo! Small Business.
http://smallbusiness.yahoo.com/r-index
/* -----------------------------------------------------------------------------
 * See the LICENSE file for information on copyright, usage and redistribution
 * of SWIG, and the README file for authors - http://www.swig.org/release.html.
 *
 * gst.cxx
 *
 * gst language module for SWIG.
 * ----------------------------------------------------------------------------- */

/* rbw */
char cvsroot_gst_cxx[] = "$Header: /cvsroot/swig/SWIG/Source/Modules/gst.cxx,v 1.12 2006/11/28 10:19:50 rbwatson Exp $";

#include "swigmod.h"
#include <ctype.h>
#include <limits.h>

class GST:public Language {
public:
  File *f_st;
  String *module;
  virtual void main(int argc, char *argv[]);
  virtual int top(Node *n);
  virtual int functionWrapper(Node *n);
  virtual int constantWrapper(Node *n);
  virtual int classDeclaration(Node *n);
  virtual int enumDeclaration(Node *n);
private:
  String *get_ffi_type(SwigType *ty);
  String *convert_literal(String *num_param, String *type);
  String *strip_parens(String *string);
  String *camel_case(bool, String *string);
  int extern_all_flag;
  int pseudo_enums_flag;
  int cstructs_flag;
  int is_function;
};

//converted 
void GST::main(int argc, char *argv[]) {
  int i;

  SWIG_library_directory("gst");
  SWIG_config_file("gst.swg");
  extern_all_flag = 0;
  pseudo_enums_flag = 0;
  cstructs_flag = 0;

  for (i = 1; i < argc; i++) {

    if (!strcmp(argv[i], "-help")) {
      Printf(stdout, "gst Options (available with -gst)\n");
      Printf(stdout,
	     " -extern-all\n"
	     "\t If this option is given then gst definitions for all the functions\n"
	     "will be created otherwise only definitions for externed functions\n"
	     "are created.\n"
	     " -generate-pseudo-enums\n"
	     "\t If this option is given then methods will be created for enum\n"
	     "names and values.\n"
	     " -generate-cstructs\n"
	     "\t If this option is given then cstructs will be created.\n");

    } else if ((Strcmp(argv[i], "-extern-all") == 0)) {
      extern_all_flag = 1;
      Swig_mark_arg(i);
    }

    else if ((Strcmp(argv[i], "-generate-pseudo-enums") == 0)) {
      pseudo_enums_flag = 1;
      Swig_mark_arg(i);
    }

    else if ((Strcmp(argv[i], "-generate-cstructs") == 0)) {
      cstructs_flag = 1;
      Swig_mark_arg(i);
    }

  }
}

int GST::top(Node *n) {

  File *f_null = NewString("");
  /* Get the output file name */
  String *outfile = Getattr(n, "outfile");
  String *output_filename;

  module = camel_case(true, Getattr(n, "name"));

  if (!outfile)
    output_filename = outfile;
  else {
    output_filename = NewString("");
    Printf(output_filename, "%s%s.st", SWIG_output_directory(),Getattr(n, "name"));
  }

  f_st = NewFile(output_filename, "w+");

  if (!f_st) {
    FileErrorDisplay(output_filename);
    SWIG_exit(EXIT_FAILURE);
  }

  Swig_register_filebyname("header", f_null);
  Swig_register_filebyname("runtime", f_null);
  Swig_register_filebyname("wrapper", f_null);
  String *header =
      NewStringf
      ("\"This is an automatically generated file. Make changes as you feel are necessary\"\n\"(but remember that if you try to regenerate this file, your changes will be lost.)\" \n\nObject subclass: #%s\n    instanceVariableNames: \'\'\n    classVariableNames: \'\'\n    poolDictionaries: \'\'\n    category: \'%s wrapper\'!\n!\n\nDLD addLibrary: \'+Libraryname\'!\n",
       module, module);

  Language::top(n);
  long len = Tell(f_st);
  Printf(f_st, "%s", header);
  long end = Tell(f_st);

  for (len--; len >= 0; len--) {
    end--;
    Seek(f_st, len, SEEK_SET);
    int ch = Getc(f_st);
    Seek(f_st, end, SEEK_SET);
    Putc(ch, f_st);
  }

  Seek(f_st, 0, SEEK_SET);
  Write(f_st, Char(header), Len(header));
  Close(f_st);
  Delete(f_st);
  return SWIG_OK;
}


int GST::functionWrapper(Node *n) {
  is_function = 1;
  String *storage = Getattr(n, "storage");
  if (!extern_all_flag && (!storage || (Strcmp(storage, "extern") && Strcmp(storage, "externc"))))
    return SWIG_OK;

  String *func_name = Getattr(n, "sym:name");
  ParmList *pl = Getattr(n, "parms");
  int argnum = 0, first = 1;

  //  Printf(f_st, "\n!%s class methodsFor: \'C call-outs\'!\n", module);
  Printf(f_st, "\n%s", camel_case(false, func_name));

  for (Parm *p = pl; p; p = nextSibling(p), argnum++) {
    String *argname = Getattr(p, "name");
    String *ffitype = get_ffi_type(Getattr(p, "type"));
    int tempargname = 0;

    if (!argname) {
      argname = NewStringf("arg%d", argnum);
      tempargname = 1;
    }

    if(first) {
      Printf(f_st, ": a%s%d ", camel_case(true, ffitype), argnum);
      first = 0;
    }
    else {
      Printf(f_st, "%s: a%s%d ", camel_case(false, argname), camel_case(true, ffitype), argnum);
    }
    
    Delete(ffitype);

    if (tempargname)
      Delete(argname);
  }

  Printf(f_st, "\n    <cCall: \'%s\' returning: #", func_name);
  String *ffitype = get_ffi_type(Getattr(n, "type"));

  if (Strcmp(ffitype, "NIL")) {	//when return type is not nil
    Printf(f_st, "%s ", ffitype);
  }
  else {
    Printf(f_st, "void ");
  }

  Printf(f_st, "\n        args: #(");

  for (Parm *p = pl; p; p = nextSibling(p), argnum++) {
    String *ffitype = get_ffi_type(Getattr(p, "type"));
    Printf(f_st, "#%s ",  ffitype);
    Delete(ffitype);
  }

  Printf(f_st, " )>!\n");
  return SWIG_OK;
}

int GST::constantWrapper(Node *n) {
  is_function = 0;
  String *type = Getattr(n, "type");
  String *converted_value = convert_literal(Getattr(n, "value"), type);
  String *name = Getattr(n, "sym:name");

  //  Printf(f_st, "\n!%s class methodsFor: 'Constants'!\n", module);
  Replace(converted_value, "\"", "\'", DOH_REPLACE_ANY);
  Printf(f_st, "\n%s\n    ^%s!\n", camel_case(false, name), converted_value);
  Delete(converted_value);
  return SWIG_OK;
}

int GST::enumDeclaration(Node *n) {
  is_function = 0;
  int count = 0;

  if(!pseudo_enums_flag)
    return SWIG_OK;

  //  Printf(f_st, "\n!%s class methodsFor: 'Constants'!\n", module);
  Printf(f_st, "\n\"Start enum analogue\"\n");    

  for (Node *c = firstChild(n); c; c = nextSibling(c)) {
    String *slot_name = Getattr(c, "name");
    String *slot_value = Getattr(c, "enumvalue");
    int len = Len(slot_value);

    if(len == 0) {
      Printf(f_st, "\n%s\n    ^%d!\n", camel_case(false, slot_name), count);
      ++count;
    } else {
      Printf(f_st, "\n%s\n    ^$%s!\n", camel_case(false, slot_name), slot_value);
    }

    Delete(slot_name);
    Delete(slot_value);
  }

  Printf(f_st, "\n\"End enum analogue\"\n");  
  return SWIG_OK;
}

// Includes structs
int GST::classDeclaration(Node *n) {
  is_function = 0;

  if(!cstructs_flag)
    return SWIG_OK;

  String *name = Getattr(n, "sym:name");
  String *kind = Getattr(n, "kind");

  if (Strcmp(kind, "struct")) {
    Printf(stderr, "Don't know how to deal with %s kind of class yet.\n", kind);
    Printf(stderr, " (name: %s)\n", name);
  //SWIG_exit(EXIT_FAILURE);
  }


  Printf(f_st, "\nCStruct subclass: #%s\n", camel_case(true, name));
  Printf(f_st, "    declaration: #(");

  for (Node *c = firstChild(n); c; c = nextSibling(c)) {

    if (Strcmp(nodeType(c), "cdecl")) {
      Printf(stderr, "Structure %s has a slot that we can't deal with.\n", name);
      Printf(stderr, "nodeType: %s, name: %s, type: %s\n", nodeType(c), Getattr(c, "name"), Getattr(c, "type"));
      //SWIG_exit(EXIT_FAILURE);
      Printf(f_st, ")>!\n");
      return SWIG_OK; /* xxx */
    }

    String *temp = Copy(Getattr(c, "decl"));
    Append(temp, Getattr(c, "type"));	//appending type to the end, otherwise wrong type
    String *lisp_type = get_ffi_type(temp);
    Delete(temp);

    String *slot_name = Getattr(c, "sym:name");
    Printf(f_st, "\n        (#%s #%s)", camel_case(false, slot_name), lisp_type);
    Delete(lisp_type);  
  }

  Printf(f_st, ")\n");
  Printf(f_st, "    classVariableNames: \'\'\n");
  Printf(f_st, "    poolDictionaries: \'\'\n");
  Printf(f_st, "    category: \'%s Wrapper\'!\n", camel_case(true, name));
  
return SWIG_OK;
}

/* utilities */
/* returns new string w/ parens stripped */
String *GST::strip_parens(String *string) {
  char *s = Char(string), *p;
  int len = Len(string);
  String *res;

  if (len == 0 || s[0] != '(' || s[len - 1] != ')') {
    return NewString(string);
  }

  p = (char *) malloc(len - 2 + 1);

  if (!p) {
    Printf(stderr, "Malloc failed\n");
    SWIG_exit(EXIT_FAILURE);
  }

  strncpy(p, s + 1, len - 1);
  p[len - 2] = 0;		/* null terminate */

  res = NewString(p);
  free(p);
  return res;
}

/* returns a camel-case string */
String *GST::camel_case(bool isclass, String *string) {
  char *s = Char(Swig_string_lower(string)), *p;
  String *cb;
  int i = 0, j = 0, hump = 0;

  p = (char *) malloc(Len(string) + 1);

  if (!p) {
    Printf(stderr, "Malloc failed\n");
    SWIG_exit(EXIT_FAILURE);
  }

  for( ; s[i]; i++ ) {

    if(s[i] == '_') {
      hump = 1;
    } else {
      if(hump) {
	p[j] = toupper(s[i]);
	hump = 0;
      }
      else {
	p[j] = s[i];
      }
      j++;
    }
  }
  p[j] = '\0';

  if(isclass == true)
    p[0] = toupper(p[0]);
  else
    p[0] = tolower(p[0]);

  cb = NewString(p);
  free(p);
  return cb;
}

String *GST::convert_literal(String *num_param, String *type) {
  String *num = strip_parens(num_param), *res;
  char *s = Char(num);

  /* Make sure doubles use 'd' instead of 'e' */
  if (!Strcmp(type, "double")) {
    String *updated = Copy(num);

    if (Replace(updated, "e", "d", DOH_REPLACE_ANY) > 1) {
      Printf(stderr, "Weird!! number %s looks invalid.\n", num);
      SWIG_exit(EXIT_FAILURE);
    }

    Delete(num);
    return updated;
  }

  if (SwigType_type(type) == T_CHAR) {
    /* Use GST syntax for character literals */
    return NewStringf("$%s", num_param);
  } else if (SwigType_type(type) == T_STRING) {
    /* Use GST syntax for string literals */
    return NewStringf("\'%s\'", num_param);
  }

  if (Len(num) < 2 || s[0] != '0') {
    return num;
  }

  /* octal or hex */
  res = NewStringf("%s%s", s[1] == 'x' ? "16r" : "8r", s + 2);
  Delete(num);
  return res;
}

String *GST::get_ffi_type(SwigType *ty) {
  Hash *typemap = Swig_typemap_search("in", ty, "", 0);

  if (typemap) {
    String *typespec = Getattr(typemap, "code");
    return NewString(typespec);
  } else if (SwigType_ispointer(ty)) {
    SwigType *cp = Copy(ty);
    SwigType_del_pointer(cp);
    String *inner_type = get_ffi_type(cp);

    if (SwigType_isfunction(cp)) {
      return inner_type;
    }

    String *str = NewStringf("cObject");

    Delete(cp);
    Delete(inner_type);
    return str;
  } else if (SwigType_isarray(ty)) {
    SwigType *cp = Copy(ty);
    String *array_dim = SwigType_array_getdim(ty, 0);

    if (!Strcmp(array_dim, "")) {	//dimension less array convert to pointer
      Delete(array_dim);
      SwigType_del_array(cp);
      SwigType_add_pointer(cp);
      String *str = get_ffi_type(cp);
      Delete(cp);
      return str;
    } else {
      SwigType_pop_arrays(cp);
      String *inner_type = get_ffi_type(cp);
      Delete(cp);

      int ndim = SwigType_array_ndim(ty);
      String *dimension;

      if (ndim == 1) {
	dimension = array_dim;
      } else {
	dimension = array_dim;

	for (int i = 1; i < ndim; i++) {
	  array_dim = SwigType_array_getdim(ty, i);
	  Append(dimension, " ");
	  Append(dimension, array_dim);
	  Delete(array_dim);
	}

	String *temp = dimension;
	dimension = NewStringf("(%s)", dimension);
	Delete(temp);
      }

      String *str;

      if (is_function)
	// str = NewStringf("(ffi:c-ptr (ffi:c-array %s %s))", inner_type, dimension);
      	str = NewStringf("cObject");
      else
	// str = NewStringf("(ffi:c-array %s %s)", inner_type, dimension);
	str = NewStringf("cObjectPtr");

      Delete(inner_type);
      Delete(dimension);
      return str;
    }
  } else if (SwigType_isfunction(ty)) {
    SwigType *cp = Copy(ty);
    SwigType *fn = SwigType_pop_function(cp);
    String *args = NewString("");
    ParmList *pl = SwigType_function_parms(fn);

    if (ParmList_len(pl) != 0) {
      Printf(args, "(:arguments ");
    }

    int argnum = 0, first = 1;

    for (Parm *p = pl; p; p = nextSibling(p), argnum++) {
      String *argname = Getattr(p, "name");
      SwigType *argtype = Getattr(p, "type");
      String *ffitype = get_ffi_type(argtype);
      int tempargname = 0;

      if (!argname) {
	argname = NewStringf("arg%d", argnum);
	tempargname = 1;
      }

      if (!first) {
	Printf(args, "\n\t\t");
      }

      Printf(args, "(%s %s)", argname, ffitype);
      first = 0;
      Delete(ffitype);

      if (tempargname)
	Delete(argname);
    }

    if (ParmList_len(pl) != 0) {
      Printf(args, ")\n");	/* finish arg list */
    }

    String *ffitype = get_ffi_type(cp);
    String *str = NewStringf("(ffi:c-function %s \t\t\t\t(:return-type %s))", args, ffitype);
    Delete(fn);
    Delete(args);
    Delete(cp);
    Delete(ffitype);
    return str;
  }

  String *str = SwigType_str(ty, 0);

  if (str) {
    char *st = Strstr(str, "struct");

    if (st) {
      st += 7;
      return NewString(st);
    }

    char *cl = Strstr(str, "class");

    if (cl) {
      cl += 6;
      return NewString(cl);
    }

  }
  return str;
}

extern "C" Language *swig_gst(void) {
  return new GST();
}

Attachment: gst.swg
Description: Binary data

_______________________________________________
help-smalltalk mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/help-smalltalk

Reply via email to