Lyst.st 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. Smalltalk createPackage: 'Lyst'!
  2. Object subclass: #Lyst
  3. instanceVariableNames: ''
  4. package: 'Lyst'!
  5. !Lyst class methodsFor: 'parsing'!
  6. parse: message
  7. | result stack anArray |
  8. anArray := message tokenize: ' '.
  9. result := #().
  10. stack := { result }.
  11. anArray do: [ :each |
  12. | asNum inner close |
  13. close := 0.
  14. inner := each.
  15. [ inner notEmpty and: [ inner first = '(' ]] whileTrue: [ inner := inner allButFirst. stack add: (stack last add: #()) ].
  16. [ inner notEmpty and: [ inner last = ')' ]] whileTrue: [ inner := inner allButLast. close := close + 1 ].
  17. (inner notEmpty and: [ inner first = '#' ]) ifTrue: [ inner := { inner allButFirst } ].
  18. asNum := inner isString ifTrue: [ (inner ifEmpty: [ 'NaN' ]) asNumber ] ifFalse: [ inner ].
  19. asNum = asNum ifTrue: [ stack last add: asNum ] ifFalse: [
  20. inner ifNotEmpty: [ stack last add: inner ] ].
  21. close timesRepeat: [ stack removeLast ] ].
  22. ^ result
  23. ! !
  24. !Array methodsFor: '*Lyst'!
  25. atYndexIn: anObject ifAbsent: aBlock
  26. | selector |
  27. selector := self first.
  28. (anObject respondsTo: selector)
  29. ifTrue: [ ^ anObject perform: selector ]
  30. ifFalse: aBlock
  31. !
  32. atYndexIn: anObject ifAbsent: aBlock put: anotherObject
  33. | selector |
  34. selector := self first asMutator.
  35. (anObject respondsTo: selector)
  36. ifTrue: [ ^ anObject perform: selector withArguments: { anotherObject } ]
  37. ifFalse: aBlock
  38. ! !
  39. !Number methodsFor: '*Lyst'!
  40. atYndexIn: anObject ifAbsent: aBlock
  41. (anObject respondsTo: #at:ifAbsent:)
  42. ifTrue: [ ^ anObject at: self ifAbsent: aBlock ]
  43. ifFalse: aBlock
  44. !
  45. atYndexIn: anObject ifAbsent: aBlock put: anotherObject
  46. (anObject respondsTo: #at:put:)
  47. ifTrue: [ ^ anObject at: self put: anotherObject ]
  48. ifFalse: aBlock
  49. ! !
  50. !Object methodsFor: '*Lyst'!
  51. atLyst: aCollection ifAbsent: aBlock
  52. ^ aCollection inject: self into: [ :soFar :segment |
  53. segment atYndexIn: soFar ifAbsent: [ ^ aBlock value ]]
  54. !
  55. atYndexIn: anObject ifAbsent: aBlock
  56. ^ aBlock value
  57. !
  58. atYndexIn: anObject ifAbsent: aBlock put: anotherObject
  59. ^ aBlock value
  60. ! !
  61. !String methodsFor: '*Lyst'!
  62. atYndexIn: anObject ifAbsent: aBlock
  63. (anObject respondsTo: #at:ifAbsent:)
  64. ifTrue: [ ^ anObject at: self ifAbsent: aBlock ]
  65. ifFalse: aBlock
  66. !
  67. atYndexIn: anObject ifAbsent: aBlock put: anotherObject
  68. (anObject respondsTo: #at:put:)
  69. ifTrue: [ ^ anObject at: self put: anotherObject ]
  70. ifFalse: aBlock
  71. ! !