Advertisement
Skyb0rg

Untitled

Apr 16th, 2020
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. ;; ----- Basics, Attributes ------------
  2.  
  3. ;; print - method on Objects, prints the object
  4. ;; space - object, contains the space character
  5.  
  6. (class Element
  7. [subclass-of Object]
  8. [ivars tag atts]
  9.  
  10. (class-method new: (t)
  11. ((super new) init: t))
  12.  
  13. (method init: (t)
  14. (set tag t)
  15. (set atts (Dictionary new))
  16. self)
  17.  
  18. (method attribute:put: (k v)
  19. (atts at:put: k v)
  20. self)
  21.  
  22. (method print ()
  23. ('< print)
  24. (tag print)
  25. (atts associationsDo:
  26. [block (assoc)
  27. (space print)
  28. ((assoc key) print)
  29. ('=" print)
  30. ((assoc value) print)
  31. ('=" print)])
  32. ('/> print)
  33. self)
  34. )
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53. ;; ----- An SVG canvas -----------------
  54.  
  55. ;; A placeholder class that helps us define a helper-method
  56. ;; we use in SVGCanvas.
  57. (class X [subclass-of Object]
  58. (method printElems ()
  59. (self do: [block (part) (part print) (space print)])))
  60. (SequenceableCollection addAllMethodsFrom: X)
  61.  
  62. ;; A String is an ordered collection that prints its elements in order
  63. ;; (i.e. like a string!)
  64. (class String
  65. [subclass-of List]
  66. (method print ()
  67. (self do: [block (x) (x print)])))
  68.  
  69. (class SVGCanvas
  70. [subclass-of Object]
  71. (method startDrawing ()
  72. ('(<svg viewBox="-5 -5 11 10"
  73. width="300px" height="300px"
  74. transform="scale(1 -1)">) printElems)
  75. (newline print))
  76.  
  77. (method stopDrawing ()
  78. ('</svg> println))
  79.  
  80. (method drawPolygon: (coord-list)
  81. (self LeftAsExercise))
  82.  
  83. (method drawEllipseAt:width:height: (center w h) [locals elem]
  84. (set elem (Element new: 'elipse))
  85. (elem attribute:put: 'cx (center x))
  86. (elem attribute:put: 'cy (center y))
  87. (elem attribute:put: 'rx w)
  88. (elem attribute:put: 'ry h)
  89. (elem print))
  90. )
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104. ;; ----- Integration test -----------------
  105.  
  106. ;; Uncomment the integration test below to try drawing one of the
  107. ;; pictures in the book.
  108.  
  109. ; (use /comp/105/build-prove-compare/examples/usmalltalk/shapes.smt)
  110. ; (val c (Circle new))
  111. ; (val s (Square new))
  112. ; (s adjustPoint:to: 'West (c location: 'East))
  113. ; (class Triangle
  114. ; [subclass-of Shape]
  115. ; ;; no additional representation
  116. ; (method drawOn: (canvas)
  117. ; (canvas drawPolygon: (self locations: '(North Southwest Southeast)))))
  118. ; (val t ((Triangle new) adjustPoint:to: 'Southwest (s location: 'East)))
  119. ; (val pic (Picture empty))
  120. ; (pic add: c)
  121. ; (pic add: s)
  122. ; (pic add: t)
  123. ; (val canvas (SVGCanvas new))
  124. ; '-------------------
  125. ; (begin (pic renderUsing: canvas)
  126. ; '-------------------)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement