home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.mdf / Apps / DevTools / COWS / Code / COWSStack.m < prev    next >
Encoding:
Text File  |  1994-03-05  |  1.8 KB  |  132 lines

  1. /*
  2.     Copyright (C) 1994 Sean Luke
  3.  
  4.     COWSStack.m
  5.     Version 1.0
  6.     Sean Luke
  7.     
  8. */
  9.  
  10.  
  11.  
  12.  
  13. #import "COWSStack.h"
  14. #import "COWSNode.h"
  15. #import "COWSStateNode.h"
  16. #import "COWSSymbolNode.h"
  17. #import <stdio.h>
  18.  
  19.  
  20. @implementation COWSStack
  21.  
  22. - init
  23.     {
  24.     id returnval= [super init];
  25.     current=NULL;
  26.     return returnval;
  27.     }
  28.     
  29. - push:this
  30.     {
  31.     [this setNext:current];
  32.     [this setPrev:NULL];
  33.     if (current!=NULL) [current setPrev:this];
  34.     current=this;
  35.     return current;
  36.     }
  37.     
  38. - pop
  39.     {
  40.     id temp=current;
  41.     if (temp!=NULL) 
  42.         {
  43.         if ([temp next]!=NULL) [[temp next] setPrev:NULL];
  44.         current=[current next];
  45.         [temp setNext:NULL];
  46.         }
  47.     return temp;
  48.     }
  49.     
  50. - top
  51.     {
  52.     return current;
  53.     }
  54.     
  55. - _symbolHelper:pos        // private helper function for topSymbol
  56.     {
  57.     if (pos==NULL)    return NULL;
  58.     if (![pos isKindOf:[COWSNode class]]) 
  59.         {
  60.         printf ("ERROR!  Something wrong with Stack!\n");
  61.         return NULL;
  62.         }
  63.     if ([pos isMemberOf:[COWSStateNode class]]) return NULL;
  64.     if ([pos isMemberOf:[COWSSymbolNode class]]) return pos;
  65.     return [self _symbolHelper:[pos next]];
  66.     }
  67.  
  68. - topSymbol
  69.     {
  70.     return [self _symbolHelper:current];
  71.     }
  72.     
  73. - _stateHelper:pos        // private helper function for topState
  74.     {
  75.     if (pos==NULL)    return NULL;
  76.     if (![pos isKindOf:[COWSNode class]]) 
  77.         {
  78.         printf ("ERROR!  Something wrong with Stack!\n");
  79.         return NULL;
  80.         }
  81.     if ([pos isMemberOf:[COWSStateNode class]]) return pos;
  82.     return [self _stateHelper:[pos next]];
  83.     }
  84.  
  85. - topState
  86.     {
  87.     return [self _stateHelper:current];
  88.     }
  89.     
  90. - clear
  91.     {
  92.     while (current!=NULL)
  93.         {
  94.         [[self pop] free];
  95.         }
  96.     return self;
  97.     }    
  98.     
  99. - (int) number
  100.     {
  101.     int x=0;
  102.     id temp=current;
  103.     while (temp!=NULL)
  104.         {
  105.         x++;
  106.         temp=[temp next];
  107.         }
  108.     return x;
  109.     }
  110.  
  111.  
  112.     
  113. - free
  114.     {
  115.     [self clear];
  116.     return [super free];
  117.     }
  118.  
  119.  
  120. - printContents
  121.     {
  122.     id temp=current;
  123.     while (temp!=NULL)
  124.         {
  125.         [temp printContents];
  126.         temp=[temp next];
  127.         }
  128.     return self;
  129.     }
  130.     
  131.     
  132. @end