-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlib-case.muf
66 lines (66 loc) · 1.96 KB
/
lib-case.muf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
@prog lib-cases
1 99999 d
1 i
(
Okay, here's an evil idea for you programmers:
Fringe came up with a truly demented way to get case statements
using just $defines, so we made it into a library. The library
basically makes the following defines:
--------
$define case begin dup $enddef
$define when if pop $enddef
$define end break then dup $enddef
$define default pop 1 if $enddef
$define endcase pop pop 1 until $enddef
--------
And what it lets you do is something like this:
--------
$include $lib/case
<data> case
<test> when <effect> end
<test> when <effect> end
<test> when <effect> end
<test> when <effect> end
default <otherwise> end
endcase
--------
This will compile to the following:
--------
<data> begin
dup <test> if pop <effect> break then
dup <test> if pop <effect> break then
dup <test> if pop <effect> break then
dup <test> if pop <effect> break then
dup pop 1 if <otherwise> break then
dup pop pop
1 until
--------
The default clause is optional and thats why the wierd 'dup pop pop'
is at the end. The <otherwise> clause is passed the value that failed
to match in any of the tests. The <tests> can be as complex as you wish,
and so can the <effect> and <otherwise> statements.
Here's an example using real code:
--------
read case
"#help" over stringcmp not swap "#h" stringcmp not or when do-help end
"#help2" over stringcmp not swap "#h2" stringcmp not or when do-help2 end
"#list" stringcmp not when do-list end
default pop give-error end
endcase
--------
Enjoy!
- Foxen
)
: main "" pop ;
.
c
q
@register lib-cases=lib/case
@register #me lib-cases=tmp/prog1
@set $tmp/prog1=V
@set $tmp/prog1=/_/de:A scroll containing a spell called lib-cases
@set $tmp/prog1=/_defs/case:begin dup
@set $tmp/prog1=/_defs/default:pop 1 if
@set $tmp/prog1=/_defs/end:break then dup
@set $tmp/prog1=/_defs/endcase:pop pop 1 until
@set $tmp/prog1=/_defs/when:if pop