File: example38.spl

package info (click to toggle)
spl 1.0~pre4-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,300 kB
  • ctags: 2,057
  • sloc: ansic: 16,076; yacc: 3,184; sh: 296; makefile: 180; xml: 156
file content (199 lines) | stat: -rw-r--r-- 2,999 bytes parent folder | download | duplicates (5)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
 *  SPL - The SPL Programming Language
 *  Copyright (C) 2004, 2005  Clifford Wolf <clifford@clifford.at>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *  example38.spl: Some more advanced object tests
 */


function test1()
{
	function getobj() {
		var strange_var = 1;

		object Foobar {
			method strange() {
				return "${ strange_var++ } ${ this } ${ strange }";
			}
		}

		return Foobar;
	}

	var class1 = getobj();
	var obj1 = new class1();

	object Class2 class1 {
	}

	var obj2 = new Class2();

	debug obj1.strange();
	debug obj2.strange();
}

function test2()
{
	object Foo {
		method foo() {
			debug "Foo:  ${this} -> foo()";
		}
		method bar() {
			return "Bar:  ${this} -> bar()";
		}
		method init() {
			debug "Init: ${this}";
			return this;
		}
	}

	object Bar Foo {
		var count = 12;
		method bar() {
			count = count + 111;
			debug "${ *Foo.bar() } --> ${ count }";
		}
	}

	var f = new Foo();
	var b = new Bar();

	b.foo();
	b.bar();

	f.foo();
	debug f.bar();

	var x = new Bar();
	x.bar();

	var x = new Bar();
	x.bar();
}

function test3()
{
	object Bla {
		var title = "a demo.";
	}

	object Foo {
		var y = 23;
		var xxx;
		method init() {
			y = 42;
			xxx[0] = new Bla();
			xxx[0].title = "This is";
			return this;
		}
	}

	var x = new Foo();

	debug x.xxx[0].title;
	debug Bla.title;
}

function test4()
{
	var foo;
	var bar;

	function regbar(blup) {
		bar = blup;
	}

	object Demo {
		var bla = 0;
		method getbla() {
			return bla;
		}
		method init(x) {
			bla = x;
			foo = getbla;
			regbar(getbla);
			return this;
		}
	}

	var foobar = new Demo(42);

	debug foobar.getbla();

	debug foo();
	debug bar();
}

function test5()
{
	object Value {
		var val;
	}

	var a = new Value();
	var b = new Value();

	function get_base_obj()
	{
		var x = a;

		object Foo {
			method set_a(val) {
				x.val = val;
			}
		}

		return Foo;
	}

	function get_derived_obj(base)
	{
		var x = b;

		object Foo base {
			method set_b(val) {
				x.val = val;
			}
		}

		return Foo;
	}

	var o = new (get_derived_obj(get_base_obj()))();

	o.set_a(23);
	o.set_b(42);

	debug "a=$a.val";
	debug "b=$b.val";
}

debug "---- Test #1 ----";
test1();

debug "---- Test #2 ----";
test2();

debug "---- Test #3 ----";
test3();

debug "---- Test #4 ----";
test4();

debug "---- Test #5 ----";
test5();