-
Notifications
You must be signed in to change notification settings - Fork 1
/
text2code.rb
348 lines (291 loc) · 12.6 KB
/
text2code.rb
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
class Fixnum
def ^(exponent); self**exponent end
end
class Float
def even?; self.whole? ? self.to_i.even? : false; end
def odd?; self.whole? ? self.to_i.odd? : false; end
def times █ self.to_i.times(&block); end
end
class Numeric
def ^(exponent); self**exponent end
def sqrt; Math.sqrt(self); end
def abs; self.abs; end
def positive?; self > 0; end
def negative?; self < 0; end
def whole?; self.round == self; end
def divisible_by?(d); self.modulo(d) == 0; end
def factor_of?(m); m.divisible_by?(self); end
def prime?
return false if !self.whole? || self <= 1
for d in 2..Math.sqrt(self) do
return false if self.divisible_by?(d)
end
return true
end
def is(inst)
Text2Code::INST_SET.send(:is, inst)
set __free_num__ to self
Text2Code::INST_SET.send(:__free_num__, inst)
end
def divided(inst)
Text2Code::INST_SET.send(:divided, inst)
set __free_num__ to self
Text2Code::INST_SET.send(:__free_num__, inst)
end
end
class String
def char_at(index); self[index-1..index-1]; end
def substring(startIndex,endIndex); self[startIndex-1...endIndex]; end
def sfind(needle); self.index(needle)+1 end
def rfind(needle); self.rindex(needle)+1 end
def get(inst)
Text2Code::INST_SET.send(:get, inst)
set __free_string__ to self
Text2Code::INST_SET.send(:__free_string__, inst)
end
def find(inst)
Text2Code::INST_SET.send(:find, inst)
set __free_string__ to self
Text2Code::INST_SET.send(:__free_string__, inst)
end
end
module Text2Code
class Utility
def self.prompt_num(msg)
puts msg
gets.to_f
end
def self.prompt_str(msg)
puts msg
gets.chomp
end
def self.length(s); s.length; end
def self.sqrt(x); Math.sqrt(x); end
def self.abs(x); x.abs; end
def self.rand_int(low, high); rand(low.floor..high.floor); end
def self.rand_dec(low, high); rand(low.to_f..high.to_f); end
end
class Variable
def initialize(val=nil)
@value = val
end
def get(*args)
@value
end
def set(val)
@value = val
end
def increase(offset)
@value += offset
end
def decrease(offset)
@value -= offset
end
def multiply(offset)
@value *= offset
end
def divide(offset)
@value /= offset
end
def method_missing(method_name, *args)
@value.send(method_name, *args)
end
end
class InstructionComponents
attr_reader :has_receiver, :arguments, :messages
#attr_reader :part_types
def initialize(instComponents=nil)
if(instComponents.class == InstructionComponents)
@has_receiver = instComponents.has_receiver
@messages = instComponents.messages
@arguments = instComponents.arguments
#@part_types = instComponents.part_types
else
@has_receiver = false
@messages = [];
@arguments = 0
#@part_types = nil
end
end
def add_type(part_type, part_val=nil)
case part_type
when :receiver
has_receiver = @has_receiver
@has_receiver = true
instComponents = InstructionComponents.new(self)
@has_receiver = has_receiver
when :message
messages = @messages
@messages = @messages + [] # copy of array
@messages.push(part_val)
instComponents = InstructionComponents.new(self)
@messages = messages
when :argument
@arguments += 1
instComponents = InstructionComponents.new(self)
@arguments -= 1
when :optional
# hack : these should be optional so not added as part of the messages
messages = @messages
@messages = @messages + [] # copy of array
@messages.push(part_val)
instComponents = InstructionComponents.new(self)
@messages = messages
end
return instComponents
end
def is_complete?
has_receiver && @messages != []
end
def has_schema(inst)
return inst.arguments.length == @arguments &&
(!!inst.receiver == @has_receiver || inst.variables.length > 0) &&
(@messages & inst.methods).length >= (@messages.length < inst.methods.length ? @messages.length : inst.methods.length) &&
(@messages & inst.methods).length >= (@messages.length > inst.methods.length ? @messages.length - 1 : inst.methods.length - 1)
# this needs to be fixed
#@messages.all? { |message| inst.methods.include?(message) } &&
#inst.methods.all? { |message| @messages.include?(message) }
#(inst.method_name == @message || inst.methods.include?(@message))
end
end
class Instruction
attr_accessor :receiver, :method_name, :arguments
attr_accessor :methods, :variables
def initialize(*args)
args[0].class == Instruction ? args[0].copy : Instruction.create_from_args(self, :args, *args)
end
def self.instruction_types
[:args, :receiver_last]
#[:receiver_last, :args]
end
def self.create_all_instruction_types(*args)
return Instruction.instruction_types.map {|type| Instruction.create_from_args(nil, type, *args)}
end
def copy
inst = Instruction.new
inst.receiver = @receiver
inst.method_name = @method_name
inst.arguments = @arguments.map {|argument| argument} # shallow copy of array
inst.methods = @methods
inst.variables = @variables
inst
end
def self.create_from_args(inst, type, *args)
inst = inst || Instruction.new
inst = args[0] if args.length > 0 && args[0].class == Instruction
#filteredArgs = args.select {|arg| arg.class != Instruction}
case type
when :receiver_last
last_arg = args.length > 0 ? args.pop : nil
inst.receiver = last_arg.class == Instruction ? last_arg.receiver : last_arg
end
inst.arguments = inst.arguments ? args + inst.arguments : args unless (args.length > 0 && args[0].class == Instruction) # for both :args and :receiver_last
#inst.arguments = inst.arguments ? filteredArgs + inst.arguments : filteredArgs
inst.methods = inst.methods || []
inst.variables = inst.variables || []
inst
end
#def construct_from_parts
#end
end
class InstructionSet
def initialize
@instruction_schemas = {:method_missing => [[InstructionComponents.new,:get]]} #name-value pairs
#@instruction_schemas = {}
@variable_set = {}
end
def add_instruction_schema(schema_parts, method_to_call)
# Consider saving current instead of previous
prev_inst_comps = InstructionComponents.new
curr_inst_comps = []
schema_parts.reverse.each_with_index do |part, index|
if part.class == Symbol
curr_inst_comps = prev_inst_comps.add_type(part)
elsif part.class == Hash
part.each do |method_name, instruction_type|
method_name = (method_name != :variable_) ? method_name : :method_missing
@instruction_schemas[method_name] = [] unless @instruction_schemas.has_key?(method_name)
curr_inst_comps = prev_inst_comps.add_type(instruction_type, method_name)
@instruction_schemas[method_name].push([prev_inst_comps, index == schema_parts.length-1 ? method_to_call : nil])
end
end
prev_inst_comps = curr_inst_comps
end
end
def create_methods
@instruction_schemas.each do |method_name, inst_component_method_pairs_arr|
self.class.send(:define_method, method_name) do |*args|
method_to_call = nil
is_variable = false
if method_name == :method_missing then
method_nameV = args.shift() # changing method_nameV to method_name makes it defined on subsequent calls?
@variable_set[method_nameV] = @variable_set[method_nameV] || Variable.new
is_variable = true
end
pair = nil
instruction_to_run = Instruction.create_all_instruction_types(*args).detect do |instruction|
instruction.methods.push(method_name) unless is_variable
instruction.variables.push(method_nameV) if is_variable
pair = inst_component_method_pairs_arr.detect { |component| component[0].has_schema(instruction) }
method_to_call = pair[1] if pair.class == Array
if pair
#instruction.method_name = instruction.method_name || pair[0].message
instruction.receiver = instruction.receiver || @variable_set[instruction.variables.shift()] if (pair && pair[0].has_receiver)
end
pair
end
if method_to_call && instruction_to_run
if is_variable && instruction_to_run.receiver == nil
instruction_to_run.receiver = @variable_set[instruction_to_run.variables.shift()]
end
instruction_to_run.receiver = instruction_to_run.receiver || Utility
return instruction_to_run.receiver.send(method_to_call, *instruction_to_run.arguments)
end
return instruction_to_run || Instruction.new(*args)
nil
end
end
end
def getMatchingSchema(partial_inst)
end
end
inst_set = InstructionSet.new
inst_set.add_instruction_schema([{:set => :message}, {:variable_ => :receiver}, {:to => :optional}, :argument], :set)
inst_set.add_instruction_schema([{:variable_ => :receiver}, {:is => :optional}, {:even => :message}], :even?)
inst_set.add_instruction_schema([{:variable_ => :receiver}, {:is => :optional}, {:odd => :message}], :odd?)
inst_set.add_instruction_schema([{:variable_ => :receiver}, {:is => :optional}, {:positive => :message}], :positive?)
inst_set.add_instruction_schema([{:variable_ => :receiver}, {:is => :optional}, {:negative => :message}], :negative?)
inst_set.add_instruction_schema([{:variable_ => :receiver}, {:is => :optional}, {:prime => :message}], :prime?)
inst_set.add_instruction_schema([{:variable_ => :receiver}, {:is => :optional}, {:whole => :message}], :integer?)
inst_set.add_instruction_schema([{:increase => :message}, {:variable_ => :receiver}, {:by => :optional}, :argument], :increase)
inst_set.add_instruction_schema([{:decrease => :message}, {:variable_ => :receiver}, {:by => :optional}, :argument], :decrease)
inst_set.add_instruction_schema([{:multiply => :message}, {:variable_ => :receiver}, {:by => :optional}, :argument], :multiply)
inst_set.add_instruction_schema([{:divide => :message}, {:variable_ => :receiver}, {:by => :optional}, :argument], :divide)
inst_set.add_instruction_schema([{:variable_ => :receiver}, {:is => :optional}, {:divisible => :message}, {:by => :optional}, :argument] , :divisible_by?)
inst_set.add_instruction_schema([{:variable_ => :receiver}, {:is => :optional}, {:factor => :message}, {:of => :optional}, :argument] , :factor_of?)
inst_set.add_instruction_schema([{:remainder => :message}, {:of => :optional}, {:variable_ => :receiver}, {:divided => :message}, {:by => :optional}, :argument] , :modulo)
inst_set.add_instruction_schema([{:absolute => :message}, {:of => :optional}, :argument], :abs) #receiver later
inst_set.add_instruction_schema([{:absolute => :message}, :argument], :abs) #receiver later
inst_set.add_instruction_schema([{:sqrt => :message}, :argument], :sqrt) #receiver later
inst_set.add_instruction_schema([{:square => :message}, {:root => :message}, {:of => :optional}, :argument], :sqrt) #receiver later
inst_set.add_instruction_schema([{:square => :message}, {:root => :message}, :argument], :sqrt) #receiver later
inst_set.add_instruction_schema([{:length => :message}, {:of => :message}, :argument], :length) # replace with receiver later
inst_set.add_instruction_schema([{:from => :optional}, {:text => :message}, {:variable_ => :receiver}, {:get => :message}, {:letter => :message}, :argument], :char_at)
inst_set.add_instruction_schema([{:from => :optional}, {:text => :message}, {:variable_ => :receiver}, {:get => :message},
{:substring => :message}, {:from => :optional}, :argument, :argument], :substring)
inst_set.add_instruction_schema([{:from => :optional}, {:text => :message}, {:variable_ => :receiver}, {:find => :message},
{:first => :message}, {:occurrence => :optional}, {:of => :optional}, :argument], :sfind)
inst_set.add_instruction_schema([{:from => :optional}, {:text => :message}, {:variable_ => :receiver}, {:find => :message},
{:last => :message}, {:occurrence => :optional}, {:of => :optional}, :argument], :rfind)
inst_set.add_instruction_schema([{:prompt => :message}, {:to => :optional}, {:get => :optional}, {:text => :message},
{:with => :optional}, {:message => :message}, :argument], :prompt_str)
inst_set.add_instruction_schema([{:prompt => :message}, {:to => :optional}, {:get => :optional}, {:number => :message},
{:with => :optional}, {:message => :message}, :argument], :prompt_num)
inst_set.add_instruction_schema([{:random => :message}, {:integer => :optional}, {:from => :optional}, :argument, :argument], :rand_int)
inst_set.add_instruction_schema([{:random => :message}, {:decimal => :optional}, {:from => :optional}, :argument, :argument], :rand_dec)
inst_set.create_methods
INST_SET = inst_set
end
def method_missing(name, *args)
Text2Code::INST_SET.send(name, *args) # redirect to standard Text2Code Instruction Set
end