File tree 5 files changed +115
-0
lines changed
5 files changed +115
-0
lines changed Original file line number Diff line number Diff line change @@ -125,6 +125,8 @@ Script: [
125
125
; face-reused: [{Face object reused (in more than one pane):} :arg1]
126
126
127
127
invalid-handle: {invalid handle}
128
+ invalid-value-for: [{invalid value} :arg1 {for:} :arg2 ]
129
+
128
130
]
129
131
130
132
Math: [
Original file line number Diff line number Diff line change @@ -160,6 +160,8 @@ standard: context [
160
160
none
161
161
]
162
162
163
+ enum: none ; is defined later in %mezz-func.r file
164
+
163
165
error : context [ ; Template used for all errors:
164
166
code: 0
165
167
type: 'user
@@ -284,6 +286,7 @@ standard: context [
284
286
none
285
287
]
286
288
289
+ bincode: none
287
290
utype: none
288
291
font: none ; mezz-graphics.h
289
292
para: none ; mezz-graphics.h
Original file line number Diff line number Diff line change @@ -78,3 +78,60 @@ task: func [
78
78
] [
79
79
make task! copy/deep reduce [spec body]
80
80
]
81
+
82
+ enum : function [
83
+ "Creates enumeration object from given specification"
84
+ spec [block! ] "Specification with names and values."
85
+ title [string! word! ] "Enumeration name"
86
+ ] [
87
+ enum-value: 0
88
+ spec: copy spec
89
+ parse spec [any [
90
+ pos: word! insert enum-value (
91
+ change pos to set-word! pos/1
92
+ enum-value: enum-value + 1
93
+ )
94
+ | some set-word! pos: [
95
+ integer! | issue! | binary! | char!
96
+ ] (
97
+ if error? try [
98
+ enum-value: to integer! pos/1
99
+ pos: change pos enum-value
100
+ enum-value: enum-value + 1
101
+ ][
102
+ cause-error 'Script 'invalid-data reduce [pos]
103
+ ]
104
+ ) :pos
105
+ | pos: 1 skip (
106
+ cause-error 'Script 'invalid-data reduce [pos]
107
+ )
108
+ ]
109
+ ]
110
+ enum: make system/standard/enum spec
111
+ enum/title*: title
112
+ enum
113
+ ]
114
+
115
+ system/standard/enum: context [
116
+ title*: none
117
+ assert : func [
118
+ "Checks if value exists as an enumeration. Throws error if not."
119
+ value [integer! ]
120
+ ] [
121
+ unless find values-of self value [
122
+ cause-error 'Script 'invalid-value-for reduce [value title*]
123
+ ]
124
+ true
125
+ ]
126
+ name : func [
127
+ "Returns name of the emumeration by its value if value exists, else none."
128
+ value [integer! ]
129
+ /local pos
130
+ ] [
131
+ all [
132
+ pos: find values-of self value
133
+ pick words-of self index? pos
134
+ ]
135
+ ]
136
+ ;@@ add some other accessor functions?
137
+ ]
Original file line number Diff line number Diff line change @@ -17,6 +17,7 @@ wrap load %units/rsa-test.r3
17
17
wrap load %units/dh-test.r3
18
18
wrap load %units/port-test.r3
19
19
wrap load %units/checksum-test.r3
20
+ wrap load %units/enum-test.r3
20
21
21
22
wrap load %units/crash-test.r3
22
23
Original file line number Diff line number Diff line change
1
+ Rebol [
2
+ Title: "Rebol enum test script"
3
+ Author: "Oldes"
4
+ File: %enum-test.red
5
+ Tabs: 4
6
+ Needs: [%../quick-test-module.r3 ]
7
+ ]
8
+
9
+ ~~~start-file~~~ "Enum"
10
+
11
+ ===start-group=== "Basic enumeration"
12
+ --test-- "enum with basic specification"
13
+ *FX-DX8: enum [
14
+ CHORUS
15
+ COMPRESSOR
16
+ DISTORTION
17
+ ECHO
18
+ FLANGER
19
+ GARGLE
20
+ I3DL2REVERB
21
+ PARAMEQ
22
+ REVERB
23
+ ] "DX8 effect ID"
24
+
25
+ --assert object? *FX-DX8
26
+ --assert 0 = *FX-DX8/CHORUS
27
+ --assert 8 = *FX-DX8/REVERB
28
+ --assert found? find [ECHO FLANGER] *FX-DX8/name 4
29
+
30
+
31
+ --test-- "enum with mixed specification"
32
+ *family: enum [
33
+ Alice: 1
34
+ Boban
35
+ Bolek
36
+ Lolek: #{ FF }
37
+ Brian
38
+ ] 'Just-Some-Names
39
+
40
+ --assert object? *family
41
+ --assert 'Boban = *family/name 2
42
+ --assert 'Lolek = *family/name 255
43
+ --assert none? *family/name 13
44
+ --assert true? *family/assert 1
45
+ --assert 256 = *family/Brian
46
+ --assert error? err: try [*family/assert 13 ]
47
+ --assert err/arg1 = 13
48
+ --assert err/arg2 = 'Just-Some-Names
49
+
50
+ ===end-group===
51
+
52
+ ~~~end-file~~~
You can’t perform that action at this time.
0 commit comments