source: mainline/uspace/dist/src/sysel/demos/switch.sy@ 34b2f54d

Last change on this file since 34b2f54d was f1bed857, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 4 years ago

Update sysel scripts

  • Property mode set to 100644
File size: 4.3 KB
Line 
1--
2-- SPDX-FileCopyrightText: 2011 Jiri Svoboda
3--
4-- SPDX-License-Identifier: BSD-3-Clause
5--
6
7--- Switch statement demo.
8--
9-- This demonstrates the switch statement. The switch statement looks like:
10--
11-- switch <s_expr> is
12-- when <e1> [, <e2> ...] do
13-- <statements1>
14-- when <f1> [, <f2> ...] do
15-- <statements2>
16-- [...]
17-- [else do
18-- <statements3>]
19-- end
20--
21-- It is mostly equivalent to the pseudocode:
22--
23-- var v : <type of s_expr>;
24-- if v == <e1> [or v == <e2> ...] then
25-- <statements1>
26-- elif v == <f1> [ or v == <f2> ...] then
27-- <statements2>
28-- [...]
29-- [else
30-- <statements3>]
31-- end
32--
33-- This means there is no artificial limitation as to what you can switch
34-- on. The expressions don't have to be constant and they can be of any type
35-- as long as the switch expression (<s_expr>) can be compared with each
36-- of the when expressions (<e1>, <e2>, <f1>, <f2>, ...).
37--
38-- Note however, the 'or' here denotes short-circuit evaluation. (Currently
39-- the 'or' operation does not use short-circuit evaluation, but that's
40-- more of a bug than a feature.
41--
42class SwitchDemo is
43 class A is
44 end
45
46 var a1 : A, static;
47 var a2 : A, static;
48 var a3 : A, static;
49 var a4 : A, static;
50
51 enum MyEnum is
52 red;
53 green;
54 blue;
55 black;
56 cyan;
57 end
58
59 -- Switch on boolean value
60 fun SwitchBool(v : bool) : string, static is
61 switch v is
62 when false do
63 return "false";
64 when true do
65 return "true";
66 end
67 end
68
69 -- Switch on char value
70 fun SwitchChar(v : char) : string, static is
71 switch v is
72 when 'a', 'b' do
73 return "a/b";
74 when 'c', 'd' do
75 return "c/d";
76 else do
77 return "<other>";
78 end
79 end
80
81 -- Switch on integer value
82 fun SwitchInt(v : int) : string, static is
83 switch v is
84 when 0, 1 do
85 return "0/1";
86 when 2, 3 do
87 return "2/3";
88 else do
89 return "<other>";
90 end
91 end
92
93 -- Switch on string value
94 fun SwitchString(v : string) : string, static is
95 switch v is
96 when "foo", "bar" do
97 return "foo/bar";
98 when "hello", "world" do
99 return "hello/world";
100 else do
101 return "<other>";
102 end
103 end
104
105 -- Switch on object reference
106 fun SwitchObj(v : A) : string, static is
107 switch v is
108 when a1, a2 do
109 return "a1/a2";
110 when a3, a4 do
111 return "a3/a4";
112 else do
113 return "<other>";
114 end
115 end
116
117 -- Switch on object reference
118 fun SwitchEnum(v : MyEnum) : string, static is
119 switch v is
120 when MyEnum.red, MyEnum.green do
121 return "red/green";
122 when MyEnum.blue, MyEnum.black do
123 return "blue/black";
124 else do
125 return "<other>";
126 end
127 end
128
129 fun Test(res : string; test_expr : string; expected : string), static is
130 Console.WriteLine(test_expr + " = '" + res + "' (expected '" +
131 expected + "')");
132 if res != expected then
133 Console.WriteLine("Error: Got unexpected result.");
134 raise new Error.Base();
135 end
136 end
137
138 fun Main(), static is
139 a1 = new A();
140 a2 = new A();
141 a3 = new A();
142 a4 = new A();
143
144 Test(SwitchBool(false), "SwitchBool(false)", "false");
145 Test(SwitchBool(true), "SwitchBool(true)", "true");
146
147 Test(SwitchChar('a'), "SwitchChar('a')", "a/b");
148 Test(SwitchChar('b'), "SwitchChar('b')", "a/b");
149 Test(SwitchChar('c'), "SwitchChar('c')", "c/d");
150 Test(SwitchChar('d'), "SwitchChar('d')", "c/d");
151 Test(SwitchChar('e'), "SwitchChar('e')", "<other>");
152
153 Test(SwitchInt(0), "SwitchInt(0)", "0/1");
154 Test(SwitchInt(1), "SwitchInt(1)", "0/1");
155 Test(SwitchInt(2), "SwitchInt(2)", "2/3");
156 Test(SwitchInt(3), "SwitchInt(3)", "2/3");
157 Test(SwitchInt(4), "SwitchInt(4)", "<other>");
158
159 Test(SwitchString("foo"), "SwitchString('foo')", "foo/bar");
160 Test(SwitchString("bar"), "SwitchString('bar')", "foo/bar");
161 Test(SwitchString("hello"), "SwitchString('hello')", "hello/world");
162 Test(SwitchString("world"), "SwitchString('world')", "hello/world");
163 Test(SwitchString("boom"), "SwitchString('boom')", "<other>");
164
165 Test(SwitchObj(a1), "SwitchObj(a1)", "a1/a2");
166 Test(SwitchObj(a2), "SwitchObj(a2)", "a1/a2");
167 Test(SwitchObj(a3), "SwitchObj(a3)", "a3/a4");
168 Test(SwitchObj(a4), "SwitchObj(a4)", "a3/a4");
169 Test(SwitchObj(nil), "SwitchObj(nil)", "<other>");
170
171 Test(SwitchEnum(MyEnum.red), "SwitchEnum(MyEnum.red)", "red/green");
172 Test(SwitchEnum(MyEnum.green), "SwitchEnum(MyEnum.green)", "red/green");
173 Test(SwitchEnum(MyEnum.blue), "SwitchEnum(MyEnum.blue)", "blue/black");
174 Test(SwitchEnum(MyEnum.black), "SwitchEnum(MyEnum.black)", "blue/black");
175 Test(SwitchEnum(MyEnum.cyan), "SwitchEnum(MyEnum.cyan)", "<other>");
176 end
177end
Note: See TracBrowser for help on using the repository browser.