home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / OL.LZH / IDOL.LZH / BUILTINS.IOL < prev    next >
Text File  |  1991-07-18  |  4KB  |  171 lines

  1. # %W% %G%
  2. #
  3. # Builtin Icon objects, roughly corresponding to the language builtins.
  4. # (These are not builtin to the Idol interpreter!)
  5. #
  6. # Taxonomy of builtin types:
  7. #
  8. #                   __Object___
  9. #                           _-'           `-_
  10. #             _-'                  `-_
  11. #          Collection            Atom_
  12. #         /    |    \                      _'     `-.
  13. #    Stack    Queue    Vector           _-'        Number
  14. #           \   /      /  |  \          _-'            /      \ 
  15. #        Deque     /      |   \          _'     Integer           Real
  16. #               \    /    |    \     /
  17. #        List    Table    String
  18. #
  19. #    
  20.  
  21. #
  22. # this is the Smalltalk-style ideal root of an inheritance hierarchy.
  23. # add your favorite methods here.
  24. #
  25. class Object()
  26.   # return the class name as a string
  27.   method class()
  28.     return image(self)[8:find("_",image(self))]
  29.   end
  30.   # generate the field names as strings
  31.   method fieldnames()
  32.     i := 1
  33.     every s := name(!(self.__state)) do {
  34.     if i>2 then s ? { tab(find(".")+1); suspend tab(0) }
  35.     i +:= 1
  36.     }
  37.   end
  38.   # generate the method names as strings
  39.   method methodnames()
  40.     every s := name(!(self.__methods)) do {
  41.     s ? { tab(find(".")+1); suspend tab(0) }
  42.     }
  43.   end
  44. end
  45.  
  46. # Collections support Icon's *?! operators
  47. class Collection : Object (theCollection)
  48.   method size()
  49.     return *self.theCollection
  50.   end
  51.   method foreach()
  52.     suspend !self.theCollection
  53.   end
  54.   method random()
  55.     return ?self.theCollection
  56.   end
  57. end
  58.  
  59. # Vectors have the ability to access individual elements
  60. class Vector : Collection()
  61.   method getElement(i)
  62.     return self.theCollection[i]
  63.   end
  64.   method setElement(i,v)
  65.     return self.theCollection[i] := v
  66.   end
  67. end
  68.  
  69. class Table : Vector(initialvalue,theCollection)
  70. initially
  71.   self.theCollection := table(self.initialvalue)
  72. end
  73.  
  74. #
  75. # The field theCollection is explicitly named so that subclasses of Stack
  76. # and Queue use these automatic initializations.  The / operator is used
  77. # to reduce the number of throw-away list allocations for subclasses which
  78. # >don't< inherit theCollection from Stack or Queue (e.g. class List).
  79. # It also allows initialization by constructor.  If one wanted to
  80. # guarantee that all Stacks start out empty but still allow class List
  81. # to be explicitly intitialized, one could remove the / here, and name
  82. # theCollection in class List, causing its initially section to override
  83. # the superclass with respect to the field theCollection.  I choose here
  84. # to maximize code sharing rather than protecting my Stack class.
  85. #
  86. # When allowing initialization by constructor one might consider
  87. # checking the type of the input to guarantee it conforms to the
  88. # type expected by the class.
  89. #
  90. class Stack : Collection(theCollection)
  91.   method push(value)
  92.     push(self.theCollection,value)
  93.   end
  94.   method pop()
  95.     return pop(self.theCollection)
  96.   end
  97. initially
  98.   /self.theCollection := []
  99. end
  100.  
  101. class Queue : Collection(theCollection)
  102.   method get()
  103.     return get(self.theCollection)
  104.   end
  105.   method put(value)
  106.     put(self.theCollection,value)
  107.   end
  108. initially
  109.   /self.theCollection := []
  110. end
  111.  
  112. # Deques are a first example of multiple inheritance.
  113. class Deque : Queue : Stack()
  114. end
  115.  
  116. #
  117. # List inherits Queue's theCollection initialization, because Queue is the
  118. # first class on List's (transitively closed) superclass list to name
  119. # theCollection explicitly
  120. #
  121. class List : Deque : Vector()
  122.   method concat(l)
  123.     return List(self.theCollection ||| l)
  124.   end
  125. end
  126.  
  127. class Atom : Object(public val)
  128.   method asString()
  129.     return string(self.val)
  130.   end
  131.   method asInteger()
  132.     return integer(self.val)
  133.   end
  134.   method asReal()
  135.     return real(self.val)
  136.   end
  137. end
  138.  
  139. class Number : Atom ()
  140.   method plus(n)
  141.     return self.val + n$val()
  142.   end
  143.   method minus(n)
  144.     return self.val - n$val()
  145.   end
  146.   method times(n)
  147.     return self.val * n$val()
  148.   end
  149.   method divide(n)
  150.     return self.val / n$val()
  151.   end
  152. end
  153.  
  154. class Integer : Number()
  155. initially
  156.   if not (self.val := integer(self.val)) then
  157.     stop("can't make Integer from ",image(self.val))
  158. end
  159.  
  160. class Real : Number()
  161. initially
  162.   if not (self.val := real(self.val)) then
  163.     stop("can't make Real from ",image(self.val))
  164. end
  165.  
  166. class String : Vector : Atom()
  167.   method concat(s)
  168.     return self.theCollection || s
  169.   end
  170. end
  171.