Lyst.st 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. | receiver selector result |
  27. selector := self first.
  28. receiver := anObject yourself. "JSObjectProxy hack"
  29. [ result := receiver perform: selector ]
  30. on: MessageNotUnderstood do: [ :mnu |
  31. ((mnu message selector = selector
  32. and: [ mnu receiver == receiver ])
  33. and: [ mnu message arguments isEmpty ])
  34. ifFalse: [ mnu resignal ].
  35. ^ aBlock value ].
  36. ^ result
  37. !
  38. atYndexIn: anObject ifAbsent: aBlock put: anotherObject
  39. | receiver selector arguments result |
  40. selector := self first asMutator.
  41. receiver := anObject yourself. "JSObjectProxy hack"
  42. arguments := { anotherObject }.
  43. [ result := receiver perform: selector withArguments: arguments ]
  44. on: MessageNotUnderstood do: [ :mnu |
  45. ((mnu message selector = selector
  46. and: [ mnu receiver == receiver ])
  47. and: [ mnu message arguments = arguments ])
  48. ifFalse: [ mnu resignal ].
  49. ^ aBlock value ].
  50. ^ result
  51. ! !
  52. !Number methodsFor: '*Lyst'!
  53. atYndexIn: anObject ifAbsent: aBlock
  54. (anObject respondsTo: #at:ifAbsent:)
  55. ifTrue: [ ^ anObject at: self ifAbsent: aBlock ]
  56. ifFalse: aBlock
  57. !
  58. atYndexIn: anObject ifAbsent: aBlock put: anotherObject
  59. (anObject respondsTo: #at:put:)
  60. ifTrue: [ ^ anObject at: self put: anotherObject ]
  61. ifFalse: aBlock
  62. ! !
  63. !Object methodsFor: '*Lyst'!
  64. atLyst: aCollection ifAbsent: aBlock
  65. ^ aCollection inject: self into: [ :soFar :segment |
  66. segment atYndexIn: soFar ifAbsent: [ ^ aBlock value ]]
  67. !
  68. atYndexIn: anObject ifAbsent: aBlock
  69. ^ aBlock value
  70. !
  71. atYndexIn: anObject ifAbsent: aBlock put: anotherObject
  72. ^ aBlock value
  73. ! !
  74. !String methodsFor: '*Lyst'!
  75. atYndexIn: anObject ifAbsent: aBlock
  76. (anObject respondsTo: #at:ifAbsent:)
  77. ifTrue: [ ^ anObject at: self ifAbsent: aBlock ]
  78. ifFalse: aBlock
  79. !
  80. atYndexIn: anObject ifAbsent: aBlock put: anotherObject
  81. (anObject respondsTo: #at:put:)
  82. ifTrue: [ ^ anObject at: self put: anotherObject ]
  83. ifFalse: aBlock
  84. ! !