source: mainline/uspace/dist/src/sysel/demos/property.sy@ 9ee55cd

serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 9ee55cd was 051b3db8, checked in by Jiri Svoboda <jiri@…>, 14 years ago

Update SBI to rev. 344 from upstream. What's new:

  • Builtin.WriteLine() renamed to Console.WriteLine()
  • Implemented 'switch' statement
  • Significantly reduced memory consumption (also increases execution speed in some cases)
  • Properties can be accessed via unqualified names
  • Exceptions raised during property accesses are now handled correctly
  • Some missing checks against expressions returning no value added
  • Property mode set to 100644
File size: 3.6 KB
Line 
1--
2-- Copyright (c) 2010 Jiri Svoboda
3-- All rights reserved.
4--
5-- Redistribution and use in source and binary forms, with or without
6-- modification, are permitted provided that the following conditions
7-- are met:
8--
9-- o Redistributions of source code must retain the above copyright
10-- notice, this list of conditions and the following disclaimer.
11-- o Redistributions in binary form must reproduce the above copyright
12-- notice, this list of conditions and the following disclaimer in the
13-- documentation and/or other materials provided with the distribution.
14-- o The name of the author may not be used to endorse or promote products
15-- derived from this software without specific prior written permission.
16--
17-- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18-- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19-- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20-- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21-- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22-- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23-- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24-- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25-- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26-- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27--
28
29class Foo is
30 var x : int;
31
32 -- Example of a named property
33 prop X : int is
34 get is
35 Console.Write("Getting value of X which is ");
36 Console.WriteLine(x);
37 return x;
38 end
39
40 set value is
41 Console.Write("Setting value of X to ");
42 Console.WriteLine(value);
43 x = value;
44 end
45 end
46
47 -- Test accessing property via an unqualified name
48 fun TestUnqualPropAcc() is
49 var i : int;
50
51 X = 1;
52 i = X;
53
54 Console.Write("TestUnqualPropAcc(): Got ");
55 Console.WriteLine(i);
56 end
57
58 -- Backing store for indexed properties
59 var iprops : int[];
60
61 -- Example of an indexed property set (i.e. an indexer)
62 prop self[index : int] : int is
63 get is
64 Console.Write("Getting property with index ");
65 Console.Write(index);
66 Console.Write(" which is ");
67 Console.WriteLine(iprops[index]);
68
69 return iprops[index];
70 end
71
72 set value is
73 Console.Write("Setting property with index ");
74 Console.Write(index);
75 Console.Write(" to ");
76 Console.WriteLine(value);
77
78 iprops[index] = value;
79 end
80 end
81
82 --
83 -- Class-type property. This is used for demonstrating property
84 -- field access. This case is still quite easy. It does not require
85 -- read-modify-write. Since class is a reference type, access
86 -- operator will read the value and dereference it, thereby
87 -- getting somewhere else (so the value will not be modified and
88 -- need not be written back).
89 --
90
91 var bprop : Bar;
92
93 prop B : Bar is
94 get is
95 Console.WriteLine("Getting B");
96 return bprop;
97 end
98 set value is
99 Console.WriteLine("Setting B");
100 bprop = value;
101 end
102 end
103
104end
105
106class Bar is
107 var i : int;
108end
109
110class PropertyDemo is
111 fun Main(), static is
112 var a : Foo;
113 var i : int;
114
115 a = new Foo();
116
117 -- Get value of named property.
118 a.X = 1;
119
120 -- Set value of named property.
121 i = a.X;
122
123 Console.Write("Main(): Got ");
124 Console.WriteLine(i);
125
126 a.TestUnqualPropAcc();
127
128 a.iprops = new int[5];
129
130 -- Set value of indexed property.
131 a[1] = 2;
132
133 -- Get value of indexed property.
134 i = a[1];
135
136 Console.Write("Main(): Got ");
137 Console.WriteLine(i);
138
139 -- Property field access
140 var b : Bar;
141
142 b = new Bar();
143
144 b.i = 42;
145 a.bprop = b;
146
147 Console.WriteLine(a.bprop.i);
148 a.bprop.i = 2;
149 Console.WriteLine(a.bprop.i);
150 end
151end
Note: See TracBrowser for help on using the repository browser.