Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Red [
- Title: "math-lab.red"
- Author: "Gregg Irwin"
- File: %math-lab.red
- Needs: 'View
- Purpose: {
- Experiment, to see what an interactive "Lab" tool might look
- like, for general language functions. i.e., take the idea of
- font-lab, effect-lab, gradient-lab, etc., and apply it to
- functions.
- It serves a few purposes:
- - Show users what functions are available in a given "category"
- - Show them live results
- - Pre-fill values of different types, to show what the funcs
- support
- - Interactive testing of values, to probe/confirm errors
- - Explore possible new functionality. e.g. pair! * percent!
- TBD: Combo boxes on args can provide
- TBD: Click to get detailed help on a function or category
- (e.g. math precedence note for all math ops)
- TBD: User entry field where they can enter their own combination
- of ops and see a result. We could even show progressive
- evaluation, maybe requiring parens.
- TBD: Sliders could be attached to arg fields, in some cases.
- }
- ]
- ;get-args: make reactor! [
- ; arg-1: does [load-num f-arg-1/text]
- ; arg-2: does [load-num f-arg-2/text]
- ;]
- ;
- ;args: make reactor! [
- ; arg-1: is [:get-args/arg-1]
- ; arg-2: is [:get-args/arg-2]
- ;]
- load-num: function [str][
- res: attempt [load str]
- if any [none? res block? res] [res: 0]
- res
- ]
- set-args: func [a b][
- f-arg-1/data: a
- f-arg-2/data: b
- ]
- lay-spec: copy [
- title "Red Math Lab"
- space 4x2
- button "integer!" [set-args 1 2]
- button "float!" [set-args 1.0 2.0]
- button "percent!" [set-args 10% 20%]
- button "pair!" [set-args 10x10 5x15]
- button "pair! int" [set-args 100x50 3]
- button "char! int" [set-args mold #"R" 2]
- return
- button "tuple!" [set-args 1.2.3 2.2.2]
- button "tuple! int" [set-args 1.2.3 3]
- button "time!" [set-args 1:2:3 2:2:2]
- button "time! int" [set-args 1:2:3 4]
- pad 0x10
- return
- text "Args" 100x18 right
- pad 12x0
- style arg-fld: field 60 center
- f-arg-1: arg-fld "1"
- f-arg-2: arg-fld "2"
- return
- pad 0x10
- style text: text 60x18 center
- style arg-1-ref: text ;react [face/text: f-arg-1/text]
- style arg-2-ref: text ;react [face/text: f-arg-2/text]
- style place-holder: text ""
- style op-lbl: text 100x18 right
- style op-result: text 100x18 left
- ]
- add-op: function [op][
- ;!! Have to use copy/deep for reactor blocks to work properly
- append lay-spec compose/deep copy/deep [
- ;(to set-word! append copy "f-op-" text)
- op-lbl (form op)
- pad 10x0
- arg-1-ref react [face/text: f-arg-1/text]
- (either arity-1? op ['place-holder][ [arg-2-ref react [face/text: f-arg-2/text]] ])
- text 25 "=="
- ;op-result react [face/text: form (to word! text) args/arg-1 args/arg-2]
- op-result react [
- face/text: attempt [
- form (to word! op)
- load-num f-arg-1/text
- (either arity-1? op [][ [load-num f-arg-2/text] ])
- ]
- ]
- return
- ]
- ]
- arity-1-ops: [absolute negate]
- arity-1?: func [op][find arity-1-ops op]
- ops: [
- absolute negate
- add subtract multiply divide modulo remainder power
- shift ;shift/left shift/logical
- ;and or xor
- same? equal? strict-equal? not-equal?
- greater? lesser? greater-or-equal? lesser-or-equal?
- ]
- foreach op ops [add-op op]
- ;print mold lay-spec
- view lay-spec
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement