home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Simulation / PDP-8 Simulator / Source Code / Assembler / Label.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-13  |  2.3 KB  |  140 lines  |  [TEXT/KAHL]

  1. /************************************************************
  2. *
  3. *
  4. *    Unit containing the Label object.
  5. *
  6. *    by Adrian Bool in cooperation with Graham Cox.
  7. *
  8. *    copyright © phantasm coding 1992.
  9. *
  10. *
  11. ************************************************************/
  12.  
  13. #include "Global.h"
  14. #include "Label.h"
  15.  
  16. #include "Label.proto.h"
  17.  
  18. lHandle newLabelList(void)
  19.     {
  20.     lHandle    temp;
  21.     
  22.     temp = (lHandle) NewHandle(sizeof(label));
  23.     
  24.     if (temp != nil)
  25.         {
  26.         (*temp)->next = nil;
  27.         (*temp)->value = 0;
  28.         copyString((*temp)->name,"");
  29.         }
  30.     return(temp);
  31.     }
  32.     
  33.     
  34. void disposeLabelList(lHandle theList)
  35.     {
  36.     lHandle        next;
  37.     
  38.     while (theList != nil)
  39.         {
  40.         next = (*theList)->next;
  41.         DisposHandle((Handle) theList);
  42.         theList = next;
  43.         }
  44.     }
  45.     
  46.  
  47. lValue getLabelValue(lHandle topLabel , char *labelName)
  48.     {
  49.     lHandle thisLabel;
  50.     short found;
  51.     
  52.     thisLabel = topLabel;
  53.     
  54.     pruneLabel(labelName);
  55.  
  56.     if (labelName[0] == '\0')
  57.         AsmError = SyntaxError;
  58.     else
  59.         {
  60.     
  61.         do
  62.             {
  63.             if (found = (strcmp(labelName,(*thisLabel)->name) == 0)) break;
  64.             if ((*thisLabel)->next == nil) break;
  65.             thisLabel = (*thisLabel)->next;
  66.             }
  67.         while(true);
  68.         
  69.         if (found)
  70.             return((*thisLabel)->value);
  71.         else
  72.             AsmError = LabelNotDefined;
  73.         }
  74.     }
  75.  
  76.  
  77. void newLabel(pHandle theProgram , char *labelName , lValue value)
  78.     {
  79.     lHandle thisLabel;
  80.     lHandle newLabel;
  81.     short found;
  82.     
  83.     pruneLabel(labelName);
  84.     
  85.     if (labelName[0] == '\0')
  86.         AsmError = SyntaxError;
  87.     else
  88.         {
  89.         /* first check to see if label is already defined */
  90.         
  91.         thisLabel = (*theProgram)->labelHeader;
  92.         
  93.         do
  94.             {
  95.             if (found = (strcmp(labelName,(*thisLabel)->name) == 0)) break;
  96.             if ((*thisLabel)->next == nil) break;
  97.             thisLabel = (*thisLabel)->next;
  98.             }
  99.         while(true);
  100.     
  101.         if (found)
  102.             AsmError = DuplicateIdentifier;
  103.         
  104.         else
  105.             {
  106.             /* Label OK if got to here */
  107.     
  108.             newLabel = (lHandle) NewHandle(sizeof(label));
  109.             
  110.             if (newLabel != nil)
  111.                 {
  112.                 (*newLabel)->next = nil;
  113.                 (*newLabel)->value = value;    
  114.                 copyString((*newLabel)->name,labelName);
  115.                 }
  116.             else
  117.                 AsmError = NotEnoughMemory;
  118.                 
  119.             (*thisLabel)->next = newLabel;
  120.             }
  121.         }
  122.     }
  123.  
  124.  
  125. void pruneLabel(char *labelName)
  126.     {
  127.     /* remove colon and make to max of 8 chars */
  128.  
  129.     short nameLength;
  130.  
  131.     nameLength = strlen(labelName);
  132.     
  133.     if (labelName[nameLength-1] == ':') labelName[nameLength-1] = (char) 0;
  134.     if (nameLength > 8) labelName[8] = (char) 0;
  135.     }
  136.     
  137.     
  138.     
  139.     
  140.