PERFORCE change 104173 for review

Spencer Whitman swhitman at FreeBSD.org
Wed Aug 16 02:28:33 UTC 2006


http://perforce.freebsd.org/chv.cgi?CH=104173

Change 104173 by swhitman at swhitman_joethecat on 2006/08/16 02:28:08

	Work on macro replacment and macro argument replacment.  Fixed a bug 
	with adding macro arguments.

Affected files ...

.. //depot/projects/soc2006/swhitman-K_Kernel_Meta-Language/k/cpp.c#17 edit

Differences ...

==== //depot/projects/soc2006/swhitman-K_Kernel_Meta-Language/k/cpp.c#17 (text+ko) ====

@@ -45,45 +45,48 @@
 };
 
 
-/* XXX This should be optimized, but works for now */
-struct macro_arg {
-  const char               *name;           /* Argument name */
-  int                      length;           /* Length of the arg name */
-  int                      *offsets;        /* Offset list */
-  TAILQ_ENTRY(macro_arg)   list;            /* Entry into the argument list */
-};
+/* XXX This should be optimized, but works for now */ 
+struct macro_arg { 
+  const char               *name;           /* Argument name */ 
+  const char               *value;          /* Current value to replace arg with */
+  int                      name_length;     /* Length of the arg name */ 
+  int                      val_length;      /* Length of current value for arg */
+  int                      *offsets;        /* Offset list */ 
+  TAILQ_ENTRY(macro_arg)   list;            /* Entry into the argument list */ 
+}; 
+
+#define OBJ_MAC_TYPE   0/* For object like macros (no paraens, no arguments) */ 
+#define FUNC_MAC_TYPE  1/* For function like macros (paraens and arguments) */ 
+#define NONE_MAC_TYPE -1/* Inital value.  Indecates type was not set */ 
 
-#define OBJ_MAC_TYPE   0/* For object like macros (no paraens, no arguments) */
-#define FUNC_MAC_TYPE  1/* For function like macros (paraens and arguments) */
-#define NONE_MAC_TYPE -1/* Inital value.  Indecates type was not set */
-struct define {
-  const char                     *name;    /* Name of the macro */
-  int                            mac_type; /* Object or function like macro */
-  TAILQ_HEAD(,macro_arg)         args;     /* Head of argument list */
-  const char                     *value;   /* Value of the macro */
-  int                            length;   /* Length of the macro value */
-  TAILQ_ENTRY(define)            list;     /* Link to list of macros */
-};
+struct define { 
+  const char                     *name;    /* Name of the macro */ 
+  int                            mac_type; /* Object or function like macro */ 
+  TAILQ_HEAD(,macro_arg)         args;     /* Head of argument list */ 
+  const char                     *value;   /* Value of the macro */ 
+  int                            length;   /* Length of the macro value */ 
+  TAILQ_ENTRY(define)            list;     /* Link to list of macros */ 
+}; 
 
-/* XXX Free const char * name value? */
+/* XXX Free const char * name? */ 
 #define FREE_ARG_LIST(DEF)						\
-  do {									\
-    struct macro_arg * tmpvar = NULL;					\
-    for(; !TAILQ_EMPTY(&(DEF)->args); tmpvar = TAILQ_FIRST(&(DEF)->args)) { \
-      TAILQ_REMOVE(&(DEF)->args, tmpvar, list);				\
-      free(tmpvar->offsets);						\
-      free(tmpvar);							\
-    }									\
-  } while(0)
+do {									\
+  struct macro_arg * tmpvar = NULL;					\
+  for(; !TAILQ_EMPTY(&(DEF)->args); tmpvar = TAILQ_FIRST(&(DEF)->args)) { \
+    TAILQ_REMOVE(&(DEF)->args, tmpvar, list);				\
+    free(tmpvar->offsets);						\
+    free(tmpvar);							\
+  }									\
+ } while(0) 
 
-/* XXX Free const char * name value? */
-/* XXX Free const char * value value? */
+/* XXX Free const char * name? */ 
+/* XXX Free const char * value? */ 
 #define FREE_DEF(DEF)						\
   do {								\
     TAILQ_REMOVE(&define,(DEF),list);				\
     FREE_ARG_LIST((DEF));					\
     free((DEF));						\
-  }while(0)
+  }while(0) 
 
 
 static TAILQ_HEAD(,iarg) iarg = TAILQ_HEAD_INITIALIZER(iarg);
@@ -189,6 +192,21 @@
 }
 
 #if 0
+
+static const char *
+replace_word(const char * word) {
+
+  struct define * tmp;
+  
+  TAILQ_FOREACH(tmp, &define, list) { 
+    if (String(word,NULL) == (tmp->name)) {
+      return (tmp->value);
+    }
+  }  
+  return word;
+}
+
+
 static const char *
 expand_macro(struct define * mac, struct arg_ind * head) {
   
@@ -196,12 +214,24 @@
 
 
 static const char *
-expand_string(char * word) {
-  /* Search the macro list to match word.  If word matches a macro, return a
-   * pointer to the expanded macro, otherwise return word 
-   */
+get_name(const char * word) {
+  const char e = strstr(word,"(");
+  if(e == NULL)
+    return word;
+      
+  return String(word,e);
+}
+
+static const char *
+get_arg(const char * word) {
+  const char e = NULL;
+}
+
+static const char *
+expand_string(const char * str) {
   
 }
+
 #endif
 
 /* -------------------------------------------------------------------*/
@@ -303,6 +333,34 @@
 {
   assert(mac->value != NULL);
   assert(mac->length > 0);
+
+  struct macro_arg * curr;
+  int macLength = mac->length;
+  int * tmpOffs = calloc((sizeof(int)* macLength),1);
+  int count = 0;
+
+  TAILQ_FOREACH(curr, &mac->args, list) {
+    char * t;
+    
+    printf("Looking for arg %V\n",curr->name);
+
+    for(t = strnstr(mac->value,curr->name,macLength); t != NULL; 
+	t = strnstr(t,curr->name,(macLength - (t - mac->value)))) {
+      if (t == NULL)
+	break;
+      tmpOffs[count] = t - mac->value;
+      count++;
+      t += curr->name_length;
+      printf("Adding offset at %i\n",tmpOffs[count-1]);
+    }
+
+    if(count > 0) {
+      curr->offsets = calloc((sizeof(int)* count),1);
+      memcpy(curr->offsets,tmpOffs,(sizeof(int)*count));
+    } else
+      printf("Arg %V did not appear in macro value %V\n",curr->name,mac->value);
+  }
+  free(tmpOffs);
 }
 
 
@@ -320,19 +378,26 @@
 
   printf("adding macro arg: %V\n",new_arg->name);
 
-  TAILQ_FOREACH(tmp, &mac->args, list) {
-    if(tmp == NULL) {
-      printf("tmp == null; inserting into tail\n");
-      TAILQ_INSERT_TAIL(&mac->args,new_arg,list);
-    }
-
-    if(tmp->name == new_arg->name) {
-      free(new_arg);
-      errx(1, "duplicate macro parameter \"%V\"",tmp->name);
+  /* Make sure something's in the arg list to begin with */
+  if(TAILQ_FIRST(&mac->args) == NULL) {
+    printf("tmp == null; inserting into tail\n");
+    TAILQ_INSERT_TAIL(&mac->args,new_arg,list);
+  }
+  else {
+    TAILQ_FOREACH(tmp, &mac->args, list) {
+      if(tmp == NULL) {
+	printf("tmp == null; inserting into tail\n");
+	TAILQ_INSERT_TAIL(&mac->args,new_arg,list);
+      }
+      
+      if(tmp->name == new_arg->name) {
+	free(new_arg);
+	errx(1, "duplicate macro parameter \"%V\"",tmp->name);
+      }
     }
   }
-
-  new_arg->length = strlen(new_arg->name);
+  
+  new_arg->name_length = strlen(new_arg->name);
 
   printf("added argument named: <%V> to macro named <%V>\n",new_arg->name,
 	 mac->name);
@@ -394,6 +459,8 @@
     printf("2Defining object macro name %V\n", p);
     mac->mac_type = OBJ_MAC_TYPE;
   }
+
+  TAILQ_INIT(&mac->args);
   
   switch (mac->mac_type) {
   
@@ -406,8 +473,6 @@
     {
       const char * arg_beg = skipspace(cfs,name_e+1,e);
 
-      TAILQ_INIT(&mac->args);
-
       /* Insert each argument name.  Error if list does not end with a ')' */
       /* XXX Should this detect empty arguments and non-ID like arguments? */
       for(p = arg_beg; p < e; p++) {
@@ -464,7 +529,8 @@
   TAILQ_FOREACH(tmp, &define, list) {
     if(tmp->name == name) {
       printf("Removing macro %s\n",name);
-      FREE_DEF(tmp);
+      /* XXX Fix freeing */
+      //FREE_DEF(tmp);
 
       /* XXX Could there still be more #defines with the same name in the list? */
       break;


More information about the p4-projects mailing list