Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 141 lines (110 sloc) 2.971 kb
be3ce3f @drahosp Import to git
drahosp authored
1 --------------------------------------------------------------------------
2 -- STANDARD LIBRARY --
3 --------------------------------------------------------------------------
4
5 -- Ha, ha, ha.
6 --
7 -- This is a bare-bones base class, needed for defining any other
8 -- classes. (While you *can* create new class hierarchies if you like,
9 -- it's rather against the whole Objective Lua spirit.)
10 --
11 -- The doesNotRecogniseSelector: support is broken, and is only included
12 -- as an example of how this will work. The noddy object system that the
13 -- Objective Lua runtime uses is incompatible with this.
14
15 @interface Object
16 @end
17
18 @implementation Object
19 + alloc
20 local o = {}
21 setmetatable(o, {__index = self._methods })
22 return o
23 end
24
25 -- Bug: these both report the object as Object, not the correct one.
26 -- We're going to have to rewrite the object system to make this work.
27
28 + doesNotRecogniseSelector: selector
29 error('Unrecognised selector '..selector..' sent to '..
30 ([self name]))
31 end
32
33 - doesNotRecogniseSelector: selector
34 error('Unrecognised selector '..selector..' sent to instance of '..
35 ([[self class] name]))
36 end
37
38 setmetatable(Object._methods, {__index = Object._methods.doesNotRecogniseSelector_})
39 setmetatable(Object, {__index = Object.doesNotRecogniseSelector_})
40
41 - init
42 return self
43 end
44 @end
45
46 --------------------------------------------------------------------------
47 -- EXAMPLE CODE --
48 --------------------------------------------------------------------------
49
50 -- A very simple example showing how to use the various Objective Lua
51 -- features. It's mostly intended to be read, rather than to do anything
52 -- useful.
53
54 @interface Organism : Object
55 @end
56
57 @interface Plant : Organism
58 @end
59
60 @interface Cabbage : Plant
61 @end
62
63 @interface Foxglove : Plant
64 @end
65
66 @interface Animal : Organism
67 @end
68
69 @interface Dog : Animal
70 @end
71
72 @interface Cow : Animal
73 @end
74
75 @implementation Organism
76 - consume
77 print("I doesn't look edible.")
78 end
79
80 - vocalise
81 print("I don't make noises.")
82 end
83
84 - announce
85 print("I'm a "..([[self class] name]).."!")
86 end
87 @end
88
89 @implementation Cabbage
90 - consume
91 print("Yuk. Vegetables.")
92 end
93 @end
94
95 @implementation Foxglove
96 - consume
97 print("Suicide is not the only option.")
98 end
99 @end
100
101 @implementation Animal
102 - consume
103 print("Mmm, proteinaceous cytoplasm.")
104 end
105 @end
106
107 @implementation Dog
108 - consume
109 [super consume]
110 print("Needs sauce.")
111 end
112
113 - vocalise
114 print("Woof!")
115 end
116 @end
117
118 @implementation Cow
119 - consume
120 [super consume]
121 print("Yum!")
122 end
123
124 - vocalise
125 print("Moo!")
126 end
127 @end
128
129 -- Now we've defined some classes, let's do something with them.
130 -- Note the traditional two-stage object construction. This does make
131 -- sense, honest.
132
133 local function process(object)
134 [object announce]
135 [object vocalise]
136 [object consume]
137 end
138
139 process([[Dog alloc] init])
140 process([[Foxglove alloc] init])
Something went wrong with that request. Please try again.