-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexp.lua
More file actions
172 lines (148 loc) · 3.41 KB
/
exp.lua
File metadata and controls
172 lines (148 loc) · 3.41 KB
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
local st = require("object_model")
-- local dbg = require("debugger")
local is_luajit = (type(jit) == "table")
local function chain(from)
setmetatable(from, { __index = _G })
return setmetatable({}, { __index = from })
end
if is_luajit then
setfenv(1, chain(st))
else
_ENV = chain(st)
end
X = Class:subclass("X")
X:addMethod("a", function(self)
print("In a")
local b = (Block:create(function()
print("BLOCK EXEC")
return_("BLOCKRETURN")
end))
self:b(b)
print("In a, end (should not display)")
end)
X:addMethod("b", function(self, ablock)
print("In b")
self:c(ablock)
print("In b, after value (should not display)")
end)
X:addMethod("c", function(self, ablock)
print("In c")
ablock:value()
print("In c, after value (should not display)")
end)
local x = X:new()
print(x:a())
X:addStMethod(
[[
d
^ self isKindOf: X
]],
{ debug = false }
)
print(x:d())
local function wrap(obj)
if type(obj) == "table" and obj.__st then
return obj
end
if obj == nil then
return World["nil"]
elseif obj == true then
return World["true"]
elseif obj == false then
return World["false"]
elseif type(obj) == "number" then
return World.Integer:create(obj)
end
end
World.Integer:addMethod("factorial", function(self)
self:_BIN_EQ(World["0"]):ifTrue_((Block:create(function()
return_(World["1"])
end)))
self:_BIN_SUP(World["0"]):ifTrue_((Block:create(function()
return_(self:_BIN_STAR((self:_BIN_MIN(World["1"])):factorial()))
end)))
return self
end)
World.Integer:addMethod("_BIN_EQ", function(self, other)
return wrap(self.value == other.value)
end)
World.Integer:addMethod("_BIN_SUP", function(self, other)
return wrap(self.value > other.value)
end)
World.Integer:addMethod("_BIN_STAR", function(self, other)
return wrap(self.value * other.value)
end)
World.Integer:addMethod("_BIN_MIN", function(self, other)
return wrap(self.value - other.value)
end)
World.Integer:addMethod("_BIN_INFEQ", function(self, other)
return wrap(self.value <= other.value)
end)
World.Integer:addStMethod(
[[
factorial
self = 0 ifTrue: [ ^ 1].
self > 0 ifTrue: [ ^ self * (self - 1) factorial ].
]],
{ debug = true }
)
print(World["10"]:factorial())
World.Integer:addStMethod(
[[
factorialWithAccumulator: acc
self = 0 ifTrue: [ ^ acc ].
self > 0 ifTrue: [ ^ (self - 1) factorialWithAccumulator: acc * self ].
]],
{ debug = false }
)
print(World["127"]:factorialWithAccumulator_(World["1"]))
print(World["127"]:factorial())
World.Integer:addStMethod([[
fibonacci
self = 0 ifTrue: [ ^ 0 ].
self = 1 ifTrue: [ ^ 1 ].
self > 1 ifTrue: [ ^ (self - 1) fibonacci + (self - 2) fibonacci ].
self < 0 ifTrue: [ self error: 'Fibonacci is not defined for negative numbers' ].
]])
World.Integer:addStMethod(
[[
timesRepeat: aBlock
| count |
count := 1.
[count <= self]
whileTrue:
[ aBlock value.
count := count + 1.
count ]
]],
{ debug = false }
)
X:addStMethod(
[[
test
| CC cc |
CC := Object subclass: 'CC'.
cc := CC new.
Transcript show: cc; cr.
^ cc
]],
{ debug = false }
)
print(x:test())
local X = Object:subclass("X")
local x = X:new()
print(x) -- displays "a X"
X:addMethod("m1_", function(self, other)
print("Hello from Lua", other)
end)
X:addStMethod([[
m2: other
Transcript show: 'Hello from Smalltalk (actually Lua) '.
Transcript show: other; cr
]], { debug = true })
X.class:addMethod("create", function (self)
return self:new()
end)
local x2 = X:create()
x2:m1_('in Lua')
x2:m2_('in Lua-Smalltalk')