Debian Codegraph @

lib/ninix/dll/aya5.rb

raw
1 # -*- coding: utf-8 -*-
2 #
3 # aya5.rb - an aya.dll(Ver.5) compatible Shiori module for ninix
4 # Copyright (C) 2002-2019 by Shyouzou Sugitani <[email protected]>
5 # Copyright (C) 2002, 2003 by MATSUMURA Namihiko <[email protected]>
6 #
7 # This program is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License (version 2) as
9 # published by the Free Software Foundation. It is distributed in the
10 # hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
11 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 # PURPOSE. See the GNU General Public License for more details.
13 #
14 #
15
16 # TODO:
17 # - 文字列内埋め込み要素の展開の動作が非互換.
18 # - システム関数:
19 # - たくさん.
20
21 require_relative "../home"
22 require_relative "../logging"
23
24
25 module Aya5
26
27 class AyaError < StandardError # XXX
28 #pass
29 end
30
31 def self.encrypt_char(char)
32 c = char[0].ord
33 j = 0
34 while j < 3
35 msb = (c & 0x80)
36 c <<= 1
37 c &= 0xff
38 unless msb.zero?
39 c |= 0x01
40 else
41 c &= 0xfe
42 end
43 j += 1
44 end
45 c ^= 0xd2
46 return c.chr
47 end
48
49 def self.decrypt_char(char)
50 c = char[0].ord
51 c ^= 0xd2
52 j = 0
53 while j < 3
54 lsb = (c & 0x01)
55 c >>= 1
56 unless lsb.zero?
57 c |= 0x80
58 else
59 c &= 0x7f
60 end
61 j += 1
62 end
63 return c.chr
64 end
65
66 def self.decrypt_readline(f)
67 line = ''
68 while true
69 c = f.read(1)
70 break if c == ''
71 line = [line, Aya5.decrypt_char(c)].join('')
72 if line.end_with?(10.chr) or \
73 line.end_with?(0xda.chr)
74 break
75 end
76 end
77 return line
78 end
79
80 def self.find_not_quoted(line, token)
81 position = 0
82 while true
83 pos_new = line.index(token, position)
84 if pos_new.nil?
85 pos_new = -1
86 break
87 elsif pos_new.zero?
88 break
89 end
90 position = line.index('"', position)
91 if not position.nil? and 0 <= position and position < pos_new
92 position += 1
93 while position < line.length - 1
94 if line[position] == '"'
95 position += 1
96 break
97 else
98 position += 1
99 next
100 end
101 end
102 else
103 break
104 end
105 end
106 return pos_new
107 end
108
109 def self.find_comment(line)
110 if line.start_with?("//")
111 return 0, line.length
112 end
113 start = line.length # not line.length - 1
114 end_ = -1
115 for token in ["//", "/*"]
116 pos_new = Aya5.find_not_quoted(line, token)
117 if 0 <= pos_new and pos_new < start
118 start = pos_new
119 if token == "/*"
120 end_ = Aya5.find_not_quoted(line, "*/")
121 if end_ >= 0
122 end_ += 2
123 end
124 else
125 end_ = line.length
126 end
127 end
128 end
129 if start == line.length
130 start = -1
131 end
132 return start, end_
133 end
134
135 def self.get_aya_version(filelist)
136 return 0 if filelist.empty?
137 dic_files = filelist
138 for filename in dic_files
139 if filename.downcase.end_with?('_shiori3.dic') # XXX
140 open(filename, 'rb', :encoding => 'CP932') do |f|
141 for line in f
142 begin
143 line = line.encode("UTF-8", :invalid => :replace, :undef => :replace)
144 v4 = line.index('for 文 version 4')
145 v5 = line.index('for AYA5')
146 if not v4.nil? and v4 > 0
147 return 4
148 elsif not v5.nil? and v5 > 0
149 return 5
150 end
151 rescue
152 return 5
153 end
154 end
155 end
156 end
157 end
158 return 3
159 end
160
161 def self.find_dict(aya_dir, f)
162 comment = 0
163 dic_files = []
164 for line in f
165 line = line.encode("UTF-8", :invalid => :replace, :undef => :replace)
166 unless comment.zero?
167 end_ = Aya5.find_not_quoted(line, '*/')
168 next if end_ < 0
169 line = line[end_ + 2..-1]
170 comment = 0
171 end
172 while true
173 start, end_ = Aya5.find_comment(line)
174 break if start < 0
175 line = "" if start.zero?
176 if end_ < 0
177 comment = 1
178 line = line[0..start-1]
179 break
180 end
181 line = [line[0..start-1], line[end_..-1]].join(' ')
182 end
183 line = line.strip()
184 next if line.empty?
185 next unless line.include?(',')
186 key, value = line.split(',', 2)
187 key.strip!
188 value.strip!
189 if key == 'dic'
190 filename = Home.get_normalized_path(value)
191 path = File.join(aya_dir, filename)
192 dic_files << path
193 end
194 end
195 return dic_files
196 end
197
198 def self.check_version(top_dir, dll_name)
199 filename = nil
200 if File.file?(File.join(top_dir, 'aya.txt'))
201 filename = File.join(top_dir, 'aya.txt')
202 elsif File.file?(File.join(top_dir, 'yaya.txt'))
203 return 6 # XXX: YAYA
204 elsif not dll_name.nil? and \
205 File.file?(File.join(top_dir, [dll_name[0..-4], 'txt'].join('')))
206 filename = File.join(top_dir, [dll_name[0..-4], 'txt'].join(''))
207 end
208 version = 0
209 unless filename.nil?
210 open(filename, :encoding => 'CP932') do |f|
211 version = Aya5.get_aya_version(Aya5.find_dict(top_dir, f))
212 end
213 end
214 return version
215 end
216
217
218 class Shiori
219 attr_reader :charset, :dbpath, :dic, :aya_dir, :saori_library
220
221 def initialize(dll_name)
222 @dll_name = dll_name
223 unless dll_name.nil?
224 @__AYA_TXT = [dll_name[0..-4], 'txt'].join('')
225 @__DBNAME = [dll_name[0..-5], '_variable.cfg'].join('')
226 else
227 @__AYA_TXT = 'aya5.txt'
228 @__DBNAME = 'aya5_variable.cfg'
229 end
230 @saori = nil
231 @dic_files = []
232 end
233
234 def use_saori(saori)
235 @saori = saori
236 end
237
238 def find(top_dir, dll_name)
239 result = 0
240 version = Aya5.check_version(top_dir, dll_name)
241 if version == 5
242 result = 200
243 elsif version == 6
244 @__AYA_TXT = 'yaya.txt'
245 @__DBNAME = 'yaya_variable.cfg'
246 result = 100
247 end
248 return result
249 end
250
251 def show_description
252 Logging::Logging.info(
253 "Shiori: AYA5 compatible module for ninix\n" \
254 " Copyright (C) 2002-2019 by Shyouzou Sugitani\n" \
255 " Copyright (C) 2002, 2003 by MATSUMURA Namihiko")
256 end
257
258 def reset
259 @boot_time = Time.new
260 @aitalk = 0
261 @dic_files = []
262 @dic = AyaDictionary.new(self)
263 @global_namespace = AyaGlobalNamespace.new(self)
264 @system_functions = AyaSystemFunctions.new(self)
265 @logfile = nil
266 @filelist = {}
267 end
268
269 def load(dir: nil)
270 @aya_dir = dir
271 @dbpath = File.join(@aya_dir, @__DBNAME)
272 @saori_library = AyaSaoriLibrary.new(@saori, @aya_dir)
273 reset()
274 begin
275 path = File.join(@aya_dir, @__AYA_TXT)
276 open(path, :encoding => 'CP932') do |aya_txt|
277 load_aya_txt(aya_txt)
278 end
279 rescue #except IOError:
280 Logging::Logging.debug('cannot read aya.txt')
281 return 0
282 rescue #except AyaError as error:
283 Logging::Logging.debug(error)
284 return 0
285 end
286 @global_namespace.load_database(self)
287 # default setting
288 unless @global_namespace.exists('log')
289 @global_namespace.put('log', '')
290 end
291 for path in @dic_files
292 basename = File.basename(path, '.*')
293 ext = File.extname(path)
294 ext = ext.downcase
295 if ext == '.ayc'
296 encrypted = true
297 else
298 encrypted = false
299 end
300 begin
301 open(path, 'rb') do |dicfile|
302 @dic.load(dicfile, encrypted)
303 end
304 rescue
305 Logging::Logging.debug('cannot read ' + path.to_s)
306 next
307 end
308 end
309 func = @dic.get_function('load')
310 unless func.nil?
311 func.call(:argv => [@aya_dir.gsub('/', "\\")])
312 end
313 return 1
314 end
315
316 def load_aya_txt(f)
317 comment = 0
318 @charset = 'CP932' # default
319 for line in f
320 line = line.force_encoding(@charset).encode("UTF-8", :invalid => :replace, :undef => :replace)
321 unless comment.zero?
322 end_ = Aya5.find_not_quoted(line, '*/')
323 next if end_ < 0
324 line = line[end_ + 2..-1]
325 comment = 0
326 end
327 while true
328 start, end_ = Aya5.find_comment(line)
329 break if start < 0
330 line = "" if start.zero?
331 if end_ < 0
332 comment = 1
333 line = line[0..start-1]
334 break
335 end
336 line = [line[0..start-1], line[end_..-1]].join(' ')
337 end
338 line = line.strip()
339 next if line.empty?
340 next unless line.include?(',')
341 key, value = line.split(',', 2)
342 key.strip!
343 value.strip!
344 evaluate_config(key, value)
345 end
346 end
347
348 def evaluate_config(key, value)
349 if key == 'charset'
350 if ['Shift_JIS', 'ShiftJIS', 'SJIS'].include?(value)
351 @charset = 'CP932'
352 elsif value == 'UTF-8'
353 @charset = 'UTF-8'
354 else # default and error
355 @charset = 'CP932'
356 end
357 elsif key == 'dic'
358 filename = Home.get_normalized_path(value)
359 path = File.join(@aya_dir, filename)
360 @dic_files << path
361 elsif key == 'msglang'
362 #pass ## FIXME
363 elsif key == 'log'
364 fail "assert" unless value.is_a?(String)
365 filename = value
366 path = File.join(@aya_dir, filename)
367 begin
368 f = open(path, 'w')
369 rescue
370 Logging::Logging.debug('cannnot open ' + path)
371 else
372 @logfile.close() unless @logfile.nil?
373 @logfile = f
374 @global_namespace.put('log', value.to_s)
375 end
376 elsif key == 'iolog'
377 #pass ## FIXME
378 elsif key == 'fncdepth'
379 #pass ## FIXME
380 end
381 end
382
383 def get_dictionary
384 @dic
385 end
386
387 def get_ghost_dir
388 @aya_dir
389 end
390
391 def get_global_namespace
392 @global_namespace
393 end
394
395 def get_system_functions
396 @system_functions
397 end
398
399 def get_boot_time
400 @boot_time
401 end
402
403 def unload
404 func = @dic.get_function('unload')
405 func.call(:argv => []) unless func.nil?
406 @global_namespace.save_database()
407 @saori_library.unload()
408 @logfile.close() unless @logfile.nil?
409 for key in @filelist.keys()
410 @filelist[key].close()
411 end
412 end
413
414 # SHIORI API
415 def request(req_string)
416 result = ''
417 func = @dic.get_function('request')
418 unless func.nil?
419 req = req_string.force_encoding(@charset).encode("UTF-8", :invalid => :replace, :undef => :replace)
420 result = func.call(:argv => [req])
421 end
422 if result.nil?
423 result = ''
424 end
425 return result.encode(@charset, :invalid => :replace, :undef => :replace)
426 end
427 end
428
429
430 class AyaDictionary
431 attr_reader :aya
432
433 def initialize(aya)
434 @aya = aya
435 @functions = {}
436 @global_macro = {}
437 end
438
439 def get_function(name)
440 if @functions.has_key?(name)
441 return @functions[name]
442 else
443 return nil
444 end
445 end
446
447 def load(f, encrypted)
448 all_lines = []
449 local_macro = {}
450 logical_line = ''
451 comment = 0
452 while true
453 if encrypted
454 line = Aya5.decrypt_readline(f)
455 else
456 line = f.gets
457 end
458 break if line.nil? # EOF
459 line = line.force_encoding(@aya.charset).encode("UTF-8", :invalid => :replace, :undef => :replace)
460 unless comment.zero?
461 end_ = Aya5.find_not_quoted(line, '*/')
462 next if end_ < 0
463 line = line[end_ + 2..-1]
464 comment = 0
465 end
466 line = line.strip()
467 next if line.empty?
468 if line.end_with?('/') and \
469 not line.end_with?('*/') and not line.end_with?('//')
470 logical_line = [logical_line, line[0..-2]].join('')
471 else
472 logical_line = [logical_line, line].join('')
473 while true
474 start, end_ = Aya5.find_comment(logical_line)
475 break if start < 0
476 logical_line = "" if start.zero?
477 if end_ < 0
478 comment = 1
479 logical_line = logical_line[0..start-1]
480 break
481 end
482 logical_line = [logical_line[0..start-1], ' ', logical_line[end_..-1]].join('')
483 end
484 logical_line = logical_line.strip()
485 next if logical_line.empty?
486 buf = logical_line
487 # preprosess
488 if buf.start_with?('#')
489 buf = buf[1..-1].strip()
490 for (tag, target) in [['define', local_macro],
491 ['globaldefine', @global_macro]]
492 if buf.start_with?(tag)
493 buf = buf[tag.length..-1].strip()
494 i = 0
495 while i < buf.length
496 if buf[i] == " " or buf[i] == "\t" or \
497 buf[i] == " "
498 key = buf[0..i-1].strip()
499 target[key] = buf[i..-1].strip()
500 break
501 end
502 i += 1
503 end
504 break
505 end
506 end
507 logical_line = '' # reset
508 next
509 end
510 for macro in [local_macro, @global_macro]
511 logical_line = preprocess(macro, logical_line)
512 end
513 # multi statement
514 list_lines = split_line(logical_line.strip())
515 unless list_lines.empty?
516 all_lines.concat(list_lines)
517 end
518 logical_line = '' # reset
519 end
520 end
521 evaluate_lines(all_lines, File.split(f.path)[1])
522 end
523
524 def split_line(line)
525 lines = []
526 while true
527 break if line.nil? or line.empty?
528 pos = line.length # not line.length - 1
529 token = ''
530 for x in ['{', '}', ';']
531 pos_new = Aya5.find_not_quoted(line, x)
532 if 0 <= pos_new and pos_new < pos
533 pos = pos_new
534 token = x
535 end
536 end
537 unless pos.zero?
538 new = line[0..pos-1].strip()
539 else # '{' or '}'
540 new = ""
541 end
542 line = line[pos + token.length..-1].strip()
543 unless new.empty?
544 lines << new
545 end
546 unless ['', ';'].include?(token)
547 lines << token
548 end
549 end
550 return lines
551 end
552
553 def preprocess(macro, line)
554 for key in macro.keys
555 value = macro[key]
556 line = line.gsub(key, value)
557 end
558 return line
559 end
560
561 SPECIAL_CHARS = [']', '(', ')', '[', '+', '-', '*', '/', '=',
562 ':', ';', '!', '{', '}', '%', '&', '#', '"',
563 '<', '>', ',', '?']
564
565 def evaluate_lines(lines, file_name)
566 prev = nil
567 name = nil
568 function = []
569 option = nil
570 block_nest = 0
571 for i in 0..lines.length-1
572 line = lines[i]
573 if line == '{'
574 unless name.nil?
575 if block_nest > 0
576 function << line
577 end
578 block_nest += 1
579 else
580 if prev.nil?
581 Logging::Logging.debug(
582 'syntax error in ' + file_name.to_s + ': unbalanced "{" at ' \
583 'the top of file')
584 else
585 Logging::Logging.debug(
586 'syntax error in ' + file_name.to_s + ': unbalanced "{" at ' \
587 'the bottom of function "' + prev.to_s + '"')
588 end
589 end
590 elsif line == '}'
591 unless name.nil?
592 block_nest -= 1
593 if block_nest > 0
594 function << line
595 elsif block_nest.zero?
596 @functions[name] = AyaFunction.new(self, name,
597 function, option)
598 # reset
599 prev = name
600 name = nil
601 function = []
602 option = nil
603 end
604 else
605 if prev.nil?
606 Logging::Logging.debug(
607 'syntax error in ' + file_name.to_s + ': unbalanced "}" at ' \
608 'the top of file')
609 else
610 Logging::Logging.debug(
611 'syntax error in ' + file_name.to_s + ': unbalanced "}" at ' \
612 'the bottom of function "' + prev.to_s + '"')
613 end
614 block_nest = 0
615 end
616 elsif name.nil?
617 if line.include?(':')
618 name, option = line.split(':', 2)
619 name.strip!
620 option.strip!
621 else
622 name = line
623 end
624 for char in SPECIAL_CHARS
625 if name.include?(char)
626 Logging::Logging.debug(
627 'illegal function name "' + name.to_s + '" in ' + file_name.to_s)
628 end
629 end
630 function = []
631 else
632 if not name.nil? and block_nest > 0
633 function << line
634 else
635 Logging::Logging.debug('syntax error in ' + file_name + ': ' + line)
636 end
637 end
638 end
639 end
640 end
641
642 class AyaFunction
643
644 TYPE_INT = 10
645 TYPE_FLOAT = 11
646 TYPE_DECISION = 12
647 TYPE_RETURN = 13
648 TYPE_BLOCK = 14
649 TYPE_SUBSTITUTION = 15
650 TYPE_INC = 16
651 TYPE_DEC = 17
652 TYPE_IF = 18
653 TYPE_WHILE = 19
654 TYPE_FOR = 20
655 TYPE_BREAK = 21
656 TYPE_CONTINUE = 22
657 TYPE_SWITCH = 23
658 TYPE_CASE = 24
659 TYPE_STRING_LITERAL = 25
660 TYPE_STRING = 26
661 TYPE_OPERATOR = 27
662 TYPE_STATEMENT = 28
663 TYPE_CONDITION = 29
664 TYPE_SYSTEM_FUNCTION = 30
665 TYPE_FUNCTION = 31
666 TYPE_ARRAY_POINTER = 32
667 TYPE_ARRAY = 33
668 TYPE_VARIABLE_POINTER = 34
669 TYPE_VARIABLE = 35
670 TYPE_TOKEN = 36
671 TYPE_NEW_ARRAY = 37
672 TYPE_FOREACH = 38
673 TYPE_PARALLEL = 39
674 TYPE_FORMULA = 40
675 CODE_NONE = 50
676 CODE_RETURN = 51
677 CODE_BREAK = 52
678 CODE_CONTINUE = 53
679 Re_f = Regexp.new('\A^[-+]?\d+(\.\d*)\z')
680 Re_d = Regexp.new('\A[-+]?\d+\z')
681 Re_d_ = Regexp.new('[-+]?\d+\z')
682 Re_b = Regexp.new('\A[-+]?0[bB][01]+\z')
683 Re_x = Regexp.new('\A[-+]?0[xX][\dA-Fa-f]+\z')
684 Re_if = Regexp.new('\Aif\s')
685 Re_others = Regexp.new('\Aothers\s')
686 Re_elseif = Regexp.new('\Aelseif\s')
687 Re_while = Regexp.new('\Awhile\s')
688 Re_for = Regexp.new('\Afor\s')
689 Re_foreach = Regexp.new('\Aforeach\s')
690 Re_switch = Regexp.new('\Aswitch\s')
691 Re_case = Regexp.new('\Acase\s')
692 Re_when = Regexp.new('\Awhen\s')
693 Re_parallel = Regexp.new('\Aparallel\s')
694 SPECIAL_CHARS = [']', '(', ')', '[', '+', '-', '*', '/', '=',
695 ':', ';', '!', '{', '}', '%', '&', '#', '"',
696 '<', '>', ',', '?']
697
698 def initialize(dic, name, lines, option)
699 @dic = dic
700 @name = name
701 @status = CODE_NONE
702 @lines = parse(lines)
703 if option == 'nonoverlap'
704 @nonoverlap = [[], [], []]
705 else
706 @nonoverlap = nil
707 end
708 if option == 'sequential'
709 @sequential = [[], []]
710 else
711 @sequential = nil
712 end
713 ## FIXME: void, array
714 end
715
716 def parse(lines)
717 result = []
718 i = 0
719 while i < lines.length
720 line = lines[i]
721 if line == '--'
722 result << [TYPE_DECISION, []]
723 elsif line == 'return'
724 result << [TYPE_RETURN, []]
725 elsif line == 'break'
726 result << [TYPE_BREAK, []]
727 elsif line == 'continue'
728 result << [TYPE_CONTINUE, []]
729 elsif line == '{'
730 inner_func = []
731 i, inner_func = get_block(lines, i)
732 result << [TYPE_BLOCK, parse(inner_func)]
733 elsif not Re_if.match(line).nil?
734 inner_blocks = []
735 while true
736 current_line = lines[i]
737 if not Re_if.match(current_line).nil?
738 condition = parse_(current_line[2..-1].strip())
739 elsif not Re_elseif.match(current_line).nil?
740 condition = parse_(current_line[6..-1].strip())
741 else
742 condition = [TYPE_CONDITION, nil]
743 end
744 inner_block = []
745 i, inner_block = get_block(lines, i + 1)
746 if condition.nil?
747 inner_blocks = []
748 break
749 end
750 entry = []
751 entry << condition
752 entry << parse(inner_block)
753 inner_blocks << entry
754 if i + 1 >= lines.length
755 break
756 end
757 next_line = lines[i + 1]
758 if Re_elseif.match(next_line).nil? and \
759 next_line != 'else'
760 break
761 end
762 i = (i + 1)
763 end
764 unless inner_blocks.empty?
765 result << [TYPE_IF, inner_blocks]
766 end
767 elsif not Re_while.match(line).nil?
768 condition = parse_(line[5..-1].strip())
769 inner_block = []
770 i, inner_block = get_block(lines, i + 1)
771 result << [TYPE_WHILE,
772 [condition, parse(inner_block)]]
773 elsif not Re_for.match(line).nil?
774 init = parse([line[3..-1].strip()]) ## FIXME(?)
775 condition = parse_(lines[i + 1])
776 reset = parse([lines[i + 2]]) ## FIXME(?)
777 inner_block = []
778 i, inner_block = get_block(lines, i + 3)
779 unless condition.nil?
780 result << [TYPE_FOR,
781 [[init, condition, reset],
782 parse(inner_block)]]
783 end
784 elsif not Re_foreach.match(line).nil?
785 name = line[7..-1].strip()
786 var = lines[i + 1]
787 i, inner_block = get_block(lines, i + 2)
788 result << [TYPE_FOREACH,
789 [[name, var], parse(inner_block)]]
790 elsif not Re_switch.match(line).nil?
791 index = parse_(line[6..-1].strip())
792 inner_block = []
793 i, inner_block = get_block(lines, i + 1)
794 result << [TYPE_SWITCH,
795 [index, parse(inner_block)]]
796 elsif not Re_case.match(line).nil?
797 left = parse_(line[4..-1].strip())
798 i, block = get_block(lines, i + 1)
799 inner_blocks = []
800 j = 0
801 while true
802 current_line = block[j]
803 unless Re_when.match(current_line).nil?
804 right = current_line[4..-1].strip()
805 else # 'others'
806 right = nil
807 end
808 inner_block = []
809 j, inner_block = get_block(block, j + 1)
810 unless right.nil?
811 argument = AyaArgument.new(right)
812 while argument.has_more_tokens()
813 entry = []
814 right = argument.next_token()
815 tokens = AyaStatement.new(right).tokens
816 if ['-', '+'].include?(tokens[0])
817 value_min = parse_statement([tokens.shift,
818 tokens.shift]) ## FIXME: parse_
819 else
820 value_min = parse_statement([tokens.shift]) ## FIXME: parse_
821 end
822 value_max = value_min
823 unless tokens.empty?
824 if tokens[0] != '-'
825 Logging::Logging.debug(
826 'syntax error in function ' \
827 '"' + @name.to_s + '": when ' + right.to_s)
828 next
829 else
830 tokens.shift
831 end
832 if tokens.length > 2 or \
833 (tokens.length == 2 and \
834 not ['-', '+'].include?(tokens[0]))
835 Logging::Logging.debug(
836 'syntax error in function ' \
837 '"' + @name + '": when ' + right.to_s)
838 next
839 else
840 value_max = parse_statement(tokens) ## FIXME: parse_
841 end
842 end
843 entry << [value_min, value_max]
844 entry << parse(inner_block)
845 inner_blocks << entry
846 end
847 else
848 entry = []
849 entry << right
850 entry << parse(inner_block)
851 inner_blocks << entry
852 end
853 break if j + 1 == block.length
854 next_line = block[j + 1]
855 if Re_when.match(next_line).nil? and \
856 next_line != 'others'
857 break
858 end
859 j += 1
860 end
861 result << [TYPE_CASE, [left, inner_blocks]]
862 elsif not Re_parallel.match(line).nil? ## FIXME
863 #pass
864 else
865 result << [TYPE_FORMULA, parse_(line)] ## FIXME
866 end
867 i += 1
868 end
869 result << [TYPE_DECISION, []]
870 return result
871 end
872
873 def find_close_(token_open, tokens, position)
874 nest = 0
875 current = position
876 if token_open == '['
877 token_close = ']'
878 elsif token_open == '('
879 token_close = ')'
880 end
881 while tokens[current..-1].count(token_close) > 0
882 pos_new = tokens[current..-1].index(token_close)
883 pos_new += current
884 break if pos_new.zero?
885 nest = tokens[position..pos_new-1].count(token_open) - tokens[position..pos_new-1].count(token_close) # - 1
886 if nest > 0
887 current = (pos_new + 1)
888 else
889 current = pos_new
890 break
891 end
892 end
893 return current
894 end
895
896 def iter_position(tokens, ope_list, reverse: 0)
897 position = 0
898 len_tokens = tokens.length
899 result = []
900 while true
901 new_pos = len_tokens
902 tokens.reverse! unless reverse.zero?
903 for ope in ope_list
904 if tokens[position..-1].include?(ope)
905 temp_pos = tokens[position..-1].index(ope)
906 temp_pos += position
907 new_pos = [new_pos, temp_pos].min
908 end
909 end
910 tokens.reverse! unless reverse.zero?
911 position = new_pos
912 break if position >= len_tokens
913 unless reverse.zero?
914 result << (len_tokens - position)
915 else
916 result << position
917 end
918 position += 1
919 end
920 return result
921 end
922
923 def find_position(tokens, ope_list, position, reverse: 0)
924 len_tokens = tokens.length
925 new_pos = len_tokens
926 tokens.reverse! unless reverse.zero?
927 for ope in ope_list
928 if tokens[position..-1].include?(ope)
929 temp_pos = tokens[position..-1].index(ope)
930 temp_pos += position
931 new_pos = [new_pos, temp_pos].min
932 end
933 end
934 tokens.reverse! unless reverse.zero?
935 position = new_pos
936 return -1 if position >= len_tokens
937 unless reverse.zero?
938 return len_tokens - 1 - position
939 else
940 return position
941 end
942 end
943
944 def get_left_(tokens, position)
945 position -= 1
946 left = tokens.delete_at(position)
947 if left.is_a?(String) # not processed
948 left = new_parse([left])
949 end
950 return left
951 end
952
953 def get_right_(tokens, position)
954 right = tokens.delete_at(position)
955 if right.is_a?(String) # not processed
956 if ['-', '+'].include?(right)
957 tmp_token = tokens.delete_at(position)
958 right = new_parse([right, tmp_token])
959 else
960 right = new_parse([right])
961 end
962 end
963 return right
964 end
965
966 def new_parse(tokens) ## FIXME
967 return [] if tokens.length.zero? ## FIXME
968 position = find_position(tokens, ['(', '['], 0)
969 while position >= 0
970 pos_end = find_close_(tokens[position], tokens, position + 1)
971 temp = []
972 token_open = tokens.delete_at(position) # open
973 for i in 0..pos_end - position - 2
974 temp << tokens.delete_at(position)
975 end
976 tokens.delete_at(position) # close
977 if token_open == '[' # array
978 name = tokens.delete_at(position - 1)
979 tokens.insert(position - 1, [TYPE_ARRAY,
980 [name, [TYPE_FORMULA, new_parse(temp)]]])
981 else
982 ope_list = ['!', '++', '--', '*', '/', '%', '+', '-', '&',
983 '==', '!=', '>=', '<=', '>', '<', '_in_', '!_in_',
984 '&&', '||', '=', ':=',
985 '+=', '-=', '*=', '/=', '%=',
986 '+:=', '-:=', '*:=', '/:=', '%:=', ',=', ','] ## FIXME
987 if position.zero? or \
988 ope_list.include?(tokens[position - 1]) # FORMULA
989 tokens.insert(position, [TYPE_FORMULA,
990 new_parse(temp)])
991 else # should be function
992 name = tokens.delete_at(position - 1)
993 if @dic.aya.get_system_functions().exists(name)
994 tokens.insert(position - 1, [TYPE_SYSTEM_FUNCTION,
995 [name,
996 [new_parse(temp)]]]) ## CHECK: arguments
997 else
998 tokens.insert(position - 1, [TYPE_FUNCTION,
999 [name,
1000 [new_parse(temp)]]]) ## CHECK: arguments
1001 end
1002 end
1003 end
1004 position = find_position(tokens, ['(', '['], 0)
1005 end
1006 for position in iter_position(tokens, ['!'])
1007 tokens.delete_at(position)
1008 right = get_right_(tokens, position)
1009 tokens.insert(position, [TYPE_CONDITION,
1010 [nil, [TYPE_OPERATOR, '!'], right]])
1011 end
1012 for position in iter_position(tokens, ['++', '--'])
1013 if tokens[position] == '++'
1014 type_ = TYPE_INC
1015 else
1016 type_ = TYPE_DEC
1017 end
1018 tokens.delete_at(position)
1019 var = tokens.delete_at(position - 1)
1020 if var.is_a?(String) # not processed
1021 right = new_parse([var]) ## FIXME
1022 end
1023 tokens.insert(position - 1, [type_, var])
1024 end
1025 position = find_position(tokens, ['*', '/', '%'], 0, :reverse => 1)
1026 while position >= 0
1027 ope = [TYPE_OPERATOR, tokens.delete_at(position)]
1028 right = get_right_(tokens, position)
1029 left = get_left_(tokens, position)
1030 tokens.insert(position - 1, [TYPE_STATEMENT, left, ope, right])
1031 position = find_position(tokens, ['*', '/', '%'], 0, :reverse => 1)
1032 end
1033 position = 0
1034 position = find_position(tokens, ['+', '-'], position)
1035 while position >= 0
1036 ope = [TYPE_OPERATOR, tokens.delete_at(position)]
1037 right = get_right_(tokens, position)
1038 ope_list = ['!_in_', '_in_',
1039 '+:=', '-:=', '*:=', '/:=', '%:=',
1040 ':=', '+=', '-=', '*=', '/=', '%=', '<=',
1041 '>=', '==', '!=', '&&', '||', ',=', '++', '--',
1042 ',', '=', '!', '+', '-', '/', '*', '%', '&']
1043 if position.zero? or ope_list.include?(tokens[position - 1])
1044 left = [TYPE_INT, 0]
1045 tokens.insert(position, [TYPE_STATEMENT, left, ope, right])
1046 else
1047 left = get_left_(tokens, position)
1048 tokens.insert(position - 1, [TYPE_STATEMENT, left, ope, right])
1049 end
1050 position = find_position(tokens, ['+', '-'], position)
1051 end
1052 for position in iter_position(tokens, ['&'])
1053 type_ = tokens[position + 1][0]
1054 var_ = tokens[position + 1][1]
1055 tokens.delete_at(position)
1056 tokens.delete_at(position) # +1
1057 if type_ == TYPE_ARRAY
1058 tokens.insert(position, [TYPE_ARRAY_POINTER, var_])
1059 elsif type_ == TYPE_VARIABLE
1060 tokens.insert(position, [TYPE_VARIABLE_POINTER, var_])
1061 elsif type_ == TYPE_TOKEN
1062 tokens.insert(position, [TYPE_VARIABLE_POINTER,
1063 [var_, nil]])
1064 else
1065 Logging::Logging.debug(
1066 'syntax error in function "' + @name.to_s + '": ' \
1067 'illegal argument "' + tokens.to_s + '"')
1068 end
1069 end
1070 position = find_position(tokens, ['==', '!=', '>=', '<=', '>', '<', '_in_', '!_in_'], 0, :reverse => 1)
1071 while position >= 0
1072 ope = [TYPE_OPERATOR, tokens.delete_at(position)]
1073 right = get_right_(tokens, position)
1074 left = get_left_(tokens, position)
1075 tokens.insert(position - 1, [TYPE_CONDITION, [left, ope, right]])
1076 position = find_position(tokens, ['==', '!=', '>=', '<=', '>', '<', '_in_', '!_in_'], 0, :reverse => 1)
1077 end
1078 position = find_position(tokens, ['&&'], 0, :reverse => 1)
1079 while position >= 0
1080 ope = [TYPE_OPERATOR, tokens.delete_at(position)]
1081 right = get_right_(tokens, position)
1082 left = get_left_(tokens, position)
1083 tokens.insert(position - 1, [TYPE_CONDITION, [left, ope, right]])
1084 position = find_position(tokens, ['&&'], 0, :reverse => 1)
1085 end
1086 position = find_position(tokens, ['||'], 0, :reverse => 1)
1087 while position >= 0
1088 ope = [TYPE_OPERATOR, tokens.delete_at(position)]
1089 right = get_right_(tokens, position)
1090 left = get_left_(tokens, position)
1091 tokens.insert(position - 1, [TYPE_CONDITION, [left, ope, right]])
1092 position = find_position(tokens, ['||'], 0, :reverse => 1)
1093 end
1094 position = find_position(tokens, ['=', ':='], 0, :reverse => 1)
1095 while position >= 0
1096 ope = [TYPE_OPERATOR, tokens.delete_at(position)]
1097 right = get_right_(tokens, position)
1098 left = get_left_(tokens, position)
1099 tokens.insert(position - 1, [TYPE_SUBSTITUTION, [left, ope, right]])
1100 position = find_position(tokens, ['=', ':='], 0, :reverse => 1)
1101 end
1102 position = find_position(tokens, ['+=', '-=', '*=', '/=', '%=', '+:=', '-:=', '*:=', '/:=', '%:=', ',='], 0, :reverse => 1)
1103 while position >= 0
1104 ope = [TYPE_OPERATOR, tokens.delete_at(position)]
1105 right = get_right_(tokens, position)
1106 left = get_left_(tokens, position)
1107 tokens.insert(position - 1, [TYPE_SUBSTITUTION, [left, ope, right]])
1108 position = find_position(tokens, ['+=', '-=', '*=', '/=', '%=', '+:=', '-:=', '*:=', '/:=', '%:=', ',='], 0, :reverse => 1)
1109 end
1110 temp = []
1111 position = find_position(tokens, [','], 0, :reverse => 0)
1112 while position >= 0
1113 tokens.delete_at(position)
1114 temp << get_right_(tokens, position)
1115 if position > 0
1116 temp.insert(0, get_left_(tokens, position))
1117 end
1118 position = find_position(tokens, [','], 0, :reverse => 0)
1119 end
1120 unless temp.empty?
1121 tokens.insert(position, [TYPE_NEW_ARRAY, temp])
1122 end
1123 for i in 0..tokens.length-1
1124 if tokens[i].is_a?(String)
1125 token = tokens.delete_at(i)
1126 tokens.insert(i, parse_token(token))
1127 end
1128 end
1129 return tokens[0] ## FIXME
1130 end
1131
1132 def parse_(line) ## FIXME
1133 statement = AyaStatement.new(line)
1134 return new_parse(statement.tokens)
1135 end
1136
1137 def parse_statement(statement_tokens)
1138 n_tokens = statement_tokens.length
1139 statement = []
1140 if n_tokens == 1
1141 statement = [TYPE_STATEMENT,
1142 parse_token(statement_tokens[0])]
1143 elsif statement_tokens[0] == '+'
1144 statement = parse_statement(statement_tokens[1..-1])
1145 elsif statement_tokens[0] == '-'
1146 tokens = ['0']
1147 tokens.concat(statement_tokens)
1148 statement = parse_statement(tokens)
1149 else
1150 ope_index = nil
1151 for ope in ['+', '-']
1152 if statement_tokens.include?(ope)
1153 new_index = statement_tokens.index(ope)
1154 if ope_index.nil? or new_index < ope_index
1155 ope_index = new_index
1156 end
1157 end
1158 end
1159 if ope_index.nil?
1160 statement_tokens.reverse!
1161 begin
1162 for ope in ['*', '/', '%']
1163 if statement_tokens.include?(ope)
1164 new_index = statement_tokens.index(ope)
1165 if ope_index.nil? or new_index < ope_index
1166 ope_index = new_index
1167 end
1168 end
1169 end
1170 unless ope_index.nil?
1171 ope_index = (-1 - ope_index)
1172 end
1173 ensure
1174 statement_tokens.reverse!
1175 end
1176 end
1177 if [nil, -1, 0, n_tokens - 1].include?(ope_index)
1178 return nil
1179 else
1180 ope = [TYPE_OPERATOR, statement_tokens[ope_index]]
1181 if statement_tokens[0..ope_index-1].length == 1
1182 if statement_tokens[0].start_with?('(')
1183 tokens = AyaStatement.new(statement_tokens[0][1..-2]).tokens
1184 left = parse_statement(tokens)
1185 else
1186 left = parse_token(
1187 statement_tokens[0..ope_index-1][0])
1188 end
1189 else
1190 left = parse_statement(statement_tokens[0..ope_index-1])
1191 end
1192 if statement_tokens[ope_index + 1..-1].length == 1
1193 if statement_tokens[-1].start_with?('(')
1194 tokens = AyaStatement(
1195 statement_tokens[ope_index + 1][1..-2]).tokens
1196 right = parse_statement(tokens)
1197 else
1198 right = parse_token(
1199 statement_tokens[ope_index + 1..-1][0])
1200 end
1201 else
1202 right = parse_statement(
1203 statement_tokens[ope_index + 1..-1])
1204 end
1205 statement = [TYPE_STATEMENT, left, ope, right]
1206 end
1207 end
1208 return statement
1209 end
1210
1211 def parse_condition(condition_tokens)
1212 n_tokens = condition_tokens.length
1213 condition = nil
1214 ope_index = nil
1215 condition_tokens.reverse!
1216 begin
1217 for ope in ['&&', '||']
1218 if condition_tokens.include?(ope)
1219 new_index = condition_tokens.index(ope)
1220 if ope_index.nil? or new_index < ope_index
1221 ope_index = new_index
1222 end
1223 end
1224 end
1225 unless ope_index.nil?
1226 ope_index = (-1 - ope_index)
1227 end
1228 ensure
1229 condition_tokens.reverse!
1230 end
1231 if ope_index.nil?
1232 for ope in ['==', '!=', '>', '<', '>=', '<=', '_in_', '!_in_']
1233 if condition_tokens.include?(ope)
1234 new_index = condition_tokens.index(ope)
1235 if ope_index.nil? or new_index < ope_index
1236 ope_index = new_index
1237 end
1238 end
1239 end
1240 if [nil, -1, 0, n_tokens - 1].include?(ope_index)
1241 Logging::Logging.debug(
1242 'syntax error in function "' + @name.to_s + '": ' \
1243 'illegal condition "' + condition_tokens.join(' ') + '"')
1244 return nil
1245 end
1246 ope = [TYPE_OPERATOR, condition_tokens[ope_index]]
1247 if condition_tokens[0..ope_index-1].length == 1
1248 left = parse_statement([condition_tokens[0..ope_index-1][0]])
1249 else
1250 left = parse_statement(condition_tokens[0..ope_index-1])
1251 end
1252 if condition_tokens[ope_index + 1..-1].length == 1
1253 right = parse_statement([condition_tokens[ope_index + 1..-1][0]])
1254 else
1255 right = parse_statement(condition_tokens[ope_index + 1..-1])
1256 end
1257 condition = [TYPE_CONDITION, [left, ope, right]]
1258 else
1259 ope = [TYPE_OPERATOR, condition_tokens[ope_index]]
1260 left = parse_condition(condition_tokens[0..ope_index-1])
1261 right = parse_condition(condition_tokens[ope_index + 1..-1])
1262 unless left.nil? or right.nil?
1263 condition = [TYPE_CONDITION, [left, ope, right]]
1264 end
1265 end
1266 return condition
1267 end
1268
1269 def parse_argument(args) ## FIXME
1270 argument = AyaArgument.new(args)
1271 arguments = []
1272 while argument.has_more_tokens()
1273 token = argument.next_token()
1274 if token.start_with?('&')
1275 result = parse_token(token[1..-1])
1276 if result[0] == TYPE_ARRAY
1277 arguments << [TYPE_ARRAY_POINTER, result[1]]
1278 elsif result[0] == TYPE_VARIABLE
1279 arguments << [TYPE_VARIABLE_POINTER, result[1]]
1280 elsif result[0] == TYPE_TOKEN
1281 arguments << [TYPE_VARIABLE_POINTER,
1282 [result[1], nil]]
1283 else
1284 Logging::Logging.debug(
1285 'syntax error in function "' + @name.to_s + '": ' \
1286 'illegal argument "' + token.to_s + '"')
1287 end
1288 elsif token.start_with?('(')
1289 unless token.end_with?(')')
1290 Logging::Logging.debug(
1291 'syntax error in function "' + @name.to_s + '": ' \
1292 'unbalanced "(" in the string(' + token.to_s + ')')
1293 return nil
1294 else
1295 statement = AyaStatement.new(token[1..-2])
1296 arguments << parse_statement(statement.tokens)
1297 end
1298 else
1299 arguments << parse_statement([token])
1300 end
1301 end
1302 return arguments
1303 end
1304
1305 def parse_token(token)
1306 result = []
1307 if not Re_f.match(token).nil?
1308 result = [TYPE_FLOAT, token]
1309 elsif not Re_d.match(token).nil?
1310 result = [TYPE_INT, token]
1311 elsif token.start_with?('"')
1312 text = token[1..-1]
1313 if text.end_with?('"')
1314 text = text[0..-2]
1315 end
1316 if text.count('"') > 0
1317 Logging::Logging.debug(
1318 'syntax error in function "' + @name.to_s + '": ' \
1319 '\'"\' in string "' + text.to_s + '"')
1320 end
1321 unless text.include?('%')
1322 result = [TYPE_STRING_LITERAL, text]
1323 else
1324 result = [TYPE_STRING, text]
1325 end
1326 elsif token.start_with?("'")
1327 text = token[1..-1]
1328 if text.end_with?("'")
1329 text = text[0..-2]
1330 end
1331 if text.count("'") > 0
1332 Logging::Logging.debug(
1333 'syntax error in function "' + @name.to_s + '": ' \
1334 "\"'\" in string \"' + text.to_s + '\"")
1335 end
1336 result = [TYPE_STRING_LITERAL, text]
1337 else
1338 pos_parenthesis_open = token.index('(')
1339 pos_block_open = token.index('[')
1340 if pos_parenthesis_open.nil? # XXX
1341 pos_parenthesis_open = -1
1342 end
1343 if pos_block_open.nil? # XXX
1344 pos_block_open = -1
1345 end
1346 if pos_parenthesis_open.zero? and Aya5.find_not_quoted(token, ',') != -1 ## FIXME: Array
1347 unless token.end_with?(')')
1348 Logging::Logging.debug(
1349 'syntax error: unbalnced "(" in "{0}"'.format(token))
1350 else
1351 result = [TYPE_NEW_ARRAY,
1352 parse_argument(token[1..-2])] ## FIXME
1353 end
1354 elsif pos_parenthesis_open != -1 and \
1355 (pos_block_open == -1 or \
1356 pos_parenthesis_open < pos_block_open) # function
1357 unless token.end_with?(')')
1358 Logging::Logging.debug(
1359 'syntax error: unbalnced "(" in "' + token.to_s + '"')
1360 else
1361 func_name = token[0..pos_parenthesis_open-1]
1362 arguments = parse_argument(
1363 token[pos_parenthesis_open + 1..-2])
1364 break_flag = false
1365 for char in SPECIAL_CHARS
1366 if func_name.include?(char)
1367 Logging::Logging.debug(
1368 'illegal character "' + char + '" in ' \
1369 'the name of function "' + token.to_s + '"')
1370 break_flag = true
1371 break
1372 end
1373 end
1374 unless break_flag
1375 if @dic.aya.get_system_functions().exists(
1376 func_name)
1377 if func_name == 'LOGGING'
1378 result = [TYPE_SYSTEM_FUNCTION,
1379 [func_name, arguments,
1380 token[pos_parenthesis_open + 1..-2]]]
1381 else
1382 result = [TYPE_SYSTEM_FUNCTION,
1383 [func_name, arguments]]
1384 end
1385 else
1386 result = [TYPE_FUNCTION,
1387 [func_name, arguments]]
1388 end
1389 end
1390 end
1391 elsif not pos_block_open.nil? and pos_block_open != -1 # array
1392 unless token.end_with?(']')
1393 Logging::Logging.debug(
1394 'syntax error: unbalanced "[" in "' + token.to_s + '"')
1395 else
1396 array_name = token[0..pos_block_open-1]
1397 index = parse_token(token[pos_block_open + 1..-2])
1398 break_flag = false
1399 for char in SPECIAL_CHARS
1400 if array_name.include?(char)
1401 Logging::Logging.debug(
1402 'illegal character "' + char.to_s + '" in ' \
1403 'the name of array "' + token.to_s + '"')
1404 break_flag = true
1405 break
1406 end
1407 end
1408 unless break_flag
1409 result = [TYPE_ARRAY, [array_name, index]]
1410 end
1411 end
1412 else # variable or function
1413 break_flag = false
1414 for char in SPECIAL_CHARS
1415 if token.include?(char)
1416 Logging::Logging.debug(
1417 'syntax error in function "' + @name.to_s + '": ' \
1418 'illegal character "' + char + '" in the name of ' \
1419 'function/variable "' + token + '"')
1420 break_flag = true
1421 break
1422 end
1423 end
1424 unless break_flag
1425 result = [TYPE_TOKEN, token]
1426 end
1427 end
1428 end
1429 return result
1430 end
1431
1432 def call(argv: nil)
1433 namespace = AyaNamespace.new(@dic.aya)
1434 _argv = []
1435 if argv.nil?
1436 namespace.put('_argc', 0)
1437 else
1438 namespace.put('_argc', argv.length)
1439 for i in 0..argv.length-1
1440 if argv[i].is_a?(Hash)
1441 _argv << argv[i]['value']
1442 else
1443 _argv << argv[i]
1444 end
1445 end
1446 end
1447 namespace.put('_argv', _argv)
1448 @status = CODE_NONE
1449 result = evaluate(namespace, @lines, -1, 0) ## FIXME
1450 unless argv.nil?
1451 for i in 0..argv.length-1
1452 if argv[i].is_a?(Hash)
1453 value = _argv[i]
1454 name = argv[i]['name']
1455 namespace = argv[i]['namespace']
1456 index = argv[i]['index']
1457 namespace.put(name, value, :index => index)
1458 end
1459 end
1460 end
1461 return result
1462 end
1463
1464 def evaluate(namespace, lines, index_to_return, is_inner_block, is_block: 1, connect: 0) ## FIXME
1465 result = []
1466 alternatives = []
1467 for line in lines
1468 next if line.nil? or line.empty?
1469 if [TYPE_DECISION, TYPE_RETURN,
1470 TYPE_BREAK, TYPE_CONTINUE].include?(line[0]) or \
1471 [CODE_RETURN, CODE_BREAK,
1472 CODE_CONTINUE].include?(@status)
1473 unless alternatives.empty?
1474 unless is_inner_block.zero?
1475 if index_to_return < 0
1476 result << alternatives.sample
1477 elsif index_to_return <= (alternatives.length - 1)
1478 result << alternatives[index_to_return]
1479 else # out of range
1480 result << ''
1481 end
1482 else
1483 result << alternatives
1484 end
1485 alternatives = []
1486 end
1487 if line[0] == TYPE_RETURN or \
1488 @status == CODE_RETURN
1489 @status = CODE_RETURN
1490 break
1491 elsif line[0] == TYPE_BREAK or \
1492 @status == CODE_BREAK
1493 @status = CODE_BREAK
1494 break
1495 elsif line[0] == TYPE_CONTINUE or \
1496 @status == CODE_CONTINUE
1497 @status = CODE_CONTINUE
1498 break
1499 end
1500 elsif line[0] == TYPE_BLOCK
1501 inner_func = line[1]
1502 local_namespace = AyaNamespace.new(@dic.aya, :parent => namespace)
1503 result_of_inner_func = evaluate(local_namespace,
1504 inner_func, -1, 1) ## FIXME
1505 unless result_of_inner_func.nil?
1506 alternatives << result_of_inner_func
1507 end
1508 elsif line[0] == TYPE_SUBSTITUTION
1509 left, ope, right = line[1]
1510 ope = ope[1]
1511 if [':=', '+:=', '-:=', '*:=', '/:=', '%:='].include?(ope)
1512 type_float = 1
1513 else
1514 type_float = 0
1515 end
1516 right_result = evaluate(namespace, [right], -1 , 1, :is_block => 0, :connect => 1) ## FIXME
1517 unless ['=', ':='].include?(ope)
1518 left_result = evaluate_token(namespace, left)
1519 right_result = operation(left_result, ope[0],
1520 right_result, type_float)
1521 ope = ope[1..-1]
1522 end
1523 result_of_substitution = substitute(namespace, left, ope, right_result)
1524 unless connect.zero? ## FIXME
1525 alternatives << result_of_substitution
1526 end
1527 elsif line[0] == TYPE_INC or \
1528 line[0] == TYPE_DEC # ++/--
1529 if line[0] == TYPE_INC
1530 ope = '++'
1531 elsif line[0] == TYPE_DEC
1532 ope = '--'
1533 else
1534 return nil # should not reach here
1535 end
1536 var_name = line[1]
1537 if var_name.start_with?('_')
1538 target_namespace = namespace
1539 else
1540 target_namespace = @dic.aya.get_global_namespace()
1541 end
1542 value = evaluate(namespace, [[TYPE_TOKEN, var_name]], -1, 1, :is_block => 0) ## FIXME
1543 index = nil
1544 if value.is_a?(Integer) or value.is_a?(Float)
1545 if ope == '++'
1546 target_namespace.put(var_name, value.to_i + 1, :index => index)
1547 elsif ope == '--'
1548 target_namespace.put(var_name, value.to_i - 1, :index => index)
1549 else
1550 return nil # should not reach here
1551 end
1552 else
1553 Logging::Logging.debug(
1554 'illegal increment/decrement:' \
1555 'type of variable ' + var_name.to_s + ' is not number')
1556 end
1557 elsif line[0] == TYPE_IF
1558 inner_blocks = line[1]
1559 n_blocks = inner_blocks.length
1560 for j in 0..n_blocks-1
1561 entry = inner_blocks[j]
1562 condition = entry[0]
1563 inner_block = entry[1]
1564 unless [0, false, nil].include?(evaluate(namespace, [condition], -1, 1, :is_block => 0)) ## FIXME
1565 local_namespace = AyaNamespace.new(@dic.aya, :parent => namespace)
1566 result_of_inner_block = evaluate(local_namespace,
1567 inner_block,
1568 -1, 1) ## FIXME
1569 unless result_of_inner_block.nil?
1570 alternatives << result_of_inner_block
1571 end
1572 break
1573 end
1574 end
1575 elsif line[0] == TYPE_WHILE
1576 condition = line[1][0]
1577 inner_block = line[1][1]
1578 fail "assert" unless condition[0] == TYPE_CONDITION or condition[0] == TYPE_INT
1579 while evaluate_condition(namespace, condition)
1580 local_namespace = AyaNamespace.new(@dic.aya, :parent => namespace)
1581 result_of_inner_block = evaluate(local_namespace,
1582 inner_block, -1, 1) ## FIXME
1583 unless result_of_inner_block.nil?
1584 alternatives << result_of_inner_block
1585 end
1586 break if @status == CODE_RETURN
1587 if @status == CODE_BREAK
1588 @status = CODE_NONE
1589 break
1590 end
1591 if @status == CODE_CONTINUE
1592 @status = CODE_NONE
1593 end
1594 end
1595 elsif line[0] == TYPE_FOR
1596 init = line[1][0][0]
1597 condition = line[1][0][1]
1598 reset = line[1][0][2]
1599 inner_block = line[1][1]
1600 evaluate(namespace, init, -1, 1, :is_block => 0) ## FIXME
1601 fail "assert" unless condition[0] == TYPE_CONDITION or condition[0] == TYPE_INT
1602 while evaluate_condition(namespace, condition)
1603 local_namespace = AyaNamespace.new(@dic.aya, :parent => namespace)
1604 result_of_inner_block = evaluate(local_namespace,
1605 inner_block, -1, 1) ## FIXME
1606 unless result_of_inner_block.nil?
1607 alternatives << result_of_inner_block
1608 end
1609 break if @status == CODE_RETURN
1610 if @status == CODE_BREAK
1611 @status = CODE_NONE
1612 break
1613 end
1614 if @status == CODE_CONTINUE
1615 @status = CODE_NONE
1616 end
1617 evaluate(namespace, reset, -1, 1, :is_block => 0) ## FIXME
1618 end
1619 elsif line[0] == TYPE_SWITCH
1620 index = evaluate_token(namespace, line[1][0])
1621 inner_block = line[1][1]
1622 begin
1623 index = Integer(index)
1624 rescue
1625 index = 0
1626 end
1627 local_namespace = AyaNamespace.new(@dic.aya, :parent => namespace)
1628 result_of_inner_block = evaluate(local_namespace,
1629 inner_block, index, 1) ## FIXME
1630 unless result_of_inner_block.nil?
1631 alternatives << result_of_inner_block
1632 end
1633 elsif line[0] == TYPE_CASE
1634 left = evaluate_token(namespace, line[1][0])
1635 inner_blocks = line[1][1]
1636 n_blocks = inner_blocks.length
1637 default_result = nil
1638 break_flag = false
1639 for j in 0..n_blocks-1
1640 entry = inner_blocks[j]
1641 inner_block = entry[1]
1642 local_namespace = AyaNamespace.new(@dic.aya, :parent => namespace)
1643 unless entry[0].nil?
1644 value_min, value_max = entry[0]
1645 value_min = evaluate_statement(namespace, value_min, 1)
1646 value_max = evaluate_statement(namespace, value_max, 1)
1647 if value_min <= left and left <= value_max
1648 result_of_inner_block = evaluate(
1649 local_namespace, inner_block, -1, 1) ## FIXME
1650 unless result_of_inner_block.nil?
1651 alternatives << result_of_inner_block
1652 break_flag = true
1653 break
1654 end
1655 end
1656 else
1657 default_result = evaluate(local_namespace,
1658 inner_block, -1, 1) ## FIXME
1659 end
1660 end
1661 unless break_flag
1662 unless default_result.nil?
1663 alternatives << default_result
1664 end
1665 end
1666 elsif line[0] == TYPE_STATEMENT
1667 result_of_func = evaluate_statement(namespace, line, 0)
1668 unless result_of_func.nil?
1669 alternatives << result_of_func
1670 end
1671 elsif line[0] == TYPE_CONDITION
1672 condition = line
1673 if condition.nil? or \
1674 evaluate_condition(namespace, condition)
1675 alternatives << true
1676 else
1677 alternatives << false
1678 end
1679 elsif line[0] == TYPE_FORMULA
1680 result_of_formula = evaluate(namespace, [line[1]], -1, 1, :is_block => 0, :connect => connect) ## FIXME
1681 unless result_of_formula.nil?
1682 alternatives << result_of_formula
1683 end
1684 elsif line[0] == TYPE_NEW_ARRAY
1685 temp = []
1686 for item in line[1]
1687 member_of_array = evaluate(namespace, [item], -1, 1, :is_block => 0, :connect => 1) ## FIXME
1688 temp << member_of_array
1689 end
1690 alternatives << temp
1691 elsif line[0] == TYPE_ARRAY
1692 system_functions = @dic.aya.get_system_functions()
1693 if line[1][0].is_a?(Array) or \
1694 system_functions.exists(line[1][0])
1695 if line[1][0].is_a?(Array) ## FIXME
1696 array = evaluate(namespace, [line[1][0]], -1, 1, :is_block => 0) ## FIXME
1697 else
1698 array = evaluate(namespace, [[TYPE_SYSTEM_FUNCTION, [line[1][0], []]]], -1, 1, :is_block => 0)
1699 end
1700 if array.is_a?(String)
1701 temp = evaluate(namespace, [line[1][1]], -1, 1, :is_block => 0) ## FIXME
1702 if temp.is_a?(Array)
1703 fail "assert" unless temp.length == 2
1704 index, delimiter = temp
1705 else
1706 index = temp
1707 delimiter = ','
1708 end
1709 fail "assert" unless index.is_a?(Integer)
1710 fail "assert" unless delimiter.is_a?(String)
1711 result_of_array = array.split(delimiter)[index]
1712 alternatives << result_of_array
1713 elsif array.is_a?(Array)
1714 index = evaluate(namespace, [line[1][1]], -1, 1, :is_block => 0) ## FIXME
1715 fail "assert" unless index.is_a?(Integer)
1716 result_of_array = array[index]
1717 alternatives << result_of_array
1718 else
1719 Logging::Logging.debug(
1720 'Oops: ' + array.to_s + ' ' + line[1][1].to_s)
1721 end
1722 else
1723 var_name = line[1][0]
1724 if var_name.start_with?('_')
1725 target_namespace = namespace
1726 else
1727 target_namespace = @dic.aya.get_global_namespace()
1728 end
1729 temp = evaluate(namespace, [line[1][1]], -1, 1, :is_block => 0) ## FIXME
1730 if temp.is_a?(Array) and temp.length > 1
1731 fail "assert" unless temp.length == 2
1732 index, delimiter = temp
1733 fail "assert" unless delimiter.is_a?(String)
1734 else
1735 index = temp
1736 delimiter = nil
1737 end
1738 ##fail "assert" unless index.is_a?(Integer)
1739 if index.is_a?(Integer) and \
1740 target_namespace.exists(var_name)
1741 unless delimiter.nil?
1742 array = target_namespace.get(var_name)
1743 temp = array.to_s.split(delimiter)
1744 if temp.length > index
1745 result_of_array = array.to_s.split(delimiter)[index]
1746 else
1747 result_of_array = ''
1748 end
1749 else
1750 result_of_array = target_namespace.get(var_name, :index => index)
1751 end
1752 alternatives << result_of_array
1753 else
1754 result_of_array = ''
1755 alternatives << result_of_array
1756 end
1757 end
1758 elsif [TYPE_INT, TYPE_TOKEN,
1759 TYPE_SYSTEM_FUNCTION,
1760 TYPE_STRING_LITERAL,
1761 TYPE_FUNCTION,
1762 TYPE_STRING,
1763 TYPE_ARRAY_POINTER,
1764 TYPE_VARIABLE_POINTER].include?(line[0])
1765 result_of_eval = evaluate_token(namespace, line)
1766 unless result_of_eval.nil?
1767 alternatives << result_of_eval
1768 end
1769 elsif line[0] == TYPE_FOREACH
1770 var_name, temp = line[1][0]
1771 if var_name.start_with?('_')
1772 target_namespace = namespace
1773 else
1774 target_namespace = @dic.aya.get_global_namespace()
1775 end
1776 array = target_namespace.get_array(var_name)
1777 for item in array
1778 if temp.start_with?('_')
1779 target_namespace = namespace
1780 else
1781 target_namespace = @dic.aya.get_global_namespace()
1782 end
1783 target_namespace.put(temp, item)
1784 result_of_block = evaluate(namespace, line[1][1], -1, 1) ## FIXME
1785 break if @status == CODE_RETURN
1786 if @status == CODE_BREAK
1787 @status = CODE_NONE
1788 break
1789 end
1790 if @status == CODE_CONTINUE
1791 @status = CODE_NONE
1792 end
1793 end
1794 else ## FIXME
1795 result_of_eval = evaluate_token(namespace, line)
1796 unless result_of_eval.nil?
1797 alternatives << result_of_eval
1798 end
1799 end
1800 end
1801 if is_inner_block.zero?
1802 unless @sequential.nil?
1803 list_ = []
1804 for alt in result
1805 list_ << alt.length
1806 end
1807 if @sequential[0] != list_
1808 @sequential[0] = list_
1809 @sequential[1] = ([0] * result.length)
1810 else
1811 for index in 0..result.length-1
1812 current = @sequential[1][index]
1813 if current < result[index].length - 1
1814 @sequential[1][index] = (current + 1)
1815 break
1816 else
1817 @sequential[1][index] = 0
1818 end
1819 end
1820 end
1821 end
1822 unless @nonoverlap.nil?
1823 list_ = []
1824 for alt in result
1825 list_ << alt.length
1826 end
1827 if @nonoverlap[0] != list_
1828 @nonoverlap[0] = list_
1829 @nonoverlap[2] = []
1830 end
1831 if @nonoverlap[2].empty?
1832 @nonoverlap[2] << ([0] * result.length)
1833 while true
1834 new = []
1835 new.concat(@nonoverlap[2][-1])
1836 break_flag = false
1837 for index in 0..result.length-1
1838 if new[index] < result[index].length - 1
1839 new[index] += 1
1840 @nonoverlap[2] << new
1841 break_flag = true
1842 break
1843 else
1844 new[index] = 0
1845 end
1846 end
1847 break unless break_flag
1848 end
1849 end
1850 next_ = Random.rand(0..@nonoverlap[2].length-1)
1851 @nonoverlap[1] = @nonoverlap[2][next_]
1852 @nonoverlap[2].delete(next_)
1853 end
1854 for index in 0..result.length-1
1855 if not @sequential.nil?
1856 result[index] = result[index][@sequential[1][index]]
1857 elsif not @nonoverlap.nil?
1858 result[index] = result[index][@nonoverlap[1][index]]
1859 else
1860 result[index] = result[index].sample
1861 end
1862 end
1863 end
1864 if result.nil? or result.empty?
1865 if is_block.zero? and not alternatives.empty? ## FIXME
1866 return alternatives[-1] ## FIXME
1867 end
1868 return nil
1869 elsif result.length == 1
1870 return result[0]
1871 else
1872 return result.map {|s| s.to_s}.join('')
1873 end
1874 end
1875
1876 def substitute(namespace, left, ope, right)
1877 if left[0] != TYPE_ARRAY
1878 var_name = left[1]
1879 else
1880 var_name = left[1][0]
1881 end
1882 if var_name.start_with?('_')
1883 target_namespace = namespace
1884 else
1885 target_namespace = @dic.aya.get_global_namespace()
1886 end
1887 if left[0] != TYPE_ARRAY
1888 target_namespace.put(var_name, right)
1889 else
1890 index = evaluate(namespace, [left[1][1]], -1, 1, :is_block => 0)
1891 begin
1892 index = Integer(index)
1893 rescue
1894 Logging::Logging.debug('Could not convert ' + index.to_s + ' to an integer')
1895 else
1896 if ope == '='
1897 elem = right
1898 elsif ope == ':='
1899 if right.is_a?(Integer)
1900 elem = right.to_f
1901 else
1902 elem = right
1903 end
1904 else
1905 return nil # should not reach here
1906 end
1907 target_namespace.put(var_name, elem, :index => index)
1908 end
1909 end
1910 return right
1911 end
1912
1913 def evaluate_token(namespace, token)
1914 result = '' # default
1915 case token[0]
1916 when TYPE_TOKEN
1917 if not Re_b.match(token[1]).nil?
1918 pos = Re_d_.match(token[1]).begin(0)
1919 result = token[1][pos..-1].to_i(2)
1920 elsif not Re_x.match(token[1]).nil?
1921 result = token[1].to_i(16)
1922 else
1923 func = @dic.get_function(token[1])
1924 system_functions = @dic.aya.get_system_functions()
1925 if not func.nil?
1926 result = func.call()
1927 elsif system_functions.exists(token[1])
1928 result = system_functions.call(namespace, token[1], [])
1929 else
1930 if token[1].start_with?('_')
1931 target_namespace = namespace
1932 else
1933 target_namespace = @dic.aya.get_global_namespace()
1934 end
1935 if target_namespace.exists(token[1])
1936 result = target_namespace.get(token[1])
1937 end
1938 end
1939 end
1940 when TYPE_STRING_LITERAL
1941 result = token[1]
1942 when TYPE_STRING
1943 result = evaluate_string(namespace, token[1])
1944 when TYPE_INT
1945 result = token[1].to_i
1946 when TYPE_FLOAT
1947 result = token[1].to_f
1948 when TYPE_SYSTEM_FUNCTION
1949 system_functions = @dic.aya.get_system_functions()
1950 func_name = token[1][0]
1951 ##fail ["assert: ", 'function ', func_name, ' not found.'].join('') unless system_functions.exists(func_name)
1952 arguments = evaluate(namespace, token[1][1], -1, 1, :is_block => 0, :connect => 1) ## FIXME
1953 unless arguments.is_a?(Array) ## FIXME
1954 arguments = [arguments]
1955 end
1956 if func_name == 'LOGGING'
1957 arguments.insert(0, token[1][2])
1958 arguments.insert(0, @name)
1959 arguments.insert(0, @dic.aya.logfile)
1960 result = system_functions.call(namespace, func_name, arguments)
1961 else
1962 result = system_functions.call(namespace, func_name, arguments)
1963 end
1964 when TYPE_FUNCTION
1965 func_name = token[1][0]
1966 func = @dic.get_function(func_name)
1967 ##fail ["assert: ", 'function ', func_name, ' not found.'].join('') unless func != nil
1968 arguments = evaluate_argument(namespace, func_name,
1969 token[1][1], false)
1970 result = func.call(:argv => arguments)
1971 when TYPE_ARRAY
1972 result = evaluate(namespace, [token], -1, 1, :is_block => 0) ## FIXME
1973 when TYPE_NEW_ARRAY
1974 result = evaluate(namespace, [token], -1, 1, :is_block => 0) ## FIXME
1975 when TYPE_VARIABLE
1976 var_name = token[1][0]
1977 if var_name.start_with?('_')
1978 target_namespace = namespace
1979 else
1980 target_namespace = @dic.aya.get_global_namespace()
1981 end
1982 if target_namespace.exists(var_name)
1983 result = target_namespace.get(var_name)
1984 end
1985 when TYPE_ARRAY_POINTER
1986 var_name = token[1][0]
1987 if var_name.start_with?('_')
1988 target_namespace = namespace
1989 else
1990 target_namespace = @dic.aya.get_global_namespace()
1991 end
1992 index = evaluate(namespace, [token[1][1]], -1, 1, :is_block => 0)
1993 begin
1994 index = Integer(index)
1995 rescue
1996 Logging::Logging.debug(
1997 'index of array has to be integer: ' + var_name.to_s + '[' + token[1][1].to_s + ']')
1998 else
1999 value = target_namespace.get(var_name, :index => index)
2000 result = {'name' => var_name,
2001 'index' => index,
2002 'namespace' => target_namespace,
2003 'value' => value}
2004 end
2005 when TYPE_VARIABLE_POINTER
2006 var_name = token[1][0]
2007 if var_name.start_with?('_')
2008 target_namespace = namespace
2009 else
2010 target_namespace = @dic.aya.get_global_namespace()
2011 end
2012 value = target_namespace.get(var_name)
2013 result = {'name' => var_name,
2014 'index' => nil,
2015 'namespace' => target_namespace,
2016 'value' => value}
2017 else
2018 Logging::Logging.debug('error in evaluate_token: ' + token.to_s)
2019 end
2020 return result
2021 end
2022
2023 def evaluate_condition(namespace, condition)
2024 result = false
2025 if condition[0] == TYPE_INT
2026 unless Integer(condition[1]).zero?
2027 return true
2028 else
2029 return false
2030 end
2031 end
2032 return true if condition[1].nil?
2033 left = condition[1][0]
2034 ope = condition[1][1]
2035 right = condition[1][2]
2036 fail "assert" unless ope[0] == TYPE_OPERATOR
2037 if left.nil? # '!'
2038 left_result = true
2039 else
2040 left_result = evaluate(namespace, [left], -1, 1, :is_block => 0, :connect => 1) ## FIXME
2041 end
2042 right_result = evaluate(namespace, [right], -1, 1, :is_block => 0, :connect => 1) ## FIXME
2043 case ope[1]
2044 when '=='
2045 result = (left_result == right_result)
2046 when '!='
2047 result = (left_result != right_result)
2048 when '_in_'
2049 if right_result.is_a?(String) and left_result.is_a?(String)
2050 if right_result.include?(left_result)
2051 result = true
2052 else
2053 result = false
2054 end
2055 else
2056 result = false
2057 end
2058 when '!_in_'
2059 if right_result.is_a?(String) and left_result.is_a?(String)
2060 unless right_result.include?(left_result)
2061 result = true
2062 else
2063 result = false
2064 end
2065 else
2066 result = false
2067 end
2068 when '<'
2069 if right_result.is_a?(String) != left_result.is_a?(String)
2070 begin
2071 left_result = Float(left_result)
2072 right_result = Float(right_result)
2073 rescue
2074 return false # XXX
2075 end
2076 end
2077 result = (left_result < right_result)
2078 when '>'
2079 if right_result.is_a?(String) != left_result.is_a?(String)
2080 begin
2081 left_result = Float(left_result)
2082 right_result = Float(right_result)
2083 rescue
2084 return false # XXX
2085 end
2086 end
2087 result = (left_result > right_result)
2088 when '<='
2089 if right_result.is_a?(String) != left_result.is_a?(String)
2090 begin
2091 left_result = Float(left_result)
2092 right_result = Float(right_result)
2093 rescue
2094 return false # XXX
2095 end
2096 end
2097 result = (left_result <= right_result)
2098 when '>='
2099 if right_result.is_a?(String) != left_result.is_a?(String)
2100 begin
2101 left_result = Float(left_result)
2102 right_result = Float(right_result)
2103 rescue
2104 return false # XXX
2105 end
2106 end
2107 result = (left_result >= right_result)
2108 when '||'
2109 result = (not [0, false, nil].include?(left_result) or not [0, false, nil].include?(right_result))
2110 when '&&'
2111 result = (not [0, false ,nil].include?(left_result) and not [0, false, nil].include?(right_result))
2112 when '!'
2113 result = ([0, false, nil].include?(right_result))
2114 else
2115 #pass
2116 end
2117 return result
2118 end
2119
2120 def evaluate_statement(namespace, statement, type_float)
2121 return '' if statement.nil? or statement.length <= 1
2122 num = statement[1..-1].length
2123 type_ = statement[0]
2124 token = statement[1]
2125 if type_ == TYPE_STATEMENT
2126 left = evaluate_statement(namespace, token, type_float)
2127 else
2128 left = evaluate_token(namespace, statement)
2129 end
2130 if num == 3
2131 ope = statement[2][1]
2132 type_ = statement[3][0]
2133 case type_
2134 when TYPE_INT
2135 token = statement[3][1]
2136 unless type_float.zero?
2137 right = token.to_f
2138 else
2139 right = token.to_i
2140 end
2141 when TYPE_FLOAT
2142 token = statement[3][1]
2143 unless type_float.zero?
2144 right = token.to_f
2145 else
2146 right = token.to_f.to_i
2147 end
2148 when TYPE_STATEMENT
2149 right = evaluate_statement(namespace, statement[3],
2150 type_float)
2151 else
2152 right = evaluate(namespace, [statement[3]], -1, 1, :is_block => 0) ## FIXME
2153 end
2154 result = operation(left, ope, right, type_float)
2155 else
2156 result = left
2157 end
2158 return result
2159 end
2160
2161 def operation(left, ope, right, type_float)
2162 unless left.is_a?(Array)
2163 begin
2164 if not type_float.zero?
2165 left = Float(left)
2166 right = Float(right)
2167 elsif ope != '+' or \
2168 (not left.is_a?(String) and not right.is_a?(String))
2169 left = Integer(left)
2170 right = Integer(right)
2171 else
2172 left = left.to_s
2173 right = right.to_s
2174 end
2175 rescue
2176 left = left.to_s
2177 right = right.to_s
2178 end
2179 end
2180 begin
2181 case ope
2182 when '+'
2183 return left + right
2184 when '-'
2185 return left - right
2186 when '*'
2187 return left * right
2188 when '/'
2189 if right.zero?
2190 return 0
2191 else
2192 if left.is_a?(Integer) and right.is_a?(Integer)
2193 return (left / right).to_i
2194 else
2195 return left / right
2196 end
2197 end
2198 when '%'
2199 return left % right
2200 when ','
2201 fail "assert" unless left.is_a?(Array)
2202 result = []
2203 result.concat(left)
2204 if right.is_a?(Array)
2205 result.concat(right)
2206 else
2207 result << right
2208 end
2209 return result
2210 end
2211 rescue
2212 Logging::Logging.debug(
2213 'illegal operation: ' + [left.to_s, ope.to_s, right.to_s].join(' '))
2214 return ''
2215 end
2216 end
2217
2218 def get_block(parent, startpoint)
2219 result = []
2220 n_lines = parent.length
2221 inner_nest_level = 0
2222 for i in startpoint..n_lines-1
2223 inner_content = parent[i]
2224 if inner_content == '{'
2225 if inner_nest_level > 0
2226 result << inner_content
2227 end
2228 inner_nest_level += 1
2229 elsif inner_content == '}'
2230 inner_nest_level -= 1
2231 if inner_nest_level > 0
2232 result << inner_content
2233 end
2234 else
2235 result << inner_content
2236 end
2237 if inner_nest_level.zero?
2238 return i, result
2239 end
2240 end
2241 return startpoint, result
2242 end
2243
2244 def evaluate_string(namespace, line)
2245 history = [] # %[n]
2246 buf = ''
2247 startpoint = 0
2248 system_functions = @dic.aya.get_system_functions()
2249 while startpoint < line.length
2250 pos = line.index('%', startpoint)
2251 if pos.nil? or pos < 0
2252 buf = [buf, line[startpoint..-1]].join('')
2253 startpoint = line.length
2254 next
2255 else
2256 unless pos.zero?
2257 buf = [buf, line[startpoint..pos-1]].join('')
2258 end
2259 startpoint = pos
2260 end
2261 break if pos == (line.length - 1) # XXX
2262 if line[pos + 1] == '('
2263 start_ = (pos + 2)
2264 nest = 0
2265 current_ = start_
2266 while line[current_..-1].count(')') > 0
2267 close_ = line.index(')', current_)
2268 break if close_.zero?
2269 nest = line[start_..close_-1].count('(') - line[start_..close_-1].count(')')
2270 if nest > 0
2271 current_ = (close_ + 1)
2272 else
2273 current_ = close_
2274 break
2275 end
2276 end
2277 lines_ = parse([line[start_..current_-1]])
2278 result_ = evaluate(namespace, lines_, -1, 1, :is_block => 0, :connect => 1)
2279 buf = [buf, result_.to_s].join('')
2280 startpoint = (current_ + 1)
2281 next
2282 end
2283 endpoint = line.length
2284 for char in SPECIAL_CHARS
2285 pos = line[0..endpoint-1].index(char, startpoint + 2)
2286 if not pos.nil? and 0 < pos and pos < endpoint
2287 endpoint = pos
2288 end
2289 end
2290 if line[startpoint + 1] == '[' # history
2291 if line[endpoint] != ']'
2292 Logging::Logging.debug(
2293 'unbalanced "%[" or illegal index in ' \
2294 'the string(' + line + ')')
2295 buf = ''
2296 break
2297 end
2298 index_token = parse_token(line[startpoint + 2..endpoint-1])
2299 index = evaluate_token(namespace, index_token)
2300 begin
2301 index = Integer(index)
2302 rescue
2303 Logging::Logging.debug(
2304 'illegal history index in the string(' + line + ')')
2305 else
2306 if 0 <= index and index < history.length
2307 buf = [buf, format(history[index])].join('')
2308 end
2309 end
2310 startpoint = (endpoint + 1)
2311 next
2312 end
2313 replaced = false
2314 while endpoint > (startpoint + 1)
2315 token = line[startpoint + 1..endpoint-1]
2316 func = @dic.get_function(token)
2317 is_system_func = system_functions.exists(token)
2318 if not func.nil? or is_system_func
2319 if endpoint < line.length and \
2320 line[endpoint] == '('
2321 end_of_parenthesis = line.index(')', endpoint + 1)
2322 if end_of_parenthesis.nil? or end_of_parenthesis < 0
2323 Logging::Logging.debug(
2324 'unbalanced "(" in the string(' + line + ')')
2325 startpoint = line.length
2326 buf = ''
2327 break
2328 end
2329 func_name = token
2330 arguments = parse_argument(
2331 line[endpoint + 1..end_of_parenthesis-1])
2332 arguments = evaluate_argument(
2333 namespace, func_name, arguments, is_system_func)
2334 if is_system_func
2335 if func_name == 'LOGGING'
2336 arguments.insert(
2337 0, line[endpoint + 1..end_of_parenthesis-1])
2338 arguments.insert(0, @name)
2339 arguments.insert(0, @dic.aya.logfile)
2340 result_of_func = system_functions.call(
2341 namespace, func_name, arguments)
2342 else
2343 result_of_func = system_functions.call(
2344 namespace, func_name, arguments)
2345 end
2346 else
2347 result_of_func = func.call(:argv => arguments)
2348 end
2349 if result_of_func.nil?
2350 result_of_func = ''
2351 end
2352 history << result_of_func
2353 buf = [buf, format(result_of_func)].join('')
2354 startpoint = (end_of_parenthesis + 1)
2355 replaced = true
2356 break
2357 elsif not func.nil?
2358 result_of_func = func.call()
2359 history << result_of_func
2360 buf = [buf, format(result_of_func)].join('')
2361 startpoint = endpoint
2362 replaced = true
2363 break
2364 else
2365 result_of_func = system_functions.call(
2366 namespace, token, [])
2367 if result_of_func.nil?
2368 result_of_func = ''
2369 end
2370 history << result_of_func
2371 buf = [buf, format(result_of_func)].join('')
2372 startpoint = endpoint
2373 replaced = true
2374 break
2375 end
2376 else
2377 if token.start_with?('_')
2378 target_namespace = namespace
2379 else
2380 target_namespace = @dic.aya.get_global_namespace()
2381 end
2382 if target_namespace.exists(token)
2383 have_index = false
2384 index = nil
2385 if endpoint < line.length and line[endpoint] == '['
2386 end_of_block = line.index(']', endpoint + 1)
2387 if end_of_block.nil? or end_of_block < 0
2388 Logging::Logging.debug(
2389 'unbalanced "[" or ' \
2390 'illegal index in the string(' + line + ')')
2391 startpoint = line.length
2392 buf = ''
2393 break
2394 end
2395 have_index = true
2396 index_token = parse_token(
2397 line[endpoint + 1..end_of_block-1])
2398 index = evaluate_token(namespace, index_token)
2399 begin
2400 index = Integer(index)
2401 rescue
2402 have_index = false
2403 index = nil
2404 end
2405 end
2406 value = target_namespace.get(token, :index => index)
2407 unless value.nil?
2408 content_of_var = value
2409 history << content_of_var
2410 buf = [buf,
2411 format(content_of_var)].join('')
2412 if have_index
2413 startpoint = (end_of_block + 1)
2414 else
2415 startpoint = endpoint
2416 end
2417 replaced = true
2418 break
2419 end
2420 end
2421 end
2422 endpoint -= 1
2423 end
2424 unless replaced
2425 buf = [buf, line[startpoint]].join('')
2426 startpoint += 1
2427 end
2428 end
2429 return buf
2430 end
2431
2432 def format(input_num)
2433 if input_num.is_a?(Float)
2434 result = round(input_num, 6).to_s
2435 else
2436 result = input_num.to_s
2437 end
2438 return result
2439 end
2440
2441 def evaluate_argument(namespace, name, argument, is_system_func)
2442 arguments = []
2443 for i in 0..argument.length-1
2444 if is_system_func and \
2445 @dic.aya.get_system_functions().not_to_evaluate(name, i)
2446 arguments << argument[i][1][1]
2447 else
2448 value = evaluate_statement(namespace, argument[i], 1)
2449 if value.is_a?(Array)
2450 arguments.concat(value)
2451 else
2452 arguments << value
2453 end
2454 end
2455 end
2456 return arguments
2457 end
2458
2459 def is_substitution(line)
2460 statement = AyaStatement.new(line)
2461 if statement.countTokens() >= 3
2462 statement.next_token() # left
2463 ope = statement.next_token()
2464 ope_list = ['=', ':=',
2465 '+=', '-=', '*=', '/=', '%=',
2466 '+:=', '-:=', '*:=', '/:=', '%:=',
2467 ',=']
2468 return true if ope_list.include?(ope)
2469 end
2470 return false
2471 end
2472
2473 def is_inc_or_dec(line)
2474 return false if line.length <= 2
2475 if line.end_with?('++') or line.end_with?('--')
2476 return true
2477 else
2478 return false
2479 end
2480 end
2481 end
2482
2483
2484 class AyaSystemFunctions
2485
2486 def initialize(aya)
2487 @aya = aya
2488 @current_charset = nil
2489 @fcharset = {}
2490 @charsetlib = nil
2491 @saori_statuscode = ''
2492 @saori_header = []
2493 @saori_value = {}
2494 @saori_protocol = ''
2495 @errno = 0
2496 @re_result = []
2497 @functions = {
2498 'ACOS' => ['ACOS', [nil], [1], nil],
2499 'ANY' => [],
2500 'ARRAYSIZE' => ['ARRAYSIZE', [0], [1], nil],
2501 'ASEARCH' => [],
2502 'ASEARCHEX' => [],
2503 'ASIN' => ['ASIN', [nil], [1], nil],
2504 'ATAN' => ['ATAN', [nil], [1], nil],
2505 'BINSTRTOI' => ['BINSTRTOI', [nil], [1], nil],
2506 'CEIL' => ['CEIL', [nil], [1], nil],
2507 'CHARSETLIB' => ['CHARSETLIB', [nil], [1], nil],
2508 'CHR' => ['CHR', [nil], [1], nil],
2509 'CHRCODE' => ['CHRCODE', [nil], [1], nil],
2510 'COS' => ['COS', [nil], [1], nil],
2511 'CUTSPACE' => ['CUTSPACE', [nil], [1], nil],
2512 'CVINT' => ['CVINT', [0], [1], nil],
2513 'CVREAL' => ['CVREAL', [0], [1], nil],
2514 'CVSTR' => ['CVSTR', [0], [1], nil],
2515 'ERASE' => ['ERASE', [nil], [3], nil],
2516 'ERASEVAR' => ['ERASEVAR', [nil], [1], nil],
2517 'EVAL' => ['EVAL', [nil], [1], nil],
2518 'FATTRIB' => [],
2519 'FCHARSET' => ['FCHARSET', [nil], [1], nil],
2520 'FCLOSE' => ['FCLOSE', [nil], [1], nil],
2521 'FCOPY' => ['FCOPY', [nil], [2], 259],
2522 'FDEL' => ['FDEL', [nil], [1], 269],
2523 'FENUM' => ['FENUM', [nil], [1, 2], 290],
2524 'FLOOR' => ['FLOOR', [nil], [1], nil],
2525 'FMOVE' => ['FMOVE', [nil], [2], 264],
2526 'FOPEN' => ['FOPEN', [nil], [2], 256],
2527 'FREAD' => ['FREAD', [nil], [1], nil],
2528 'FRENAME' => ['FRENAME', [nil], [2], 273],
2529 'FSIZE' => ['FSIZE', [nil], [1], 278],
2530 'FWRITE' => ['FWRITE', [nil], [2], nil],
2531 'FWRITE2' => ['FWRITE2', [nil], [2], nil],
2532 'GETDELIM' => ['GETDELIM', [0], [1], nil],
2533 'GETLASTERROR' => ['GETLASTERROR', [nil], [nil], nil],
2534 'GETMEMINFO' => [],
2535 'GETSETTING' => ['GETSETTING', [nil], [1], nil],
2536 'GETSTRBYTES' => ['GETSTRBYTES', [nil], [1, 2], nil],
2537 'GETTICKCOUNT' => ['GETTICKCOUNT', [nil], [0], nil],
2538 'GETTIME' => ['GETTIME', [nil], [0], nil],
2539 'GETTYPE' => ['GETTYPE', [nil], [1], nil],
2540 'HEXSTRTOI' => ['HEXSTRTOI', [nil], [1], nil],
2541 'IARRAY' => ['IARRAY', [nil], [nil], nil],
2542 'INSERT' => ['INSERT', [nil], [3], nil],
2543 'ISFUNC' => ['ISFUNC', [nil], [1], nil],
2544 'ISINTSTR' => ['ISINTSTR', [nil], [1], nil],
2545 'ISREALSTR' => ['ISREALSTR', [nil], [1], nil],
2546 'ISVAR' => ['ISVAR', [nil], [1], nil],
2547 'LETTONAME' => ['LETTONAME', [nil], [2], nil],
2548 'LOADLIB' => ['LOADLIB', [nil], [1], 16],
2549 'LOG' => ['LOG', [nil], [1], nil],
2550 'LOG10' => ['LOG10', [nil], [1], nil],
2551 'LOGGING' => [],
2552 'LSO' => [],
2553 'MKDIR' => ['MKDIR', [nil], [1], 282],
2554 'POW' => ['POW', [nil], [2], nil],
2555 'RAND' => ['RAND', [nil], [0, 1], nil],
2556 'RE_GETLEN' => [],
2557 'RE_GETPOS' => [],
2558 'RE_GETSTR' => ['RE_GETSTR', [nil], [0], nil],
2559 'RE_GREP' => [],
2560 'RE_MATCH' => [],
2561 'RE_REPLACE' => [],
2562 'RE_SEARCH' => [],
2563 'RE_SPLIT' => ['RE_SPLIT', [nil], [2], nil],
2564 'REPLACE' => ['REPLACE', [nil], [3], nil],
2565 'REQUESTLIB' => ['REQUESTLIB', [nil], [2], nil],
2566 'RMDIR' => ['RMDIR', [nil], [1], 286],
2567 'ROUND' => ['ROUND', [nil], [1], nil],
2568 'SAVEVAR' => ['SAVEVAR', [nil], [nil], nil],
2569 'SETDELIM' => ['SETDELIM', [0], [2], nil],
2570 'SETLASTERROR' => ['SETLASTERROR', [nil], [1], nil],
2571 'SIN' => ['SIN', [nil], [1], nil],
2572 'SPLIT' => ['SPLIT', [nil], [2, 3], nil],
2573 'SPLITPATH' => ['SPLITPATH', [nil], [1], nil ],
2574 'SQRT' => ['SQRT', [nil], [1], nil],
2575 'STRFORM' => [],
2576 'STRLEN' => ['STRLEN', [1], [1], nil],
2577 'STRSTR' => ['STRSTR', [3], [3], nil],
2578 'SUBSTR' => ['SUBSTR', [nil], [3], nil],
2579 'TAN' => ['TAN', [nil], [1], nil],
2580 'TOBINSTR' => ['TOBINSTR', [nil], [1], nil],
2581 'TOHEXSTR' => ['TOHEXSTR', [nil], [1], nil],
2582 'TOINT' => ['TOINT', [nil], [1], nil],
2583 'TOLOWER' => ['TOLOWER', [nil], [1], nil],
2584 'TOREAL' => ['TOREAL', [nil], [1], nil],
2585 'TOSTR' => ['TOSTR', [nil], [1], nil],
2586 'TOUPPER' => ['TOUPPER', [nil], [1], nil],
2587 'UNLOADLIB' => ['UNLOADLIB', [nil], [1], nil],
2588 #'LOGGING' => ['LOGGING', [nil], [4], nil]
2589 }
2590 end
2591
2592 def exists(name)
2593 @functions.include?(name)
2594 end
2595
2596 def call(namespace, name, argv)
2597 @errno = 0
2598 if not @functions.include?(name)
2599 return ''
2600 elsif not @functions[name] # not implemented yet
2601 Logging::Logging.warning(
2602 'aya5.py: SYSTEM FUNCTION "' + name.to_s + '" is not implemented yet.')
2603 return ''
2604 elsif check_num_args(name, argv)
2605 return method(@functions[name][0]).call(namespace, argv)
2606 else
2607 return ''
2608 end
2609 end
2610
2611 def not_to_evaluate(name, index)
2612 return false unless @functions.include?(name) # XXX
2613 if @functions[name][1].include?(index)
2614 return true
2615 else
2616 return false
2617 end
2618 end
2619
2620 def check_num_args(name, argv)
2621 list_num = @functions[name][2]
2622 if list_num == [nil]
2623 return true
2624 else
2625 return true if list_num.include?(argv.length)
2626 list_num.sort!
2627 if argv.length < list_num[0]
2628 errno = @functions[name][3]
2629 @errno = errno unless errno.nil?
2630 Logging::Logging.debug(
2631 [name.to_s, ': called with too few argument(s)'].join(''))
2632 return false
2633 end
2634 return true
2635 end
2636 end
2637
2638 def ACOS(namespace, argv)
2639 begin
2640 result = math.acos(Float(argv[0]))
2641 rescue
2642 return -1
2643 end
2644 return select_math_type(result)
2645 end
2646
2647 def ANY(namespace, argv)
2648 #pass
2649 end
2650
2651 def ARRAYSIZE(namespace, argv)
2652 argv.length ## FIXME
2653 end
2654
2655 def ASEARCH(namespace, argv)
2656 #pass
2657 end
2658
2659 def ASEARCHEX(namespace, argv)
2660 #pass
2661 end
2662
2663 def ASIN(namespace, argv)
2664 begin
2665 result = math.asin(Float(argv[0]))
2666 rescue
2667 return -1
2668 end
2669 return select_math_type(result)
2670 end
2671
2672 def ATAN(namespace, argv)
2673 begin
2674 result = math.atan(Float(argv[0]))
2675 rescue
2676 return -1
2677 end
2678 return select_math_type(result)
2679 end
2680
2681 def BINSTRTOI(namespace, argv)
2682 begin
2683 return argv[0].to_s.to_i(2)
2684 rescue
2685 return 0
2686 end
2687 end
2688
2689 def CEIL(namespace, argv)
2690 begin
2691 return math.ceil(argv[0].to_f).to_i
2692 rescue
2693 return -1
2694 end
2695 end
2696
2697 def CHARSETLIB(namespace, argv)
2698 begin
2699 value = Integer(argv[0])
2700 rescue
2701 return
2702 end
2703 case value
2704 when 0
2705 @charsetlib = 'CP932'
2706 when 1
2707 @charsetlib = 'UTF-8'
2708 when 127
2709 @charsetlib = @aya.charset
2710 end
2711 end
2712
2713 def CHR(namespace, argv)
2714 begin
2715 return Integer(argv[0]).chr
2716 rescue
2717 return ''
2718 end
2719 end
2720
2721 def CHRCODE(namespace, argv)
2722 line = argv[0].to_s
2723 unless line.empty?
2724 return line[0].encode('utf-16', 'ignore') # UCS-2
2725 else
2726 return ''
2727 end
2728 end
2729
2730 def COS(namespace, argv)
2731 begin
2732 result = math.cos(argv[0].to_f)
2733 rescue
2734 return -1
2735 end
2736 return select_math_type(result)
2737 end
2738
2739 def CUTSPACE(namespace, argv)
2740 return argv[0].to_s.strip()
2741 end
2742
2743 def CVINT(namespace, argv)
2744 var = argv[0].to_s
2745 target_namespace = select_namespace(namespace, var)
2746 token = target_namespace.get(var)
2747 begin
2748 result = Integer(token)
2749 rescue
2750 result = 0
2751 end
2752 target_namespace.put(var, result)
2753 return nil
2754 end
2755
2756 def CVREAL(namespace, argv)
2757 var = argv[0].to_s
2758 target_namespace = select_namespace(namespace, var)
2759 token = target_namespace.get(var)
2760 begin
2761 result = Float(token)
2762 rescue
2763 result = 0.0
2764 end
2765 target_namespace.put(var, result)
2766 return nil
2767 end
2768
2769 def CVSTR(namespace, argv)
2770 name = argv[0].to_s
2771 target_namespace = select_namespace(namespace, name)
2772 value = target_namespace.get(name).to_s
2773 target_namespace.put(name, value)
2774 return nil
2775 end
2776
2777 def ERASE(namespace, argv)
2778 line = argv[0].to_s
2779 begin
2780 start = argv[1].to_i
2781 bytes = argv[2].to_i
2782 rescue
2783 return ''
2784 end
2785 return [line[0..start-1], line[start + bytes..-1]].join('').encode("UTF-8", :invalid => :replace, :undef => :replace) # XXX
2786 end
2787
2788 def ERASEVAR(namespace, argv)
2789 var = argv[0].to_s
2790 target_namespace = select_namespace(namespace, var)
2791 target_namespace.remove(var)
2792 return nil # XXX
2793 end
2794
2795 def EVAL(namespace, argv)
2796 script = argv[0]
2797 func = AyaFunction.new(@aya.dic, '', [script], nil)
2798 result = func.call()
2799 return result
2800 end
2801
2802 def FATTRIB(namespace, argv)
2803 #pass
2804 end
2805
2806 def FCHARSET(namespace, argv)
2807 begin
2808 value = Integer(argv[0])
2809 rescue
2810 return
2811 end
2812 case value
2813 when 0
2814 @current_charset = 'CP932'
2815 when 1
2816 @current_charset = 'UTF-8'
2817 when 127
2818 @current_charset = @aya.charset
2819 end
2820 end
2821
2822 def FCLOSE(namespace, argv)
2823 filename = Home.get_normalized_path(argv[0].to_s)
2824 path = File.join(@aya.aya_dir, filename)
2825 norm_path = File.expand_path(path)
2826 if @aya.filelist.include?(norm_path)
2827 @aya.filelist[norm_path].close()
2828 @aya.filelist.delete(norm_path)
2829 @f_charset.delete(norm_path)
2830 end
2831 return nil
2832 end
2833
2834 def FCOPY(namespace, argv)
2835 src = Home.get_normalized_path(argv[0].to_s)
2836 head, tail = File.split(src)
2837 dst = [Home.get_normalized_path(argv[1].to_s), '/', tail].join('')
2838 src_path = File.join(@aya.aya_dir, src)
2839 dst_path = File.join(@aya.aya_dir, dst)
2840 result = 0
2841 if not File.file?(src_path)
2842 @errno = 260
2843 elsif not File.directory?(dst_path)
2844 @errno = 261
2845 else
2846 begin
2847 shutil.copyfile(src_path, dst_path)
2848 rescue
2849 @errno = 262
2850 else
2851 result = 1
2852 end
2853 end
2854 return result
2855 end
2856
2857 def FDEL(namespace, argv)
2858 filename = Home.get_normalized_path(argv[0].to_s)
2859 path = File.join(@aya.aya_dir, filename)
2860 result = 0
2861 unless File.file?(path)
2862 @errno = 270
2863 else
2864 begin
2865 File.delete(path)
2866 rescue
2867 @errno = 271
2868 else
2869 result = 1
2870 end
2871 end
2872 return result
2873 end
2874
2875 def FENUM(namespace, argv)
2876 if argv.length >= 2
2877 separator = argv[1].to_s
2878 else
2879 separator = ','
2880 end
2881 dirname = Home.get_normalized_path(argv[0].to_s)
2882 path = File.join(@aya.aya_dir, dirname)
2883 filelist = []
2884 begin
2885 filelist = Dir.entries(path).reject{|entry| entry =~ /^\.{1,2}$/}
2886 rescue
2887 @errno = 291
2888 end
2889 result = ''
2890 for index in 0..filelist.length-1
2891 path = File.join(@aya.aya_dir, dirname, filelist[index])
2892 if File.directory?(path)
2893 result = [result, "\\"].join('')
2894 end
2895 result = [result, filelist[index]].join('')
2896 if index != (filelist.length - 1)
2897 result = [result, separator].join('')
2898 end
2899 end
2900 return result
2901 end
2902
2903 def FLOOR(namespace, argv)
2904 begin
2905 return math.floor(argv[0].to_f).to_i
2906 rescue
2907 return -1
2908 end
2909 end
2910
2911 def FMOVE(namespace, argv)
2912 src = Home.get_normalized_path(argv[0].to_s)
2913 head, tail = File.split(src)
2914 dst = [Home.get_normalized_path(argv[1].to_s), '/', tail].join('')
2915 src_path = File.join(@aya.aya_dir, src)
2916 dst_path = File.join(@aya.aya_dir, dst)
2917 result = 0
2918 head, tail = File.split(dst_path)
2919 if not File.file?(src_path)
2920 @errno = 265
2921 elsif not File.directory?(head)
2922 @errno = 266
2923 else
2924 begin
2925 File.rename(src_path, dst_path)
2926 rescue
2927 @errno = 267
2928 else
2929 result = 1
2930 end
2931 end
2932 return result
2933 end
2934
2935 def FOPEN(namespace, argv)
2936 filename = Home.get_normalized_path(argv[0].to_s)
2937 accessmode = argv[1].to_s
2938 result = 0
2939 path = File.join(@aya.aya_dir, filename)
2940 norm_path = File.expand_path(path)
2941 if @aya.filelist.include?(norm_path)
2942 result = 2
2943 else
2944 begin
2945 @aya.filelist[norm_path] = open(path, [accessmode[0], 'b'].join('')) # XXX
2946 rescue
2947 @errno = 257
2948 else
2949 if @current_charset.nil?
2950 @current_charset = @aya.charset
2951 end
2952 @f_charset[norm_path] = @current_charset
2953 result = 1
2954 end
2955 end
2956 return result
2957 end
2958
2959 def FREAD(namespace, argv)
2960 filename = Home.get_normalized_path(argv[0].to_s)
2961 path = File.join(@aya.aya_dir, filename)
2962 norm_path = File.expand_path(path)
2963 result = -1
2964 if @aya.filelist.include?(norm_path)
2965 f = @aya.filelist[norm_path]
2966 result = f.readline().force_encoding(@fcharset[norm_path])
2967 if result.nil?
2968 result = -1
2969 elsif result.end_with?("\r\n")
2970 result = result[0..-3]
2971 elsif result.end_with?("\n")
2972 result = result[0..-2]
2973 end
2974 end
2975 return result
2976 end
2977
2978 def FRENAME(namespace, argv)
2979 src = Home.get_normalized_path(argv[0].to_s)
2980 dst = Home.get_normalized_path(argv[1].to_s)
2981 src_path = File.join(@aya.aya_dir, src)
2982 dst_path = File.join(@aya.aya_dir, dst)
2983 result = 0
2984 head, tail = File.split(dst_path)
2985 if not File.exist?(src_path)
2986 @errno = 274
2987 elsif not File.directory?(head)
2988 @errno = 275
2989 else
2990 begin
2991 File.rename(src_path, dst_path)
2992 rescue
2993 @errno = 276
2994 else
2995 result = 1
2996 end
2997 end
2998 return result
2999 end
3000
3001 def FSIZE(namespace, argv)
3002 filename = Home.get_normalized_path(argv[0].to_s)
3003 path = File.join(@aya.aya_dir, filename)
3004 size = -1
3005 unless File.exist?(path)
3006 @errno = 279
3007 else
3008 begin
3009 size = File.size(path)
3010 rescue
3011 @errno = 280
3012 end
3013 end
3014 return size
3015 end
3016
3017 def FWRITE(namespace, argv)
3018 filename = Home.get_normalized_path(argv[0].to_s)
3019 path = File.join(@aya.aya_dir, filename)
3020 norm_path = File.expand_path(path)
3021 data = [argv[1].to_s, "\n"].join('')
3022 if @aya.filelist.include?(norm_path)
3023 f = @aya.filelist[norm_path]
3024 f.write(data.encode(@fcharset[norm_path], 'ignore'))
3025 end
3026 return nil
3027 end
3028
3029 def FWRITE2(namespace, argv)
3030 filename = Home.get_normalized_path(argv[0].to_s)
3031 path = File.join(@aya.aya_dir, filename)
3032 norm_path = File.expand_path(path)
3033 data = argv[1].to_s
3034 if @aya.filelist.include?(norm_path)
3035 f = @aya.filelist[norm_path]
3036 f.write(data.encode(@fcharset[norm_path], 'ignore'))
3037 end
3038 return nil
3039 end
3040
3041 def GETDELIM(namespace, argv)
3042 name = argv[0].to_s
3043 target_namespace = select_namespace(namespace, name)
3044 return target_namespace.get_separator(name)
3045 end
3046
3047 def GETLASTERROR(namespace, argv)
3048 @errno
3049 end
3050
3051 def GETMEMINFO(namespace, argv)
3052 #pass
3053 end
3054
3055 def GETSETTING(namespace, argv)
3056 begin
3057 value = Integer(argv[0])
3058 rescue
3059 return ''
3060 end
3061 case value
3062 when 0
3063 result = '5'
3064 when 1
3065 if @aya.charset == 'CP932'
3066 result = 0
3067 elsif @aya.charset == 'UTF-8'
3068 result = 1
3069 else
3070 result = 2
3071 end
3072 when 2
3073 result = @aya.aya_dir
3074 else
3075 result = ''
3076 end
3077 return result
3078 end
3079
3080 def GETSTRBYTES(namespace, argv)
3081 line = argv[0].to_s
3082 if argv.length > 1
3083 begin
3084 value = Integer(argv[1])
3085 rescue
3086 value = 0
3087 end
3088 else
3089 value = 0
3090 end
3091 case value
3092 when 0
3093 result = line.encode('CP932', 'ignore').length
3094 when 1
3095 result = line.encode('utf-8', 'ignore').length
3096 when 2
3097 result = line.encode(@aya.charset, 'ignore').length
3098 else
3099 result = -1
3100 end
3101 return result
3102 end
3103
3104 def GETTICKCOUNT(namespace, argv)
3105 past = (Time.now - @aya.get_boot_time())
3106 return (past * 1000.0).to_i
3107 end
3108
3109 def GETTIME(namespace, argv)
3110 t = Time.now
3111 return [t.year, t.month, t.day, t.wday, t.hour, t.min, t.sec]
3112 end
3113
3114 def GETTYPE(namespace, argv)
3115 if argv[0].is_a?(Integer)
3116 result = 1
3117 elsif argv[0].is_a?(Float)
3118 result = 2
3119 elsif argv[0].is_a?(String)
3120 result = 3
3121 elsif 0 ## FIXME: array
3122 result = 4
3123 else
3124 result = 0
3125 end
3126 return result
3127 end
3128
3129 def HEXSTRTOI(namespace, argv)
3130 begin
3131 return argv[0].to_s.to_i(16)
3132 rescue
3133 return 0
3134 end
3135 end
3136
3137 def IARRAY(namespace, argv)
3138 return [] # AyaVariable.new('', :new_array => true) ## FIXME
3139 end
3140
3141 def INSERT(namespace, argv)
3142 line = argv[0].to_s
3143 begin
3144 start = Integer(argv[1])
3145 rescue
3146 return ''
3147 end
3148 to_insert = argv[2].to_s
3149 if start < 0
3150 start = 0
3151 end
3152 return [line[0..start-1], to_insert, line[start..-1]].join('')
3153 end
3154
3155 def ISFUNC(namespace, argv)
3156 if not argv[0].is_a?(String)
3157 return 0
3158 elsif not @aya.dic.get_function(argv[0]).nil?
3159 return 1
3160 elsif @aya.get_system_functions().exists(argv[0])
3161 return 2
3162 else
3163 return 0
3164 end
3165 end
3166
3167 def ISINTSTR(namespace, argv)
3168 begin
3169 Integer(argv[0].to_s)
3170 return 1
3171 rescue
3172 return 0
3173 end
3174 end
3175
3176 def ISREALSTR(namespace, argv)
3177 begin
3178 Float(argv[0].to_s)
3179 return 1
3180 rescue
3181 return 0
3182 end
3183 end
3184
3185 def ISVAR(namespace, argv)
3186 var = argv[0].to_s
3187 if var.start_with?('_')
3188 if namespace.exists(var)
3189 return 2
3190 else
3191 return 0
3192 end
3193 else
3194 if @aya.get_global_namespace().exists(var)
3195 return 1
3196 else
3197 return 0
3198 end
3199 end
3200 end
3201
3202 def LETTONAME(namespace, argv)
3203 var = argv[0].to_s
3204 value = argv[1]
3205 return nil if var.empty?
3206 target_namespace = select_namespace(namespace, var)
3207 target_namespace.put(var, value)
3208 return nil
3209 end
3210
3211 def LOADLIB(namespace, argv)
3212 dll = argv[0].to_s
3213 result = 0
3214 unless dll.empty?
3215 if @charsetlib.nil?
3216 @charsetlib = @aya.charset
3217 end
3218 @aya.saori_library.set_charset(@charsetlib)
3219 result = @aya.saori_library.load(dll, @aya.aya_dir)
3220 @errno = 17 if result.zero?
3221 end
3222 return result
3223 end
3224
3225 def LOG(namespace, argv)
3226 begin
3227 argv[0].to_f
3228 rescue
3229 return -1
3230 end
3231 return 0 if argv[0].to_f.zero?
3232 result = math.log(argv[0].to_f)
3233 return select_math_type(result)
3234 end
3235
3236 def LOG10(namespace, argv)
3237 begin
3238 argv[0].to_f
3239 rescue
3240 return -1
3241 end
3242 return 0 if argv[0].to_f.zero?
3243 result = math.log10(argv[0].to_f)
3244 return select_math_type(result)
3245 end
3246
3247 def LSO(namespace, argv)
3248 #pass
3249 end
3250
3251 def MKDIR(namespace, argv)
3252 dirname = Home.get_normalized_path(argv[0].to_s)
3253 path = File.join(@aya.aya_dir, dirname)
3254 result = 0
3255 head, tail = File.split(path)
3256 unless File.directory?(head)
3257 @errno = 283
3258 else
3259 begin
3260 Dir.mkdir(path, 0o755)
3261 rescue
3262 @errno = 284
3263 else
3264 result = 1
3265 end
3266 end
3267 return result
3268 end
3269
3270 def POW(namespace, argv)
3271 begin
3272 result = math.pow(argv[0].to_f, argv[1].to_f)
3273 rescue
3274 return -1
3275 end
3276 return select_math_type(result)
3277 end
3278
3279 def RAND(namespace, argv)
3280 if argv.empty?
3281 return Random.rand(0..99)
3282 else
3283 begin
3284 Integer(argv[0])
3285 rescue
3286 return -1
3287 end
3288 return Random.rand(0..argv[0].to_i-1)
3289 end
3290 end
3291
3292 def RE_GETLEN(namespace, argv)
3293 #pass
3294 end
3295
3296 def RE_GETPOS(namespace, argv)
3297 #pass
3298 end
3299
3300 def RE_GETSTR(namespace, argv)
3301 #result_array = AyaVariable.new('', :new_array => true)
3302 #result_array.put(@re_result)
3303 #return result_array
3304 return @re_result
3305 end
3306
3307 def RE_GREP(namespace, argv)
3308 #pass
3309 end
3310
3311 def RE_MATCH(namespace, argv)
3312 #pass
3313 end
3314
3315 def RE_REPLACE(namespace, argv)
3316 #pass
3317 end
3318
3319 def RE_SEARCH(namespace, argv)
3320 #pass
3321 end
3322
3323 def RE_SPLIT(namespace, argv)
3324 line = argv[0].to_s
3325 re_split = Regexp.new(argv[1].to_s)
3326 # if argv.length > 2
3327 # begin
3328 # max = argv[2]
3329 # rescue
3330 # return []
3331 # else
3332 # result = re_split.split(line, max)
3333 # end
3334 # else
3335 result = line.split(re_split)
3336 # end
3337 @re_result = line.scan(re_split)
3338 #result_array = AyaVariable.new('', :new_array => true)
3339 #result_array.put(result)
3340 #return result_array
3341 return result
3342 end
3343
3344 def REPLACE(namespace, argv)
3345 line = argv[0].to_s
3346 old = argv[1].to_s
3347 new = argv[2].to_s
3348 return line.gsub(old, new)
3349 end
3350
3351 def REQUESTLIB(namespace, argv)
3352 response = @aya.saori_library.request(argv[0], argv[1])
3353 header = response.split(/\r?\n/, 0)
3354 @saori_statuscode = ''
3355 @saori_header = []
3356 @saori_value = {}
3357 @saori_protocol = ''
3358 unless header.nil? or header.empty?
3359 line = header.shift
3360 line = line.strip()
3361 if line.include?(' ')
3362 @saori_protocol, @saori_statuscode = line.split(' ', 2)
3363 @saori_protocol.strip!
3364 @saori_statuscode.strip!
3365 end
3366 for line in header
3367 next unless line.include?(':')
3368 key, value = line.split(':', 2)
3369 key.strip!
3370 value.strip!
3371 unless key.empty?
3372 @saori_header << key
3373 @saori_value[key] = value
3374 end
3375 end
3376 end
3377 return response
3378 end
3379
3380 def RMDIR(namespace, argv)
3381 dirname = Home.get_normalized_path(argv[0].to_s)
3382 path = File.join(@aya.aya_dir, dirname)
3383 result = 0
3384 unless File.directory?(path)
3385 @errno = 287
3386 else
3387 begin
3388 Dir.rmdir(path)
3389 rescue
3390 @errno = 288
3391 else
3392 result = 1
3393 end
3394 end
3395 return result
3396 end
3397
3398 def ROUND(namespace, argv)
3399 begin
3400 value = math.floor(Float(argv[0]) + 0.5)
3401 rescue
3402 return -1
3403 end
3404 return value.to_i
3405 end
3406
3407 def SAVEVAR(namespace, argv)
3408 @aya.get_global_namespace().save_database()
3409 end
3410
3411 def SETDELIM(namespace, argv)
3412 name = argv[0].to_s
3413 separator = argv[1].to_s
3414 target_namespace = select_namespace(namespace, name)
3415 target_namespace.set_separator(name, separator)
3416 return nil
3417 end
3418
3419 def SETLASTERROR(namespace, argv)
3420 begin
3421 value = Integer(argv[0])
3422 rescue
3423 return
3424 end
3425 @errno = value
3426 end
3427
3428 def SIN(namespace, argv)
3429 begin
3430 result = math.sin(Float(argv[0]))
3431 rescue
3432 return -1
3433 end
3434 return select_math_type(result)
3435 end
3436
3437 def SPLIT(namespace, argv)
3438 line = argv[0].to_s
3439 result = line.split(argv[1].to_s, argv[2].to_i + 1)
3440 return result
3441 end
3442
3443 def SPLITPATH(namespace, argv)
3444 line = argv[0].to_s
3445 drive, path = "", line # XXX
3446 dirname, filename = File.split(path)
3447 basename = File.basename(filename, ".*")
3448 ext = File.extname(filename)
3449 return [drive, dirname, basename, ext]
3450 end
3451
3452 def SQRT(namespace, argv)
3453 begin
3454 arg = Float(argv[0])
3455 rescue
3456 return -1
3457 end
3458 if arg < 0.0
3459 return -1
3460 else
3461 result = math.sqrt(arg)
3462 return select_math_type(result)
3463 end
3464 end
3465
3466 def STRFORM(namespace, argv)
3467 #pass
3468 end
3469
3470 def STRLEN(namespace, argv)
3471 line = argv[0].to_s
3472 return line.length
3473 end
3474
3475 def STRSTR(namespace, argv)
3476 line = argv[0].to_s
3477 to_find = argv[1].to_s
3478 begin
3479 start = Integer(argv[2])
3480 rescue
3481 return -1
3482 end
3483 result = line.index(to_find, start)
3484 return result
3485 end
3486
3487 def SUBSTR(namespace, argv)
3488 line = argv[0].to_s
3489 begin
3490 start = Integer(argv[1])
3491 num = Integer(argv[2])
3492 rescue
3493 return ''
3494 end
3495 return line[start..start + num-1]
3496 end
3497
3498 def TAN(namespace, argv)
3499 begin
3500 result = math.tan(Float(argv[0]))
3501 rescue
3502 return -1
3503 end
3504 return select_math_type(result)
3505 end
3506
3507 def TOBINSTR(namespace, argv)
3508 begin
3509 i = Integer(argv[0])
3510 rescue
3511 return ''
3512 end
3513 if i < 0
3514 i = abs(i)
3515 numsin = '-'
3516 else
3517 numsin = ''
3518 end
3519 line = ''
3520 while not i.zero?
3521 mod = (i % 2)
3522 i = (i / 2).to_i
3523 line = [mod.to_s, line].join('')
3524 end
3525 line = [numsin, line].join('')
3526 return line
3527 end
3528
3529 def TOHEXSTR(namespace, argv)
3530 begin
3531 return argv[0].to_i.to_s(16)
3532 rescue
3533 return ''
3534 end
3535 end
3536
3537 def TOINT(namespace, argv)
3538 token = argv[0].to_s
3539 begin
3540 value = Integer(token)
3541 rescue
3542 return 0
3543 else
3544 return value
3545 end
3546 end
3547
3548 def TOLOWER(namespace, argv)
3549 argv[0].to_s.downcase
3550 end
3551
3552 def TOREAL(namespace, argv)
3553 token = argv[0].to_s
3554 begin
3555 value = Float(token)
3556 rescue
3557 return 0.0
3558 else
3559 return value
3560 end
3561 end
3562
3563 def TOSTR(namespace, argv)
3564 argv[0].to_s
3565 end
3566
3567 def TOUPPER(namespace, argv)
3568 argv[0].to_s.upcase
3569 end
3570
3571 def UNLOADLIB(namespace, argv)
3572 unless argv[0].to_s.empty?
3573 @aya.saori_library.unload(:name => argv[0].to_s)
3574 end
3575 return nil
3576 end
3577
3578 def LOGGING(namespace, argv) ## FIXME
3579 return nil if argv[0].nil?
3580 logfile = argv[0]
3581 line = ['> function ', argv[1].to_s, ' : ', argv[2].to_s].join('')
3582 unless argv[3].nil?
3583 line = [line, ' = '].join('')
3584 if argv[3].is_a?(Integer) or argv[3].is_a?(Float)
3585 line = [line, argv[3].to_s].join('')
3586 else
3587 line = [line, '"', argv[3].to_s, '"'].join('')
3588 end
3589 end
3590 line = [line, "\n"].join('')
3591 logfile.write(line)
3592 logfile.write("\n")
3593 return nil
3594 end
3595
3596 def select_math_type(value)
3597 if math.floor(value) == value
3598 return value.to_i
3599 else
3600 return value
3601 end
3602 end
3603
3604 def select_namespace(namespace, name)
3605 if name.start_with?('_')
3606 return namespace
3607 else
3608 return @aya.get_global_namespace()
3609 end
3610 end
3611 end
3612
3613
3614 class AyaNamespace
3615
3616 def initialize(aya, parent: nil)
3617 @aya = aya
3618 @parent = parent
3619 @table = {}
3620 end
3621
3622 def put(name, content, index: nil)
3623 if not @parent.nil? and @parent.exists(name)
3624 @parent.put(name, content, :index => index)
3625 elsif index.nil?
3626 unless exists(name)
3627 @table[name] = AyaVariable.new(name)
3628 end
3629 @table[name].put(content)
3630 elsif exists(name) and index >=0
3631 @table[name].put(content, :index => index)
3632 else
3633 #pass # ERROR
3634 end
3635 end
3636
3637 def get(name, index: nil)
3638 if @table.include?(name)
3639 return @table[name].get(:index => index)
3640 elsif not @parent.nil? and @parent.exists(name)
3641 return @parent.get(name, :index => index)
3642 else
3643 return nil
3644 end
3645 end
3646
3647 def get_array(name)
3648 if @table.include?(name)
3649 return @table[name].get_array()
3650 elsif not @parent.nil? and @parent.exists(name)
3651 return @parent.get_array(name)
3652 else
3653 return []
3654 end
3655 end
3656
3657 def get_separator(name)
3658 if not @parent.nil? and @parent.exists(name)
3659 return @parent.get_separator(name)
3660 elsif @table.include?(name)
3661 @table[name].get_separator()
3662 else
3663 return '' # ERROR
3664 end
3665 end
3666
3667 def set_separator(name, separator)
3668 if not @parent.nil? and @parent.exists(name)
3669 @parent.set_separator(name, separator)
3670 elsif @table.include?(name)
3671 @table[name].set_separator(separator)
3672 else
3673 #pass # ERROR
3674 end
3675 end
3676
3677 def get_size(name)
3678 if @table.include?(name)
3679 return @table[name].get_size()
3680 elsif not @parent.nil? and @parent.exists(name)
3681 return @parent.get_size(name)
3682 else
3683 return 0
3684 end
3685 end
3686
3687 def remove(name) # only works with local table
3688 if @table.include?(name)
3689 @table.delete(name)
3690 end
3691 end
3692
3693 def exists(name)
3694 result = (@table.include?(name) or \
3695 (not @parent.nil? and @parent.exists(name)))
3696 return result
3697 end
3698 end
3699
3700
3701 class AyaGlobalNamespace < AyaNamespace
3702
3703 def load_database(aya)
3704 begin
3705 open(aya.dbpath, [email protected]) do |f|
3706 line = f.readline()
3707 return 1 unless line.start_with?('# Format: v1.2')
3708 for line in f
3709 comma = line.find(',')
3710 if comma >= 0
3711 key = line[:comma]
3712 else
3713 next
3714 end
3715 value = line[comma + 1..-1].strip()
3716 comma = find_not_quoted(value, ',')
3717 if comma >= 0
3718 separator = value[comma + 1..-1].strip()
3719 separator = separator[1..-2]
3720 value = value[0..comma-1].strip()
3721 value = value[1..-2]
3722 put(key, value)
3723 @table[key].set_separator(separator)
3724 elsif value != 'None'
3725 if value.include?('.')
3726 put(key, Float(value))
3727 else
3728 put(key, Integer(value))
3729 end
3730 else
3731 #pass
3732 end
3733 end
3734 end
3735 rescue
3736 return 1
3737 end
3738 return 0
3739 end
3740
3741 def save_database
3742 begin
3743 open(@aya.dbpath, 'w', :encoding => @aya.charset) do |f|
3744 f.write("# Format: v1.2\n")
3745 for key in @table.keys()
3746 line = @table[key].dump()
3747 f.write([line, "\n"].join('')) unless line.nil?
3748 end
3749 end
3750 rescue #except IOError:
3751 Logging::Logging.debug('aya.py: cannot write database (ignored)')
3752 return
3753 end
3754 end
3755 end
3756
3757
3758 class AyaStatement
3759 attr_reader :tokens
3760
3761 SPECIAL_CHARS = '=+-*/<>|&!:,_'
3762
3763 def initialize(line)
3764 @n_tokens = 0
3765 @tokens = []
3766 @position_of_next_token = 0
3767 tokenize(line)
3768 end
3769
3770 def tokenize(line) ## FIXME: '[', ']'
3771 token_startpoint = 0
3772 block_nest_level = 0
3773 length = line.length
3774 i = 0
3775 while i < length
3776 c = line[i]
3777 if c == '('
3778 block_nest_level += 1
3779 unless i.zero?
3780 append_unless_empty(line[token_startpoint..i-1].strip())
3781 end
3782 @tokens << '('
3783 i += 1
3784 token_startpoint = i
3785 elsif c == ')'
3786 block_nest_level -= 1
3787 unless i.zero?
3788 append_unless_empty(line[token_startpoint..i-1].strip())
3789 end
3790 @tokens << ')'
3791 i += 1
3792 token_startpoint = i
3793 elsif c == '['
3794 unless i.zero?
3795 append_unless_empty(line[token_startpoint..i-1].strip())
3796 end
3797 @tokens << '['
3798 i += 1
3799 token_startpoint = i
3800 elsif c == ']'
3801 unless i.zero?
3802 append_unless_empty(line[token_startpoint..i-1].strip())
3803 end
3804 @tokens << ']'
3805 i += 1
3806 token_startpoint = i
3807 elsif c == '"'
3808 position = line.index('"', i + 1)
3809 unless position or position < 0 ## FIXME
3810 fail SystemExit ## FIXME
3811 end
3812 i = position
3813 @tokens << line[token_startpoint..position]
3814 token_startpoint = (position + 1)
3815 i = (position + 1)
3816 elsif c == "'"
3817 position = line.index("'", i + 1)
3818 if position.nil? or position < 0 ## FIXME
3819 fail SystemExit ## FIXME
3820 end
3821 i = position
3822 @tokens << line[token_startpoint..position]
3823 token_startpoint = position + 1
3824 i = (position + 1)
3825 elsif c == ' ' or c == '\t' or c == ' '
3826 unless i.zero?
3827 append_unless_empty(line[token_startpoint..i-1].strip())
3828 end
3829 i += 1
3830 token_startpoint = i
3831 elsif SPECIAL_CHARS.include?(c)
3832 ope_list = ['!_in_', '_in_',
3833 '+:=', '-:=', '*:=', '/:=', '%:=',
3834 ':=', '+=', '-=', '*=', '/=', '%=', '<=',
3835 '>=', '==', '!=', '&&', '||', ',=', '++', '--',
3836 ',', '=', '!', '+', '-', '/', '*', '%', '&']
3837 break_flag = false
3838 for ope in ope_list
3839 if line[i..-1].start_with?(ope) ## FIXME
3840 unless i.zero?
3841 append_unless_empty(line[token_startpoint..i-1].strip())
3842 end
3843 num = ope.length
3844 @tokens << line[i..i + num -1]
3845 i += num
3846 token_startpoint = i
3847 break_flag = true
3848 break
3849 end
3850 end
3851 unless break_flag
3852 i += 1
3853 end
3854 else
3855 i += 1
3856 end
3857 end
3858 append_unless_empty(line[token_startpoint..-1].strip())
3859 @n_tokens = @tokens.length
3860 end
3861
3862 def append_unless_empty(token)
3863 @tokens << token unless token.nil? or token.empty?
3864 end
3865
3866 def has_more_tokens
3867 return (@position_of_next_token < @n_tokens)
3868 end
3869
3870 def countTokens
3871 @n_tokens
3872 end
3873
3874 def next_token
3875 return nil unless has_more_tokens()
3876 result = @tokens[@position_of_next_token]
3877 @position_of_next_token += 1
3878 return result
3879 end
3880 end
3881
3882
3883 class AyaVariable
3884
3885 TYPE_STRING = 0
3886 TYPE_INT = 1
3887 TYPE_REAL = 2
3888 TYPE_ARRAY = 3
3889 TYPE_NEW_ARRAY = 4
3890
3891 def initialize(name, new_array: false)
3892 @name = name
3893 @line = ''
3894 @separator = ','
3895 if new_array
3896 @type = TYPE_NEW_ARRAY
3897 else
3898 @type = nil
3899 end
3900 @array = []
3901 end
3902
3903 def get_array
3904 @array
3905 end
3906
3907 def get_separator
3908 @separator
3909 end
3910
3911 def set_separator(separator)
3912 return if @type != TYPE_STRING
3913 @separator = separator
3914 reset()
3915 end
3916
3917 def reset
3918 return if @type != TYPE_STRING
3919 @position = 0
3920 @is_empty = false
3921 @array = []
3922 while not @is_empty
3923 separator_position = @line.index(@separator, @position)
3924 if separator_position.nil?
3925 token = @line[@position..-1]
3926 @is_empty = true
3927 else
3928 token = @line[@position..separator_position-1]
3929 @position = (separator_position + @separator.length)
3930 end
3931 @array << token
3932 end
3933 end
3934
3935 def get_size
3936 @array.length
3937 end
3938
3939 def get(index: nil)
3940 if index.nil?
3941 case @type
3942 when TYPE_STRING
3943 result = @line.to_s
3944 when TYPE_INT
3945 result = @line.to_i
3946 when TYPE_REAL
3947 result = @line.to_f
3948 when TYPE_NEW_ARRAY
3949 result = @array ## FIXME(?)
3950 else
3951 result = ''
3952 end
3953 return result
3954 end
3955 if 0 <= index and index < @array.length
3956 value = @array[index]
3957 case @type
3958 when TYPE_STRING
3959 result = value.to_s
3960 when TYPE_INT
3961 result = value.to_i
3962 when TYPE_REAL
3963 result = value.to_f
3964 when TYPE_ARRAY
3965 result = value
3966 when TYPE_NEW_ARRAY
3967 result = value ## FIXME
3968 else
3969 result = nil # should not reach here
3970 end
3971 elsif index.nil?
3972 case @type
3973 when TYPE_STRING
3974 result = @line.to_s
3975 when TYPE_INT
3976 result = @line.to_i
3977 when TYPE_REAL
3978 result = @line.to_f
3979 when TYPE_NEW_ARRAY
3980 result = @array ## FIXME(?)
3981 else
3982 result = ''
3983 end
3984 else
3985 result = ''
3986 end
3987 return result
3988 end
3989
3990 def put(value, index: nil)
3991 if index.nil?
3992 @line = value.to_s
3993 if value.is_a?(String)
3994 @type = TYPE_STRING
3995 elsif value.is_a?(Integer)
3996 @type = TYPE_INT
3997 elsif value.is_a?(Float)
3998 @type = TYPE_REAL
3999 elsif value.is_a?(Array)
4000 @type = TYPE_NEW_ARRAY # CHECK
4001 @array = value
4002 end
4003 reset()
4004 elsif index < 0
4005 #pass
4006 else
4007 if @type == TYPE_STRING
4008 @line = ''
4009 for i in [email protected]
4010 if i == index
4011 @line = [@line, value.to_s].join('')
4012 else
4013 @line = [@line, @array[i]].join('')
4014 end
4015 if i != (@array.length - 1)
4016 @line = [@line, @separator].join('')
4017 end
4018 end
4019 if index >= @array.length
4020 for i in @array.length..index
4021 if i == index
4022 @line = [@line, @separator,
4023 value.to_s].join('')
4024 else
4025 @line = [@line, @separator,
4026 ''].join('')
4027 end
4028 end
4029 end
4030 reset()
4031 elsif @type == TYPE_ARRAY
4032 if 0 <= index and index < @array.length
4033 @array[index] = value
4034 end
4035 elsif @type == TYPE_NEW_ARRAY
4036 if 0 <= index and index < @array.length
4037 @array[index] = value
4038 elsif index > 0 ## FIXME
4039 for _ in @array.length..index-2
4040 @array << '' # XXX
4041 end
4042 @array << value
4043 ##print(@array, index, value)
4044 Logging::Logging.info('!!! WARNING !!!')
4045 Logging::Logging.info('NOT YET IMPLEMENTED')
4046 else
4047 #pass # ERROR
4048 end
4049 else
4050 #pass # ERROR
4051 end
4052 end
4053 end
4054
4055 def dump
4056 line = nil
4057 if @type == TYPE_STRING
4058 line = (@name.to_s + ', "' + @line.to_s + '", "' + @separator.to_s + '"')
4059 elsif @type == TYPE_NEW_ARRAY
4060 #pass ## FIXME
4061 elsif @type != TYPE_ARRAY
4062 line = (@name.to_s + ', ' + @line.to_s)
4063 else
4064 #pass
4065 end
4066 return line
4067 end
4068 end
4069
4070
4071 class AyaArgument
4072
4073 def initialize(line)
4074 @line = line.strip()
4075 @length = @line.length
4076 @current_position = 0
4077 end
4078
4079 def has_more_tokens
4080 return (@current_position != -1 and \
4081 @current_position < @length)
4082 end
4083
4084 def next_token
4085 return nil unless has_more_tokens()
4086 startpoint = @current_position
4087 @current_position = position_of_next_token()
4088 if @current_position == -1
4089 token = @line[startpoint..-1]
4090 else
4091 token = @line[startpoint..@current_position-2]
4092 end
4093 return token.strip()
4094 end
4095
4096 def position_of_next_token
4097 locked = true
4098 position = @current_position
4099 parenthesis_nest_level = 0
4100 while position < @length
4101 c = @line[position]
4102 case c
4103 when '"'
4104 return position unless locked
4105 while position < @length-1
4106 position += 1
4107 break if @line[position] == '"'
4108 end
4109 when '('
4110 parenthesis_nest_level += 1
4111 when ')'
4112 parenthesis_nest_level -= 1
4113 when ','
4114 locked = false if parenthesis_nest_level.zero?
4115 else
4116 return position unless locked
4117 end
4118 position += 1
4119 end
4120 return -1
4121 end
4122 end
4123
4124
4125 class AyaSaoriLibrary
4126
4127 def initialize(saori, top_dir)
4128 @saori_list = {}
4129 @saori = saori
4130 @current_charset = nil
4131 @charset = {}
4132 end
4133
4134 def set_charset(charset)
4135 fail "assert" unless ['CP932', 'Shift_JIS', 'UTF-8'].include?(charset)
4136 @current_charset = charset
4137 end
4138
4139 def load(name, top_dir)
4140 result = 0
4141 head, name = File.split(name.gsub("\\", '/')) # XXX: don't encode here
4142 top_dir = File.join(top_dir, head)
4143 unless @saori.nil? or @saori_list.include?(name)
4144 module_ = @saori.request(name)
4145 @saori_list[name] = module_ unless module_.nil?
4146 end
4147 if @saori_list.include?(name)
4148 result = @saori_list[name].load(:dir => top_dir)
4149 end
4150 @charset[name] = @current_charset
4151 return result
4152 end
4153
4154 def unload(name: nil)
4155 unless name.nil?
4156 name = File.split(name.gsub("\\", '/'))[-1] # XXX: don't encode here
4157 if @saori_list.include?(name)
4158 @saori_list[name].unload()
4159 @saori_list.delete(name)
4160 @charset.delete(name)
4161 end
4162 else
4163 for key in @saori_list.keys()
4164 @saori_list[key].unload()
4165 end
4166 end
4167 return nil
4168 end
4169
4170 def request(name, req)
4171 result = '' # FIXME
4172 name = File.split(name.gsub("\\", '/'))[-1] # XXX: don't encode here
4173 if not name.nil? and not name.empty? and @saori_list.include?(name)
4174 result = @saori_list[name].request(
4175 req.encode(@current_charset))
4176 end
4177 return result.encode(@current_charset)
4178 end
4179 end
4180 end