]> git.sur5r.net Git - cc65/blobdiff - src/cc65/macrotab.c
Fixed a bug
[cc65] / src / cc65 / macrotab.c
index 632142c76d8a67542d1f8c46f95148666539c3ec..45a290fe7f6845a89d379ae121b75bba4be0acb2 100644 (file)
 #include <stdio.h>
 #include <string.h>
 
-#include "../common/hashstr.h"
-#include "../common/xmalloc.h"
+/* common */
+#include "hashstr.h"
+#include "xmalloc.h"
 
+/* cc65 */
 #include "error.h"
 #include "macrotab.h"
 
@@ -77,7 +79,7 @@ Macro* NewMacro (const char* Name)
     unsigned Len = strlen(Name);
 
     /* Allocate the structure */
-    Macro* M = xmalloc (sizeof(Macro) + Len);
+    Macro* M = (Macro*) xmalloc (sizeof(Macro) + Len);
 
     /* Initialize the data */
     M->Next       = 0;
@@ -112,8 +114,8 @@ void FreeMacro (Macro* M)
 
 
 
-void AddNumericMacro (const char* Name, long Val)
-/* Add a macro for a numeric constant */
+void DefineNumericMacro (const char* Name, long Val)
+/* Define a macro for a numeric constant */
 {
     char Buf[64];
 
@@ -121,13 +123,13 @@ void AddNumericMacro (const char* Name, long Val)
     sprintf (Buf, "%ld", Val);
 
     /* Handle as text macro */
-    AddTextMacro (Name, Buf);
+    DefineTextMacro (Name, Buf);
 }
 
 
 
-void AddTextMacro (const char* Name, const char* Val)
-/* Add a macro for a textual constant */
+void DefineTextMacro (const char* Name, const char* Val)
+/* Define a macro for a textual constant */
 {
     /* Create a new macro */
     Macro* M = NewMacro (Name);
@@ -150,7 +152,7 @@ void InsertMacro (Macro* M)
 
     /* Allocate the ActualArgs parameter array */
     if (M->ArgCount > 0) {
-       M->ActualArgs = xmalloc (M->ArgCount * sizeof(char*));
+       M->ActualArgs = (char const**) xmalloc (M->ArgCount * sizeof(char*));
     }
 
     /* Get the hash value of the macro name */
@@ -237,8 +239,8 @@ Macro* FindMacro (const char* Name)
 int IsMacro (const char* Name)
 /* Return true if the given name is the name of a macro, return false otherwise */
 {
-    return FindMacro(Name) != 0;
-}
+    return MaybeMacro(Name[0]) && FindMacro(Name) != 0;
+}             
 
 
 
@@ -281,7 +283,7 @@ void AddMacroArg (Macro* M, const char* Arg)
     for (I = 0; I < M->ArgCount; ++I) {
        if (strcmp (M->FormalArgs[I], Arg) == 0) {
            /* Found */
-           Error (ERR_DUPLICATE_MACRO_ARG, Arg);
+           Error ("Duplicate macro parameter: `%s'", Arg);
            break;
        }
     }
@@ -289,11 +291,11 @@ void AddMacroArg (Macro* M, const char* Arg)
     /* Check if we have enough room available, otherwise expand the array
      * that holds the formal argument list.
      */
-    if (M->ArgCount >= M->MaxArgs) {
+    if (M->ArgCount >= (int) M->MaxArgs) {
        /* We must expand the array */
        char** OldArgs = M->FormalArgs;
        M->MaxArgs += 10;
-       M->FormalArgs = xmalloc (M->MaxArgs * sizeof(char*));
+       M->FormalArgs = (char**) xmalloc (M->MaxArgs * sizeof(char*));
        memcpy (M->FormalArgs, OldArgs, M->ArgCount * sizeof (char*));
        xfree (OldArgs);
     }
@@ -304,6 +306,29 @@ void AddMacroArg (Macro* M, const char* Arg)
 
 
 
+int MacroCmp (const Macro* M1, const Macro* M2)
+/* Compare two macros and return zero if both are identical. */
+{
+    int I;
+
+    /* Argument count must be identical */
+    if (M1->ArgCount != M2->ArgCount) {
+       return 1;
+    }
+
+    /* Compare the arguments */
+    for (I = 0; I < M1->ArgCount; ++I) {
+       if (strcmp (M1->FormalArgs[I], M2->FormalArgs[I]) != 0) {
+           return 1;
+       }
+    }
+
+    /* Compare the replacement */
+    return strcmp (M1->Replacement, M2->Replacement);
+}
+
+
+
 void PrintMacroStats (FILE* F)
 /* Print macro statistics to the given text file. */
 {