File: objexamp.fs

package info (click to toggle)
gforth 0.7.3+dfsg-9
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 5,992 kB
  • sloc: ansic: 8,535; sh: 3,666; lisp: 1,778; makefile: 1,019; yacc: 186; sed: 141; lex: 102; awk: 21
file content (185 lines) | stat: -rw-r--r-- 2,626 bytes parent folder | download | duplicates (10)
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
\ examples and tests for objects.fs

\ written by Anton Ertl 1996-1998
\ public domain

cr object heap-new print

object class

:noname ( object -- )
    drop ." undefined" ;
overrides print
end-class nothing

nothing heap-new constant undefined

cr undefined print

\ instance variables and this
object class
    cell% inst-var n
m: ( object -- )
   0 n ! ;m
overrides construct
m: ( object -- )
    n @ . ;m
overrides print
m: ( object -- )
    1 n +! ;m
method inc
end-class counter

counter heap-new constant counter1

cr
counter1 print
counter1 inc
counter1 print
counter1 inc
counter1 inc
counter1 inc
counter1 print
counter1 print

\ examples of static binding

cr undefined bind object print
: object-print ( object -- )
    [bind] object print ;

cr undefined object-print

\ interface

\ sorry, a meaningful example would be too long

interface
selector add ( n object -- )
selector val ( object -- n )
end-interface foobar

counter class
    foobar implementation

m: ( object -- )
    this [parent] inc
    n @ 10 mod 0=
    if
	." xcounter " this object-print ." made another ten" cr
    then
;m overrides inc
    
m: ( n object -- )
    0 do
	this inc
    loop
;m overrides add

m: ( object -- n )
    n @
;m overrides val

end-class xcounter


object class
    foobar implementation

    cell% inst-var n

m: ( n object -- )
    n !
;m overrides construct

m: ( object -- )
    n @ .
;m overrides print

m: ( n object -- )
    n +!
;m overrides add

protected

create protected1

protected

create protected2

cr order

public

create public1

cr order

\ we leave val undefined
end-class int

\ a perhaps more sensible class structure would be to have int as
\ superclass of counter, but that would not exercise interfaces

xcounter dict-new constant x
create y 3 int dict-new drop \ same as "3 int dict-new constant y"

cr
y print cr
20 x add
20 y add
x val .
\ y val . \ undefined
y print
cr
int push-order
order cr
words cr
int drop-order
order
cr

\ test override of inherited interface selector
xcounter class

m: ( object -- n )
    this [parent] val 2*
;m overrides val

end-class ycounter

ycounter dict-new constant z
cr
z print cr
z val . cr
z inc
z val . cr
1 z add
z val . cr

\ test inst-value
object class
    foobar implementation

    inst-value N

    m: ( n object -- )
        this [parent] construct \ currently does nothing, but who knows
        [to-inst] N
    ;m overrides construct

    m: ( object -- )
        N .
    ;m overrides print

    m: ( object -- n )
        N
    ;m overrides val
end-class const-int

5 const-int heap-new constant five
five print
five val 1+ . cr
.s cr