File: oofsampl.fs

package info (click to toggle)
gforth 0.7.3%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 5,888 kB
  • ctags: 1,977
  • sloc: ansic: 8,535; sh: 3,666; lisp: 1,778; makefile: 1,011; yacc: 186; sed: 141; lex: 102; awk: 21
file content (199 lines) | stat: -rw-r--r-- 4,920 bytes parent folder | download | duplicates (12)
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
\ oof.fs	Object Oriented FORTH
\ 		This file is (c) 1996 by Bernd Paysan
\			e-mail: paysan@informatik.tu-muenchen.de
\
\		Please copy and share this program, modify it for your system
\		and improve it as you like. But don't remove this notice.
\
\		Thank you.
\

\ Data structures: data                                28nov93py

: place ( addr1 n addr2 -- )
    over >r rot over 1+ r> move c! ;

: i!  postpone ! ; immediate
: i@  postpone @ ; immediate

object class data       \ abstract data class
        cell var ref  \ reference counter
        method !        method @        method .
        method null     method atom?    method #
how:    : atom? ( -- flag )  true ;
        : #     ( -- n )     0 ;
        : null  ( -- addr )  new ;
class;

\ Data structures: int                                 30apr93py

data class int
        cell var value
how:    : !     value i! ;
        : @     value i@ ;
        : .     @ 0 .r ;
        : init  ( data -- )  ! ;
        : dispose  -1 ref +!
          ref i@ 0> 0= IF  super dispose  THEN ;
        : null  0 new ;
class;



\ Data structures: list                                17nov93py

0 Value nil

data class lists
        data ptr first  data ptr next
        method empty?   method ?
how:    : null  nil ;
        : atom? false ;
class;

lists class nil-class

how:    : empty? true ;
        : dispose ;
        : .  ." ()" ;
class;

nil-class : (nil                (nil self TO nil
nil (nil bind first             nil (nil bind next

    
\ Data structures: list                                12mar94py

lists class linked
how:    : empty? false ;
        : #      next # 1+ ;
        : ?      first . ;
        : @      first @ ;
        : !      first ! ;
        : init ( first next -- )
                 dup >o 1 ref +! o> bind next
		 dup >o 1 ref +! o> bind first ;
        : .      self >o  [char] (
                 BEGIN  emit ? next atom? next self o> >o
                        IF ."  . " data . o> ." )" EXIT THEN bl
                        empty?  UNTIL  o> drop ." )" ;
        : dispose  -1 ref +!  ref i@ 0> 0=
          IF   first dispose  next dispose  super dispose  THEN ;
class;
      
\ Data structures: string                              04dec93py

int class string
how:    : !     ( addr count -- )
                value i@ over 1+ resize throw value i!
                value i@ place ;
        : @     ( -- addr count )  value i@ count ;
        : .     @ type ;
        : init  ( addr count -- )
          dup 1+ allocate throw value i! value i@ place ;
        : null  S" " new ;
        : dispose ref i@ 1- 0> 0=
          IF  value i@ free throw  THEN  super dispose ;
class;

\ Data sturctures: pointer                             17nov93py

data class pointer
        data ptr container
        method ptr!
how:    : !     container ! ;
        : @     container @ ;
        : .     container . ;
        : #     container # ;
        : init  ( data -- )  dup >o 1 ref +! o> bind container ;
        : ptr!  ( data -- )  container dispose  init ;
        : dispose  -1 ref +!  ref i@ 0> 0=
          IF  container dispose super dispose  THEN ;
        : null  nil new ;
class;

\ Data sturctures: array                               30apr93py

data class array
        data [] container
        cell var range
how:    : !     container ! ;
        : @     container @ ;
        : .     [char] [
                # 0 ?DO emit I container . [char] , LOOP drop ." ]" ;
        : init  ( data n -- )  range i!  bind container ;
        : dispose  -1 ref +!  ref i@ 0> 0=
          IF  # 0 ?DO  I container dispose  LOOP
              super dispose  THEN ;
        : null  nil 0 new ;
        : #     range i@ ;
        : atom? false ;
class;

\ Data structure utilities                             17nov93py

: cons  linked new ;
: list  nil cons ;
: car   >o lists first self o> ;
: cdr   >o lists next  self o> ;
: print >o data . o> ;
: ddrop >o data dispose o> ;
: make-string string new ;
: $"    state @ IF  postpone S" postpone make-string exit  THEN
  [char] " parse make-string ;       immediate

\ Examples

$" This" $" is" $" a" list cons $" example" $" list" list cons list cons cons
cr dup print
cr dup car print
cr dup cdr cdr car print
pointer : list1
cr list1 .

1 2 3  3 int new[] 3 array : lotus
cr lotus .
cr 2 lotus @ .
cr 0 lotus @ .
cr 5 1 lotus ! lotus .

\ Interface test

interface bla
method fasel
method blubber
method Hu
how:
: fasel ." Bla Fasel" Hu ;
: blubber ." urps urps" Hu fasel ;
interface;

object class test
bla
method .
how:
: Hu ." ! " ;
: . fasel ;
class;

test : test1
cr test1 fasel
cr test1 blubber
cr test1 .
cr test1 self >o bla blubber o>

\ This should output the following lines:
\
\ (This (is a) (example list))
\ This
\ (example list)
\ (This (is a) (example list))
\ [1,2,3]
\ 3 
\ 1 
\ [1,5,3]
\ Bla Fasel! 
\ urps urps! Bla Fasel!
\ Bla Fasel! 
\ urps urps! Bla Fasel!