Skip to content

Commit c5cfb69

Browse files
committed
FEAT: adding function ENUM for making standard enumeration objects from given specification
See the included test file for usage examples.
1 parent 7c07c7b commit c5cfb69

File tree

5 files changed

+115
-0
lines changed

5 files changed

+115
-0
lines changed

src/boot/errors.r

+2
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ Script: [
125125
; face-reused: [{Face object reused (in more than one pane):} :arg1]
126126

127127
invalid-handle: {invalid handle}
128+
invalid-value-for: [{invalid value} :arg1 {for:} :arg2]
129+
128130
]
129131

130132
Math: [

src/boot/sysobj.r

+3
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ standard: context [
160160
none
161161
]
162162

163+
enum: none ; is defined later in %mezz-func.r file
164+
163165
error: context [ ; Template used for all errors:
164166
code: 0
165167
type: 'user
@@ -284,6 +286,7 @@ standard: context [
284286
none
285287
]
286288

289+
bincode: none
287290
utype: none
288291
font: none ; mezz-graphics.h
289292
para: none ; mezz-graphics.h

src/mezz/mezz-func.r

+57
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,60 @@ task: func [
7878
][
7979
make task! copy/deep reduce [spec body]
8080
]
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+
]

src/tests/run-tests.r3

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ wrap load %units/rsa-test.r3
1717
wrap load %units/dh-test.r3
1818
wrap load %units/port-test.r3
1919
wrap load %units/checksum-test.r3
20+
wrap load %units/enum-test.r3
2021

2122
wrap load %units/crash-test.r3
2223

src/tests/units/enum-test.r3

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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~~~

0 commit comments

Comments
 (0)