-
Notifications
You must be signed in to change notification settings - Fork 1
/
casio-prog-txt-to-au3.rb
418 lines (379 loc) · 12 KB
/
casio-prog-txt-to-au3.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#! ruby
#
# This app converts transcribed Casio CFX-9800G programs from text form
# into an AutoIt script which will type in the program text and then
# run it.
#
# It expects one argument, the input text file name.
#
# The output is written to the file of the same name, but with an ".au3"
# extension. The script will overwrite files without prompting.
require 'set'
class AutoItTranslator
def initialize
# 'text' holds the plain text (for "Send") so far this line (if any).
@text = ''
# 'out' holds the AutoIt source code so far
@out = ''
# 'menu' holds the current menu state in the programme text editor
@menu = []
# 'referenced_filenames' holds the filenames of other programs referenced by this
# program
@referenced_filenames = Set.new
# The set of filenames in the calc which have been used. This is needed
# to detect name collisions and to avoid re-entering the same prog twice.
@used_calc_prog_names = Set.new
# If necessary, you can define regex replacements for the program text
@line_replacements = {}
end
# Before starting a key sequence (e.g. for a keyword like Goto),
# or at the end of the current line, we write all the plain text
# we have so far using 'Send'
def flush_text comment = ''
@out << "Send('#{@text}')"
unless comment.empty?
@out << ' ; ' << comment
@out << " (state is #{@menu})" unless @menu.empty?
end
@out << "\n"
elsif !comment.empty?
raise "Comment '#{comment}' given, but no text to flush"
end
@text = ''
end
def click button_name, comment = button_name
flush_text
x,y = case button_name
when 'SHIFT' then [73,376]
when 'OPTN' then [117,376]
when 'VARS' then [159,376]
when 'ALPHA' then [73,413]
when 'x^2' then [117,413]
when '^' then [159,413]
when 'EXIT' then [202,413]
when 'ln' then [159,451]
when 'sin' then [202,451]
when 'cos' then [246,451]
when 'tan' then [290,451]
when '->' then [290,487]
when 'fraction' then [73,487]
when '4' then [78,578]
when 'EXP' then [182,663]
else raise "Bad button_name '#{button_name}'"
end
@out << "MouseClick('left', $x + #{x}, $y + #{y}) ; #{comment}"
@out << " (state is #{@menu.join ' -> '})" unless @menu.empty?
@out << "\n"
end
def enter_menu menu_spec
menu_spec = menu_spec.split(/ -> /)
# It is unreliable to short-cut menu navigation
# (i.e. to get from A -> B to A -> C by pressing EXIT then C, rather than starting again
# at A), but we can at least skip navigation if we're already in place
return if menu_spec == @menu
# move down the hierarchy to the new menu.
# we assume that the inital menu is always reachable and that it discards the current
# menu state (e.g. pressing "PRGM" will get you into the PRGM menu even if you're currently
# inside the OPTN menu)
@menu.clear
menu_spec.each do |menu|
@menu.push menu
# Each case statement in this switch describes the last action in the chain
case @menu.join ' -> '
when 'PRGM'
click 'SHIFT'
click 'VARS', 'PRGM'
when 'PRGM -> CTL'
@text << '{F2}'
flush_text 'CTL'
when 'PRGM -> JUMP'
@text << '{F3}'
flush_text 'JUMP'
when 'PRGM -> more'
@text << '{F6}'
flush_text 'more'
when 'PRGM -> more -> REL'
@text << '{F3}'
flush_text 'REL'
when 'OPTN'
click 'OPTN'
when 'OPTN -> more'
@text << '{F6}'
flush_text 'more'
when 'OPTN -> more -> PROB'
@text << '{F3}'
flush_text 'PROB'
when 'OPTN -> more -> NUM'
@text << '{F4}'
flush_text 'NUM'
when 'CATALOG'
click 'SHIFT'
click '4', 'CATALOG'
when 'SKTCH'
click 'SHIFT'
@text << '{F4}'
flush_text 'SKTCH'
when 'SKTCH -> GRPH'
@text << '{F5}'
flush_text 'GRPH'
when 'SKTCH -> GRPH -> more'
@text << '{F6}'
flush_text 'more'
else
raise "Bad menu '#{@menu.join ' -> '}'"
end
end
end
# Converts the given txt line into au3
def translate_line line
if line =~ %r{^// fx-emulator: (.*)}
case $1
when 'Orange off'
@orange_behaviour = 'off'
when %r{s/(.*)/(.*)/}
@line_replacements[Regexp.new($1)] = $2
when /append "(.*)"/
@appended_lines << $1
else
raise "Unrecognised 'fx-emulator' command: #{$1}"
end
end
return if line =~ %r{^//}
@line_replacements.each do |from,to|
line = line.sub from, to
end
# (see below)
convert_next_comma_to_plus = false
line.scan /Prog "[^"]+"|Lbl |Goto |=\>|Isz |Dsz |e\^|Deg|Range |Int |Frac |Abs |(?:Orange |Green )?Plot |(?:Orange |Green )?Line|Ran#|-\>|\<=|\>=|!=|Graph Y(?:\<|\>|=|\>=|\<=)|Cls|Mcl|Deg|sin |cos |tan |sqrt|G\<-\>T|./ do |token|
case token
when '#'
enter_menu 'PRGM'
@text << "{F5}"
flush_text 'print result'
when 'Lbl ', 'Goto ', '=>', 'Isz ', 'Dsz '
enter_menu 'PRGM -> JUMP'
key = case token
when 'Lbl ' then 1
when 'Goto ' then 2
when '=>' then 3
when 'Isz ' then 4
when 'Dsz ' then 5
else raise "invalid token '#{token}'"
end
@text << "{F#{key}}"
flush_text token
when 'Abs ', 'Int ', 'Frac ', 'Rnd ', 'Intg ', 'RndFi '
enter_menu 'OPTN -> more -> NUM'
key = case token
when 'Abs ' then 1
when 'Int ' then 2
when 'Frac ' then 3
when 'Rnd ' then 4
when 'Intg ' then 5
when 'RndFi ' then 5
else raise "invalid token '#{token}'"
end
@text << "{F#{key}}"
flush_text token
when 'Range '
click 'SHIFT'
@text << '{F3}{F1}'
flush_text 'ViewWindow'
when /(Orange|Green)? ?Plot /
if $1 == 'Orange' && @orange_behaviour == 'off'
# To turn "Plot a,b" into a noop, we rewrite it as a+b
convert_next_comma_to_plus = true
else
enter_menu ''
click 'SHIFT'
@text << '{F4}{F6}{F1}{F1}'
flush_text 'Plot'
end
when /(Orange|Green)? ?Line/
if $1 == 'Orange' && @orange_behaviour == 'off'
# Turn "Orange Line" into a noop
@text << '0'
else
enter_menu ''
click 'SHIFT'
@text << '{F4}{F6}{F2}{F1}'
flush_text 'Line'
end
when '!=', '<=', '>='
enter_menu 'PRGM -> more -> REL'
key = case token
when '!=' then 2
when '>=' then 5
when '<=' then 6
else raise "invalid token '#{token}'"
end
@text << "{F#{key}}"
flush_text token
when 'Cls'
enter_menu 'SKTCH'
@text << "{F1}"
flush_text token
when 'Graph Y='
enter_menu 'SKTCH -> GRPH'
@text << "{F1}"
flush_text token
when 'Graph Y<=', 'Graph Y<', 'Graph Y>=', 'Graph Y>'
enter_menu 'SKTCH -> GRPH -> more'
key = case token
when 'Graph Y>' then 1
when 'Graph Y<' then 2
when 'Graph Y>=' then 3
when 'Graph Y<=' then 4
else raise "invalid token '#{token}'"
end
@text << "{F#{key}}"
flush_text token
when 'Deg'
enter_menu 'CATALOG'
@text << 'D'
@text << ("{DOWN}" * 10) << '{ENTER}'
flush_text token
when 'r'
click 'ALPHA'
click 'x^2', 'rho'
when 't'
click 'ALPHA'
click '^', 'theta'
when 'e^'
click 'SHIFT'
click 'ln', 'e^x'
when 'sqrt'
click 'SHIFT'
click 'x^2', 'sqrt'
when '!'
enter_menu 'OPTN -> more -> PROB'
@text << '{F1}'
flush_text 'x!'
when 'Ran#'
enter_menu 'OPTN -> more -> PROB'
@text << '{F4}'
flush_text 'Ran#'
when '|'
click 'fraction'
when 'ln', '->', 'sin ', 'cos ', 'tan '
click token.strip
when 'e'
click 'EXP'
when /Prog "([^"]+)"/
enter_menu 'PRGM -> CTL'
@text << "{F1}"
flush_text 'Prog'
@referenced_filenames << $1
prog_name = filename_to_calc_prog_name($1)
@text << "\"#{prog_name}\""
when 'Mcl'
raise "The fx-9960G does not appear to support memory clear!"
when /^[+^]$/
@text << "{#{token}}"
when "'"
@text << "''"
when ','
if convert_next_comma_to_plus
convert_next_comma_to_plus = false
@text << '+'
else
@text << ','
end
when /^[-"A-Z0-9?:\/()*= ,.<>]$/
@text << token
else
raise "invalid token '#{token}' on line '#{line}'"
end
end
# end of the line:
# if the line did not end with "#" then a newline is needed
unless line =~ /\#$/
@text << '{ENTER}'
end
flush_text
end
def add_global_preamble
@out << <<END
Run('fx-9860g-emulator/NewGraph.exe', 'fx-9860g-emulator')
WinWait('NewGraph', '', 1)
WinActivate('NewGraph')
$pos = WinGetPos('[ACTIVE]')
$x = $pos[0]
$y = $pos[1]
Send('B') ; open program menu
END
end
def add_preamble_for_prog prog_name
unless prog_name =~ /[A-Za-z]{1,8}/
raise "Program name invalid: '#{prog_name}'"
end
unless @used_calc_prog_names.add? prog_name
raise "Program name already used: '#{prog_name}'"
end
@out << <<END
Send('{F3}') ; start new prog
; Send the program name in lowercase to avoid a strange ALPHA-LOCK behaviour
Send('#{prog_name.downcase}{ENTER}') ; file name - at most 8 chars
; program text starts here
END
end
def add_postamble_for_prog
flush_text
@out << <<END
; end program text
END
click 'SHIFT'
click 'EXIT', 'QUIT'
@menu.clear
end
def add_global_postamble main_prog_name
idx = @used_calc_prog_names.sort.find_index main_prog_name
idx.times do
@test << '{DOWN}'
end
@text << '{F1}'
flush_text 'EXE'
end
def filename_to_calc_prog_name filename
filename[/([A-Z0-9]*).txt$/i, 1].upcase
end
# returns the au3 text as a string
def translate_file infilename, outfilename
unless infilename =~ /\.txt$/
raise "Expecting an input filename ending in '.txt', got '#{filename}'"
end
@in_dir = File.dirname(infilename)
@referenced_filenames << File.basename(infilename)
translated_filenames = Set.new
add_global_preamble
while next_filename = (@referenced_filenames - translated_filenames).first
begin
translated_filenames << next_filename
prog_name = filename_to_calc_prog_name next_filename
add_preamble_for_prog prog_name
File.open(File.join(@in_dir, next_filename), 'r') do |f|
@appended_lines = []
f.each do |line|
translate_line line
end
@appended_lines.each do |line|
translate_line line
end
end
add_postamble_for_prog
rescue => e
raise e, "#{e} for file '#{next_filename}'", e.backtrace
end
end
add_global_postamble filename_to_calc_prog_name(File.basename(infilename))
File.open(outfilename, 'w') do |f|
f << @out
end
end
end
# Entry point:
raise "Expecting two arguments: #{$0} <input-prog.txt> <output-script.au3>" \
unless ARGV.length == 2
tr = AutoItTranslator.new
tr.translate_file ARGV[0], ARGV[1]