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