Advertisement
Guest User

Untitled

a guest
Jul 29th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. Red [
  2. Title: "math-lab.red"
  3. Author: "Gregg Irwin"
  4. File: %math-lab.red
  5. Needs: 'View
  6. Purpose: {
  7. Experiment, to see what an interactive "Lab" tool might look
  8. like, for general language functions. i.e., take the idea of
  9. font-lab, effect-lab, gradient-lab, etc., and apply it to
  10. functions.
  11. It serves a few purposes:
  12. - Show users what functions are available in a given "category"
  13. - Show them live results
  14. - Pre-fill values of different types, to show what the funcs
  15. support
  16. - Interactive testing of values, to probe/confirm errors
  17. - Explore possible new functionality. e.g. pair! * percent!
  18. TBD: Combo boxes on args can provide
  19. TBD: Click to get detailed help on a function or category
  20. (e.g. math precedence note for all math ops)
  21. TBD: User entry field where they can enter their own combination
  22. of ops and see a result. We could even show progressive
  23. evaluation, maybe requiring parens.
  24. TBD: Sliders could be attached to arg fields, in some cases.
  25. }
  26. ]
  27.  
  28. ;get-args: make reactor! [
  29. ; arg-1: does [load-num f-arg-1/text]
  30. ; arg-2: does [load-num f-arg-2/text]
  31. ;]
  32. ;
  33. ;args: make reactor! [
  34. ; arg-1: is [:get-args/arg-1]
  35. ; arg-2: is [:get-args/arg-2]
  36. ;]
  37.  
  38.  
  39. load-num: function [str][
  40. res: attempt [load str]
  41. if any [none? res block? res] [res: 0]
  42. res
  43. ]
  44.  
  45. set-args: func [a b][
  46. f-arg-1/data: a
  47. f-arg-2/data: b
  48. ]
  49.  
  50. lay-spec: copy [
  51. title "Red Math Lab"
  52. space 4x2
  53.  
  54. button "integer!" [set-args 1 2]
  55. button "float!" [set-args 1.0 2.0]
  56. button "percent!" [set-args 10% 20%]
  57. button "pair!" [set-args 10x10 5x15]
  58. button "pair! int" [set-args 100x50 3]
  59. button "char! int" [set-args mold #"R" 2]
  60. return
  61. button "tuple!" [set-args 1.2.3 2.2.2]
  62. button "tuple! int" [set-args 1.2.3 3]
  63. button "time!" [set-args 1:2:3 2:2:2]
  64. button "time! int" [set-args 1:2:3 4]
  65. pad 0x10
  66. return
  67.  
  68. text "Args" 100x18 right
  69. pad 12x0
  70. style arg-fld: field 60 center
  71. f-arg-1: arg-fld "1"
  72. f-arg-2: arg-fld "2"
  73. return
  74. pad 0x10
  75.  
  76. style text: text 60x18 center
  77. style arg-1-ref: text ;react [face/text: f-arg-1/text]
  78. style arg-2-ref: text ;react [face/text: f-arg-2/text]
  79. style place-holder: text ""
  80. style op-lbl: text 100x18 right
  81. style op-result: text 100x18 left
  82. ]
  83.  
  84. add-op: function [op][
  85. ;!! Have to use copy/deep for reactor blocks to work properly
  86. append lay-spec compose/deep copy/deep [
  87. ;(to set-word! append copy "f-op-" text)
  88. op-lbl (form op)
  89. pad 10x0
  90. arg-1-ref react [face/text: f-arg-1/text]
  91. (either arity-1? op ['place-holder][ [arg-2-ref react [face/text: f-arg-2/text]] ])
  92. text 25 "=="
  93. ;op-result react [face/text: form (to word! text) args/arg-1 args/arg-2]
  94. op-result react [
  95. face/text: attempt [
  96. form (to word! op)
  97. load-num f-arg-1/text
  98. (either arity-1? op [][ [load-num f-arg-2/text] ])
  99. ]
  100. ]
  101. return
  102. ]
  103. ]
  104.  
  105. arity-1-ops: [absolute negate]
  106. arity-1?: func [op][find arity-1-ops op]
  107.  
  108.  
  109. ops: [
  110. absolute negate
  111. add subtract multiply divide modulo remainder power
  112. shift ;shift/left shift/logical
  113. ;and or xor
  114. same? equal? strict-equal? not-equal?
  115. greater? lesser? greater-or-equal? lesser-or-equal?
  116. ]
  117. foreach op ops [add-op op]
  118.  
  119. ;print mold lay-spec
  120. view lay-spec
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement