| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# |
| 3 |
# satori.rb - a "里々" compatible Shiori module for ninix |
| 4 |
# Copyright (C) 2002 by Tamito KAJIYAMA |
| 5 |
# Copyright (C) 2002, 2003 by MATSUMURA Namihiko <[email protected]> |
| 6 |
# Copyright (C) 2002-2019 by Shyouzou Sugitani <[email protected]> |
| 7 |
# Copyright (C) 2003, 2004 by Shun-ichi TAHARA <[email protected]> |
| 8 |
# Copyright (C) 2024, 2025 by Tatakinov |
| 9 |
# |
| 10 |
# This program is free software; you can redistribute it and/or modify it |
| 11 |
# under the terms of the GNU General Public License (version 2) as |
| 12 |
# published by the Free Software Foundation. It is distributed in the |
| 13 |
# hope that it will be useful, but WITHOUT ANY WARRANTY; without even the |
| 14 |
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 15 |
# PURPOSE. See the GNU General Public License for more details. |
| 16 |
# |
| 17 |
|
| 18 |
# TODO: |
| 19 |
# - φエスケープ: 特殊記号の無効化, replaceの無効化など |
| 20 |
# - イベントを単語群で定義できるように |
| 21 |
# - 文と単語群の重複回避 |
| 22 |
# - ≫ |
| 23 |
# - コミュニケート |
| 24 |
# - 内部呼び出し: 単語の追加, sync |
| 25 |
# - $トーク中のなでられ反応 |
| 26 |
# - $BalloonOffset0, $BalloonOffset1 |
| 27 |
# - マルチキャラクタ |
| 28 |
|
| 29 |
|
| 30 |
require 'nkf' |
| 31 |
require "pathname" |
| 32 |
|
| 33 |
require_relative "../home" |
| 34 |
require_relative "../logging" |
| 35 |
require_relative "../error" |
| 36 |
|
| 37 |
module Satori |
| 38 |
|
| 39 |
NODE_TEXT = 1 |
| 40 |
NODE_REF = 2 |
| 41 |
NODE_SIDE = 3 |
| 42 |
NODE_ASSIGNMENT = 4 |
| 43 |
NODE_JUMP = 5 |
| 44 |
NODE_SEARCH = 6 |
| 45 |
NODE_CHOICE = 7 |
| 46 |
NODE_CALL = 8 |
| 47 |
NODE_SAORI = 9 |
| 48 |
NODE_OR_EXPR = 20 |
| 49 |
NODE_AND_EXPR = 21 |
| 50 |
NODE_COMP_EXPR = 22 |
| 51 |
NODE_ADD_EXPR = 23 |
| 52 |
NODE_MUL_EXPR = 24 |
| 53 |
NODE_POW_EXPR = 25 |
| 54 |
NODE_UNARY_EXPR = 26 |
| 55 |
NODE_TAG_JUMP = 50 |
| 56 |
|
| 57 |
def self.encrypt(s) |
| 58 |
buf = [] |
| 59 |
t = s.length |
| 60 |
p = ((t + 1) / 2).to_i |
| 61 |
for n in 0..p-1 |
| 62 |
buf << s[n] |
| 63 |
if s[p..-1].length > n |
| 64 |
buf << s[-n - 1] |
| 65 |
end |
| 66 |
end |
| 67 |
return buf |
| 68 |
end |
| 69 |
|
| 70 |
def self.decrypt(s) |
| 71 |
buf = [] |
| 72 |
t = s.length |
| 73 |
for n in 0.step(t-1, 2) |
| 74 |
buf << s[n..-1][0..0] # XXX |
| 75 |
end |
| 76 |
if (t % 2).zero? |
| 77 |
p = 1 |
| 78 |
else |
| 79 |
p = 2 |
| 80 |
end |
| 81 |
for n in p.step(t-1, 2) |
| 82 |
buf << s[-n..-1][0..0] # XXX |
| 83 |
end |
| 84 |
return buf.join('') |
| 85 |
end |
| 86 |
|
| 87 |
def self.list_dict(top_dir) |
| 88 |
buf = [] |
| 89 |
begin |
| 90 |
dir_list = Pathname(top_dir).children().map {|x| x.relative_path_from(Pathname(top_dir)).to_path } |
| 91 |
rescue #except OSError: |
| 92 |
dir_list = [] |
| 93 |
end |
| 94 |
for filename in dir_list |
| 95 |
basename = File.basename(filename, '.*') |
| 96 |
ext = File.extname(filename) |
| 97 |
ext = ext.downcase |
| 98 |
if (filename.downcase.start_with?('dic') and \ |
| 99 |
['.txt', '.sat'].include?(ext)) or \ |
| 100 |
['replace.txt', 'replace_after.txt', |
| 101 |
'satori_conf.txt', 'satori_conf.sat'].include?(filename.downcase) # XXX |
| 102 |
buf << File.join(top_dir, filename) |
| 103 |
end |
| 104 |
end |
| 105 |
return buf |
| 106 |
end |
| 107 |
|
| 108 |
class Filter |
| 109 |
|
| 110 |
def initialize(rules) |
| 111 |
@rules = [] |
| 112 |
for pat, rep in rules |
| 113 |
@rules << [pat, rep] |
| 114 |
end |
| 115 |
end |
| 116 |
|
| 117 |
def apply(text) |
| 118 |
for pat, rep in @rules |
| 119 |
text = text.gsub(pat, rep) |
| 120 |
end |
| 121 |
return text |
| 122 |
end |
| 123 |
end |
| 124 |
|
| 125 |
### PARSER ### |
| 126 |
|
| 127 |
def self.read_tab_file(path, encrypted: false) |
| 128 |
lineno = 0 |
| 129 |
buf = [] |
| 130 |
open(path, 'rb') do |f| |
| 131 |
for line in f |
| 132 |
lineno += 1 |
| 133 |
if line.end_with?("\r\n") |
| 134 |
line = line[0..-3] |
| 135 |
elsif line.end_with?("\r") or line.end_with?("\n") |
| 136 |
line = line[0..-2] |
| 137 |
end |
| 138 |
if encrypted |
| 139 |
line = Satori.decrypt(Satori.decrypt(line)) |
| 140 |
end |
| 141 |
begin |
| 142 |
line = line.force_encoding(NKF.guess(line)).encode('utf-8', :invalid => :replace, :undef => :replace) |
| 143 |
rescue => e #except UnicodeError as e: |
| 144 |
Logging::Logging.debug('satori.py: ' + e.to_s + ' in ' + path.to_s + ' (line ' + lineno.to_s + ')') |
| 145 |
next |
| 146 |
end |
| 147 |
begin |
| 148 |
old, new = line.split("\t", 2) |
| 149 |
rescue #except ValueError: |
| 150 |
Logging::Logging.debug('satori.py: invalid line in ' + path.to_s + ' (line ' + lineno.to_s + ')') |
| 151 |
next |
| 152 |
end |
| 153 |
unless old.nil? or new.nil? |
| 154 |
buf << [old, new] |
| 155 |
end |
| 156 |
end |
| 157 |
end |
| 158 |
return buf |
| 159 |
end |
| 160 |
|
| 161 |
|
| 162 |
class Parser |
| 163 |
attr_reader :anchor_filter, :talk, :word |
| 164 |
|
| 165 |
def initialize |
| 166 |
@talk = {} |
| 167 |
@word = {} |
| 168 |
@variable = [] |
| 169 |
@parenthesis = 0 |
| 170 |
@replace_filter = Filter.new([]) |
| 171 |
@anchor_list = [] |
| 172 |
@anchor_filter = Filter.new(@anchor_list) |
| 173 |
@is_anchor = false |
| 174 |
@saori = [] |
| 175 |
@separator = ["\1", ',', ',', '、', '、'] |
| 176 |
@count = {'Talk' => 0, |
| 177 |
'NoNameTalk' => 0, |
| 178 |
'EventTalk' => 0, |
| 179 |
'OtherTalk' => 0, |
| 180 |
'Words' => 0, |
| 181 |
'Word' => 0, |
| 182 |
'Variable' => 0, |
| 183 |
'Anchor' => 0, |
| 184 |
'Parenthesis' => 0, |
| 185 |
'Parentheres' => 0, ## XXX |
| 186 |
'Line' => 0,} |
| 187 |
end |
| 188 |
|
| 189 |
def set_saori(saori_list) |
| 190 |
@saori = saori_list |
| 191 |
end |
| 192 |
|
| 193 |
def get_count(name) |
| 194 |
if @count.include?(name) |
| 195 |
return @count[name] |
| 196 |
else |
| 197 |
return 0 |
| 198 |
end |
| 199 |
end |
| 200 |
|
| 201 |
def load_replace_file(path) |
| 202 |
@replace_filter = Filter.new(Satori.read_tab_file(path)) |
| 203 |
end |
| 204 |
|
| 205 |
def get_dict |
| 206 |
return @talk, @word |
| 207 |
end |
| 208 |
|
| 209 |
def read(path) |
| 210 |
basename = File.basename(path, '.*') |
| 211 |
ext = File.extname(path) |
| 212 |
ext = ext.downcase |
| 213 |
if ext == '.sat' |
| 214 |
encrypted = true |
| 215 |
else |
| 216 |
encrypted = false |
| 217 |
end |
| 218 |
filename = File.basename(path) |
| 219 |
if filename.downcase.start_with?('dicanchor') # XXX |
| 220 |
@is_anchor = true |
| 221 |
else |
| 222 |
@is_anchor = false |
| 223 |
end |
| 224 |
open(path, 'rb') do |f| |
| 225 |
read_file(f, :path => path, :encrypted => encrypted) |
| 226 |
end |
| 227 |
if @is_anchor |
| 228 |
@anchor_filter = Filter.new(@anchor_list) |
| 229 |
end |
| 230 |
end |
| 231 |
|
| 232 |
def read_file(f, path: nil, encrypted: false) |
| 233 |
lineno = 0 |
| 234 |
linelist = [] |
| 235 |
line_buffer = nil # XXX |
| 236 |
phi_escape = {} # key = lineno: [position] |
| 237 |
parser = nil # XXX |
| 238 |
for line in f |
| 239 |
if line_buffer.nil? |
| 240 |
lineno += 1 |
| 241 |
phi_escape[lineno] = [] |
| 242 |
end |
| 243 |
if line.end_with?("\r\n") |
| 244 |
line = line[0..-3] |
| 245 |
elsif line.end_with?("\r") or line.end_with?("\n") |
| 246 |
line = line[0..-2] |
| 247 |
end |
| 248 |
if encrypted |
| 249 |
line = Satori.decrypt(Satori.decrypt(line)) |
| 250 |
end |
| 251 |
begin |
| 252 |
line = line.force_encoding(NKF.guess(line)).encode('utf-8', :invalid => :replace, :undef => :replace) |
| 253 |
rescue => e #except UnicodeError as e: |
| 254 |
if path.nil? |
| 255 |
Logging::Logging.debug('satori.py: ' + e.to_s + ' (line ' + lineno.to_s + ')') |
| 256 |
else |
| 257 |
Logging::Logging.debug('satori.py: ' + e.to_s + ' in ' + path.to_s + ' (line ' + lineno.to_s + ')') |
| 258 |
end |
| 259 |
next |
| 260 |
end |
| 261 |
unless line_buffer.nil? |
| 262 |
line = [line_buffer, line].join('') |
| 263 |
line_buffer = nil |
| 264 |
end |
| 265 |
pos = 0 |
| 266 |
while line[pos..-1].count('φ') >0 |
| 267 |
pos = line.index('φ', pos) |
| 268 |
if pos == (line.length - 1) |
| 269 |
line_buffer = line[0..-2] |
| 270 |
break |
| 271 |
else |
| 272 |
phi_escape[lineno] << pos |
| 273 |
=begin |
| 274 |
if pos.zero? |
| 275 |
line = line[1..-1] |
| 276 |
else |
| 277 |
line = [line[0..pos-1], line[pos + 1..-1]].join('') |
| 278 |
end |
| 279 |
=end |
| 280 |
end |
| 281 |
pos += 1 |
| 282 |
end |
| 283 |
next unless line_buffer.nil? |
| 284 |
pos = 0 |
| 285 |
while line[pos..-1].count('#') > 0 |
| 286 |
pos = line.index('#', pos) |
| 287 |
unless phi_escape[lineno].include?(pos - 1) |
| 288 |
if pos.zero? |
| 289 |
line = "" |
| 290 |
else |
| 291 |
line = line[0..pos-1] |
| 292 |
end |
| 293 |
break |
| 294 |
end |
| 295 |
end |
| 296 |
if line.nil? or line.empty? |
| 297 |
next |
| 298 |
end |
| 299 |
if line.start_with?('*') and not phi_escape[lineno].include?(0) |
| 300 |
unless linelist.empty? |
| 301 |
method(parser).call(linelist, phi_escape) |
| 302 |
end |
| 303 |
parser = 'parse_talk' |
| 304 |
linelist = [lineno, line] |
| 305 |
elsif line.start_with?('@') and not phi_escape[lineno].include?(0) |
| 306 |
unless linelist.empty? |
| 307 |
method(parser).call(linelist, phi_escape) |
| 308 |
end |
| 309 |
parser = 'parse_word_group' |
| 310 |
linelist = [lineno, line] |
| 311 |
elsif not linelist.empty? |
| 312 |
# apply replace.txt |
| 313 |
line = @replace_filter.apply(line) ## FIXME: phi_escape |
| 314 |
linelist << line |
| 315 |
end |
| 316 |
end |
| 317 |
# for no in phi_escape: |
| 318 |
# if phi_escape[no]: |
| 319 |
# print('PHI:', no, phi_escape[no]) |
| 320 |
# end |
| 321 |
# end |
| 322 |
unless linelist.empty? |
| 323 |
method(parser).call(linelist, phi_escape) |
| 324 |
end |
| 325 |
@count['Line'] = @count['Line'] + lineno |
| 326 |
talk = 0 |
| 327 |
eventtalk = 0 |
| 328 |
for key in @talk.keys |
| 329 |
value = @talk[key] |
| 330 |
number = value.length |
| 331 |
talk += number |
| 332 |
if key.start_with?('On') |
| 333 |
eventtalk += number |
| 334 |
end |
| 335 |
end |
| 336 |
@count['Talk'] = talk |
| 337 |
@count['EventTalk'] = eventtalk |
| 338 |
if @talk.include?('') |
| 339 |
@count['NoNameTalk'] = @talk[''].length |
| 340 |
end |
| 341 |
@count['OtherTalk'] = (@count['Talk'] \ |
| 342 |
- @count['NoNameTalk'] \ |
| 343 |
- @count['EventTalk']) |
| 344 |
@count['Words'] = @word.length |
| 345 |
word = 0 |
| 346 |
for value in @word.values() |
| 347 |
word += value.length |
| 348 |
end |
| 349 |
@count['Word'] = word |
| 350 |
@count['Anchor'] = @anchor_list.length |
| 351 |
@count['Variable'] = @variable.length |
| 352 |
@count['Parenthesis'] = @parenthesis |
| 353 |
@count['Parentheres'] = @parenthesis |
| 354 |
end |
| 355 |
|
| 356 |
def parse_talk(linelist, phi_escape) |
| 357 |
lineno = linelist[0] |
| 358 |
buf = [] |
| 359 |
line = linelist[1] |
| 360 |
fail "assert" unless line.start_with?('*') |
| 361 |
name = line[1..-1] |
| 362 |
if pos = name =~ /\t/ |
| 363 |
cond = parse_expression(name[pos + 1 ..]) |
| 364 |
name = name[0 .. pos - 1] |
| 365 |
else |
| 366 |
# trueになるものであれば何でも良い。 |
| 367 |
cond = parse_expression('1==1') |
| 368 |
end |
| 369 |
while linelist.length > 3 and not linelist[-1] |
| 370 |
linelist.delete_at(-1) |
| 371 |
end |
| 372 |
prev = '' |
| 373 |
num_open = 0 |
| 374 |
num_close = 0 |
| 375 |
for n in 2..linelist.length-1 |
| 376 |
line = linelist[n] |
| 377 |
offset = 0 |
| 378 |
while pos = (line[offset ..] =~ /(?<=^|[^φ])(?:φφ)*(/) |
| 379 |
num_open += 1 |
| 380 |
offset += pos + 1 |
| 381 |
end |
| 382 |
offset = 0 |
| 383 |
while pos = (line[offset ..] =~ /(?<=^|[^φ])(?:φφ)*(/) |
| 384 |
num_close += 1 |
| 385 |
offset += pos + 1 |
| 386 |
end |
| 387 |
if num_open > 0 and num_open != num_close |
| 388 |
if n == (linelist.length - 1) |
| 389 |
Logging::Logging.debug( |
| 390 |
'satori.py: syntax error (unbalanced parens)') |
| 391 |
else |
| 392 |
prev = [prev, linelist[n]].join('') |
| 393 |
next |
| 394 |
end |
| 395 |
else |
| 396 |
num_open = 0 |
| 397 |
num_close = 0 |
| 398 |
end |
| 399 |
line = [prev, linelist[n]].join('') |
| 400 |
prev = '' |
| 401 |
current_lineno = (lineno + n - 2) |
| 402 |
if not line.empty? and line[0] == '$' and not phi_escape[current_lineno].include?(0) |
| 403 |
node = parse_assignment(line) |
| 404 |
unless node.nil? |
| 405 |
buf << node |
| 406 |
end |
| 407 |
elsif not line.empty? and line[0] == '>' and not phi_escape[current_lineno].include?(0) |
| 408 |
node = parse_jump(line) |
| 409 |
unless node.nil? |
| 410 |
buf << node |
| 411 |
end |
| 412 |
elsif not line.empty? and line[0] == '≧' and not phi_escape[current_lineno].include?(0) |
| 413 |
node = parse_tag_jump(line) |
| 414 |
unless node.nil? |
| 415 |
buf << node |
| 416 |
end |
| 417 |
elsif not line.empty? and line[0] == '≫' and not phi_escape[current_lineno].include?(0) |
| 418 |
node = parse_search(line) |
| 419 |
unless node.nil? |
| 420 |
buf << node |
| 421 |
end |
| 422 |
elsif not line.empty? and line[0] == '_' and not phi_escape[current_lineno].include?(0) |
| 423 |
node = parse_choice(line) |
| 424 |
unless node.nil? |
| 425 |
buf << node |
| 426 |
end |
| 427 |
else |
| 428 |
nodelist = parse_talk_word(line) |
| 429 |
unless nodelist.nil? |
| 430 |
buf.concat(nodelist) |
| 431 |
end |
| 432 |
end |
| 433 |
end |
| 434 |
unless buf.empty? |
| 435 |
unless @talk.include?(name) |
| 436 |
@talk[name] = [] |
| 437 |
end |
| 438 |
@talk[name] << [buf, cond] |
| 439 |
if @is_anchor |
| 440 |
@anchor_list << [name, "\\_a[" + name.to_s + ']' + name.to_s + "\\_a"] |
| 441 |
end |
| 442 |
end |
| 443 |
end |
| 444 |
|
| 445 |
def parse_word_group(linelist, phi_escape) |
| 446 |
lineno = linelist[0] |
| 447 |
buf = [] |
| 448 |
line = linelist[1] |
| 449 |
fail "assert" unless line.start_with?('@') |
| 450 |
name = line[1..-1] |
| 451 |
if pos = name =~ /\t/ |
| 452 |
cond = parse_expression(name[pos + 1 ..]) |
| 453 |
name = name[0 .. pos - 1] |
| 454 |
else |
| 455 |
# trueになるものであれば何でも良い。 |
| 456 |
cond = parse_expression('1==1') |
| 457 |
end |
| 458 |
prev = '' |
| 459 |
num_open = 0 |
| 460 |
num_close = 0 |
| 461 |
for n in 2..linelist.length-1 |
| 462 |
line = linelist[n] |
| 463 |
offset = 0 |
| 464 |
while pos = (line[offset ..] =~ /(?<=^|[^φ])(?:φφ)*(/) |
| 465 |
num_open += 1 |
| 466 |
offset += pos + 1 |
| 467 |
end |
| 468 |
offset = 0 |
| 469 |
while pos = (line[offset ..] =~ /(?<=^|[^φ])(?:φφ)*(/) |
| 470 |
num_close += 1 |
| 471 |
offset += pos + 1 |
| 472 |
end |
| 473 |
if num_open > 0 and num_open != num_close |
| 474 |
if n == (linelist.length - 1) |
| 475 |
Logging::Logging.debug( |
| 476 |
'satori.py: syntax error (unbalanced parens)') |
| 477 |
else |
| 478 |
prev = [prev, linelist[n]].join('') |
| 479 |
next |
| 480 |
end |
| 481 |
else |
| 482 |
num_open = 0 |
| 483 |
num_close = 0 |
| 484 |
end |
| 485 |
line = [prev, linelist[n]].join('') |
| 486 |
prev = '' |
| 487 |
next if line.empty? |
| 488 |
word = parse_word(line) |
| 489 |
unless word.nil? |
| 490 |
buf << word |
| 491 |
end |
| 492 |
end |
| 493 |
unless buf.empty? |
| 494 |
unless @word.include?(name) |
| 495 |
@word[name] = [] |
| 496 |
end |
| 497 |
@word[name] << [buf, cond] |
| 498 |
end |
| 499 |
end |
| 500 |
|
| 501 |
def parse_assignment(line) |
| 502 |
fail "assert" unless line[0] == '$' |
| 503 |
n = line =~ /(?<=^|[^φ])(?:φφ)*[\t =]/ |
| 504 |
if n.nil? |
| 505 |
break_flag = false |
| 506 |
else |
| 507 |
break_flag = true |
| 508 |
end |
| 509 |
unless break_flag |
| 510 |
Logging::Logging.debug('satori.py: syntax error (expected a tab or equal)') |
| 511 |
return nil |
| 512 |
end |
| 513 |
name_str = line[1..n-1] #.join('') # XXX |
| 514 |
name = parse_word(line[1..n-1]) |
| 515 |
if line[n] == '=' |
| 516 |
n += 1 |
| 517 |
value = parse_expression(line[n..-1]) |
| 518 |
else |
| 519 |
sep = line[n] |
| 520 |
while n < line.length and line[n] == sep |
| 521 |
n += 1 |
| 522 |
end |
| 523 |
value = parse_word(line[n..-1]) |
| 524 |
end |
| 525 |
if name_str == '引数区切り削除' |
| 526 |
sep = line[n..-1] #.join('') # XXX |
| 527 |
if @separator.include?(sep) |
| 528 |
@separator.delete(sep) |
| 529 |
end |
| 530 |
return nil |
| 531 |
elsif name_str == '引数区切り追加' |
| 532 |
sep = line[n..-1] #.join('') # XXX |
| 533 |
unless @separator.include?(sep) |
| 534 |
@separator << sep |
| 535 |
end |
| 536 |
return nil |
| 537 |
end |
| 538 |
unless @variable.include?(name) |
| 539 |
@variable << name |
| 540 |
end |
| 541 |
return [NODE_ASSIGNMENT, name, value] |
| 542 |
end |
| 543 |
|
| 544 |
def parse_jump(line) |
| 545 |
fail "assert" unless line[0] == '>' |
| 546 |
n = line =~ /(?<=^|[^φ])(?:φφ)*\t/ |
| 547 |
if n.nil? |
| 548 |
break_flag = false |
| 549 |
else |
| 550 |
break_flag = true |
| 551 |
end |
| 552 |
unless break_flag |
| 553 |
n = line.length |
| 554 |
end |
| 555 |
target = parse_word(line[1..n-1]) |
| 556 |
while n < line.length and line[n] == "\t" |
| 557 |
n += 1 |
| 558 |
end |
| 559 |
if n < line.length |
| 560 |
condition = parse_expression(line[n..-1]) |
| 561 |
else |
| 562 |
condition = nil |
| 563 |
end |
| 564 |
return [NODE_JUMP, target, condition] |
| 565 |
end |
| 566 |
|
| 567 |
def parse_tag_jump(line) |
| 568 |
fail "assert" unless line[0] == '≧' |
| 569 |
line = line[1 ..] |
| 570 |
a = line.split(' ').map do |x| |
| 571 |
parse_word(x) |
| 572 |
end |
| 573 |
return [NODE_TAG_JUMP, a.shift, a] |
| 574 |
end |
| 575 |
|
| 576 |
def parse_search(line) |
| 577 |
return [NODE_SEARCH] |
| 578 |
end |
| 579 |
|
| 580 |
def parse_choice(line) |
| 581 |
fail "assert" unless line[0] == '_' |
| 582 |
n = line =~ /(?<=^|[^φ])(?:φφ)*\t/ |
| 583 |
if n.nil? |
| 584 |
break_flag = false |
| 585 |
else |
| 586 |
break_flag = true |
| 587 |
end |
| 588 |
unless break_flag |
| 589 |
n = line.length |
| 590 |
end |
| 591 |
label = parse_word(line[1..n-1]) |
| 592 |
while n < line.length and line[n] == "\t" |
| 593 |
n += 1 |
| 594 |
end |
| 595 |
if n < line.length |
| 596 |
id_ = parse_word(line[n..-1]) |
| 597 |
else |
| 598 |
id_ = nil |
| 599 |
end |
| 600 |
return [NODE_CHOICE, label, id_] |
| 601 |
end |
| 602 |
|
| 603 |
def parse_talk_word(line) |
| 604 |
buf = parse_word(line) |
| 605 |
buf << [NODE_TEXT, ["\\n"]] |
| 606 |
return buf |
| 607 |
end |
| 608 |
|
| 609 |
def parse_word(line) |
| 610 |
buffer_ = [] |
| 611 |
text = [] |
| 612 |
while not line.nil? and not line.empty? |
| 613 |
if line[0] == ':' |
| 614 |
unless text.empty? |
| 615 |
buffer_ << [NODE_TEXT, text] |
| 616 |
text = [] |
| 617 |
end |
| 618 |
buffer_ << [NODE_SIDE, [line[0]]] |
| 619 |
line = line[1..-1] |
| 620 |
elsif line[0] == '(' |
| 621 |
@parenthesis += 1 |
| 622 |
unless text.empty? |
| 623 |
buffer_ << [NODE_TEXT, text] |
| 624 |
text = [] |
| 625 |
end |
| 626 |
line = parse_parenthesis(line, buffer_) |
| 627 |
else |
| 628 |
text << line[0] |
| 629 |
line = line[1..-1] |
| 630 |
end |
| 631 |
end |
| 632 |
unless text.empty? |
| 633 |
buffer_ << [NODE_TEXT, text] |
| 634 |
end |
| 635 |
return buffer_ |
| 636 |
end |
| 637 |
|
| 638 |
def find_close(text, position) |
| 639 |
nest = 0 |
| 640 |
current = position |
| 641 |
while text[current..-1].count(')') > 0 |
| 642 |
pos_new = text.index(')', current) |
| 643 |
break if pos_new.zero? |
| 644 |
nest = text[position..pos_new-1].count('(') - text[position..pos_new-1].count(')') |
| 645 |
if nest > 0 |
| 646 |
current = (pos_new + 1) |
| 647 |
else |
| 648 |
current = pos_new |
| 649 |
break |
| 650 |
end |
| 651 |
end |
| 652 |
return current |
| 653 |
end |
| 654 |
|
| 655 |
def split_(text, sep) |
| 656 |
buf = [] |
| 657 |
pos_end = -1 |
| 658 |
while true |
| 659 |
position = text.index('(', pos_end + 1) |
| 660 |
if position.nil? or position == -1 |
| 661 |
ll = text[pos_end + 1..-1].split(sep, -1) |
| 662 |
if buf.length > 0 |
| 663 |
last = buf.pop() |
| 664 |
buf << [last, ll[0]].join('') |
| 665 |
unless ll[1..-1].nil? |
| 666 |
buf.concat(ll[1..-1]) |
| 667 |
end |
| 668 |
else |
| 669 |
buf.concat(ll) |
| 670 |
end |
| 671 |
break |
| 672 |
else |
| 673 |
if position.zero? |
| 674 |
ll = [""] |
| 675 |
else |
| 676 |
ll = text[pos_end + 1..position-1].split(sep, -1) |
| 677 |
end |
| 678 |
pos_end = find_close(text, position + 1) |
| 679 |
last = ll.pop() |
| 680 |
ll << [last, text[position..pos_end]].join('') |
| 681 |
if buf.length > 0 |
| 682 |
last = buf.pop() |
| 683 |
buf << [last, ll[0]].join('') |
| 684 |
unless ll[1..-1].nil? |
| 685 |
buf.concat(ll[1..-1]) |
| 686 |
end |
| 687 |
else |
| 688 |
buf.concat(ll) |
| 689 |
end |
| 690 |
end |
| 691 |
end |
| 692 |
buf = buf.map {|x| x.strip() } |
| 693 |
return buf |
| 694 |
end |
| 695 |
|
| 696 |
def parse_parenthesis(line, buf) |
| 697 |
text_ = [] |
| 698 |
fail "assert" unless line[0] == '(' |
| 699 |
line = line[1..-1] ## FIXME |
| 700 |
depth = 1 |
| 701 |
count = 1 |
| 702 |
phi = false |
| 703 |
while not line.nil? and not line.empty? |
| 704 |
if line[0] == ')' and not phi |
| 705 |
depth -= 1 |
| 706 |
if text_.length != count # () |
| 707 |
text_ << '' |
| 708 |
end |
| 709 |
if depth.zero? |
| 710 |
line = line[1..-1] |
| 711 |
break |
| 712 |
end |
| 713 |
text_ << line[0] |
| 714 |
line = line[1..-1] |
| 715 |
elsif line[0] == '(' and not phi |
| 716 |
depth += 1 |
| 717 |
count += 1 |
| 718 |
text_ << line[0] |
| 719 |
line = line[1..-1] |
| 720 |
elsif line[0] == 'φ' |
| 721 |
phi = true |
| 722 |
line = line[1..-1] |
| 723 |
else |
| 724 |
phi = false |
| 725 |
text_ << line[0] |
| 726 |
line = line[1..-1] |
| 727 |
end |
| 728 |
end |
| 729 |
unless text_.empty? |
| 730 |
sep = nil |
| 731 |
s = (text_.is_a?(Array)) ? (text_.join) : (text_) |
| 732 |
pos = s =~ /(?<=^|[^φ])(?:φφ)*(?:#{@separator.join('|')})/ |
| 733 |
sep = text_[pos] unless pos.nil? |
| 734 |
unless sep.nil? |
| 735 |
list_ = split_(text_.join(''), sep) ## FIXME |
| 736 |
else |
| 737 |
list_ = [text_.join('')] |
| 738 |
end |
| 739 |
if ['単語の追加', 'sync', 'loop', 'call', 'バイト値', |
| 740 |
'set', 'remember', 'nop', '合成単語群', |
| 741 |
'when', 'times', 'while', 'for'].include?(list_[0]) or list_[0].end_with?('の数') |
| 742 |
function = list_[0] |
| 743 |
args = [] |
| 744 |
if list_.length > 1 |
| 745 |
for i in 1..list_.length-1 |
| 746 |
args << parse_expression(list_[i]) |
| 747 |
end |
| 748 |
end |
| 749 |
buf << [NODE_CALL, function, args] |
| 750 |
return line |
| 751 |
elsif Ssu::Saori::FUNC_NAME.include?(list_[0]) |
| 752 |
buf << [NODE_SAORI, list_[0], list_[1 ..].map do |x| |
| 753 |
parse_word(x) |
| 754 |
end] |
| 755 |
elsif @saori.include?(list_[0]) |
| 756 |
function = list_[0] |
| 757 |
args = [] |
| 758 |
if list_.length > 1 |
| 759 |
for i in 1..list_.length |
| 760 |
# XXX: parse as text |
| 761 |
args << parse_word(list_[i]) |
| 762 |
end |
| 763 |
end |
| 764 |
buf << [NODE_SAORI, function, args] |
| 765 |
return line |
| 766 |
else |
| 767 |
if list_.length > 1 # XXX |
| 768 |
nodelist = [[NODE_TEXT, ['(']]] |
| 769 |
nodelist.concat(parse_word(text_)) |
| 770 |
nodelist << [NODE_TEXT, [')']] |
| 771 |
buf << [NODE_REF, nodelist] |
| 772 |
return line |
| 773 |
else |
| 774 |
nodelist = [[NODE_TEXT, ['(']]] |
| 775 |
nodelist.concat(parse_word(text_)) |
| 776 |
nodelist << [NODE_TEXT, [')']] |
| 777 |
buf << [NODE_REF, nodelist] |
| 778 |
return line |
| 779 |
end |
| 780 |
end |
| 781 |
end |
| 782 |
#buf << [NODE_TEXT, ['']] # XXX |
| 783 |
return line |
| 784 |
end |
| 785 |
|
| 786 |
def parse_expression(line) |
| 787 |
default = [[NODE_TEXT, line[0..-1]]] |
| 788 |
begin |
| 789 |
line, buf = get_or_expr(line) |
| 790 |
rescue ValueError => err |
| 791 |
Logging::Logging.debug(err.message) |
| 792 |
return default |
| 793 |
rescue |
| 794 |
return default |
| 795 |
end |
| 796 |
return default unless line.nil? or line.empty? |
| 797 |
return buf |
| 798 |
end |
| 799 |
|
| 800 |
def get_or_expr(line) |
| 801 |
line, and_expr = get_and_expr(line) |
| 802 |
buf = [NODE_OR_EXPR, and_expr] |
| 803 |
while not line.nil? and not line.empty? and ['|', '|'].include?(line[0]) |
| 804 |
line = line[1..-1] |
| 805 |
if not line.nil? and not line.empty? and ['|', '|'].include?(line[0]) |
| 806 |
line = line[1..-1] |
| 807 |
else |
| 808 |
fail ValueError, 'broken OR operator: ' + line |
| 809 |
end |
| 810 |
line, and_expr = get_and_expr(line) |
| 811 |
buf << and_expr |
| 812 |
end |
| 813 |
if buf.length == 2 |
| 814 |
return line, buf[1] |
| 815 |
end |
| 816 |
return line, [buf] |
| 817 |
end |
| 818 |
|
| 819 |
def get_and_expr(line) |
| 820 |
line, comp_expr = get_comp_expr(line) |
| 821 |
buf = [NODE_AND_EXPR, comp_expr] |
| 822 |
while not line.nil? and not line.empty? and ['&', '&'].include?(line[0]) |
| 823 |
line = line[1..-1] |
| 824 |
if not line.nil? and not line.empty? and ['&', '&'].include?(line[0]) |
| 825 |
line = line[1..-1] |
| 826 |
else |
| 827 |
fail ValueError, 'broken AND operator: ' + line |
| 828 |
end |
| 829 |
line, comp_expr = get_comp_expr(line) |
| 830 |
buf << comp_expr |
| 831 |
end |
| 832 |
if buf.length == 2 |
| 833 |
return line, buf[1] |
| 834 |
end |
| 835 |
return line, [buf] |
| 836 |
end |
| 837 |
|
| 838 |
def get_comp_expr(line) |
| 839 |
line, buf = get_add_expr(line) |
| 840 |
if not line.nil? and not line.empty? and ['<', '<'].include?(line[0]) |
| 841 |
line = line[1..-1] |
| 842 |
op = '<' |
| 843 |
if not line.nil? and not line.empty? and ['=', '='].include?(line[0]) |
| 844 |
line = line[1..-1] |
| 845 |
op = '<=' |
| 846 |
end |
| 847 |
line, add_expr = get_add_expr(line) |
| 848 |
return line, [[NODE_COMP_EXPR, buf, op, add_expr]] |
| 849 |
elsif not line.nil? and not line.empty? and ['>', '>'].include?(line[0]) |
| 850 |
line = line[1..-1] |
| 851 |
op = '>' |
| 852 |
if not line.nil? and not line.empty? and ['=', '='].include?(line[0]) |
| 853 |
line = line[1..-1] |
| 854 |
op = '>=' |
| 855 |
end |
| 856 |
line, add_expr = get_add_expr(line) |
| 857 |
return line, [[NODE_COMP_EXPR, buf, op, add_expr]] |
| 858 |
elsif not line.nil? and not line.empty? and ['=', '='].include?(line[0]) |
| 859 |
line = line[1..-1] |
| 860 |
if not line.nil? and not line.empty? and ['=', '='].include?(line[0]) |
| 861 |
line = line[1..-1] |
| 862 |
else |
| 863 |
fail ValueError, 'broken EQUAL operator: ' + line |
| 864 |
end |
| 865 |
line, add_expr = get_add_expr(line) |
| 866 |
return line, [[NODE_COMP_EXPR, buf, '==', add_expr]] |
| 867 |
elsif not line.nil? and not line.empty? and ['!', '!'].include?(line[0]) |
| 868 |
line = line[1..-1] |
| 869 |
if not line.nil? and not line.empty? and ['=', '='].include?(line[0]) |
| 870 |
line = line[1..-1] |
| 871 |
else |
| 872 |
fail ValueError, 'broken NOT EQUAL operator: ' + line |
| 873 |
end |
| 874 |
line, add_expr = get_add_expr(line) |
| 875 |
return line, [[NODE_COMP_EXPR, buf, '!=', add_expr]] |
| 876 |
end |
| 877 |
return line, buf |
| 878 |
end |
| 879 |
|
| 880 |
def get_add_expr(line) |
| 881 |
line, mul_expr = get_mul_expr(line) |
| 882 |
buf = [NODE_ADD_EXPR, mul_expr] |
| 883 |
while not line.nil? and not line.empty? and ['+', '+', '-', '−', '-'].include?(line[0]) |
| 884 |
if ['+', '+'].include?(line[0]) |
| 885 |
buf << '+' |
| 886 |
else |
| 887 |
buf << '-' |
| 888 |
end |
| 889 |
line = line[1..-1] |
| 890 |
line, mul_expr = get_mul_expr(line) |
| 891 |
buf << mul_expr |
| 892 |
end |
| 893 |
if buf.length == 2 |
| 894 |
return line, buf[1] |
| 895 |
end |
| 896 |
return line, [buf] |
| 897 |
end |
| 898 |
|
| 899 |
def get_mul_expr(line) |
| 900 |
line, pow_expr = get_pow_expr(line) |
| 901 |
buf = [NODE_MUL_EXPR, pow_expr] |
| 902 |
while not line.nil? and not line.empty? and \ |
| 903 |
['*', '*', '×', '/', '/', '÷', '%', '%'].include?(line[0]) |
| 904 |
if ['*', '*', '×'].include?(line[0]) |
| 905 |
buf << '*' |
| 906 |
elsif ['/', '/', '÷'].include?(line[0]) |
| 907 |
buf << '/' |
| 908 |
else |
| 909 |
buf << '%' |
| 910 |
end |
| 911 |
line = line[1..-1] |
| 912 |
line, pow_expr = get_pow_expr(line) |
| 913 |
buf << pow_expr |
| 914 |
end |
| 915 |
if buf.length == 2 |
| 916 |
return line, buf[1] |
| 917 |
end |
| 918 |
return line, [buf] |
| 919 |
end |
| 920 |
|
| 921 |
def get_pow_expr(line) |
| 922 |
line, unary_expr = get_unary_expr(line) |
| 923 |
buf = [NODE_POW_EXPR, unary_expr] |
| 924 |
while not line.nil? and not line.empty? and ['^', '^'].include?(line[0]) |
| 925 |
line = line[1..-1] |
| 926 |
line, unary_expr = get_unary_expr(line) |
| 927 |
buf << unary_expr |
| 928 |
end |
| 929 |
if buf.length == 2 |
| 930 |
return line, buf[1] |
| 931 |
end |
| 932 |
return line, [buf] |
| 933 |
end |
| 934 |
|
| 935 |
def get_unary_expr(line) |
| 936 |
if not line.nil? and not line.empty? and ['-', '−', '-'].include?(line[0]) |
| 937 |
line = line[1..-1] |
| 938 |
line, unary_expr = get_unary_expr(line) |
| 939 |
return line, [[NODE_UNARY_EXPR, '-', unary_expr]] |
| 940 |
end |
| 941 |
if not line.nil? and not line.empty? and ['!', '!'].include?(line[0]) |
| 942 |
line = line[1..-1] |
| 943 |
line, unary_expr = get_unary_expr(line) |
| 944 |
return line, [[NODE_UNARY_EXPR, '!', unary_expr]] |
| 945 |
end |
| 946 |
if not line.nil? and not line.empty? and line[0] == '(' |
| 947 |
line = line[1..-1] |
| 948 |
line, buf = get_or_expr(line) |
| 949 |
if not line.nil? and not line.empty? and line[0] == ')' |
| 950 |
line = line[1..-1] |
| 951 |
else |
| 952 |
fail ValueError, 'expected a close paren: ' + line |
| 953 |
end |
| 954 |
return line, buf |
| 955 |
end |
| 956 |
return get_factor(line) |
| 957 |
end |
| 958 |
|
| 959 |
Operators = [ |
| 960 |
'|', '|', '&', '&', '<', '<', '>', '>', '=', '=', '!', '!', |
| 961 |
'+', '+', '-', '−', '-', '*', '*', '×', '/', '/', '÷', '%', '%', |
| 962 |
'^', '^', '(', ')'] |
| 963 |
|
| 964 |
def get_factor(line) |
| 965 |
buf = [] |
| 966 |
while not line.nil? and not line.empty? and not Operators.include?(line[0]) |
| 967 |
if not line.nil? and not line.empty? and line[0] == '(' |
| 968 |
line = parse_parenthesis(line, buf) |
| 969 |
next |
| 970 |
end |
| 971 |
text = [] |
| 972 |
while not line.nil? and not line.empty? and not Operators.include?(line[0]) and line[0] != '(' |
| 973 |
text << line[0] |
| 974 |
line = line[1..-1] |
| 975 |
end |
| 976 |
unless text.empty? |
| 977 |
buf << [NODE_TEXT, text] |
| 978 |
end |
| 979 |
end |
| 980 |
if buf.empty? |
| 981 |
fail ValueError, 'expected a constant: ' + line |
| 982 |
end |
| 983 |
return line, buf |
| 984 |
end |
| 985 |
|
| 986 |
def print_nodelist(node_list, depth: 0) |
| 987 |
for node in node_list |
| 988 |
indent = (' ' * depth) |
| 989 |
case node[0] |
| 990 |
when NODE_TEXT |
| 991 |
temp = node[1].map {|x| x.encode('utf-8', :invalid => :replace, :undef => :replace) }.join('') |
| 992 |
print([indent, 'NODE_TEXT "' + temp + '"'].join(''), "\n") |
| 993 |
when NODE_REF |
| 994 |
print([indent, 'NODE_REF'].join(''), "\n") |
| 995 |
print_nodelist(node[1], :depth => depth + 1) |
| 996 |
when NODE_CALL |
| 997 |
print([indent, 'NODE_CALL'].join(''), "\n") |
| 998 |
print([indent, node[1]].join, "\n") |
| 999 |
for i in 0..node[2].length-1 |
| 1000 |
print_nodelist(node[2][i], :depth => depth + 1) |
| 1001 |
end |
| 1002 |
when NODE_SAORI |
| 1003 |
print([indent, 'NODE_SAORI'].join(''), "\n") |
| 1004 |
for i in 0..node[2].length-1 |
| 1005 |
print_nodelist(node[2][i], :depth => depth + 1) |
| 1006 |
end |
| 1007 |
when NODE_SIDE |
| 1008 |
print([indent, 'NODE_SIDE'].join(''), "\n") |
| 1009 |
when NODE_ASSIGNMENT |
| 1010 |
print([indent, 'NODE_ASSIGNMENT'].join(''), "\n") |
| 1011 |
print([indent, 'variable'].join(''), "\n") |
| 1012 |
print_nodelist(node[1], :depth => depth + 1) |
| 1013 |
print([indent, 'value'].join(''), "\n") |
| 1014 |
print_nodelist(node[2], :depth => depth + 1) |
| 1015 |
when NODE_JUMP |
| 1016 |
print([indent, 'NODE_JUMP'].join(''), "\n") |
| 1017 |
print([indent, 'name'].join(''), "\n") |
| 1018 |
print_nodelist(node[1], :depth => depth + 1) |
| 1019 |
unless node[2].nil? |
| 1020 |
print([indent, 'condition'].join(''), "\n") |
| 1021 |
print_nodelist(node[2], :depth => depth + 1) |
| 1022 |
end |
| 1023 |
when NODE_TAG_JUMP |
| 1024 |
print([indent, 'NODE_TAG_JUMP'].join(''), "\n") |
| 1025 |
print([indent, 'name'].join(''), "\n") |
| 1026 |
print_nodelist(node[1], :depth => depth + 1) |
| 1027 |
unless node[2].nil? |
| 1028 |
print([indent, 'tags'].join(''), "\n") |
| 1029 |
node[2].each do |x| |
| 1030 |
print_nodelist(x, :depth => depth + 1) |
| 1031 |
end |
| 1032 |
end |
| 1033 |
when NODE_SEARCH |
| 1034 |
print([indent, 'NODE_SEARCH'].join(''), "\n") |
| 1035 |
when NODE_CHOICE |
| 1036 |
print([indent, 'NODE_CHOICE'].join(''), "\n") |
| 1037 |
print([indent, 'label'].join(''), "\n") |
| 1038 |
print_nodelist(node[1], :depth => depth + 1) |
| 1039 |
unless node[2].nil? |
| 1040 |
print([indent, 'id'].join(''), "\n") |
| 1041 |
print_nodelist(node[2], :depth => depth + 1) |
| 1042 |
end |
| 1043 |
when NODE_OR_EXPR |
| 1044 |
print([indent, 'NODE_OR_EXPR'].join(''), "\n") |
| 1045 |
print_nodelist(node[1], :depth => depth + 1) |
| 1046 |
for i in 2..node.length-1 |
| 1047 |
print([indent, 'op ||'].join(''), "\n") |
| 1048 |
print_nodelist(node[i], :depth => depth + 1) |
| 1049 |
end |
| 1050 |
when NODE_AND_EXPR |
| 1051 |
print([indent, 'NODE_ADD_EXPR'].join(''), "\n") |
| 1052 |
print_nodelist(node[1], :depth => depth + 1) |
| 1053 |
for i in 2..node.length-1 |
| 1054 |
print([indent, 'op &&'].join(''), "\n") |
| 1055 |
print_nodelist(node[i], :depth => depth + 1) |
| 1056 |
end |
| 1057 |
when NODE_COMP_EXPR |
| 1058 |
print([indent, 'NODE_COMP_EXPR'].join(''), "\n") |
| 1059 |
print_nodelist(node[1], :depth => depth + 1) |
| 1060 |
print([indent, 'op'].join(''), node[2], "\n") |
| 1061 |
print_nodelist(node[3], :depth => depth + 1) |
| 1062 |
when NODE_ADD_EXPR |
| 1063 |
print([indent, 'NODE_ADD_EXPR'].join(''), "\n") |
| 1064 |
print_nodelist(node[1], :depth => depth + 1) |
| 1065 |
for i in 2.step(node.length-1, 2) |
| 1066 |
print([indent, 'op'].join(''), node[i], "\n") |
| 1067 |
print_nodelist(node[i + 1], :depth => depth + 1) |
| 1068 |
end |
| 1069 |
when NODE_MUL_EXPR |
| 1070 |
print([indent, 'NODE_MUL_EXPR'].join(''), "\n") |
| 1071 |
print_nodelist(node[1], :depth => depth + 1) |
| 1072 |
for i in 2.step(node.length-1, 2) |
| 1073 |
print([indent, 'op'].join(''), node[i], "\n") |
| 1074 |
print_nodelist(node[i + 1], :depth => depth + 1) |
| 1075 |
end |
| 1076 |
when NODE_POW_EXPR |
| 1077 |
print([indent, 'NODE_POW_EXPR'].join(''), "\n") |
| 1078 |
print_nodelist(node[1], :depth => depth + 1) |
| 1079 |
for i in 2..node.length-1 |
| 1080 |
print([indent, 'op ^'].join(''), "\n") |
| 1081 |
print_nodelist(node[i], :depth => depth + 1) |
| 1082 |
end |
| 1083 |
when NODE_UNARY_EXPR |
| 1084 |
print([indent, 'NODE_UNARY_EXPR'].join(''), "\n") |
| 1085 |
print([indent, 'op'].join(''), node[1], "\n") |
| 1086 |
print_nodelist(node[2], :depth => depth + 1) |
| 1087 |
else |
| 1088 |
fail RuntimeError, 'should not reach here' |
| 1089 |
end |
| 1090 |
end |
| 1091 |
end |
| 1092 |
end |
| 1093 |
|
| 1094 |
# expression := or_expr |
| 1095 |
# or_expr := and_expr ( op_op and_expr )* |
| 1096 |
# or_op := "||" |
| 1097 |
# and_expr := comp_expr ( and_op comp_expr )* |
| 1098 |
# and_op := "&&" |
| 1099 |
# comp_expr := add_expr ( comp_op add_expr )? |
| 1100 |
# comp_op := "<" | ">" | "<=" | ">=" | "==" | "!=" |
| 1101 |
# add_expr := mul_expr ( add_op mul_expr )* |
| 1102 |
# add_op := "+" | "−" |
| 1103 |
# mul_expr := pow_expr ( mul_op pow_expr )* |
| 1104 |
# mul_op := "×" | "÷" | "*" | "/" | "%" |
| 1105 |
# pow_expr := unary_expr ( pow_op unary_expr )* |
| 1106 |
# pow_op := "^" |
| 1107 |
# unary_expr := unary_op unary_expr | "(" or_expr ")" | factor |
| 1108 |
# unary_op := "−" | "!" |
| 1109 |
# factor := ( constant | reference )* |
| 1110 |
|
| 1111 |
|
| 1112 |
### INTERPRETER ### |
| 1113 |
|
| 1114 |
class SATORI |
| 1115 |
|
| 1116 |
DBNAME = 'satori_savedata.txt' |
| 1117 |
EDBNAME = 'satori_savedata.sat' |
| 1118 |
|
| 1119 |
def initialize(satori_dir: nil) |
| 1120 |
satori_init(:satori_dir => satori_dir) |
| 1121 |
end |
| 1122 |
|
| 1123 |
def satori_init(satori_dir: nil) |
| 1124 |
@satori_dir = satori_dir |
| 1125 |
@dbpath = File.join(satori_dir, DBNAME) |
| 1126 |
@saori_function = {} |
| 1127 |
@parser = Parser.new() |
| 1128 |
reset() |
| 1129 |
end |
| 1130 |
|
| 1131 |
def reset |
| 1132 |
@word = {} |
| 1133 |
@talk = {} |
| 1134 |
@variable = {} |
| 1135 |
@replace_filter = Filter.new([]) |
| 1136 |
@reset_surface = true |
| 1137 |
@mouse_move_count = {} |
| 1138 |
@mouse_wheel_count = {} |
| 1139 |
@touch_threshold = 60 |
| 1140 |
@touch_timeout = 2 |
| 1141 |
@current_surface = [0, 10] |
| 1142 |
@default_surface = [0, 10] |
| 1143 |
@add_to_surface = [0, 0] |
| 1144 |
@newline = "\\n[half]" |
| 1145 |
@newline_script = '' |
| 1146 |
@save_interval = 0 |
| 1147 |
@save_timer = 0 |
| 1148 |
@url_list = {} |
| 1149 |
@boot_script = nil |
| 1150 |
@script_history = ([nil] * 64) |
| 1151 |
@wait_percent = 100 |
| 1152 |
@random_talk = -1 |
| 1153 |
@reserved_talk = {} |
| 1154 |
@silent_time = 0 |
| 1155 |
@choice_id = nil |
| 1156 |
@choice_label = nil |
| 1157 |
@choice_number = nil |
| 1158 |
@timer = {} |
| 1159 |
@time_start = nil |
| 1160 |
@runtime = 0 # accumulative |
| 1161 |
@runcount = 1 # accumulative |
| 1162 |
@folder_change = false |
| 1163 |
@saori_value = {} |
| 1164 |
end |
| 1165 |
|
| 1166 |
def load |
| 1167 |
buf = [] |
| 1168 |
for path in Satori.list_dict(@satori_dir) |
| 1169 |
filename = File.basename(path) |
| 1170 |
case filename |
| 1171 |
when 'replace.txt' |
| 1172 |
@parser.load_replace_file(path) |
| 1173 |
when 'replace_after.txt' |
| 1174 |
load_replace_file(path) |
| 1175 |
when 'satori_conf.txt', 'satori_conf.sat' |
| 1176 |
load_config_file(path) |
| 1177 |
else |
| 1178 |
buf << path |
| 1179 |
end |
| 1180 |
end |
| 1181 |
for path in buf |
| 1182 |
@parser.read(path) |
| 1183 |
end |
| 1184 |
@talk, @word = @parser.get_dict() |
| 1185 |
load_database() |
| 1186 |
@time_start = Time.new |
| 1187 |
get_event_response('OnSatoriLoad') |
| 1188 |
@boot_script = get_event_response('OnSatoriBoot') |
| 1189 |
end |
| 1190 |
|
| 1191 |
def load_config_file(path) |
| 1192 |
parser = Parser.new() |
| 1193 |
parser.read(path) |
| 1194 |
talk, word = parser.get_dict() |
| 1195 |
for nodelist in (talk.include?('初期化') ? talk['初期化'] : []) |
| 1196 |
expand(nodelist) |
| 1197 |
end |
| 1198 |
end |
| 1199 |
|
| 1200 |
def load_replace_file(path) |
| 1201 |
@replace_filter = Filter.new(Satori.read_tab_file(path)) |
| 1202 |
end |
| 1203 |
|
| 1204 |
def load_database |
| 1205 |
if @variable['セーブデータ暗号化'] == '有効' |
| 1206 |
encrypted = true |
| 1207 |
@dbpath = File.join(@satori_dir, EDBNAME) |
| 1208 |
else |
| 1209 |
encrypted = false |
| 1210 |
@dbpath = File.join(@satori_dir, DBNAME) |
| 1211 |
end |
| 1212 |
begin |
| 1213 |
database = Satori.read_tab_file(@dbpath, :encrypted => encrypted) |
| 1214 |
rescue #except IOError: |
| 1215 |
database = [] |
| 1216 |
end |
| 1217 |
for name, value in database |
| 1218 |
if name.start_with?('b\'') or name.start_with?('b"') |
| 1219 |
next # XXX: 文字コード変換に問題のあったバージョン対策 |
| 1220 |
end |
| 1221 |
assign(name, value) |
| 1222 |
end |
| 1223 |
end |
| 1224 |
|
| 1225 |
def save_database |
| 1226 |
if @variable['セーブデータ暗号化'] == '有効' |
| 1227 |
encrypted = true |
| 1228 |
@dbpath = File.join(@satori_dir, EDBNAME) |
| 1229 |
else |
| 1230 |
encrypted = false |
| 1231 |
@dbpath = File.join(@satori_dir, DBNAME) |
| 1232 |
end |
| 1233 |
begin |
| 1234 |
open(@dbpath, 'wb') do |f| |
| 1235 |
for name in @variable.keys |
| 1236 |
value = @variable[name] |
| 1237 |
if ['前回終了時サーフェス0', |
| 1238 |
'前回終了時サーフェス1', |
| 1239 |
'デフォルトサーフェス0', |
| 1240 |
'デフォルトサーフェス1'].include?(name) |
| 1241 |
next |
| 1242 |
end |
| 1243 |
line = (name.to_s + "\t" + value.to_s) |
| 1244 |
line = line.encode('CP932', :invalid => :replace, :undef => :replace) |
| 1245 |
if encrypted |
| 1246 |
line = Satori.encrypt(Satori.encrypt(line)).join('') |
| 1247 |
end |
| 1248 |
f.write(line) |
| 1249 |
f.write("\r\n") |
| 1250 |
end |
| 1251 |
for side in [0, 1] |
| 1252 |
name = ('デフォルトサーフェス' + side.to_s) |
| 1253 |
value = to_zenkaku(@default_surface[side].to_s) |
| 1254 |
line = (name.to_s + "\t" + value.to_s) |
| 1255 |
line = line.encode('CP932', :invalid => :replace, :undef => :replace) |
| 1256 |
if encrypted |
| 1257 |
line = Satori.encrypt(Satori.encrypt(line)).join('') |
| 1258 |
end |
| 1259 |
f.write(line) |
| 1260 |
f.write("\r\n") |
| 1261 |
end |
| 1262 |
for side in [0, 1] |
| 1263 |
name = ('前回終了時サーフェス' + side.to_s) |
| 1264 |
value = to_zenkaku(@current_surface[side].to_s) |
| 1265 |
line = (name.to_s + "\t" + value.to_s) |
| 1266 |
line = line.encode('CP932', :invalid => :replace, :undef => :replace) |
| 1267 |
if encrypted |
| 1268 |
line = Satori.encrypt(Satori.encrypt(line)).join('') |
| 1269 |
end |
| 1270 |
f.write(line) |
| 1271 |
f.write("\r\n") |
| 1272 |
end |
| 1273 |
name = '起動回数' |
| 1274 |
value = to_zenkaku(@runcount.to_s) |
| 1275 |
line = (name.to_s + "\t" + value.to_s) |
| 1276 |
line = line.encode('CP932', :invalid => :replace, :undef => :replace) |
| 1277 |
if encrypted |
| 1278 |
line = Satori.encrypt(Satori.encrypt(line)).join('') |
| 1279 |
end |
| 1280 |
f.write(line) |
| 1281 |
f.write("\r\n") |
| 1282 |
for name in @timer.keys |
| 1283 |
value = to_zenkaku(@timer[name]) |
| 1284 |
line = (name.to_s + "\t" + value.to_s) |
| 1285 |
line = line.encode('CP932', :invalid => :replace, :undef => :replace) |
| 1286 |
if encrypted |
| 1287 |
line = Satori.encrypt(Satori.encrypt(line)).join('') |
| 1288 |
end |
| 1289 |
f.write(line) |
| 1290 |
f.write("\r\n") |
| 1291 |
end |
| 1292 |
for name in @reserved_talk.keys |
| 1293 |
value = to_zenkaku(@reserved_talk[name]) |
| 1294 |
line = (['次から', value, '回目のトーク'].join('') + "\t" + name.to_s) |
| 1295 |
line = line.encode('CP932', :invalid => :replace, :undef => :replace) |
| 1296 |
if encrypted |
| 1297 |
line = Satori.encrypt(Satori.encrypt(line)).join('') |
| 1298 |
end |
| 1299 |
f.write(line) |
| 1300 |
f.write("\r\n") |
| 1301 |
end |
| 1302 |
end |
| 1303 |
rescue #except IOError: |
| 1304 |
Logging::Logging.debug('satori.py: cannot write ' + @dbpath.to_s) |
| 1305 |
return |
| 1306 |
end |
| 1307 |
end |
| 1308 |
|
| 1309 |
def finalize |
| 1310 |
get_event_response('OnSatoriUnload') |
| 1311 |
accumulative_runtime = (@runtime + get_runtime()) |
| 1312 |
assign('単純累計秒', to_zenkaku(accumulative_runtime)) |
| 1313 |
save_database() |
| 1314 |
end |
| 1315 |
|
| 1316 |
# SHIORI/1.0 API |
| 1317 |
def getaistringrandom |
| 1318 |
get_script('') |
| 1319 |
end |
| 1320 |
|
| 1321 |
def getaistringfromtargetword(word) |
| 1322 |
'' |
| 1323 |
end |
| 1324 |
|
| 1325 |
def getdms |
| 1326 |
'' |
| 1327 |
end |
| 1328 |
|
| 1329 |
def getword(word_type) |
| 1330 |
'' |
| 1331 |
end |
| 1332 |
|
| 1333 |
# SHIORI/2.2 API |
| 1334 |
EVENT_MAP = { |
| 1335 |
'OnFirstBoot' => '初回', |
| 1336 |
'OnBoot' => '起動', |
| 1337 |
'OnClose' => '終了', |
| 1338 |
'OnGhostChanging' => '他のゴーストへ変更', |
| 1339 |
'OnGhostChanged' => '他のゴーストから変更', |
| 1340 |
'OnVanishSelecting' => '消滅指示', |
| 1341 |
'OnVanishCancel' => '消滅撤回', |
| 1342 |
'OnVanishSelected' => '消滅決定', |
| 1343 |
'OnVanishButtonHold' => '消滅中断', |
| 1344 |
} |
| 1345 |
|
| 1346 |
def get_event_response(event, |
| 1347 |
ref0: nil, ref1: nil, ref2: nil, ref3: nil, |
| 1348 |
ref4: nil, ref5: nil, ref6: nil, ref7: nil) |
| 1349 |
@event = event |
| 1350 |
@reference = [ref0, ref1, ref2, ref3, ref4, ref5, ref6, ref7] |
| 1351 |
tail = '\e' |
| 1352 |
case event |
| 1353 |
when 'OnUpdateReady' |
| 1354 |
begin |
| 1355 |
ref0 = (Integer(ref0) + 1).to_s |
| 1356 |
@reference[0] = ref0 |
| 1357 |
rescue |
| 1358 |
#pass |
| 1359 |
end |
| 1360 |
when 'OnMouseMove' |
| 1361 |
key = [ref3, ref4] # side, part |
| 1362 |
count, timestamp = (@mouse_move_count.include?(key) ? @mouse_move_count[key] : [0, 0]) |
| 1363 |
if (Time.now - timestamp).to_i > @touch_timeout |
| 1364 |
count = 0 |
| 1365 |
end |
| 1366 |
count += 1 |
| 1367 |
if count >= @touch_threshold |
| 1368 |
event = (ref3.to_s + ref4.to_s + 'なでられ') |
| 1369 |
count = 0 |
| 1370 |
end |
| 1371 |
@mouse_move_count[key] = [count, Time.now] |
| 1372 |
when 'OnMouseWheel' |
| 1373 |
key = [ref3, ref4] # side, part |
| 1374 |
count, timestamp = (@mouse_wheel_count.include?(key) ? @mouse_wheel_count[key] : [0, 0]) |
| 1375 |
if (Time.now - timestamp).to_i > 2 |
| 1376 |
count = 0 |
| 1377 |
end |
| 1378 |
count += 1 |
| 1379 |
if count >= 2 |
| 1380 |
event = (ref3.to_s + ref4.to_s + 'ころころ') |
| 1381 |
count = 0 |
| 1382 |
end |
| 1383 |
@mouse_wheel_count[key] = [count, Time.now] |
| 1384 |
when 'OnSecondChange' |
| 1385 |
@silent_time += 1 |
| 1386 |
if @save_interval > 0 |
| 1387 |
@save_timer -= 1 |
| 1388 |
if @save_timer <= 0 |
| 1389 |
save_database() |
| 1390 |
@save_timer = @save_interval |
| 1391 |
end |
| 1392 |
end |
| 1393 |
if ref3 != '0' # cantalk |
| 1394 |
# check random talk timer |
| 1395 |
if @random_talk.zero? |
| 1396 |
reset_random_talk_interval() |
| 1397 |
elsif @random_talk > 0 |
| 1398 |
@random_talk -= 1 |
| 1399 |
if @random_talk.zero? |
| 1400 |
event = get_reserved_talk() |
| 1401 |
unless event.nil? |
| 1402 |
@reference[0] = to_zenkaku(1) |
| 1403 |
else |
| 1404 |
@reference[0] = to_zenkaku(0) |
| 1405 |
end |
| 1406 |
@reference[1] = event |
| 1407 |
script = get_script('OnTalk') |
| 1408 |
unless script.nil? |
| 1409 |
@script_history.shift |
| 1410 |
@script_history. << script |
| 1411 |
return script |
| 1412 |
end |
| 1413 |
@reference[0] = ref0 |
| 1414 |
@reference[1] = ref1 |
| 1415 |
end |
| 1416 |
end |
| 1417 |
end |
| 1418 |
# check user-defined timers |
| 1419 |
for name in @timer.keys() |
| 1420 |
count = @timer[name] - 1 |
| 1421 |
if count > 0 |
| 1422 |
@timer[name] = count |
| 1423 |
elsif ref3 != '0' # cantalk |
| 1424 |
@timer.delete(name) |
| 1425 |
event = name[0..-7] |
| 1426 |
break |
| 1427 |
end |
| 1428 |
end |
| 1429 |
when 'OnSurfaceChange' |
| 1430 |
@current_surface[0] = ref0 |
| 1431 |
@current_surface[1] = ref1 |
| 1432 |
when 'OnChoiceSelect' |
| 1433 |
@choice_id = ref0 |
| 1434 |
@choice_label = ref1 |
| 1435 |
@choice_number = ref2 |
| 1436 |
unless @talk.include?('OnChoiceSelect') |
| 1437 |
event = ref0 |
| 1438 |
end |
| 1439 |
when 'OnChoiceEnter' |
| 1440 |
@choice_id = ref1 |
| 1441 |
@choice_label = ref0 |
| 1442 |
@choice_number = ref2 |
| 1443 |
when 'OnAnchorSelect' |
| 1444 |
if @talk.include?(ref0) |
| 1445 |
event = ref0 |
| 1446 |
end |
| 1447 |
when 'sakura.recommendsites', 'sakura.portalsites', 'kero.recommendsites' |
| 1448 |
return get_url_list(event) |
| 1449 |
when 'OnRecommandedSiteChoice' |
| 1450 |
script = get_url_script(ref0, ref1) |
| 1451 |
unless script.nil? |
| 1452 |
@script_history.shift |
| 1453 |
@script_history << script |
| 1454 |
end |
| 1455 |
return script |
| 1456 |
when *EVENT_MAP.keys |
| 1457 |
if ['OnBoot', 'OnGhostChanged'].include?(event) |
| 1458 |
unless @boot_script.nil? |
| 1459 |
script = @boot_script |
| 1460 |
@boot_script = nil |
| 1461 |
@script_history.shift |
| 1462 |
@script_history << script |
| 1463 |
return script |
| 1464 |
end |
| 1465 |
end |
| 1466 |
if ['OnClose', 'OnGhostChanging'].include?(event) |
| 1467 |
if event == 'OnClose' |
| 1468 |
tail = '\-\e' |
| 1469 |
end |
| 1470 |
script = get_script('OnSatoriClose', :tail => tail) |
| 1471 |
unless script.nil? |
| 1472 |
@script_history.shift |
| 1473 |
@script_history << script |
| 1474 |
return script |
| 1475 |
end |
| 1476 |
end |
| 1477 |
unless @talk.include?(event) |
| 1478 |
event = EVENT_MAP[event] |
| 1479 |
end |
| 1480 |
end |
| 1481 |
script = get_script(event, :tail => tail) |
| 1482 |
unless script.nil? |
| 1483 |
@script_history.shift |
| 1484 |
@script_history << script |
| 1485 |
end |
| 1486 |
return script |
| 1487 |
end |
| 1488 |
|
| 1489 |
# SHIORI/2.4 API |
| 1490 |
def teach(word) |
| 1491 |
name = @variable['教わること'] |
| 1492 |
unless name.nil? |
| 1493 |
@variable[name] = word |
| 1494 |
script = get_script([name, 'を教えてもらった'].join('')) |
| 1495 |
@script_history.shift |
| 1496 |
@script_history << script |
| 1497 |
return script |
| 1498 |
end |
| 1499 |
return nil |
| 1500 |
end |
| 1501 |
|
| 1502 |
# SHIORI/2.5 API |
| 1503 |
def getstring(name) |
| 1504 |
if @word.include?(name) |
| 1505 |
word = [] |
| 1506 |
@word[name].filter do |_, cond| |
| 1507 |
next not(['0', '0'].include?(expand(cond))) |
| 1508 |
end.each do |list, _| |
| 1509 |
word.concat(list) |
| 1510 |
end |
| 1511 |
return expand(word.sample) unless word.empty? |
| 1512 |
end |
| 1513 |
return nil |
| 1514 |
end |
| 1515 |
|
| 1516 |
# internal |
| 1517 |
def get_reserved_talk |
| 1518 |
reserved = nil |
| 1519 |
for key in @reserved_talk.keys |
| 1520 |
@reserved_talk[key] -= 1 |
| 1521 |
if @reserved_talk[key] <= 0 |
| 1522 |
reserved = key |
| 1523 |
end |
| 1524 |
end |
| 1525 |
unless reserved.nil? |
| 1526 |
@reserved_talk.delete(reserved) |
| 1527 |
else |
| 1528 |
reserved = '' |
| 1529 |
end |
| 1530 |
return reserved |
| 1531 |
end |
| 1532 |
|
| 1533 |
Re_reservation = Regexp.new('\A次から(([[:digit:]])+)(〜(([[:digit:]])+))?回目のトーク') |
| 1534 |
|
| 1535 |
def assign(name, value) |
| 1536 |
if name.end_with?('タイマ') |
| 1537 |
if @talk.include?(name[0..-4]) |
| 1538 |
add_timer(name, value) |
| 1539 |
end |
| 1540 |
elsif name == '全タイマ解除' |
| 1541 |
if value == '実行' |
| 1542 |
delete_all_timers() |
| 1543 |
end |
| 1544 |
elsif name == '辞書リロード' |
| 1545 |
if value == '実行' |
| 1546 |
reload() |
| 1547 |
end |
| 1548 |
elsif name == '手動セーブ' |
| 1549 |
if value == '実行' |
| 1550 |
save_database() |
| 1551 |
end |
| 1552 |
elsif not Re_reservation.match(name).nil? |
| 1553 |
return nil if value.nil? or value.empty? |
| 1554 |
match = Re_reservation.match(name) |
| 1555 |
number = to_integer(match[1]) |
| 1556 |
number = Array(number..to_integer(match[4])).sample unless match[4].nil? |
| 1557 |
while true |
| 1558 |
break_flag = false |
| 1559 |
for key in @reserved_talk.keys |
| 1560 |
if self.reserved_talk[key] == number |
| 1561 |
number += 1 |
| 1562 |
break_flag = true |
| 1563 |
break |
| 1564 |
end |
| 1565 |
end |
| 1566 |
break unless break_flag |
| 1567 |
end |
| 1568 |
@reserved_talk[value] = number |
| 1569 |
elsif name == '次のトーク' |
| 1570 |
return nil if value.nil? or value.empty? |
| 1571 |
number = 1 |
| 1572 |
while true |
| 1573 |
break_flag = false |
| 1574 |
for key in @reserved_talk.keys |
| 1575 |
if self.reserved_talk[key] == number |
| 1576 |
number += 1 |
| 1577 |
break_flag = true |
| 1578 |
break |
| 1579 |
end |
| 1580 |
end |
| 1581 |
break unless break_flag |
| 1582 |
end |
| 1583 |
@reserved_talk[value] = number |
| 1584 |
elsif name == 'トーク予約のキャンセル' |
| 1585 |
if value == '*' |
| 1586 |
@reserved_talk = {} |
| 1587 |
elsif @reserved_talk.include?(value) |
| 1588 |
@reserved_talk.delete(value) |
| 1589 |
end |
| 1590 |
elsif name == '起動回数' |
| 1591 |
@runcount = (to_integer(value) + 1) |
| 1592 |
elsif value.nil? or value.empty? |
| 1593 |
if @variable.include?(name) |
| 1594 |
@variable.delete(name) |
| 1595 |
end |
| 1596 |
else |
| 1597 |
@variable[name] = value |
| 1598 |
if ['喋り間隔', '喋り間隔誤差'].include?(name) |
| 1599 |
reset_random_talk_interval() |
| 1600 |
elsif name == '教わること' |
| 1601 |
return '\![open,teachbox]' |
| 1602 |
elsif name == '会話時サーフェス戻し' |
| 1603 |
if value == '有効' |
| 1604 |
@reset_surface = true |
| 1605 |
elsif value == '無効' |
| 1606 |
@reset_surface = false |
| 1607 |
end |
| 1608 |
elsif name == 'デフォルトサーフェス0' |
| 1609 |
value = to_integer(value) |
| 1610 |
@default_surface[0] = value unless value.nil? |
| 1611 |
elsif name == 'デフォルトサーフェス1' |
| 1612 |
value = to_integer(value) |
| 1613 |
@default_surface[1] = value unless value.nil? |
| 1614 |
elsif name == 'サーフェス加算値0' |
| 1615 |
value = to_integer(value) |
| 1616 |
unless value.nil? |
| 1617 |
@default_surface[0] = value |
| 1618 |
@add_to_surface[0] = value |
| 1619 |
end |
| 1620 |
elsif name == 'サーフェス加算値1' |
| 1621 |
value = to_integer(value) |
| 1622 |
unless value.nil? |
| 1623 |
@default_surface[1] = value |
| 1624 |
@add_to_surface[1] = value |
| 1625 |
end |
| 1626 |
elsif name == '単純累計秒' |
| 1627 |
@runtime = to_integer(value) |
| 1628 |
elsif name == 'なでられ反応回数' |
| 1629 |
@touch_threshold = to_integer(value) |
| 1630 |
elsif name == 'なでられ持続秒数' |
| 1631 |
@touch_timeout = to_integer(value) |
| 1632 |
elsif name == 'スコープ切り換え時' |
| 1633 |
@newline = value |
| 1634 |
elsif name == 'さくらスクリプトによるスコープ切り換え時' |
| 1635 |
@newline_script = value |
| 1636 |
elsif name == '自動挿入ウェイトの倍率' |
| 1637 |
if value.end_with?('%') |
| 1638 |
value = value[0..-2] |
| 1639 |
elsif value.end_with?('%') |
| 1640 |
value = value[0..-2] |
| 1641 |
end |
| 1642 |
value = to_integer(value) |
| 1643 |
if not value.nil? and value >= 0 and value <= 1000 |
| 1644 |
@wait_percent = value |
| 1645 |
end |
| 1646 |
elsif name == '自動セーブ間隔' |
| 1647 |
@save_interval = to_integer(value) |
| 1648 |
@save_timer = @save_interval |
| 1649 |
elsif name == '辞書フォルダ' |
| 1650 |
@folder_change = true |
| 1651 |
end |
| 1652 |
end |
| 1653 |
return nil |
| 1654 |
end |
| 1655 |
|
| 1656 |
def change_folder |
| 1657 |
value = @variable['辞書フォルダ'] |
| 1658 |
dir_list = value.split(',', -1) |
| 1659 |
@parser = Parser.new() |
| 1660 |
@parser.set_saori(@saori_function.keys()) |
| 1661 |
for path in Satori.list_dict(@satori_dir) |
| 1662 |
filename = File.basename(path) |
| 1663 |
if filename == 'replace.txt' |
| 1664 |
@parser.load_replace_file(path) |
| 1665 |
elsif filename == 'replace_after.txt' |
| 1666 |
load_replace_file(path) |
| 1667 |
end |
| 1668 |
end |
| 1669 |
buf = [] |
| 1670 |
for dir_ in dir_list |
| 1671 |
dir_ = Home.get_normalized_path(dir_) |
| 1672 |
dict_dir = File.join(@satori_dir, dir_) |
| 1673 |
unless File.directory?(dict_dir) |
| 1674 |
Logging::Logging.debug('satori.py: cannot read ' + dict_dir.to_s) |
| 1675 |
next |
| 1676 |
end |
| 1677 |
for path in Satori.list_dict(dict_dir) |
| 1678 |
filename = File.basename(path) |
| 1679 |
if filename == 'replace.txt' ## XXX |
| 1680 |
@parser.load_replace_file(path) |
| 1681 |
elsif filename == 'replace_after.txt' ## XXX |
| 1682 |
load_replace_file(path) |
| 1683 |
elsif ['satori_conf.txt', 'satori_conf.sat'].include?(filename) |
| 1684 |
#pass |
| 1685 |
else |
| 1686 |
buf << path |
| 1687 |
end |
| 1688 |
end |
| 1689 |
end |
| 1690 |
for path in buf |
| 1691 |
@parser.read(path) |
| 1692 |
end |
| 1693 |
@talk, @word = @parser.get_dict() |
| 1694 |
end |
| 1695 |
|
| 1696 |
def reload |
| 1697 |
finalize() |
| 1698 |
reset() |
| 1699 |
load() |
| 1700 |
end |
| 1701 |
|
| 1702 |
def reset_random_talk_interval |
| 1703 |
interval = get_integer('喋り間隔', :error => 'ignore') |
| 1704 |
if interval.nil? or interval.zero? |
| 1705 |
@random_talk = -1 |
| 1706 |
return |
| 1707 |
end |
| 1708 |
rate = get_integer('喋り間隔誤差', :error => 'ignore') |
| 1709 |
if rate.nil? |
| 1710 |
rate = 0.1 |
| 1711 |
else |
| 1712 |
rate = ([[rate, 1].max, 100].min / 100.0) |
| 1713 |
end |
| 1714 |
diff = (interval * rate).to_i |
| 1715 |
@random_talk = Array(interval - diff..interval + diff).sample |
| 1716 |
end |
| 1717 |
|
| 1718 |
def add_timer(name, value) |
| 1719 |
count = to_integer(value) |
| 1720 |
if count.nil? or count.zero? |
| 1721 |
if @timer.include?(name) |
| 1722 |
@timer.delete(name) |
| 1723 |
end |
| 1724 |
else |
| 1725 |
@timer[name] = count |
| 1726 |
end |
| 1727 |
end |
| 1728 |
|
| 1729 |
def delete_all_timers |
| 1730 |
@timer = {} |
| 1731 |
end |
| 1732 |
|
| 1733 |
def get_script(name, head: '\1', tail: '\e') |
| 1734 |
if @reset_surface |
| 1735 |
@current_reset_surface = [true, true] |
| 1736 |
else |
| 1737 |
@current_reset_surface = [false, false] |
| 1738 |
end |
| 1739 |
script, is_talk = get(name, default: nil) |
| 1740 |
if not script.nil? and not script.empty? and script != "\\n" |
| 1741 |
if is_talk |
| 1742 |
##Logging::Logging.debug('make("' + script.encode('utf-8', :invalid => :replace, :undef => :replace) + '")') |
| 1743 |
return make([head, script, tail].join('')) |
| 1744 |
else |
| 1745 |
return script |
| 1746 |
end |
| 1747 |
end |
| 1748 |
return nil |
| 1749 |
end |
| 1750 |
|
| 1751 |
def get_url_list(name) |
| 1752 |
@url_list[name] = [] |
| 1753 |
list_ = @talk[name] |
| 1754 |
return nil if list_.nil? |
| 1755 |
list_ = list_.filter do |_, cond| |
| 1756 |
next not(['0', '0'].include?(expand(cond))) |
| 1757 |
end.map do |talk, _| |
| 1758 |
next talk |
| 1759 |
end |
| 1760 |
return nil if list_.empty? |
| 1761 |
url_list = '' |
| 1762 |
for i in (list_.length-1).step(-1, -1) |
| 1763 |
nodelist = list_[i] |
| 1764 |
title = '' |
| 1765 |
j = 0 |
| 1766 |
while j < nodelist.length |
| 1767 |
node = nodelist[j] |
| 1768 |
j += 1 |
| 1769 |
if node[0] == NODE_TEXT |
| 1770 |
if node[1] == ["\\n"] |
| 1771 |
break |
| 1772 |
else |
| 1773 |
title = [title, node[1].join('')].join('') |
| 1774 |
end |
| 1775 |
else |
| 1776 |
#pass |
| 1777 |
end |
| 1778 |
end |
| 1779 |
next if title.empty? |
| 1780 |
if title == '-' |
| 1781 |
unless url_list.empty? |
| 1782 |
url_list = [url_list, 2.chr].join('') |
| 1783 |
end |
| 1784 |
url_list = [url_list, title, 1.chr].join('') |
| 1785 |
next |
| 1786 |
end |
| 1787 |
url = '' |
| 1788 |
while j < nodelist.length |
| 1789 |
node = nodelist[j] |
| 1790 |
j += 1 |
| 1791 |
if node[0] == NODE_TEXT |
| 1792 |
if node[1] == ["\\n"] |
| 1793 |
break |
| 1794 |
else |
| 1795 |
url = [url, node[1].join('')].join('') |
| 1796 |
end |
| 1797 |
else |
| 1798 |
#pass |
| 1799 |
end |
| 1800 |
end |
| 1801 |
next if url.empty? |
| 1802 |
bannar = '' |
| 1803 |
while j < nodelist.length |
| 1804 |
node = nodelist[j] |
| 1805 |
j += 1 |
| 1806 |
if node[0] == NODE_TEXT |
| 1807 |
if node[1] == ["\\n"] |
| 1808 |
break |
| 1809 |
else |
| 1810 |
bannar = [bannar, node[1].join('')].join('') |
| 1811 |
end |
| 1812 |
else |
| 1813 |
#pass |
| 1814 |
end |
| 1815 |
end |
| 1816 |
if nodelist[j..-1] |
| 1817 |
script = nodelist[j..-1] |
| 1818 |
else |
| 1819 |
script = nil |
| 1820 |
end |
| 1821 |
@url_list[name] << [title, url, bannar, script] |
| 1822 |
unless url_list.empty? |
| 1823 |
url_list = [url_list, 2.chr].join('') |
| 1824 |
end |
| 1825 |
url_list = [url_list, title, 1.chr, url, 1.chr, bannar].join('') |
| 1826 |
end |
| 1827 |
if url_list.empty? |
| 1828 |
url_list = nil |
| 1829 |
end |
| 1830 |
return url_list |
| 1831 |
end |
| 1832 |
|
| 1833 |
def get_url_script(title, url) |
| 1834 |
script = nil |
| 1835 |
if @reset_surface |
| 1836 |
@current_reset_surface = [true, true] |
| 1837 |
else |
| 1838 |
@current_reset_surface = [false, false] |
| 1839 |
end |
| 1840 |
for key in @url_list.keys |
| 1841 |
for item in @url_list[key] |
| 1842 |
if item[0] == title and item[1] == url |
| 1843 |
unless item[3].nil? |
| 1844 |
script = expand(item[3]) |
| 1845 |
if not script.nil? and not script.empty? and script != "\\n" |
| 1846 |
script = make(['\1', script, '\e'].join('')) |
| 1847 |
break |
| 1848 |
end |
| 1849 |
end |
| 1850 |
end |
| 1851 |
end |
| 1852 |
end |
| 1853 |
return script |
| 1854 |
end |
| 1855 |
|
| 1856 |
Redundant_tags = [ |
| 1857 |
[Regexp.new('(\\\\[01hu])+(\\\\[01hu])'), 2], #lambda {|m| m[2] }], |
| 1858 |
[Regexp.new('(\\n)+(\\e|$)'), 2], #lambda {|m| m[2] }], |
| 1859 |
[Regexp.new('(\\e)+'), 1], #lambda {|m| m[1] }], |
| 1860 |
] |
| 1861 |
Re_newline = Regexp.new('((\\n)*)(\\e)') |
| 1862 |
Re_0 = Regexp.new('\\\\[0h]') |
| 1863 |
Re_1 = Regexp.new('\\\\[1u]') |
| 1864 |
Re_wait_after = Regexp.new('\A、|。|,|.') |
| 1865 |
Re_wait_before = Regexp.new('\A\\\\[01hunce]') |
| 1866 |
Re_tag = Regexp.new('\A\\\\[ehunjcxtqzy*v0123456789fmia!&+\-\-\-]|' \ |
| 1867 |
'\\\\[sb][0-9]?|\\w[0-9]|\\_[wqslvVbe+cumna]|' \ |
| 1868 |
'\\__[ct]|\\URL') |
| 1869 |
|
| 1870 |
def make(script) |
| 1871 |
return nil if script.nil? |
| 1872 |
# make anchor |
| 1873 |
buf = [] |
| 1874 |
i = 0 |
| 1875 |
while true |
| 1876 |
match = Re_tag.match(script, i) |
| 1877 |
if not match.nil? and match.begin(0) == i |
| 1878 |
start = match.begin(0) |
| 1879 |
end_ = match.end(0) |
| 1880 |
if start > 0 |
| 1881 |
buf << @parser.anchor_filter.apply(script[i..start-1]) |
| 1882 |
end |
| 1883 |
buf << script[start..end_-1] |
| 1884 |
i = end_ |
| 1885 |
else |
| 1886 |
buf << @parser.anchor_filter.apply(script[i..-1]) |
| 1887 |
break |
| 1888 |
end |
| 1889 |
end |
| 1890 |
script = buf.join('') |
| 1891 |
# apply replace_after.txt |
| 1892 |
script = @replace_filter.apply(script) |
| 1893 |
# remove redundant tags |
| 1894 |
for pattern, replace in Redundant_tags |
| 1895 |
#script, count = pattern.subn(replace, script) |
| 1896 |
match = pattern.match(script) |
| 1897 |
script = script.sub(pattern, match[replace]) unless match.nil? |
| 1898 |
end |
| 1899 |
# remove redundant newline tags |
| 1900 |
match = Re_newline.match(script) |
| 1901 |
unless match.nil? |
| 1902 |
tag = match[3] |
| 1903 |
if tag == '\e' |
| 1904 |
if match.begin(0).zero? |
| 1905 |
script = script[match.end(0)..-1] |
| 1906 |
else |
| 1907 |
script = [script[0..match.begin(0)-1], |
| 1908 |
tag, script[match.end(0)..-1]].join('') |
| 1909 |
end |
| 1910 |
else |
| 1911 |
fail RuntimeError, 'should not reach here' |
| 1912 |
end |
| 1913 |
end |
| 1914 |
# insert newline |
| 1915 |
i = 1 |
| 1916 |
while true |
| 1917 |
match = Re_0.match(script, i) |
| 1918 |
unless match.nil? |
| 1919 |
end_ = match.end(0) |
| 1920 |
match = Re_0.match(script, end_) |
| 1921 |
unless match.nil? |
| 1922 |
start = match.begin(0) |
| 1923 |
if start < @newline.length or \ |
| 1924 |
script[start - @newline.length..start-1] != @newline |
| 1925 |
script = (script[0..match.end(0)-1] + @newline_script + script[match.end(0)..-1]) |
| 1926 |
end |
| 1927 |
else |
| 1928 |
break |
| 1929 |
end |
| 1930 |
i = end_ |
| 1931 |
else |
| 1932 |
break |
| 1933 |
end |
| 1934 |
end |
| 1935 |
i = 1 |
| 1936 |
while true |
| 1937 |
match = Re_1.match(script, i) |
| 1938 |
unless match.nil? |
| 1939 |
end_ = match.end(0) |
| 1940 |
match = Re_1.match(script, end_) |
| 1941 |
unless match.nil? |
| 1942 |
start = match.begin(0) |
| 1943 |
if start < @newline.length or \ |
| 1944 |
script[start - @newline.length..start-1] != @newline |
| 1945 |
script = (script[0..match.end(0)-1] + @newline_script + script[match.end(0)..-1]) |
| 1946 |
end |
| 1947 |
else |
| 1948 |
break |
| 1949 |
end |
| 1950 |
i = end_ |
| 1951 |
else |
| 1952 |
break |
| 1953 |
end |
| 1954 |
end |
| 1955 |
# insert waits |
| 1956 |
buf = [] |
| 1957 |
n = 0 |
| 1958 |
i, j = 0, script.length |
| 1959 |
while i < j |
| 1960 |
match = Re_wait_after.match(script, i) |
| 1961 |
if not match.nil? and match.begin(0).zero? # FIXME |
| 1962 |
buf << match[0] |
| 1963 |
buf.concat(make_wait(n)) |
| 1964 |
n = 0 |
| 1965 |
i = match.end(0) |
| 1966 |
next |
| 1967 |
end |
| 1968 |
match = Re_wait_before.match(script, i) |
| 1969 |
if not match.nil? and match.begin(0).zero? # FIXME |
| 1970 |
buf.concat(make_wait(n)) |
| 1971 |
buf << match[0] |
| 1972 |
n = 0 |
| 1973 |
i = match.end(0) |
| 1974 |
next |
| 1975 |
end |
| 1976 |
if script[i] == '[' |
| 1977 |
pos = script.index(']', i) |
| 1978 |
if pos > i |
| 1979 |
buf << script[i..pos] |
| 1980 |
i = (pos + 1) |
| 1981 |
next |
| 1982 |
end |
| 1983 |
end |
| 1984 |
match = Re_tag.match(script, i) |
| 1985 |
if not match.nil? and match.begin(0).zero? # FIXME |
| 1986 |
buf << script[i..match.end(0)-1] |
| 1987 |
i = match.end(0) |
| 1988 |
else |
| 1989 |
buf << script[i] |
| 1990 |
n += 3 |
| 1991 |
i += 1 |
| 1992 |
end |
| 1993 |
end |
| 1994 |
return buf.join('') |
| 1995 |
end |
| 1996 |
|
| 1997 |
def make_wait(ms) |
| 1998 |
buf = [] |
| 1999 |
n = ((ms + 25) * @wait_percent / 100 / 50).to_i |
| 2000 |
while n > 0 |
| 2001 |
buf << '\w' + [n, 9].min.to_s |
| 2002 |
n -= 9 |
| 2003 |
end |
| 2004 |
return buf |
| 2005 |
end |
| 2006 |
|
| 2007 |
def get(name, default: '') |
| 2008 |
talk = [] |
| 2009 |
if @talk.include?(name) |
| 2010 |
talk += @talk[name].map do |talk, cond| |
| 2011 |
[talk, cond, true] |
| 2012 |
end |
| 2013 |
end |
| 2014 |
if @word.include?(name) |
| 2015 |
talk += @word[name].map do |talk, cond| |
| 2016 |
[talk, cond, false] |
| 2017 |
end |
| 2018 |
end |
| 2019 |
talk.delete_if do |_, cond, _| |
| 2020 |
['0', '0'].include?(expand(cond)) |
| 2021 |
end |
| 2022 |
return default, true if talk.empty? |
| 2023 |
sample = talk.sample |
| 2024 |
return expand(sample[0]), sample[2] |
| 2025 |
end |
| 2026 |
|
| 2027 |
def get_tag_jump(prefix, list) |
| 2028 |
talk = [] |
| 2029 |
@talk.each do |k, v| |
| 2030 |
a = k.split(' ') |
| 2031 |
if a.shift == prefix |
| 2032 |
v.each do |t, _| |
| 2033 |
talk << [t, a] |
| 2034 |
end |
| 2035 |
end |
| 2036 |
end |
| 2037 |
@word.each do |k, v| |
| 2038 |
a = k.split(' ') |
| 2039 |
if a.shift == prefix |
| 2040 |
v.each do |t, _| |
| 2041 |
talk << [t, a] |
| 2042 |
end |
| 2043 |
end |
| 2044 |
end |
| 2045 |
talk.keep_if do |_, tags| |
| 2046 |
list.all? do |x| |
| 2047 |
tags.include?(x) |
| 2048 |
end |
| 2049 |
end |
| 2050 |
return nil if talk.empty? |
| 2051 |
sample = talk.sample |
| 2052 |
return expand(sample[0]) |
| 2053 |
end |
| 2054 |
|
| 2055 |
def expand_internal(nodelist, caller_history: nil, side: 1) |
| 2056 |
buffer = [] |
| 2057 |
src = expand(nodelist, caller_history: caller_history, side: side, is_in_ref: true) |
| 2058 |
@parser.parse_parenthesis(src, buffer) |
| 2059 |
dest = expand(buffer, caller_history: caller_history, side: side, is_in_ref: true) |
| 2060 |
if src == dest |
| 2061 |
return false, nil |
| 2062 |
end |
| 2063 |
return true, dest |
| 2064 |
end |
| 2065 |
|
| 2066 |
def expand(nodelist, caller_history: nil, side: 1, is_in_ref: false) |
| 2067 |
return '' if nodelist.nil? |
| 2068 |
buf = [] |
| 2069 |
history = [] |
| 2070 |
talk = false |
| 2071 |
newline = [nil, nil] |
| 2072 |
for node in nodelist |
| 2073 |
case node[0] |
| 2074 |
when NODE_REF |
| 2075 |
h = caller_history.nil? ? history : caller_history |
| 2076 |
unless is_in_ref |
| 2077 |
expanded, value = expand_internal(node[1], caller_history: h, side: side) |
| 2078 |
else |
| 2079 |
expanded = false |
| 2080 |
value = nil |
| 2081 |
end |
| 2082 |
unless expanded |
| 2083 |
value = get_reference(node[1], h, side, is_in_ref) |
| 2084 |
end |
| 2085 |
unless value.nil? or value.empty? |
| 2086 |
talk = true |
| 2087 |
buf << value |
| 2088 |
history << value |
| 2089 |
unless caller_history.nil? |
| 2090 |
caller_history << value |
| 2091 |
end |
| 2092 |
end |
| 2093 |
when NODE_CALL |
| 2094 |
function = node[1] |
| 2095 |
args = node[2] |
| 2096 |
value = call_function( |
| 2097 |
function, args, |
| 2098 |
(not caller_history.nil?) ? caller_history : history, |
| 2099 |
side) |
| 2100 |
unless value.nil? or value.empty? |
| 2101 |
talk = true |
| 2102 |
buf << value |
| 2103 |
history << value |
| 2104 |
unless caller_history.nil? |
| 2105 |
caller_history << value |
| 2106 |
end |
| 2107 |
end |
| 2108 |
when NODE_SAORI |
| 2109 |
if @variable['SAORI引数の計算'] == '無効' |
| 2110 |
expand_only = true |
| 2111 |
else |
| 2112 |
expand_only = false |
| 2113 |
end |
| 2114 |
unless caller_history.nil? |
| 2115 |
value = call_saori( |
| 2116 |
node[1], |
| 2117 |
calc_args(node[2], caller_history, :expand_only => expand_only), |
| 2118 |
caller_history, side) |
| 2119 |
else |
| 2120 |
value = call_saori( |
| 2121 |
node[1], |
| 2122 |
calc_args(node[2], history, :expand_only => expand_only), |
| 2123 |
history, side) |
| 2124 |
end |
| 2125 |
unless value.nil? or value.empty? |
| 2126 |
talk = true |
| 2127 |
buf << value |
| 2128 |
history << value |
| 2129 |
unless caller_history.nil? |
| 2130 |
caller_history << value |
| 2131 |
end |
| 2132 |
end |
| 2133 |
when NODE_TEXT |
| 2134 |
phi = false |
| 2135 |
if node[1].is_a?(Array) |
| 2136 |
buf.concat([node[1].filter do |x| |
| 2137 |
if phi |
| 2138 |
phi = false |
| 2139 |
next true |
| 2140 |
elsif x == 'φ' |
| 2141 |
phi = true |
| 2142 |
next false |
| 2143 |
end |
| 2144 |
next true |
| 2145 |
end]) |
| 2146 |
else |
| 2147 |
buf.concat([node[1].gsub(/φ(.)/, '\1')]) |
| 2148 |
end |
| 2149 |
talk = true |
| 2150 |
when NODE_SIDE |
| 2151 |
if talk |
| 2152 |
newline[side] = @newline |
| 2153 |
else |
| 2154 |
newline[side] = nil |
| 2155 |
end |
| 2156 |
talk = false |
| 2157 |
if side.zero? |
| 2158 |
side = 1 |
| 2159 |
else |
| 2160 |
side = 0 |
| 2161 |
end |
| 2162 |
buf << "\\" + side.to_s |
| 2163 |
if @current_reset_surface[side] |
| 2164 |
buf << '\s[' + @default_surface[side].to_s + ']' |
| 2165 |
@current_reset_surface[side] = false |
| 2166 |
end |
| 2167 |
unless newline[side].nil? |
| 2168 |
buf << newline[side].to_s |
| 2169 |
end |
| 2170 |
when NODE_ASSIGNMENT |
| 2171 |
key = expand(node[1]) |
| 2172 |
value = expand(node[2]) |
| 2173 |
result = assign(key, value) |
| 2174 |
Logging::Logging.debug('Var ' + key.to_s + ' <= ' + value.to_s) |
| 2175 |
unless result.nil? |
| 2176 |
buf << result |
| 2177 |
end |
| 2178 |
when NODE_JUMP |
| 2179 |
if node[2].nil? or \ |
| 2180 |
not ['0', '0'].include?(expand(node[2])) |
| 2181 |
target = expand(node[1]) |
| 2182 |
if target == 'OnTalk' |
| 2183 |
@reference[1] = get_reserved_talk() |
| 2184 |
if @reference[1] |
| 2185 |
@reference[0] = to_zenkaku(1) |
| 2186 |
else |
| 2187 |
@reference[0] = to_zenkaku(0) |
| 2188 |
end |
| 2189 |
end |
| 2190 |
script, is_talk = get(target, :default => nil) |
| 2191 |
if not script.nil? and not script.empty? and script != "\\n" |
| 2192 |
buf << ['\1', script].join('') |
| 2193 |
break |
| 2194 |
end |
| 2195 |
end |
| 2196 |
when NODE_TAG_JUMP |
| 2197 |
target = expand(node[1]) |
| 2198 |
tag_list = node[2].map do |x| |
| 2199 |
expand(x) |
| 2200 |
end |
| 2201 |
script = get_tag_jump(target, tag_list) |
| 2202 |
unless script.nil? |
| 2203 |
buf << ['\1', script].join |
| 2204 |
end |
| 2205 |
when NODE_SEARCH ## FIXME |
| 2206 |
buf << '' |
| 2207 |
when NODE_CHOICE |
| 2208 |
label = expand(node[1]) |
| 2209 |
if node[2].nil? |
| 2210 |
id_ = label |
| 2211 |
else |
| 2212 |
id_ = expand(node[2]) |
| 2213 |
end |
| 2214 |
buf << '\q[' + label + ',' + id_ + ']\n' |
| 2215 |
talk = true |
| 2216 |
when NODE_OR_EXPR |
| 2217 |
break_flag = false |
| 2218 |
for i in 1..node.length-1 |
| 2219 |
unless ['0', '0'].include?(expand(node[i])) |
| 2220 |
buf << '1' |
| 2221 |
break_flag = true |
| 2222 |
break |
| 2223 |
end |
| 2224 |
end |
| 2225 |
unless break_flag |
| 2226 |
buf << '0' |
| 2227 |
end |
| 2228 |
when NODE_AND_EXPR |
| 2229 |
break_flag = false |
| 2230 |
for i in 1..node.length-1 |
| 2231 |
if ['0', '0'].include?(expand(node[i])) |
| 2232 |
buf << '0' |
| 2233 |
break_flag = true |
| 2234 |
break |
| 2235 |
end |
| 2236 |
end |
| 2237 |
unless break_flag |
| 2238 |
buf << '1' |
| 2239 |
end |
| 2240 |
when NODE_COMP_EXPR |
| 2241 |
operand1 = expand(node[1]) |
| 2242 |
operand2 = expand(node[3]) |
| 2243 |
n1 = to_integer(operand1) |
| 2244 |
n2 = to_integer(operand2) |
| 2245 |
if not (n1.nil? or n2.nil?) |
| 2246 |
operand1 = n1 |
| 2247 |
operand2 = n2 |
| 2248 |
elsif n1.nil? and n2.nil? and \ |
| 2249 |
['<', '>', '<=', '>='].include?(node[2]) |
| 2250 |
operand1 = operand1.length |
| 2251 |
operand2 = operand2.length |
| 2252 |
end |
| 2253 |
case node[2] |
| 2254 |
when '==' |
| 2255 |
buf << to_zenkaku(operand1 == operand2 ? 1 : 0) |
| 2256 |
when '!=' |
| 2257 |
buf << to_zenkaku(operand1 != operand2 ? 1 : 0) |
| 2258 |
when '<' |
| 2259 |
buf << to_zenkaku(operand1 < operand2 ? 1 : 0) |
| 2260 |
when '>' |
| 2261 |
buf << to_zenkaku(operand1 > operand2 ? 1 : 0) |
| 2262 |
when '<=' |
| 2263 |
buf << to_zenkaku(operand1 <= operand2 ? 1 : 0) |
| 2264 |
when '>=' |
| 2265 |
buf << to_zenkaku(operand1 >= operand2 ? 1 : 0) |
| 2266 |
else |
| 2267 |
fail RuntimeError, 'should not reach here' |
| 2268 |
end |
| 2269 |
when NODE_ADD_EXPR |
| 2270 |
value_str = expand(node[1]) |
| 2271 |
value = to_integer(value_str) |
| 2272 |
for i in 2.step(node.length-1, 2) |
| 2273 |
operand_str = expand(node[i + 1]) |
| 2274 |
operand = to_integer(operand_str) |
| 2275 |
if node[i] == '-' |
| 2276 |
if value.nil? or operand.nil? |
| 2277 |
value_str = value_str.gsub(operand_str, '') |
| 2278 |
value = nil |
| 2279 |
else |
| 2280 |
value -= operand |
| 2281 |
value_str = to_zenkaku(value) |
| 2282 |
end |
| 2283 |
next |
| 2284 |
end |
| 2285 |
value = 0 if value.nil? |
| 2286 |
operand = 0 if operand.nil? |
| 2287 |
if node[i] == '+' |
| 2288 |
value += operand |
| 2289 |
else |
| 2290 |
fail RuntimeError, 'should not reach here' |
| 2291 |
end |
| 2292 |
end |
| 2293 |
if value.nil? |
| 2294 |
buf << value_str |
| 2295 |
else |
| 2296 |
buf << to_zenkaku(value) |
| 2297 |
end |
| 2298 |
when NODE_MUL_EXPR |
| 2299 |
value_str = expand(node[1]) |
| 2300 |
value = to_integer(value_str) |
| 2301 |
for i in 2.step(node.length-1, 2) |
| 2302 |
operand_str = expand(node[i + 1]) |
| 2303 |
operand = to_integer(operand_str) |
| 2304 |
if node[i] == '*' |
| 2305 |
if value.nil? and operand.nil? |
| 2306 |
value_str = '' |
| 2307 |
value = nil |
| 2308 |
elsif value.nil? |
| 2309 |
value_str *= operand |
| 2310 |
value = nil |
| 2311 |
elsif operand.nil? |
| 2312 |
value_str *= operand_str |
| 2313 |
value = nil |
| 2314 |
else |
| 2315 |
value *= operand |
| 2316 |
end |
| 2317 |
next |
| 2318 |
end |
| 2319 |
value = 0 if value.nil? |
| 2320 |
operand = 0 if operand.nil? |
| 2321 |
if node[i] == '/' |
| 2322 |
value = (value / operand).to_i |
| 2323 |
elsif node[i] == '%' |
| 2324 |
value = (value % operand) |
| 2325 |
else |
| 2326 |
fail RuntimeError, 'should not reach here' |
| 2327 |
end |
| 2328 |
end |
| 2329 |
if value.nil? |
| 2330 |
buf << value_str |
| 2331 |
else |
| 2332 |
buf << to_zenkaku(value) |
| 2333 |
end |
| 2334 |
when NODE_POW_EXPR |
| 2335 |
value = to_integer(expand(node[1])) |
| 2336 |
value = 0 if value.nil? |
| 2337 |
for i in 2..node.length-1 |
| 2338 |
operand = to_integer(expand(node[i])) |
| 2339 |
operand = 0 if operand.nil? |
| 2340 |
value **= operand |
| 2341 |
end |
| 2342 |
buf << to_zenkaku(value) |
| 2343 |
when NODE_UNARY_EXPR |
| 2344 |
value = expand(node[2]) |
| 2345 |
if node[1] == '-' |
| 2346 |
value = to_integer(value) |
| 2347 |
value = 0 if value.nil? |
| 2348 |
value = -value |
| 2349 |
elsif node[1] == '!' |
| 2350 |
value = (['0', '0'].include?(value) ? 1 : 0) |
| 2351 |
else |
| 2352 |
fail RuntimeError, 'should not reach here' |
| 2353 |
end |
| 2354 |
buf << to_zenkaku(value) |
| 2355 |
else |
| 2356 |
fail RuntimeError, 'should not reach here' |
| 2357 |
end |
| 2358 |
end |
| 2359 |
return buf.join('').strip() |
| 2360 |
end |
| 2361 |
|
| 2362 |
Re_random = Regexp.new('\A乱数((-|−|+|[-+])?([[:digit:]])+)(?:~|〜)((-|−|+|[-+])?([[:digit:]])+)') |
| 2363 |
Re_is_empty = Regexp.new('\A(変数|文|単語群)「(.*)」の存在') |
| 2364 |
Re_n_reserved = Regexp.new('\A次から(([[:digit:]])+)回目のトーク') |
| 2365 |
Re_is_reserved = Regexp.new('\Aトーク「(.*)」の予約有無') |
| 2366 |
|
| 2367 |
def get_reference(nodelist, history, side, is_in_ref = false) |
| 2368 |
key = expand(nodelist[1..-2], :caller_history => history, is_in_ref: is_in_ref) |
| 2369 |
if key == 'SenderType' or key == 'SenderType' |
| 2370 |
return @sender_type |
| 2371 |
elsif not key.nil? and ['R', 'R'].include?(key[0]) |
| 2372 |
n = to_integer(key[1..-1]) |
| 2373 |
if not n.nil? and 0 <= n and n < @reference.length ## FIXME |
| 2374 |
return '' if @reference[n].nil? |
| 2375 |
return @reference[n].to_s |
| 2376 |
end |
| 2377 |
elsif key and ['S', 'S'].include?(key[0]) |
| 2378 |
n = to_integer(key[1..-1]) |
| 2379 |
unless n.nil? |
| 2380 |
if @saori_value.include?(key) |
| 2381 |
return '' if @saori_value[key].nil? |
| 2382 |
return @saori_value[key].to_s |
| 2383 |
else |
| 2384 |
return '' |
| 2385 |
end |
| 2386 |
end |
| 2387 |
elsif not key.nil? and ['H', 'H'].include?(key[0]) |
| 2388 |
##Logging::Logging.debug(['["', history.join('", "'), '"]'].join('')) |
| 2389 |
n = to_integer(key[1..-1]) |
| 2390 |
if not n.nil? and 1 <= n and n < history.length + 1 ## FIXME |
| 2391 |
return history[n-1] |
| 2392 |
end |
| 2393 |
end |
| 2394 |
n = to_integer(key) |
| 2395 |
unless n.nil? |
| 2396 |
return '\s[' + (n + @add_to_surface[side]).to_s + ']' |
| 2397 |
end |
| 2398 |
if @word.include?(key) and not (list = @word[key].filter do |_, cond| |
| 2399 |
next not(['0', '0'].include?(expand(cond))) |
| 2400 |
end).empty? |
| 2401 |
word = [] |
| 2402 |
list.each do |l, _| |
| 2403 |
word.concat(l) |
| 2404 |
end |
| 2405 |
return expand(word.sample, :caller_history => history, |
| 2406 |
:side => side, is_in_ref: is_in_ref) |
| 2407 |
elsif @talk.include?(key) and not (list = @talk[key].filter do |_, cond| |
| 2408 |
next not(['0', '0'].include?(expand(cond))) |
| 2409 |
end).empty? |
| 2410 |
@reference = [nil] * 8 |
| 2411 |
talk = list.map do |t, _| |
| 2412 |
next t |
| 2413 |
end |
| 2414 |
return expand(talk.sample, :side => 1, is_in_ref: is_in_ref) |
| 2415 |
elsif @variable.include?(key) |
| 2416 |
Logging::Logging.debug('Var ' + key + ': ' + @variable[key].to_s) |
| 2417 |
return @variable[key] |
| 2418 |
elsif @timer.include?(key) |
| 2419 |
return to_zenkaku(@timer[key]) |
| 2420 |
elsif is_reserved(key) |
| 2421 |
return get_reserved(key) |
| 2422 |
elsif not Re_random.match(key).nil? |
| 2423 |
match = Re_random.match(key) |
| 2424 |
i = to_integer(match[1]) |
| 2425 |
j = to_integer(match[4]) |
| 2426 |
if i < j |
| 2427 |
return to_zenkaku(Array(i..j).sample) |
| 2428 |
else |
| 2429 |
return to_zenkaku(Array(j..i).sample) |
| 2430 |
end |
| 2431 |
elsif not Re_n_reserved.match(key).nil? |
| 2432 |
match = Re_n_reserved.match(key) |
| 2433 |
number = to_integer(match[1]) |
| 2434 |
for key in @reserved_talk.keys |
| 2435 |
if @reserved_talk[key] == number |
| 2436 |
return key |
| 2437 |
end |
| 2438 |
end |
| 2439 |
return '' |
| 2440 |
elsif not Re_is_reserved.match(key).nil? |
| 2441 |
match = Re_is_reserved.match(key) |
| 2442 |
name = match[1] |
| 2443 |
if @reserved_talk.include?(name) |
| 2444 |
return to_zenkaku(1) |
| 2445 |
else |
| 2446 |
return to_zenkaku(0) |
| 2447 |
end |
| 2448 |
elsif not Re_is_empty.match(key).nil? |
| 2449 |
match = Re_is_empty.match(key) |
| 2450 |
type_ = match[1] |
| 2451 |
name = match[2] |
| 2452 |
case type_ |
| 2453 |
when '変数' |
| 2454 |
if @variable.include?(name) |
| 2455 |
return to_zenkaku(1) |
| 2456 |
else |
| 2457 |
return to_zenkaku(0) |
| 2458 |
end |
| 2459 |
when '文' |
| 2460 |
if @talk.include?(name) |
| 2461 |
return to_zenkaku(1) |
| 2462 |
else |
| 2463 |
return to_zenkaku(0) |
| 2464 |
end |
| 2465 |
when '単語群' |
| 2466 |
if @word.include?(name) |
| 2467 |
return to_zenkaku(1) |
| 2468 |
else |
| 2469 |
return to_zenkaku(0) |
| 2470 |
end |
| 2471 |
end |
| 2472 |
end |
| 2473 |
return '(' + key.to_s + ')' |
| 2474 |
end |
| 2475 |
|
| 2476 |
def calc_args(args, history, expand_only: false) |
| 2477 |
buf = [] |
| 2478 |
for i in 0..args.length-1 |
| 2479 |
value = expand(args[i], :caller_history => history) |
| 2480 |
line = value ## FIXME |
| 2481 |
if expand_only or line.empty? |
| 2482 |
buf << value |
| 2483 |
elsif ['-', '-', '−', '+', '+'].include?(line[0]) and line.length == 1 # XXX |
| 2484 |
buf << value |
| 2485 |
elsif NUMBER.include?(line[0]) |
| 2486 |
begin ## FIXME |
| 2487 |
line, expr = @parser.get_add_expr(line) |
| 2488 |
result = to_integer(expand(expr, :caller_history => history)).to_s |
| 2489 |
if result.nil? |
| 2490 |
buf << value |
| 2491 |
else |
| 2492 |
buf << result |
| 2493 |
end |
| 2494 |
rescue |
| 2495 |
buf << value |
| 2496 |
end |
| 2497 |
else |
| 2498 |
buf << value |
| 2499 |
end |
| 2500 |
end |
| 2501 |
return buf |
| 2502 |
end |
| 2503 |
|
| 2504 |
def call_function(name, args, history, side) |
| 2505 |
if name == '単語の追加' |
| 2506 |
#pass ## FIXME |
| 2507 |
elsif name == 'call' |
| 2508 |
ref = expand(args[0], :caller_history => history) |
| 2509 |
args = args[1..-1] |
| 2510 |
for i in 0..args.length-1 |
| 2511 |
name = ['A', to_zenkaku(i)].join('') |
| 2512 |
@variable[name] = expand(args[i], :caller_history => history) |
| 2513 |
end |
| 2514 |
result = get_reference([[NODE_TEXT, '('], [NODE_TEXT, ref], |
| 2515 |
[NODE_TEXT, ')']], history, side) |
| 2516 |
for i in 0..args.length-1 |
| 2517 |
name = ['A', to_zenkaku(i)].join('') |
| 2518 |
@variable.delete(name) |
| 2519 |
end |
| 2520 |
return result |
| 2521 |
elsif name == 'remember' |
| 2522 |
number = to_integer(expand(args[0], :caller_history => history)) |
| 2523 |
if number > 0 and number <= 64 and @script_history[-number] |
| 2524 |
return @script_history[-number] |
| 2525 |
else |
| 2526 |
return '' |
| 2527 |
end |
| 2528 |
elsif name == 'loop' |
| 2529 |
ref = expand(args[0], :caller_history => history) |
| 2530 |
if args.length < 2 |
| 2531 |
return '' |
| 2532 |
elsif args.length == 2 |
| 2533 |
start = 1 |
| 2534 |
end_ = to_integer(expand(args[1], :caller_history => history)) + 1 |
| 2535 |
step = 1 |
| 2536 |
elsif args.length == 3 |
| 2537 |
start = to_integer(expand(args[1], :caller_history => history)) |
| 2538 |
end_ = to_integer(expand(args[2], :caller_history => history)) + 1 |
| 2539 |
step = 1 |
| 2540 |
elsif args.length >= 4 |
| 2541 |
start = to_integer(expand(args[1], :caller_history => history)) |
| 2542 |
end_ = to_integer(expand(args[2], :caller_history => history)) |
| 2543 |
step = to_integer(expand(args[3], :caller_history => history)) |
| 2544 |
if step > 0 |
| 2545 |
end_ = (end_ + 1) |
| 2546 |
elsif step < 0 |
| 2547 |
end_ = (end_ - 1) |
| 2548 |
else |
| 2549 |
return '' # infinite loop |
| 2550 |
end |
| 2551 |
end |
| 2552 |
name = [ref, 'カウンタ'].join('') |
| 2553 |
buf = [] |
| 2554 |
for i in start.step(end_-1, step) |
| 2555 |
@variable[name] = to_zenkaku(i) |
| 2556 |
buf << get_reference([[NODE_TEXT, '('], |
| 2557 |
[NODE_TEXT, ref], |
| 2558 |
[NODE_TEXT, ')']], |
| 2559 |
history, side) |
| 2560 |
end |
| 2561 |
return buf.join('') |
| 2562 |
elsif name == "バイト値" |
| 2563 |
if args.nil? |
| 2564 |
return '' |
| 2565 |
end |
| 2566 |
n = to_integer(expand(args[0], :caller_history => history)) |
| 2567 |
return n.chr |
| 2568 |
elsif name == 'sync' ## FIXME |
| 2569 |
#pass |
| 2570 |
elsif name == 'set' |
| 2571 |
if args.nil? |
| 2572 |
name = '' |
| 2573 |
else |
| 2574 |
name = expand(args[0], :caller_history => history) |
| 2575 |
end |
| 2576 |
if args.length < 2 |
| 2577 |
value = '' |
| 2578 |
else |
| 2579 |
value = expand(args[1], :caller_history => history) |
| 2580 |
end |
| 2581 |
unless name.empty? |
| 2582 |
Logging::Logging.debug('Var ' + name + ' <= ' + value.to_s) |
| 2583 |
if value.empty? |
| 2584 |
if @variable.include?(name) |
| 2585 |
@variable.delete(name) |
| 2586 |
end |
| 2587 |
else |
| 2588 |
@variable[name] = value |
| 2589 |
end |
| 2590 |
end |
| 2591 |
return '' |
| 2592 |
elsif name == 'nop' |
| 2593 |
for i in 0..args.length-1 |
| 2594 |
expand(args[i], :caller_history => history) |
| 2595 |
end |
| 2596 |
#pass |
| 2597 |
elsif name == '合成単語群' ## FIXME: not tested |
| 2598 |
words = [] |
| 2599 |
for i in 0..args.length-1 ## FIXME |
| 2600 |
name = expand(args[i], :caller_history => history) |
| 2601 |
word = @word[name] |
| 2602 |
words.concat(word) unless word.nil? |
| 2603 |
end |
| 2604 |
unless words.empty? |
| 2605 |
return expand(words.sample) |
| 2606 |
end |
| 2607 |
elsif name.end_with?('の数') |
| 2608 |
if ['R', 'R'].include?(name[0]) |
| 2609 |
return @reference.length |
| 2610 |
elsif ['A', 'A'].include?(name[0]) # len(args) |
| 2611 |
#pass ## FIXME |
| 2612 |
elsif ['S', 'S'].include?(name[0]) |
| 2613 |
##return @saori_value.length |
| 2614 |
#pass ## FIXME |
| 2615 |
#elif ['H', 'H'].include?(name[0]) |
| 2616 |
#return history.length |
| 2617 |
#elif ['C', 'C'].include?(name[0]) |
| 2618 |
#return count.length |
| 2619 |
else ## FIXME |
| 2620 |
#pass |
| 2621 |
end |
| 2622 |
elsif name == 'when' |
| 2623 |
fail "assert" unless args.length > 1 |
| 2624 |
condition = expand(args[0], :caller_history => history) |
| 2625 |
if ['0', '0'].include?(condition) |
| 2626 |
if args.length > 2 |
| 2627 |
return expand(args[2], :caller_history => history) |
| 2628 |
else |
| 2629 |
return '' |
| 2630 |
end |
| 2631 |
else |
| 2632 |
return expand(args[1], :caller_history => history) |
| 2633 |
end |
| 2634 |
elsif name == 'times' ## FIXME |
| 2635 |
print('TIMES: ', args.length, " ", args) |
| 2636 |
#pass |
| 2637 |
elsif name == 'while' ## FIXME |
| 2638 |
print('WHILE :', args.length, " ", args) |
| 2639 |
#pass |
| 2640 |
elsif name == 'for' ## FIXME |
| 2641 |
print('FOR :', args.length, " ", args) |
| 2642 |
#pass |
| 2643 |
else |
| 2644 |
fail RuntimeError, 'should not reach here' |
| 2645 |
end |
| 2646 |
return '' |
| 2647 |
end |
| 2648 |
|
| 2649 |
def call_saori(name, args, history, side) |
| 2650 |
return '' |
| 2651 |
end |
| 2652 |
|
| 2653 |
def get_runtime |
| 2654 |
return (Time.new - @time_start).to_i |
| 2655 |
end |
| 2656 |
|
| 2657 |
def get_integer(name, error: 'strict') |
| 2658 |
value = @variable[name] |
| 2659 |
return nil if value.nil? |
| 2660 |
return to_integer(value, :error => error) |
| 2661 |
end |
| 2662 |
|
| 2663 |
NUMBER = { |
| 2664 |
'0' => '0', '0' => '0', |
| 2665 |
'1' => '1', '1' => '1', |
| 2666 |
'2' => '2', '2' => '2', |
| 2667 |
'3' => '3', '3' => '3', |
| 2668 |
'4' => '4', '4' => '4', |
| 2669 |
'5' => '5', '5' => '5', |
| 2670 |
'6' => '6', '6' => '6', |
| 2671 |
'7' => '7', '7' => '7', |
| 2672 |
'8' => '8', '8' => '8', |
| 2673 |
'9' => '9', '9' => '9', |
| 2674 |
'+' => '+', '+' => '+', |
| 2675 |
'−' => '-', '-' => '-', '-' => '-', |
| 2676 |
} |
| 2677 |
|
| 2678 |
def to_integer(line, error: 'strict') |
| 2679 |
buf = [] |
| 2680 |
for char in line.chars |
| 2681 |
if NUMBER.include?(char) |
| 2682 |
buf << NUMBER[char] |
| 2683 |
else |
| 2684 |
if ['.', '.'].include?(char) # XXX |
| 2685 |
buf << '.' |
| 2686 |
elsif error == 'strict' |
| 2687 |
return nil |
| 2688 |
end |
| 2689 |
end |
| 2690 |
end |
| 2691 |
begin |
| 2692 |
return Integer(buf.join('')) |
| 2693 |
rescue #except ValueError: |
| 2694 |
if buf.include?('.') # XXX |
| 2695 |
return Integer(Float(buf.join(''))) |
| 2696 |
end |
| 2697 |
return nil |
| 2698 |
end |
| 2699 |
end |
| 2700 |
|
| 2701 |
def is_number(line) |
| 2702 |
return (not to_integer(line).nil?) |
| 2703 |
end |
| 2704 |
|
| 2705 |
ZENKAKU = { |
| 2706 |
'0' => '0', '1' => '1', '2' => '2', '3' => '3', '4' => '4', |
| 2707 |
'5' => '5', '6' => '6', '7' => '7', '8' => '8', '9' => '9', |
| 2708 |
'+' => '+', '-' => '−', |
| 2709 |
} |
| 2710 |
|
| 2711 |
def to_zenkaku(s) |
| 2712 |
buf = s.to_s.chars |
| 2713 |
for i in 0..buf.length-1 |
| 2714 |
if ZENKAKU.include?(buf[i]) |
| 2715 |
buf[i] = ZENKAKU[buf[i]] |
| 2716 |
end |
| 2717 |
end |
| 2718 |
return buf.join('') |
| 2719 |
end |
| 2720 |
|
| 2721 |
RESERVED = [ |
| 2722 |
'現在年', '現在月', '現在日', '現在曜日', |
| 2723 |
'現在時', '現在分', '現在秒', |
| 2724 |
'起動時', '起動分', '起動秒', |
| 2725 |
'累計時', '累計分', '累計秒', |
| 2726 |
'OS起動時', 'OS起動分', 'OS起動秒', |
| 2727 |
'単純起動分', '単純起動秒', |
| 2728 |
'単純累計分', '単純累計秒', |
| 2729 |
'単純OS起動分', '単純OS起動秒', |
| 2730 |
'最終トークからの経過秒', |
| 2731 |
'サーフェス0', 'サーフェス1', |
| 2732 |
'選択ID', '選択ラベル', '選択番号', |
| 2733 |
'予約トーク数', |
| 2734 |
'起動回数', |
| 2735 |
'Sender', 'Event', 'Charset', |
| 2736 |
'Reference0', 'Reference1', 'Reference2', 'Reference3', |
| 2737 |
'Reference4', 'Reference5', 'Reference6', 'Reference7', |
| 2738 |
'countTalk', 'countNoNameTalk', 'countEventTalk', 'countOtherTalk', |
| 2739 |
'countWords', 'countWord', 'countVariable', 'countAnchor', |
| 2740 |
'countParenthesis', 'countParentheres', 'countLine', |
| 2741 |
] |
| 2742 |
|
| 2743 |
def is_reserved(s) |
| 2744 |
return RESERVED.include?(s) |
| 2745 |
end |
| 2746 |
|
| 2747 |
DAYOFWEEK = ['月', '火', '水', '木', '金', '土', '日'] |
| 2748 |
|
| 2749 |
def get_reserved(s) |
| 2750 |
now = Time.now |
| 2751 |
case s |
| 2752 |
when '現在年' |
| 2753 |
return to_zenkaku(now.year) |
| 2754 |
when '現在月' |
| 2755 |
return to_zenkaku(now.month) |
| 2756 |
when '現在日' |
| 2757 |
return to_zenkaku(now.day) |
| 2758 |
when '現在時' |
| 2759 |
return to_zenkaku(now.hour) |
| 2760 |
when '現在分' |
| 2761 |
return to_zenkaku(now.min) |
| 2762 |
when '現在秒' |
| 2763 |
return to_zenkaku(now.sec) |
| 2764 |
when '現在曜日' |
| 2765 |
return DAYOFWEEK[now.wday] |
| 2766 |
end |
| 2767 |
runtime = get_runtime() |
| 2768 |
case s |
| 2769 |
when '起動時' |
| 2770 |
return to_zenkaku((runtime / 3600).to_i) |
| 2771 |
when '起動分' |
| 2772 |
return to_zenkaku((runtime / 60).to_i % 60) |
| 2773 |
when '起動秒' |
| 2774 |
return to_zenkaku(runtime % 60) |
| 2775 |
when '単純起動分' |
| 2776 |
return to_zenkaku((runtime / 60).to_i) |
| 2777 |
when '単純起動秒' |
| 2778 |
return to_zenkaku(runtime) |
| 2779 |
end |
| 2780 |
accumulative_runtime = @runtime + get_runtime() |
| 2781 |
case s |
| 2782 |
when '累計時' |
| 2783 |
return to_zenkaku((accumulative_runtime / 3600).to_i) |
| 2784 |
when '累計分' |
| 2785 |
return to_zenkaku((accumulative_runtime / 60).to_i % 60) |
| 2786 |
when '累計秒' |
| 2787 |
return to_zenkaku(accumulative_runtime % 60) |
| 2788 |
when '単純累計分' |
| 2789 |
return to_zenkaku((accumulative_runtime / 60).to_i) |
| 2790 |
when '単純累計秒' |
| 2791 |
return to_zenkaku(accumulative_runtime) |
| 2792 |
when '起動回数' |
| 2793 |
return to_zenkaku(@runcount) |
| 2794 |
when '最終トークからの経過秒' |
| 2795 |
return to_zenkaku(@silent_time) |
| 2796 |
when 'サーフェス0' |
| 2797 |
return @current_surface[0].to_i |
| 2798 |
when 'サーフェス1' |
| 2799 |
return @current_surface[1].to_i |
| 2800 |
when '選択ID' |
| 2801 |
return '' if self.choice_id.nil? |
| 2802 |
return @choice_id |
| 2803 |
when '選択ラベル' |
| 2804 |
return '' if @choice_label.nil? |
| 2805 |
return @choice_label |
| 2806 |
when '選択番号' |
| 2807 |
return '' if @choice_number.nil? |
| 2808 |
return to_zenkaku(@choice_number) |
| 2809 |
when '予約トーク数' |
| 2810 |
return to_zenkaku(@reserved_talk.length) |
| 2811 |
when 'Sender' |
| 2812 |
return 'ninix' |
| 2813 |
when 'Event' |
| 2814 |
return @event |
| 2815 |
when 'Charset' |
| 2816 |
return 'UTF-8' |
| 2817 |
when /\AReference/ |
| 2818 |
n = s[9..-1].to_i |
| 2819 |
return '' if @reference[n].nil? |
| 2820 |
return @reference[n].to_s |
| 2821 |
when /\Acount/ |
| 2822 |
return to_zenkaku(@parser.get_count(s[5..-1])) |
| 2823 |
end |
| 2824 |
return '?'.encode('utf-8', :invalid => :replace, :undef => :replace) |
| 2825 |
end |
| 2826 |
end |
| 2827 |
|
| 2828 |
|
| 2829 |
class Shiori < SATORI |
| 2830 |
|
| 2831 |
def initialize(dll_name) |
| 2832 |
@dll_name = dll_name |
| 2833 |
@saori = nil |
| 2834 |
@saori_function = {} |
| 2835 |
end |
| 2836 |
|
| 2837 |
def use_saori(saori) |
| 2838 |
@saori = saori |
| 2839 |
end |
| 2840 |
|
| 2841 |
def load(dir: nil) |
| 2842 |
satori_init(:satori_dir => dir) |
| 2843 |
@saori_library = SatoriSaoriLibrary.new(@saori, self) |
| 2844 |
super() |
| 2845 |
return 1 |
| 2846 |
end |
| 2847 |
|
| 2848 |
def load_config_file(path) |
| 2849 |
parser = Parser.new() |
| 2850 |
parser.read(path) |
| 2851 |
talk, word = parser.get_dict() |
| 2852 |
for nodelist in (talk.include?('初期化') ? talk['初期化'].filter do |_, cond| |
| 2853 |
next not(['0', '0'].include?(expand(cond))) |
| 2854 |
end.map do |t, _| |
| 2855 |
next t |
| 2856 |
end : []) |
| 2857 |
expand(nodelist) |
| 2858 |
end |
| 2859 |
@saori_function = {} |
| 2860 |
saori_nodelist = [] |
| 2861 |
if word.include?('SAORI') |
| 2862 |
word['SAORI'].filter do |_, cond| |
| 2863 |
next not(['0', '0'].include?(expand(cond))) |
| 2864 |
end.map do |w, _| |
| 2865 |
next saori_nodelist.concat(w) |
| 2866 |
end |
| 2867 |
end |
| 2868 |
for nodelist in saori_nodelist |
| 2869 |
if nodelist[0][0] == NODE_TEXT |
| 2870 |
list_ = nodelist[0][1].join('').split(',', -1) |
| 2871 |
if list_.length >= 2 and not list_[0].nil? and not list_[1].nil? |
| 2872 |
head, tail = File.split(list_[1]) |
| 2873 |
saori_dir = File.join(@satori_dir, head) |
| 2874 |
result = @saori_library.load(list_[1], saori_dir) |
| 2875 |
unless result.zero? |
| 2876 |
@saori_function[list_[0]] = list_[1..-1] |
| 2877 |
else |
| 2878 |
@saori_function[list_[0]] = nil |
| 2879 |
##Logging::Logging.error('satori.py: cannot load ' + list_[1].to_s) |
| 2880 |
end |
| 2881 |
end |
| 2882 |
end |
| 2883 |
end |
| 2884 |
# ssuはsatoriに取り込まれているが |
| 2885 |
# 別モジュールなので読み込むことで代替する |
| 2886 |
unless @saori_library.load('ssu.dll', @satori_dir).zero? |
| 2887 |
Ssu::Saori::FUNC_NAME.each do |x| |
| 2888 |
@saori_function[x] = ['ssu.dll', x] |
| 2889 |
end |
| 2890 |
end |
| 2891 |
@parser.set_saori(@saori_function.keys()) |
| 2892 |
end |
| 2893 |
|
| 2894 |
def reload |
| 2895 |
finalize() |
| 2896 |
reset() |
| 2897 |
load(@satori_dir) |
| 2898 |
end |
| 2899 |
|
| 2900 |
def unload |
| 2901 |
finalize |
| 2902 |
@saori_library.unload() |
| 2903 |
end |
| 2904 |
|
| 2905 |
def find(top_dir, dll_name) |
| 2906 |
result = 0 |
| 2907 |
unless Satori.list_dict(top_dir).empty? |
| 2908 |
result = 100 |
| 2909 |
end |
| 2910 |
return result |
| 2911 |
end |
| 2912 |
|
| 2913 |
def show_description |
| 2914 |
Logging::Logging.info( |
| 2915 |
"Shiori: SATORI compatible module for ninix\n" \ |
| 2916 |
" Copyright (C) 2002 by Tamito KAJIYAMA\n" \ |
| 2917 |
" Copyright (C) 2002, 2003 by MATSUMURA Namihiko\n" \ |
| 2918 |
" Copyright (C) 2002-2019 by Shyouzou Sugitani\n" \ |
| 2919 |
" Copyright (C) 2003, 2004 by Shun-ichi TAHARA\n" \ |
| 2920 |
" Copyright (C) 2024, 2025 by Tatakinov") |
| 2921 |
end |
| 2922 |
|
| 2923 |
def request(req_string) |
| 2924 |
if @folder_change |
| 2925 |
change_folder() |
| 2926 |
@folder_change = false |
| 2927 |
end |
| 2928 |
header = req_string.encode('UTF-8', :invalid => :replace, :undef => :replace).split(/\r?\n/, 0) |
| 2929 |
req_header = {} |
| 2930 |
line = header.shift |
| 2931 |
unless line.nil? |
| 2932 |
line = line.strip() |
| 2933 |
req_list = line.split(nil, -1) |
| 2934 |
if req_list.length >= 2 |
| 2935 |
command = req_list[0].strip() |
| 2936 |
protocol = req_list[1].strip() |
| 2937 |
end |
| 2938 |
for line in header |
| 2939 |
line = line.strip() |
| 2940 |
next if line.empty? |
| 2941 |
next unless line.include?(':') |
| 2942 |
key, value = line.split(':', 2) |
| 2943 |
key.strip! |
| 2944 |
value.strip! |
| 2945 |
begin |
| 2946 |
value = Integer(value) |
| 2947 |
rescue |
| 2948 |
value = value.to_s |
| 2949 |
end |
| 2950 |
req_header[key] = value |
| 2951 |
end |
| 2952 |
end |
| 2953 |
@sender_type = req_header.include?('SenderType') ? req_header['SenderType'] : '' |
| 2954 |
result = '' |
| 2955 |
to = nil |
| 2956 |
if req_header.include?('ID') |
| 2957 |
if req_header['ID'] == 'dms' |
| 2958 |
result = getdms() |
| 2959 |
elsif req_header['ID'] == 'OnAITalk' |
| 2960 |
result = getaistringrandom() |
| 2961 |
elsif ["\\ms", "\\mz", "\\ml", "\\mc", "\\mh", \ |
| 2962 |
"\\mt", "\\me", "\\mp", "\\m?"].include?(req_header['ID']) |
| 2963 |
result = getword(req_header['ID']) |
| 2964 |
elsif req_header['ID'] == 'otherghostname' ## FIXME |
| 2965 |
##otherghost = [] |
| 2966 |
##for n in 0..127 |
| 2967 |
## if req_header.include?(['Reference', n.to_s].join('')) |
| 2968 |
## otherghost << req_header[['Reference', |
| 2969 |
## n.to_s].join('')].join('')) |
| 2970 |
## end |
| 2971 |
##end |
| 2972 |
##result = self.otherghostname(otherghost) |
| 2973 |
#pass |
| 2974 |
elsif req_header['ID'] == 'OnTeach' |
| 2975 |
if req_header.include?('Reference0') |
| 2976 |
teach(req_header['Reference0']) |
| 2977 |
end |
| 2978 |
else |
| 2979 |
ref = [] |
| 2980 |
for n in 0..7 |
| 2981 |
if req_header.include?(['Reference', n.to_s].join('')) |
| 2982 |
ref << req_header[ |
| 2983 |
['Reference', n.to_s].join('')] |
| 2984 |
else |
| 2985 |
ref << nil |
| 2986 |
end |
| 2987 |
end |
| 2988 |
@reference = ref |
| 2989 |
result = getstring(req_header['ID']) |
| 2990 |
if result.nil? |
| 2991 |
ref0, ref1, ref2, ref3, ref4, ref5, ref6, ref7 = ref |
| 2992 |
result = get_event_response( |
| 2993 |
req_header['ID'], |
| 2994 |
:ref0 => ref0, :ref1 => ref1, :ref2 => ref2, :ref3 => ref3, |
| 2995 |
:ref4 => ref4, :ref5 => ref5, :ref6 => ref6, :ref7 => ref7) |
| 2996 |
end |
| 2997 |
end |
| 2998 |
if result.nil? |
| 2999 |
result = '' |
| 3000 |
end |
| 3001 |
to = nil ##communicate_to() ## FIXME |
| 3002 |
end |
| 3003 |
unless result.empty? |
| 3004 |
@silent_time = 0 |
| 3005 |
end |
| 3006 |
result = ("SHIORI/3.0 200 OK\r\n" \ |
| 3007 |
"Sender: Satori\r\n" \ |
| 3008 |
"Charset: UTF-8\r\n" \ |
| 3009 |
"Value: " + result.to_s + "\r\n") |
| 3010 |
unless to.nil? |
| 3011 |
result = [result, "Reference0: " + to.to_s + "\r\n"].join('') |
| 3012 |
end |
| 3013 |
result = [result, "\r\n"].join('') |
| 3014 |
return result.encode('UTF-8', :invalid => :replace, :undef => :replace) |
| 3015 |
end |
| 3016 |
|
| 3017 |
def call_saori(name, args, history, side) |
| 3018 |
if not @saori_function.include?(name) or \ |
| 3019 |
@saori_function[name].nil? |
| 3020 |
return '' |
| 3021 |
end |
| 3022 |
saori_statuscode = '' |
| 3023 |
saori_header = [] |
| 3024 |
saori_value = {} |
| 3025 |
saori_protocol = '' |
| 3026 |
req = ("EXECUTE SAORI/1.0\r\n" \ |
| 3027 |
"Sender: Satori\r\n" \ |
| 3028 |
"SecurityLevel: local\r\n" \ |
| 3029 |
"Charset: Shift_JIS\r\n") ## XXX |
| 3030 |
default_args = @saori_function[name][1..-1] |
| 3031 |
n = default_args.length |
| 3032 |
for i in 0..default_args.length-1 |
| 3033 |
req = [req, "Argument" + i.to_s + ": " + default_args[i].to_s + "\r\n"].join('') |
| 3034 |
end |
| 3035 |
for i in 0..args.length-1 |
| 3036 |
argument = args[i] |
| 3037 |
unless argument.nil? |
| 3038 |
req = [req, |
| 3039 |
"Argument#{(i + n)}: #{argument}\r\n"].join('') |
| 3040 |
end |
| 3041 |
end |
| 3042 |
req = [req, "\r\n"].join('') |
| 3043 |
response = @saori_library.request( |
| 3044 |
@saori_function[name][0], |
| 3045 |
req.encode('CP932', :invalid => :replace, :undef => :replace)) |
| 3046 |
header = response.split(/\r?\n/, 0) |
| 3047 |
unless header.empty? |
| 3048 |
line = header.shift |
| 3049 |
line = line.force_encoding('CP932').encode("UTF-8", :invalid => :replace, :undef => :replace).strip() |
| 3050 |
if line.include?(' ') |
| 3051 |
saori_protocol, saori_statuscode = line.split(' ', 2) |
| 3052 |
saori_protocol.strip! |
| 3053 |
saori_statuscode.strip! |
| 3054 |
end |
| 3055 |
for line in header |
| 3056 |
line = line.force_encoding('CP932').encode("UTF-8", :invalid => :replace, :undef => :replace).strip() |
| 3057 |
next if line.empty? |
| 3058 |
next unless line.include?(':') |
| 3059 |
key, value = line.split(':', 2) |
| 3060 |
key = key.strip() |
| 3061 |
unless key.empty? |
| 3062 |
saori_header << key |
| 3063 |
saori_value[key] = value.strip() |
| 3064 |
end |
| 3065 |
end |
| 3066 |
end |
| 3067 |
for key in saori_value.keys |
| 3068 |
next unless key.start_with?('Value') |
| 3069 |
begin |
| 3070 |
i = Integer(key[5..-1]) |
| 3071 |
rescue |
| 3072 |
next |
| 3073 |
end |
| 3074 |
name = ['S', to_zenkaku(i)].join('') |
| 3075 |
@saori_value[name] = saori_value[key] # overwrite |
| 3076 |
end |
| 3077 |
if saori_value.include?('Result') |
| 3078 |
return saori_value['Result'] |
| 3079 |
else |
| 3080 |
return '' |
| 3081 |
end |
| 3082 |
end |
| 3083 |
end |
| 3084 |
|
| 3085 |
|
| 3086 |
class SatoriSaoriLibrary |
| 3087 |
|
| 3088 |
def initialize(saori, satori) |
| 3089 |
@saori_list = {} |
| 3090 |
@saori = saori |
| 3091 |
@satori = satori |
| 3092 |
end |
| 3093 |
|
| 3094 |
def load(name, top_dir) |
| 3095 |
result = 0 |
| 3096 |
if @saori and not @saori_list.include?(name) |
| 3097 |
module_ = @saori.request(name) |
| 3098 |
unless module_.nil? |
| 3099 |
@saori_list[name] = module_ |
| 3100 |
end |
| 3101 |
end |
| 3102 |
if @saori_list.include?(name) |
| 3103 |
result = @saori_list[name].load(:dir => top_dir) |
| 3104 |
end |
| 3105 |
return result |
| 3106 |
end |
| 3107 |
|
| 3108 |
def unload |
| 3109 |
for key in @saori_list.keys() |
| 3110 |
@saori_list[key].unload() |
| 3111 |
end |
| 3112 |
return nil |
| 3113 |
end |
| 3114 |
|
| 3115 |
def request(name, req) |
| 3116 |
result = '' # FIXME |
| 3117 |
if not name.nil? and @saori_list.include?(name) |
| 3118 |
result = @saori_list[name].request(req) |
| 3119 |
end |
| 3120 |
return result |
| 3121 |
end |
| 3122 |
end |
| 3123 |
end |