| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# |
| 3 |
# misaka.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 |
# |
| 8 |
# This program is free software; you can redistribute it and/or modify it |
| 9 |
# under the terms of the GNU General Public License (version 2) as |
| 10 |
# published by the Free Software Foundation. It is distributed in the |
| 11 |
# hope that it will be useful, but WITHOUT ANY WARRANTY; without even the |
| 12 |
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 13 |
# PURPOSE. See the GNU General Public License for more details. |
| 14 |
# |
| 15 |
|
| 16 |
# TODO: |
| 17 |
# error recovery |
| 18 |
# - ${foo} |
| 19 |
# - $(foo) ? |
| 20 |
|
| 21 |
require "stringio" |
| 22 |
|
| 23 |
require_relative "../home" |
| 24 |
require_relative "../logging" |
| 25 |
|
| 26 |
begin |
| 27 |
require 'charlock_holmes' |
| 28 |
rescue LoadError |
| 29 |
CharlockHolmes = nil |
| 30 |
end |
| 31 |
|
| 32 |
module Misaka |
| 33 |
|
| 34 |
class MisakaError < StandardError # ValueError |
| 35 |
# pass |
| 36 |
end |
| 37 |
|
| 38 |
def self.lexical_error(path: nil, position: nil) |
| 39 |
if path.nil? and position.nil? |
| 40 |
error = 'lexical error' |
| 41 |
elsif path.nil? |
| 42 |
at = ('line ' + position[0].to_s + ', column ' + position[1].to_s) |
| 43 |
error = ('lexical error at ' + at.to_s) |
| 44 |
elsif position.nil? |
| 45 |
error = ('lexical error in ' + path.to_s) |
| 46 |
else |
| 47 |
at = ('line ' + position[0].to_s + ', column ' + position[1].to_s) |
| 48 |
error = ('lexical error at ' + at.to_s + ' in ' + path.to_s) |
| 49 |
end |
| 50 |
fail MisakaError.new(error) |
| 51 |
end |
| 52 |
|
| 53 |
def self.syntax_error(message, path: nil, position: nil) |
| 54 |
if path.nil? and position.nil? |
| 55 |
error = ('syntax error (' + message.to_s + ')') |
| 56 |
elsif path.nil? |
| 57 |
at = ('line ' + position[0].to_s + ', column ' + position[1].to_s) |
| 58 |
error = ('syntax error at ' + at.to_s + ' (' + message.to_s{1} + ')') |
| 59 |
elsif position.nil? |
| 60 |
error = ('syntax error in ' + path.to_s + ' (' + message.to_s{1} + ')') |
| 61 |
else |
| 62 |
at = ('line ' + position[0].to_s + ', column ' + position[1].to_s) |
| 63 |
error = ('syntax error at ' + at.to_s + ' in ' + path.to_s + ' (' + message.to_s + ')') |
| 64 |
end |
| 65 |
fail MisakaError.new(error) |
| 66 |
end |
| 67 |
|
| 68 |
def self.list_dict(top_dir) |
| 69 |
buf = [] |
| 70 |
begin |
| 71 |
filelist, debug, error = Misaka.read_misaka_ini(top_dir) |
| 72 |
rescue #except Exception as e: |
| 73 |
filelist = [] |
| 74 |
end |
| 75 |
for filename in filelist |
| 76 |
buf << File.join(top_dir, filename) |
| 77 |
end |
| 78 |
return buf |
| 79 |
end |
| 80 |
|
| 81 |
def self.read_misaka_ini(top_dir) |
| 82 |
path = File.join(top_dir, 'misaka.ini') |
| 83 |
filelist = [] |
| 84 |
debug = 0 |
| 85 |
error = 0 |
| 86 |
open(path) do |f| |
| 87 |
while true |
| 88 |
line = f.gets |
| 89 |
break if line.nil? |
| 90 |
line = line.strip() |
| 91 |
next if line.empty? or line.start_with?('//') |
| 92 |
case line |
| 93 |
when 'dictionaries' |
| 94 |
line = f.gets |
| 95 |
if line.strip() != '{' |
| 96 |
Misaka.syntax_error('expected an open brace', :path => path) |
| 97 |
end |
| 98 |
while true |
| 99 |
line = f.gets |
| 100 |
Misaka.syntax_error('unexpected end of file', :path => path) if line.nil? |
| 101 |
line = line.strip() |
| 102 |
break if line == '}' |
| 103 |
next if line.empty? or line.start_with?('//') |
| 104 |
filelist << Home.get_normalized_path(line) |
| 105 |
end |
| 106 |
when 'debug,0' |
| 107 |
debug = 0 |
| 108 |
when 'debug,1' |
| 109 |
debug = 1 |
| 110 |
when 'error,0' |
| 111 |
error = 0 |
| 112 |
when 'error,1' |
| 113 |
error = 1 |
| 114 |
when 'propertyhandler,0' |
| 115 |
#pass |
| 116 |
when 'propertyhandler,1' |
| 117 |
#pass |
| 118 |
else |
| 119 |
Logging::Logging.debug("misaka.rb: unknown directive '#{line}'") |
| 120 |
end |
| 121 |
end |
| 122 |
end |
| 123 |
return filelist, debug, error |
| 124 |
end |
| 125 |
|
| 126 |
### LEXER ### |
| 127 |
|
| 128 |
TOKEN_NEWLINE = 1 |
| 129 |
TOKEN_WHITESPACE = 2 |
| 130 |
TOKEN_OPEN_BRACE = 3 |
| 131 |
TOKEN_CLOSE_BRACE = 4 |
| 132 |
TOKEN_OPEN_PAREN = 5 |
| 133 |
TOKEN_CLOSE_PAREN = 6 |
| 134 |
TOKEN_OPEN_BRACKET = 7 |
| 135 |
TOKEN_CLOSE_BRACKET = 8 |
| 136 |
TOKEN_DOLLAR = 9 |
| 137 |
TOKEN_COMMA = 11 |
| 138 |
TOKEN_SEMICOLON = 12 |
| 139 |
TOKEN_OPERATOR = 13 |
| 140 |
TOKEN_DIRECTIVE = 14 |
| 141 |
TOKEN_TEXT = 15 |
| 142 |
|
| 143 |
|
| 144 |
class Lexer |
| 145 |
attr_reader :buffer |
| 146 |
|
| 147 |
Re_comment = Regexp.new('\A[ \t]*//[^\r\n]*') |
| 148 |
Re_newline = Regexp.new('\A(\r\n|\r|\n)') |
| 149 |
Patterns = [ |
| 150 |
[TOKEN_NEWLINE, Re_newline], |
| 151 |
[TOKEN_WHITESPACE, Regexp.new('\A[ \t]+')], |
| 152 |
[TOKEN_OPEN_BRACE, Regexp.new('\A{')], |
| 153 |
[TOKEN_CLOSE_BRACE, Regexp.new('\A}')], |
| 154 |
[TOKEN_OPEN_PAREN, Regexp.new('\A\(')], |
| 155 |
[TOKEN_CLOSE_PAREN, Regexp.new('\A\)')], |
| 156 |
[TOKEN_OPEN_BRACKET, Regexp.new('\A\[')], |
| 157 |
[TOKEN_CLOSE_BRACKET, Regexp.new('\A\]')], |
| 158 |
[TOKEN_DOLLAR, Regexp.new('\A\$')], |
| 159 |
[TOKEN_COMMA, Regexp.new('\A,')], |
| 160 |
[TOKEN_SEMICOLON, Regexp.new('\A;')], |
| 161 |
[TOKEN_OPERATOR, Regexp.new('\A([!=]=|[<>]=?|=[<>]|&&|\|\||\+\+|--|[+\-*/]?=|[+\-*/%\^])')], |
| 162 |
[TOKEN_DIRECTIVE, Regexp.new('\A#[_A-Za-z][_A-Za-z0-9]*')], |
| 163 |
#[TOKEN_TEXT, Regexp.new("[!&|]|(\\[,\"]|[\x01\x02#'.0-9:?@A-Z\\_`a-z~]|[\x80-\xff].)+")], |
| 164 |
[TOKEN_TEXT, Regexp.new("\\A((\"[^\"]*\")|[!&|]|(\\\\[,\"]|[\x01\x02#'.0-9:?@A-Z\\\\_`a-z~\"]|[\\u0080-\\uffff]+)+)")], |
| 165 |
] |
| 166 |
Token_names = { |
| 167 |
TOKEN_WHITESPACE => 'whitespace', |
| 168 |
TOKEN_NEWLINE => 'a newline', |
| 169 |
TOKEN_OPEN_BRACE => 'an open brace', |
| 170 |
TOKEN_CLOSE_BRACE => 'a close brace', |
| 171 |
TOKEN_OPEN_PAREN => 'an open paren', |
| 172 |
TOKEN_CLOSE_PAREN => 'a close paren', |
| 173 |
TOKEN_OPEN_BRACKET => 'an open bracket', |
| 174 |
TOKEN_CLOSE_BRACKET => 'a close bracket', |
| 175 |
TOKEN_DOLLAR => 'a dollar', |
| 176 |
TOKEN_COMMA => 'a comma', |
| 177 |
TOKEN_SEMICOLON => 'a semicolon', |
| 178 |
TOKEN_OPERATOR => 'an operator', |
| 179 |
TOKEN_DIRECTIVE => 'an directive', |
| 180 |
TOKEN_TEXT => 'text', |
| 181 |
} |
| 182 |
|
| 183 |
def initialize(charset: 'CP932') |
| 184 |
@buffer = [] |
| 185 |
@charset = charset |
| 186 |
end |
| 187 |
|
| 188 |
def _match(data, line, column, path) |
| 189 |
temp = data.clone |
| 190 |
while temp.length > 0 |
| 191 |
if column.zero? |
| 192 |
match = Re_comment.match(temp) |
| 193 |
unless match.nil? |
| 194 |
column = (column + match[0].length) |
| 195 |
temp = match.post_match |
| 196 |
next |
| 197 |
end |
| 198 |
end |
| 199 |
break_flag = false |
| 200 |
for token, pattern in Patterns |
| 201 |
match = pattern.match(temp) |
| 202 |
unless match.nil? |
| 203 |
lexeme = match[0] |
| 204 |
if token == TOKEN_TEXT and \ |
| 205 |
lexeme.start_with?('"') and lexeme.end_with?('"') |
| 206 |
unless lexeme.include?('$') |
| 207 |
@buffer << [token, lexeme[1..-2], [line, column]] |
| 208 |
else # XXX |
| 209 |
line, column = _match(lexeme[1..-2], line, column + 1, path) |
| 210 |
column += 1 |
| 211 |
end |
| 212 |
else |
| 213 |
@buffer << [token, lexeme, [line, column]] |
| 214 |
end |
| 215 |
temp = match.post_match |
| 216 |
break_flag = true |
| 217 |
break |
| 218 |
end |
| 219 |
end |
| 220 |
unless break_flag |
| 221 |
###print(temp[0..100 - 1]) |
| 222 |
Misaka.lexical_error(:path => path, :position => [line, column]) |
| 223 |
end |
| 224 |
if token == TOKEN_NEWLINE |
| 225 |
line = (line + 1) |
| 226 |
column = 0 |
| 227 |
else |
| 228 |
column = (column + lexeme.length) |
| 229 |
end |
| 230 |
end |
| 231 |
return line, column |
| 232 |
end |
| 233 |
|
| 234 |
def read(f, path: nil) |
| 235 |
line = 1 |
| 236 |
column = 0 |
| 237 |
data = f.read() |
| 238 |
line, column = _match(data, line, column, path) |
| 239 |
@buffer << [TOKEN_NEWLINE, '', [line, column]] |
| 240 |
@path = path |
| 241 |
end |
| 242 |
|
| 243 |
def get_position |
| 244 |
@position |
| 245 |
end |
| 246 |
|
| 247 |
def pop |
| 248 |
begin |
| 249 |
token, lexeme, @position = @buffer.shift |
| 250 |
rescue #except IndexError: |
| 251 |
Misaka.syntax_error('unexpected end of file', :path => @path) |
| 252 |
end |
| 253 |
###print(token, repr(lexeme)) |
| 254 |
return token, lexeme |
| 255 |
end |
| 256 |
|
| 257 |
def pop_check(expected) |
| 258 |
token, lexeme = pop() |
| 259 |
if token != expected |
| 260 |
Misaka.syntax_error(['exptected ', Token_names[expected], ', but returns ', Token_names[token]].join(''), |
| 261 |
:path => @path, :position => get_position()) |
| 262 |
end |
| 263 |
return lexeme |
| 264 |
end |
| 265 |
|
| 266 |
def look_ahead(index: 0) |
| 267 |
begin |
| 268 |
token, lexeme, position = @buffer[index] |
| 269 |
rescue #except IndexError: |
| 270 |
Misaka.syntax_error('unexpected end of file', :path => @path) |
| 271 |
end |
| 272 |
return token, lexeme |
| 273 |
end |
| 274 |
|
| 275 |
def skip_space(accept_eof: false) |
| 276 |
while not @buffer.empty? |
| 277 |
token, lexeme = look_ahead() |
| 278 |
return false unless [TOKEN_NEWLINE, TOKEN_WHITESPACE].include?(token) |
| 279 |
pop() |
| 280 |
end |
| 281 |
unless accept_eof |
| 282 |
Misaka.syntax_error('unexpected end of file', :path => @path) |
| 283 |
end |
| 284 |
return true |
| 285 |
end |
| 286 |
|
| 287 |
def skip_line |
| 288 |
while not @buffer.empty? |
| 289 |
token, lexeme = pop() |
| 290 |
break if token == TOKEN_NEWLINE |
| 291 |
end |
| 292 |
end |
| 293 |
end |
| 294 |
|
| 295 |
### PARSER ### |
| 296 |
|
| 297 |
NODE_TEXT = 1 |
| 298 |
NODE_STRING = 2 |
| 299 |
NODE_VARIABLE = 3 |
| 300 |
NODE_INDEXED = 4 |
| 301 |
NODE_ASSIGNMENT = 5 |
| 302 |
NODE_FUNCTION = 6 |
| 303 |
NODE_IF = 7 |
| 304 |
NODE_CALC = 8 |
| 305 |
NODE_AND_EXPR = 9 |
| 306 |
NODE_OR_EXPR = 10 |
| 307 |
NODE_COMP_EXPR = 11 |
| 308 |
NODE_ADD_EXPR = 12 |
| 309 |
NODE_MUL_EXPR = 13 |
| 310 |
NODE_POW_EXPR = 14 |
| 311 |
|
| 312 |
|
| 313 |
class Parser |
| 314 |
|
| 315 |
def initialize(charset) |
| 316 |
@lexer = Lexer.new(:charset => charset) |
| 317 |
@charset = charset |
| 318 |
end |
| 319 |
|
| 320 |
def read(f, path: nil) |
| 321 |
@lexer.read(f, :path => path) |
| 322 |
@path = path |
| 323 |
end |
| 324 |
|
| 325 |
def get_dict |
| 326 |
common = nil |
| 327 |
groups = [] |
| 328 |
# skip junk tokens at the beginning of the file |
| 329 |
while true |
| 330 |
if @lexer.skip_space(:accept_eof => true) |
| 331 |
return common, groups |
| 332 |
end |
| 333 |
token, lexeme = @lexer.look_ahead() |
| 334 |
if token == TOKEN_DIRECTIVE and lexeme == '#_Common' or \ |
| 335 |
token == TOKEN_DOLLAR |
| 336 |
break |
| 337 |
end |
| 338 |
@lexer.skip_line() |
| 339 |
end |
| 340 |
token, lexeme = @lexer.look_ahead() |
| 341 |
if token == TOKEN_DIRECTIVE and lexeme == '#_Common' |
| 342 |
common = get_common() |
| 343 |
if @lexer.skip_space(:accept_eof => true) |
| 344 |
return common, groups |
| 345 |
end |
| 346 |
end |
| 347 |
while true |
| 348 |
groups << get_group() |
| 349 |
if @lexer.skip_space(:accept_eof => true) |
| 350 |
return common, groups |
| 351 |
end |
| 352 |
end |
| 353 |
return nil, [] |
| 354 |
end |
| 355 |
|
| 356 |
def get_common |
| 357 |
@lexer.pop() |
| 358 |
token, lexeme = @lexer.look_ahead() |
| 359 |
if token == TOKEN_WHITESPACE |
| 360 |
@lexer.pop() |
| 361 |
end |
| 362 |
@lexer.pop_check(TOKEN_NEWLINE) |
| 363 |
condition = get_brace_expr() |
| 364 |
@lexer.pop_check(TOKEN_NEWLINE) |
| 365 |
return condition |
| 366 |
end |
| 367 |
|
| 368 |
def get_group |
| 369 |
# get group name |
| 370 |
buf = [] |
| 371 |
@lexer.pop_check(TOKEN_DOLLAR) |
| 372 |
while true |
| 373 |
token, lexeme = @lexer.look_ahead() |
| 374 |
##if token == TOKEN_TEXT or \ |
| 375 |
## token == TOKEN_OPERATOR and \ |
| 376 |
## ['+', '-', '*', '/', '%'].include?(lexeme) |
| 377 |
## XXX |
| 378 |
unless [TOKEN_NEWLINE, TOKEN_COMMA, TOKEN_SEMICOLON].include?(token) |
| 379 |
token, lexeme = @lexer.pop() |
| 380 |
buf << unescape(lexeme) |
| 381 |
else |
| 382 |
break |
| 383 |
end |
| 384 |
end |
| 385 |
if buf.empty? |
| 386 |
Misaka.syntax_error('null identifier', |
| 387 |
:path => @path, |
| 388 |
:position => @lexer.get_position()) |
| 389 |
end |
| 390 |
name = ['$', buf.join('')].join('') |
| 391 |
# get group parameters |
| 392 |
parameters = [] |
| 393 |
while true |
| 394 |
token, lexeme = @lexer.look_ahead() |
| 395 |
if token == TOKEN_WHITESPACE |
| 396 |
@lexer.pop() |
| 397 |
end |
| 398 |
token, lexeme = @lexer.look_ahead() |
| 399 |
if token == TOKEN_NEWLINE |
| 400 |
break |
| 401 |
elsif [TOKEN_COMMA, TOKEN_SEMICOLON].include?(token) |
| 402 |
@lexer.pop() |
| 403 |
else |
| 404 |
Misaka.syntax_error('expected a delimiter', |
| 405 |
:path => @path, |
| 406 |
:position => @lexer.get_position()) |
| 407 |
end |
| 408 |
token, lexeme = @lexer.look_ahead() |
| 409 |
if token == TOKEN_WHITESPACE |
| 410 |
@lexer.pop() |
| 411 |
end |
| 412 |
token, lexeme = @lexer.look_ahead() |
| 413 |
break if token == TOKEN_NEWLINE |
| 414 |
token, lexeme = @lexer.look_ahead() |
| 415 |
if token == TOKEN_OPEN_BRACE |
| 416 |
parameters << get_brace_expr() |
| 417 |
elsif token == TOKEN_TEXT |
| 418 |
token, lexeme = @lexer.pop() |
| 419 |
parameters << [NODE_TEXT, unescape(lexeme)] |
| 420 |
else |
| 421 |
Misaka.syntax_error('expected a parameter or brace expression', |
| 422 |
:path => @path, |
| 423 |
:position => @lexer.get_position()) |
| 424 |
end |
| 425 |
end |
| 426 |
# get sentences |
| 427 |
@lexer.pop_check(TOKEN_NEWLINE) |
| 428 |
sentences = [] |
| 429 |
while true |
| 430 |
break if @lexer.skip_space(:accept_eof => true) |
| 431 |
token, lexeme = @lexer.look_ahead() |
| 432 |
break if token == TOKEN_DOLLAR |
| 433 |
sentence = get_sentence() |
| 434 |
next if sentence.nil? |
| 435 |
sentences << sentence |
| 436 |
end |
| 437 |
return [name, parameters, sentences] |
| 438 |
end |
| 439 |
|
| 440 |
def get_sentence |
| 441 |
token, lexeme = @lexer.look_ahead() |
| 442 |
if token == TOKEN_OPEN_BRACE |
| 443 |
token, lexeme = @lexer.look_ahead(:index => 1) |
| 444 |
if token == TOKEN_NEWLINE |
| 445 |
@lexer.pop_check(TOKEN_OPEN_BRACE) |
| 446 |
token, lexeme = @lexer.look_ahead() |
| 447 |
if token == TOKEN_WHITESPACE |
| 448 |
@lexer.pop() |
| 449 |
end |
| 450 |
@lexer.pop_check(TOKEN_NEWLINE) |
| 451 |
line = [] |
| 452 |
while true |
| 453 |
@lexer.skip_space() |
| 454 |
token, lexeme = @lexer.look_ahead() |
| 455 |
break if token == TOKEN_CLOSE_BRACE |
| 456 |
for node in get_line() |
| 457 |
line << node |
| 458 |
end |
| 459 |
@lexer.pop_check(TOKEN_NEWLINE) |
| 460 |
end |
| 461 |
@lexer.pop_check(TOKEN_CLOSE_BRACE) |
| 462 |
token, lexeme = @lexer.look_ahead() |
| 463 |
if token == TOKEN_WHITESPACE |
| 464 |
@lexer.pop() |
| 465 |
end |
| 466 |
else |
| 467 |
begin |
| 468 |
line = get_line() # beginning with brace expression |
| 469 |
rescue # except MisakaError as error: |
| 470 |
Logging::Logging.debug(error) |
| 471 |
@lexer.skip_line() |
| 472 |
return nil |
| 473 |
end |
| 474 |
end |
| 475 |
else |
| 476 |
begin |
| 477 |
line = get_line() |
| 478 |
rescue => error # except MisakaError as error: |
| 479 |
Logging::Logging.debug(error) |
| 480 |
@lexer.skip_line() |
| 481 |
return nil |
| 482 |
end |
| 483 |
end |
| 484 |
@lexer.pop_check(TOKEN_NEWLINE) |
| 485 |
return line |
| 486 |
end |
| 487 |
|
| 488 |
def is_whitespace(node) |
| 489 |
return (node[0] == NODE_TEXT and node[1].strip().empty?) |
| 490 |
end |
| 491 |
|
| 492 |
def unescape(text) |
| 493 |
text = text.gsub('\\,', ',') |
| 494 |
text = text.gsub('\\"', '"') |
| 495 |
return text |
| 496 |
end |
| 497 |
|
| 498 |
def get_word |
| 499 |
buf = [] |
| 500 |
while true |
| 501 |
token, lexeme = @lexer.look_ahead() |
| 502 |
if token == TOKEN_TEXT |
| 503 |
token, lexeme = @lexer.pop() |
| 504 |
if unescape(lexeme).start_with?('"') and \ |
| 505 |
unescape(lexeme).end_with?('"') |
| 506 |
buf << [NODE_STRING, unescape(lexeme)[1..-2]] |
| 507 |
else |
| 508 |
buf << [NODE_TEXT, unescape(lexeme)] |
| 509 |
end |
| 510 |
elsif [TOKEN_WHITESPACE, TOKEN_DOLLAR].include?(token) |
| 511 |
token, lexeme = @lexer.pop() |
| 512 |
buf << [NODE_TEXT, lexeme] |
| 513 |
elsif token == TOKEN_OPEN_BRACE |
| 514 |
buf << get_brace_expr() |
| 515 |
else |
| 516 |
break |
| 517 |
end |
| 518 |
end |
| 519 |
# strip whitespace at the beginning and/or end of line |
| 520 |
if not buf.empty? and is_whitespace(buf[0]) |
| 521 |
buf.delete_at(0) |
| 522 |
end |
| 523 |
if not buf.empty? and is_whitespace(buf[-1]) |
| 524 |
buf.delete_at(-1) |
| 525 |
end |
| 526 |
return buf |
| 527 |
end |
| 528 |
|
| 529 |
def get_line |
| 530 |
buf = [] |
| 531 |
while true |
| 532 |
token, lexeme = @lexer.look_ahead() |
| 533 |
if [TOKEN_NEWLINE, TOKEN_CLOSE_BRACE].include?(token) |
| 534 |
break |
| 535 |
elsif token == TOKEN_TEXT |
| 536 |
token, lexeme = @lexer.pop() |
| 537 |
buf << [NODE_TEXT, unescape(lexeme)] |
| 538 |
elsif [TOKEN_WHITESPACE, TOKEN_DOLLAR, |
| 539 |
TOKEN_OPEN_PAREN, TOKEN_CLOSE_PAREN, |
| 540 |
TOKEN_OPEN_BRACKET, TOKEN_CLOSE_BRACKET, |
| 541 |
TOKEN_OPERATOR, TOKEN_COMMA, TOKEN_SEMICOLON, |
| 542 |
TOKEN_DIRECTIVE].include?(token) |
| 543 |
token, lexeme = @lexer.pop() |
| 544 |
buf << [NODE_TEXT, lexeme] |
| 545 |
elsif token == TOKEN_OPEN_BRACE |
| 546 |
buf << get_brace_expr() |
| 547 |
else |
| 548 |
fail RuntimeError.new('should not reach here') |
| 549 |
end |
| 550 |
end |
| 551 |
# strip whitespace at the beginning and/or end of line |
| 552 |
if not buf.empty? and is_whitespace(buf[0]) |
| 553 |
buf.delete_at(0) |
| 554 |
end |
| 555 |
if not buf.empty? and is_whitespace(buf[-1]) |
| 556 |
buf.delete_at(-1) |
| 557 |
end |
| 558 |
return buf |
| 559 |
end |
| 560 |
|
| 561 |
def get_brace_expr |
| 562 |
@lexer.pop_check(TOKEN_OPEN_BRACE) |
| 563 |
@lexer.skip_space() |
| 564 |
@lexer.pop_check(TOKEN_DOLLAR) |
| 565 |
@lexer.skip_space() |
| 566 |
# get identifier (function or variable) |
| 567 |
nodelist = [[NODE_TEXT, '$']] |
| 568 |
while true |
| 569 |
token, lexeme = @lexer.look_ahead() |
| 570 |
if token == TOKEN_TEXT or \ |
| 571 |
(token == TOKEN_OPERATOR and ['+', '-', '*', '/', '%'].include?(lexeme)) |
| 572 |
token, lexeme = @lexer.pop() |
| 573 |
nodelist << [NODE_TEXT, unescape(lexeme)] |
| 574 |
elsif token == TOKEN_OPEN_BRACE |
| 575 |
nodelist << get_brace_expr() |
| 576 |
else |
| 577 |
break |
| 578 |
end |
| 579 |
end |
| 580 |
if nodelist.length == 1 |
| 581 |
Misaka.syntax_error('null identifier', |
| 582 |
:path => @path, |
| 583 |
:position => @lexer.get_position()) |
| 584 |
end |
| 585 |
@lexer.skip_space() |
| 586 |
token, lexeme = @lexer.look_ahead() |
| 587 |
if token == TOKEN_OPEN_PAREN |
| 588 |
# function |
| 589 |
name = nodelist.map {|node| node[1] }.join('') |
| 590 |
if name == '$if' |
| 591 |
cond_expr = get_cond_expr() |
| 592 |
then_clause = [[NODE_TEXT, 'true']] |
| 593 |
else_clause = [[NODE_TEXT, 'false']] |
| 594 |
@lexer.skip_space() |
| 595 |
token, lexeme = @lexer.look_ahead() |
| 596 |
if token == TOKEN_OPEN_BRACE |
| 597 |
@lexer.pop_check(TOKEN_OPEN_BRACE) |
| 598 |
@lexer.skip_space() |
| 599 |
then_clause = [] |
| 600 |
while true |
| 601 |
line = get_line() |
| 602 |
for node in line |
| 603 |
then_clause << node |
| 604 |
end |
| 605 |
token, lexem = @lexer.pop() |
| 606 |
break if token == TOKEN_CLOSE_BRACE |
| 607 |
fail "assert" unless token == TOKEN_NEWLINE |
| 608 |
end |
| 609 |
@lexer.skip_space() |
| 610 |
token, lexeme = @lexer.look_ahead() |
| 611 |
if token == TOKEN_TEXT and lexeme == 'else' |
| 612 |
@lexer.pop() |
| 613 |
@lexer.skip_space() |
| 614 |
@lexer.pop_check(TOKEN_OPEN_BRACE) |
| 615 |
@lexer.skip_space() |
| 616 |
else_clause = [] |
| 617 |
while true |
| 618 |
line = get_line() |
| 619 |
for node in line |
| 620 |
else_clause << node |
| 621 |
end |
| 622 |
token, lexem = @lexer.pop() |
| 623 |
break if token == TOKEN_CLOSE_BRACE |
| 624 |
fail "assert" unless token == TOKEN_NEWLINE |
| 625 |
end |
| 626 |
elsif token == TOKEN_OPEN_BRACE ## XXX |
| 627 |
@lexer.pop_check(TOKEN_OPEN_BRACE) |
| 628 |
@lexer.skip_space() |
| 629 |
else_clause = [] |
| 630 |
while true |
| 631 |
line = get_line() |
| 632 |
for node in line |
| 633 |
else_clause << node |
| 634 |
end |
| 635 |
token, lexem = @lexer.pop() |
| 636 |
break if token == TOKEN_CLOSE_BRACE |
| 637 |
fail "assert" unless token == TOKEN_NEWLINE |
| 638 |
end |
| 639 |
else |
| 640 |
else_clause = [[NODE_TEXT, '']] |
| 641 |
end |
| 642 |
end |
| 643 |
node = [NODE_IF, cond_expr, then_clause, else_clause] |
| 644 |
elsif name == '$calc' |
| 645 |
node = [NODE_CALC, get_add_expr()] |
| 646 |
else |
| 647 |
@lexer.pop_check(TOKEN_OPEN_PAREN) |
| 648 |
@lexer.skip_space() |
| 649 |
args = [] |
| 650 |
token, lexeme = @lexer.look_ahead() |
| 651 |
if token != TOKEN_CLOSE_PAREN |
| 652 |
while true |
| 653 |
args << get_argument() |
| 654 |
@lexer.skip_space() |
| 655 |
token, lexeme = @lexer.look_ahead() |
| 656 |
break if token != TOKEN_COMMA |
| 657 |
@lexer.pop() |
| 658 |
@lexer.skip_space() |
| 659 |
end |
| 660 |
end |
| 661 |
@lexer.pop_check(TOKEN_CLOSE_PAREN) |
| 662 |
node = [NODE_FUNCTION, name, args] |
| 663 |
end |
| 664 |
else |
| 665 |
# variable |
| 666 |
token, lexeme = @lexer.look_ahead() |
| 667 |
if token == TOKEN_OPERATOR |
| 668 |
operator = @lexer.pop_check(TOKEN_OPERATOR) |
| 669 |
case operator |
| 670 |
when '=', '+=', '-=', '*=', '/=' |
| 671 |
@lexer.skip_space() |
| 672 |
value = get_add_expr() |
| 673 |
when '++' |
| 674 |
operator = '+=' |
| 675 |
value = [[NODE_TEXT, '1']] |
| 676 |
when '--' |
| 677 |
operator = '-=' |
| 678 |
value = [[NODE_TEXT, '1']] |
| 679 |
else |
| 680 |
Misaka.syntax_error(['bad operator ', operator].join(''), |
| 681 |
:path => @path, |
| 682 |
:position => @lexer.get_position()) |
| 683 |
end |
| 684 |
node = [NODE_ASSIGNMENT, nodelist, operator, value] |
| 685 |
elsif token == TOKEN_OPEN_BRACKET |
| 686 |
@lexer.pop_check(TOKEN_OPEN_BRACKET) |
| 687 |
@lexer.skip_space() |
| 688 |
index = get_word() |
| 689 |
@lexer.skip_space() |
| 690 |
@lexer.pop_check(TOKEN_CLOSE_BRACKET) |
| 691 |
node = [NODE_INDEXED, nodelist, index] |
| 692 |
else |
| 693 |
node = [NODE_VARIABLE, nodelist] |
| 694 |
end |
| 695 |
end |
| 696 |
@lexer.skip_space() |
| 697 |
@lexer.pop_check(TOKEN_CLOSE_BRACE) |
| 698 |
return node |
| 699 |
end |
| 700 |
|
| 701 |
def get_argument |
| 702 |
buf = [] |
| 703 |
while true |
| 704 |
token, lexeme = @lexer.look_ahead() |
| 705 |
case token |
| 706 |
when TOKEN_TEXT |
| 707 |
token, lexeme = @lexer.pop() |
| 708 |
if unescape(lexeme).start_with?('"') and \ |
| 709 |
unescape(lexeme).end_with?('"') |
| 710 |
buf << [NODE_STRING, unescape(lexeme)[1..-2]] |
| 711 |
else |
| 712 |
buf << [NODE_TEXT, unescape(lexeme)] |
| 713 |
end |
| 714 |
when TOKEN_WHITESPACE, TOKEN_DOLLAR, |
| 715 |
TOKEN_OPEN_BRACKET, TOKEN_CLOSE_BRACKET, |
| 716 |
TOKEN_OPERATOR, TOKEN_SEMICOLON |
| 717 |
token, lexeme = @lexer.pop() |
| 718 |
buf << [NODE_TEXT, lexeme] |
| 719 |
when TOKEN_OPEN_BRACE |
| 720 |
buf << get_brace_expr() |
| 721 |
when TOKEN_NEWLINE |
| 722 |
@lexer.skip_space() |
| 723 |
else |
| 724 |
break |
| 725 |
end |
| 726 |
end |
| 727 |
# strip whitespace at the beginning and/or end of line |
| 728 |
if not buf.empty? and is_whitespace(buf[0]) |
| 729 |
buf.delete_at(0) |
| 730 |
end |
| 731 |
if not buf.empty? and is_whitespace(buf[-1]) |
| 732 |
buf.delete_at(-1) |
| 733 |
end |
| 734 |
return buf |
| 735 |
end |
| 736 |
|
| 737 |
def get_cond_expr |
| 738 |
@lexer.pop_check(TOKEN_OPEN_PAREN) |
| 739 |
@lexer.skip_space() |
| 740 |
or_expr = get_or_expr() |
| 741 |
@lexer.skip_space() |
| 742 |
@lexer.pop_check(TOKEN_CLOSE_PAREN) |
| 743 |
return or_expr |
| 744 |
end |
| 745 |
|
| 746 |
def get_or_expr |
| 747 |
buf = [NODE_OR_EXPR] |
| 748 |
buf << get_and_expr() |
| 749 |
while true |
| 750 |
@lexer.skip_space() |
| 751 |
token, lexeme = @lexer.look_ahead() |
| 752 |
break unless token == TOKEN_OPERATOR and lexeme == '||' |
| 753 |
@lexer.pop() |
| 754 |
@lexer.skip_space() |
| 755 |
buf << get_and_expr() |
| 756 |
end |
| 757 |
if buf.length > 2 |
| 758 |
return buf |
| 759 |
else |
| 760 |
return buf[1] |
| 761 |
end |
| 762 |
end |
| 763 |
|
| 764 |
def get_and_expr |
| 765 |
buf = [NODE_AND_EXPR] |
| 766 |
buf << get_sub_expr() |
| 767 |
while true |
| 768 |
@lexer.skip_space() |
| 769 |
token, lexeme = @lexer.look_ahead() |
| 770 |
break unless token == TOKEN_OPERATOR and lexeme == '&&' |
| 771 |
@lexer.pop() |
| 772 |
@lexer.skip_space() |
| 773 |
buf << get_sub_expr() |
| 774 |
end |
| 775 |
if buf.length > 2 |
| 776 |
return buf |
| 777 |
else |
| 778 |
return buf[1] |
| 779 |
end |
| 780 |
end |
| 781 |
|
| 782 |
def get_sub_expr |
| 783 |
token, lexeme = @lexer.look_ahead() |
| 784 |
if token == TOKEN_OPEN_PAREN |
| 785 |
return get_cond_expr() |
| 786 |
else |
| 787 |
return get_comp_expr() |
| 788 |
end |
| 789 |
end |
| 790 |
|
| 791 |
def get_comp_expr |
| 792 |
buf = [NODE_COMP_EXPR] |
| 793 |
buf << get_add_expr() |
| 794 |
@lexer.skip_space() |
| 795 |
token, lexeme = @lexer.look_ahead() |
| 796 |
if token == TOKEN_OPERATOR and \ |
| 797 |
['==', '!=', '<', '<=', '=<', '>', '>=', '=>'].include?(lexeme) |
| 798 |
if lexeme == '=<' |
| 799 |
lexeme = '<=' |
| 800 |
elsif lexeme == '=>' |
| 801 |
lexeme = '>=' |
| 802 |
end |
| 803 |
buf << lexeme |
| 804 |
@lexer.pop() |
| 805 |
@lexer.skip_space() |
| 806 |
buf << get_add_expr() |
| 807 |
end |
| 808 |
if buf.length == 2 |
| 809 |
buf << '==' |
| 810 |
buf << [[NODE_TEXT, 'true']] |
| 811 |
end |
| 812 |
return buf |
| 813 |
end |
| 814 |
|
| 815 |
def get_add_expr |
| 816 |
buf = [NODE_ADD_EXPR] |
| 817 |
buf << get_mul_expr() |
| 818 |
while true |
| 819 |
@lexer.skip_space() |
| 820 |
token, lexeme = @lexer.look_ahead() |
| 821 |
break unless token == TOKEN_OPERATOR and ['+', '-'].include?(lexeme) |
| 822 |
buf << lexeme |
| 823 |
@lexer.pop() |
| 824 |
@lexer.skip_space() |
| 825 |
buf << get_mul_expr() |
| 826 |
end |
| 827 |
if buf.length > 2 |
| 828 |
return [buf] |
| 829 |
else |
| 830 |
return buf[1] |
| 831 |
end |
| 832 |
end |
| 833 |
|
| 834 |
def get_mul_expr |
| 835 |
buf = [NODE_MUL_EXPR] |
| 836 |
buf << get_pow_expr() |
| 837 |
while true |
| 838 |
@lexer.skip_space() |
| 839 |
token, lexeme = @lexer.look_ahead() |
| 840 |
break unless token == TOKEN_OPERATOR and ['*', '/', '%'].include?(lexeme) |
| 841 |
buf << lexeme |
| 842 |
@lexer.pop() |
| 843 |
@lexer.skip_space() |
| 844 |
buf << get_pow_expr() |
| 845 |
end |
| 846 |
if buf.length > 2 |
| 847 |
return [buf] |
| 848 |
else |
| 849 |
return buf[1] |
| 850 |
end |
| 851 |
end |
| 852 |
|
| 853 |
def get_pow_expr |
| 854 |
buf = [NODE_POW_EXPR] |
| 855 |
buf << get_unary_expr() |
| 856 |
while true |
| 857 |
@lexer.skip_space() |
| 858 |
token, lexeme = @lexer.look_ahead() |
| 859 |
break unless token == TOKEN_OPERATOR and lexeme == '^' |
| 860 |
@lexer.pop() |
| 861 |
@lexer.skip_space() |
| 862 |
buf << get_unary_expr() |
| 863 |
end |
| 864 |
if buf.length > 2 |
| 865 |
return [buf] |
| 866 |
else |
| 867 |
return buf[1] |
| 868 |
end |
| 869 |
end |
| 870 |
|
| 871 |
def get_unary_expr |
| 872 |
token, lexeme = @lexer.look_ahead() |
| 873 |
if token == TOKEN_OPERATOR and ['+', '-'].include?(lexeme) |
| 874 |
buf = [NODE_ADD_EXPR, [[NODE_TEXT, '0']], lexeme] |
| 875 |
@lexer.pop() |
| 876 |
@lexer.skip_space() |
| 877 |
buf << get_unary_expr() |
| 878 |
return [buf] |
| 879 |
else |
| 880 |
return get_factor() |
| 881 |
end |
| 882 |
end |
| 883 |
|
| 884 |
def get_factor |
| 885 |
token, lexeme = @lexer.look_ahead() |
| 886 |
if token == TOKEN_OPEN_PAREN |
| 887 |
@lexer.pop_check(TOKEN_OPEN_PAREN) |
| 888 |
@lexer.skip_space() |
| 889 |
add_expr = get_add_expr() |
| 890 |
@lexer.skip_space() |
| 891 |
@lexer.pop_check(TOKEN_CLOSE_PAREN) |
| 892 |
return add_expr |
| 893 |
else |
| 894 |
return get_word() |
| 895 |
end |
| 896 |
end |
| 897 |
|
| 898 |
# for debug |
| 899 |
def dump_list(nodelist, depth: 0) |
| 900 |
for node in nodelist |
| 901 |
dump_node(node, depth) |
| 902 |
end |
| 903 |
end |
| 904 |
|
| 905 |
def dump_node(node, depth) |
| 906 |
indent = ' ' * depth |
| 907 |
case node[0] |
| 908 |
when NODE_TEXT |
| 909 |
print([indent, 'TEXT'].join(''), " ", \ |
| 910 |
['"', node[1], '"'].join(''), "\n") |
| 911 |
when NODE_STRING |
| 912 |
print([indent, 'STRING'].join(''), " ", \ |
| 913 |
['"', node[1], '"'].join(''), "\n") |
| 914 |
when NODE_VARIABLE |
| 915 |
print([indent, 'VARIABLE'].join(''), "\n") |
| 916 |
dump_list(node[1], :depth => depth + 1) |
| 917 |
when NODE_INDEXED |
| 918 |
print([indent, 'INDEXED'].join(''), "\n") |
| 919 |
dump_list(node[1], :depth => depth + 1) |
| 920 |
print([indent, 'index'].join(''), "\n") |
| 921 |
dump_list(node[2], :depth => depth + 1) |
| 922 |
when NODE_ASSIGNMENT |
| 923 |
print([indent, 'ASSIGNMENT'].join(''), "\n") |
| 924 |
dump_list(node[1], :depth => depth + 1) |
| 925 |
print([indent, 'op'].join(''), " ", node[2], "\n") |
| 926 |
dump_list(node[3], :depth => depth + 1) |
| 927 |
when NODE_FUNCTION |
| 928 |
print([indent, 'FUNCTION'].join(''), " ", node[1], "\n") |
| 929 |
for i in 0..node[2].length-1 |
| 930 |
print([indent, 'args[' + i.to_s + ']'].join(''), "\n") |
| 931 |
dump_list(node[2][i], :depth => depth + 1) |
| 932 |
end |
| 933 |
when NODE_IF |
| 934 |
print([indent, 'IF'].join(''), "\n") |
| 935 |
dump_node(node[1], depth + 1) |
| 936 |
print([indent, 'then'].join(''), "\n") |
| 937 |
dump_list(node[2], :depth => depth + 1) |
| 938 |
print([indent, 'else'].join(''), "\n") |
| 939 |
dump_list(node[3], :depth => depth + 1) |
| 940 |
when NODE_CALC |
| 941 |
print([indent, 'CALC'].join(''), "\n") |
| 942 |
dump_list(node[1], :depth => depth + 1) |
| 943 |
when NODE_AND_EXPR |
| 944 |
print([indent, 'AND_EXPR'].join(''), "\n") |
| 945 |
for i in 1..node.length-1 |
| 946 |
dump_node(node[i], depth + 1) |
| 947 |
end |
| 948 |
when NODE_OR_EXPR |
| 949 |
print([indent, 'OR_EXPR'].join(''), "\n") |
| 950 |
for i in 1..node.length-1 |
| 951 |
dump_node(node[i], depth + 1) |
| 952 |
end |
| 953 |
when NODE_COMP_EXPR |
| 954 |
print([indent, 'COMP_EXPR'].join(''), "\n") |
| 955 |
dump_list(node[1], :depth => depth + 1) |
| 956 |
print([indent, 'op'].join(''), " ", node[2], "\n") |
| 957 |
dump_list(node[3], :depth => depth + 1) |
| 958 |
when NODE_ADD_EXPR |
| 959 |
print([indent, 'ADD_EXPR'].join(''), "\n") |
| 960 |
dump_list(node[1], :depth => depth + 1) |
| 961 |
for i in 2.step(node.length-1, 2) |
| 962 |
print([indent, 'op'].join(''), " ", node[i], "\n") |
| 963 |
dump_list(node[i + 1], :depth => depth + 1) |
| 964 |
end |
| 965 |
when NODE_MUL_EXPR |
| 966 |
print([indent, 'MUL_EXPR'].join(''), "\n") |
| 967 |
dump_list(node[1], :depth => depth + 1) |
| 968 |
for i in 2.step(node.length-1, 2) |
| 969 |
print([indent, 'op'].join(''), " ", node[i], "\n") |
| 970 |
dump_list(node[i + 1], :depth => depth + 1) |
| 971 |
end |
| 972 |
when NODE_POW_EXPR |
| 973 |
print([indent, 'POW_EXPR'].join(''), "\n") |
| 974 |
dump_list(node[1], :depth => depth + 1) |
| 975 |
for i in 2..node.length-1 |
| 976 |
print([indent, 'op ^'].join(''), "\n") |
| 977 |
dump_list(node[i], :depth => depth + 1) |
| 978 |
end |
| 979 |
else |
| 980 |
print(node, "\n") |
| 981 |
fail RuntimeError.new('should not reach here') |
| 982 |
end |
| 983 |
end |
| 984 |
end |
| 985 |
|
| 986 |
# <<< syntax >>> |
| 987 |
# dict := sp ( common sp )? ( group sp )* |
| 988 |
# sp := ( NEWLINE | WHITESPACE )* |
| 989 |
# common := "#_Common" NEWLINE brace_expr NEWLINE |
| 990 |
# group := group_name ( delimiter ( STRING | brace_expr ) )* |
| 991 |
# ( delimiter )? NEWLINE ( sentence )* |
| 992 |
# group_name := DOLLAR ( TEXT )+ |
| 993 |
# delimiter := ( WHITESPACE )? ( COMMA | SEMICOLON ) ( WHITESPACE )? |
| 994 |
# sentence := line NEWLINE | |
| 995 |
# OPEN_BRACE NEWLINE ( line NEWLINE )* CLOSE_BRACE NEWLINE |
| 996 |
# word := ( TEXT | WHITESPACE | DOLLAR | brace_expr | string )* |
| 997 |
# line := ( TEXT | WHITESPACE | DOLLAR | brace_expr | QUOTE | |
| 998 |
# OPEN_PAREN | CLOSE_PAREN | OPEN_BRACKET | CLOSE_BRACKET | |
| 999 |
# OPERATOR | COMMA | SEMICOLON | DIRECTIVE )* |
| 1000 |
# string := QUOTE ( |
| 1001 |
# TEXT | WHITESPACE | DOLLAR | OPEN_BRACE | CLOSE_BRACE | |
| 1002 |
# OPEN_PAREN | CLOSE_PAREN | OPEN_BRACKET | CLOSE_BRACKET | |
| 1003 |
# OPERATOR | COMMA | SEMICOLON | DIRECTIVE )* |
| 1004 |
# QUOTE |
| 1005 |
# brace_expr := variable | indexed | assignment | increment | |
| 1006 |
# function | if | calc |
| 1007 |
# variable := OPEN_BRACE sp var_name sp CLOSE_BRACE |
| 1008 |
# indexed := OPEN_BRACE sp var_name sp |
| 1009 |
# OPEN_BRACKET sp word sp CLOSE_BRACKET sp CLOSE_BRACE |
| 1010 |
# var_name := DOLLAR ( TEXT | brace_expr )+ |
| 1011 |
# assignment := OPEN_BRACE sp var_name sp assign_ops sp add_expr sp CLOSE_BRACE |
| 1012 |
# assign_ops := "=" | "+=" | "-=" | "*=" | "/=" |
| 1013 |
# increment := OPEN_BRACE sp var_name sp inc_ops sp CLOSE_BRACE |
| 1014 |
# inc_ops := "+" "+" | "-" "-" |
| 1015 |
# function := OPEN_BRACE sp DOLLAR sp ( TEXT )+ sp OPEN_PAREN |
| 1016 |
# ( sp argument ( sp COMMA sp argument )* sp )? |
| 1017 |
# CLOSE_PAREN sp CLOSE_BRACE |
| 1018 |
# argument := ( TEXT | WHITESPACE | DOLLAR | brace_expr | string | |
| 1019 |
# OPEN_BRACKET | CLOSE_BRACKET | OPERATOR | SEMICOLON )* |
| 1020 |
# if := OPEN_BRACE sp DOLLAR sp "if" sp cond_expr |
| 1021 |
# ( sp OPEN_BRACE ( sp line )* sp CLOSE_BRACE ( sp "else" |
| 1022 |
# sp OPEN_BRACE ( sp line )* sp CLOSE_BRACE )? sp )? |
| 1023 |
# sp CLOSE_BRACE |
| 1024 |
# calc := OPEN_BRACE sp DOLLAR sp "calc" sp add_expr sp CLOSE_BRACE |
| 1025 |
# cond_expr := OPEN_PAREN sp or_expr sp CLOSE_PAREN |
| 1026 |
# or_expr := and_expr ( sp "||" sp and_expr )* |
| 1027 |
# and_expr := sub_expr ( sp "&&" sp sub_expr )* |
| 1028 |
# sub_expr := comp_expr | cond_expr |
| 1029 |
# comp_expr := add_expr ( sp comp_op sp add_expr )? |
| 1030 |
# comp_op := "==" | "!=" | "<" | "<=" | ">" | ">=" |
| 1031 |
# add_expr := mul_expr (sp add_op sp mul_expr)* |
| 1032 |
# add_op := "+" | "-" |
| 1033 |
# mul_expr := pow_expr (sp mul_op sp pow_expr)* |
| 1034 |
# mul_op := "*" | "/" | "%" |
| 1035 |
# pow_expr := unary_expr (sp pow_op sp unary_expr)* |
| 1036 |
# pow_op := "^" |
| 1037 |
# unary_expr := unary_op sp unary_expr | factor |
| 1038 |
# unary_op := "+" | "-" |
| 1039 |
# factor := OPEN_PAREN sp add_expr sp CLOSE_PAREN | word |
| 1040 |
|
| 1041 |
|
| 1042 |
### INTERPRETER ### |
| 1043 |
|
| 1044 |
class Group |
| 1045 |
|
| 1046 |
def initialize(misaka, name, item_list: nil) |
| 1047 |
@misaka = misaka |
| 1048 |
@name = name |
| 1049 |
if item_list.nil? ## FIXME |
| 1050 |
@list = [] |
| 1051 |
else |
| 1052 |
@list = item_list |
| 1053 |
end |
| 1054 |
end |
| 1055 |
|
| 1056 |
def __len__ |
| 1057 |
@list.length |
| 1058 |
end |
| 1059 |
|
| 1060 |
def __getitem__(index) |
| 1061 |
@list[index] |
| 1062 |
end |
| 1063 |
|
| 1064 |
def get |
| 1065 |
return nil if @list.empty? |
| 1066 |
return @list.sample |
| 1067 |
end |
| 1068 |
|
| 1069 |
def copy(name) |
| 1070 |
MisakaArray.new(@misaka, name, @list) |
| 1071 |
end |
| 1072 |
end |
| 1073 |
|
| 1074 |
|
| 1075 |
class NonOverlapGroup < Group |
| 1076 |
|
| 1077 |
def initialize(misaka, name, item_list: nil) |
| 1078 |
super(misaka, name, :item_list => item_list) |
| 1079 |
@indexes = [] |
| 1080 |
end |
| 1081 |
|
| 1082 |
def get |
| 1083 |
return nil if @list.empty? |
| 1084 |
if @indexes.empty? |
| 1085 |
@indexes = Array([email protected]) |
| 1086 |
end |
| 1087 |
i = Array([email protected]).sample |
| 1088 |
index = @indexes.delete_at(i) |
| 1089 |
return @list[index] |
| 1090 |
end |
| 1091 |
end |
| 1092 |
|
| 1093 |
|
| 1094 |
class SequentialGroup < Group |
| 1095 |
|
| 1096 |
def initialize(misaka, name, item_list: nil) |
| 1097 |
super(misaka, name, :item_list => item_list) |
| 1098 |
@indexes = [] |
| 1099 |
end |
| 1100 |
|
| 1101 |
def get |
| 1102 |
return nil if @list.empty? |
| 1103 |
if @indexes.empty? |
| 1104 |
@indexes = Array([email protected]) |
| 1105 |
end |
| 1106 |
index = @indexes.shift |
| 1107 |
return @list[index] |
| 1108 |
end |
| 1109 |
end |
| 1110 |
|
| 1111 |
|
| 1112 |
class MisakaArray < Group |
| 1113 |
|
| 1114 |
def append(s) |
| 1115 |
@list << [[NODE_TEXT, s.to_s]] |
| 1116 |
end |
| 1117 |
|
| 1118 |
def index(s) |
| 1119 |
for i in [email protected] |
| 1120 |
if s == @misaka.expand(@list[i]) |
| 1121 |
return i |
| 1122 |
end |
| 1123 |
end |
| 1124 |
return -1 |
| 1125 |
end |
| 1126 |
|
| 1127 |
def pop |
| 1128 |
return nil if @list.empty? |
| 1129 |
i = Array([email protected]).sample |
| 1130 |
item = @list.delete_at(i) |
| 1131 |
return item |
| 1132 |
end |
| 1133 |
|
| 1134 |
def popmatchl(s) |
| 1135 |
buf = [] |
| 1136 |
for i in [email protected] |
| 1137 |
t = @misaka.expand(@list[i]) |
| 1138 |
if t.start_with?(s) |
| 1139 |
buf << i |
| 1140 |
end |
| 1141 |
end |
| 1142 |
return nil if buf.empty? |
| 1143 |
i = buf.sample |
| 1144 |
item = @list.delete_at(i) |
| 1145 |
return item |
| 1146 |
end |
| 1147 |
end |
| 1148 |
|
| 1149 |
|
| 1150 |
TYPE_SCHOLAR = 1 |
| 1151 |
TYPE_ARRAY = 2 |
| 1152 |
|
| 1153 |
|
| 1154 |
class Shiori |
| 1155 |
|
| 1156 |
DBNAME = 'misaka.db' |
| 1157 |
|
| 1158 |
def initialize(dll_name) |
| 1159 |
@dll_name = dll_name |
| 1160 |
@charset = 'CP932' |
| 1161 |
end |
| 1162 |
|
| 1163 |
def use_saori(saori) |
| 1164 |
@saori = saori |
| 1165 |
end |
| 1166 |
|
| 1167 |
def find(top_dir, dll_name) |
| 1168 |
result = 0 |
| 1169 |
if not Misaka.list_dict(top_dir).empty? |
| 1170 |
result = 210 |
| 1171 |
elsif File.file?(File.join(top_dir, 'misaka.ini')) |
| 1172 |
result = 200 |
| 1173 |
end |
| 1174 |
return result |
| 1175 |
end |
| 1176 |
|
| 1177 |
def show_description |
| 1178 |
Logging::Logging.info( |
| 1179 |
"Shiori: MISAKA compatible module for ninix\n" \ |
| 1180 |
" Copyright (C) 2002 by Tamito KAJIYAMA\n" \ |
| 1181 |
" Copyright (C) 2002, 2003 by MATSUMURA Namihiko\n" \ |
| 1182 |
" Copyright (C) 2002-2019 by Shyouzou Sugitani") |
| 1183 |
end |
| 1184 |
|
| 1185 |
def reset |
| 1186 |
@dict = {} |
| 1187 |
@variable = {} |
| 1188 |
@constant = {} |
| 1189 |
@misaka_debug = 0 |
| 1190 |
@misaka_error = 0 |
| 1191 |
@reference = [nil, nil, nil, nil, nil, nil, nil, nil] |
| 1192 |
@mouse_move_count = {} |
| 1193 |
@random_talk = -1 |
| 1194 |
@otherghost = [] |
| 1195 |
@communicate = ['', ''] |
| 1196 |
reset_request() |
| 1197 |
end |
| 1198 |
|
| 1199 |
def load(dir: Dir::getwd) |
| 1200 |
@misaka_dir = dir |
| 1201 |
@dbpath = File.join(@misaka_dir, DBNAME) |
| 1202 |
@saori_library = SaoriLibrary.new(@saori, @misaka_dir) |
| 1203 |
reset() |
| 1204 |
begin |
| 1205 |
filelist, debug, error = Misaka.read_misaka_ini(@misaka_dir) |
| 1206 |
rescue #except IOError: |
| 1207 |
Logging::Logging.debug('cannot read misaka.ini') |
| 1208 |
return 0 |
| 1209 |
rescue #except MisakaError as error: |
| 1210 |
Logging::Logging.debug(error) |
| 1211 |
return 0 |
| 1212 |
end |
| 1213 |
@misaka_debug = debug |
| 1214 |
@misaka_error = error |
| 1215 |
global_variables = [] |
| 1216 |
global_constants = [] |
| 1217 |
# charset auto-detection |
| 1218 |
unless CharlockHolmes.nil? |
| 1219 |
detector = CharlockHolmes::EncodingDetector.new |
| 1220 |
for filename in filelist |
| 1221 |
path = File.join(@misaka_dir, filename) |
| 1222 |
begin |
| 1223 |
f = open(path, 'rb') |
| 1224 |
rescue #except IOError: |
| 1225 |
Logging::Logging.debug('cannot read ' + filename.to_s) |
| 1226 |
next |
| 1227 |
end |
| 1228 |
ext = File.extname(filename) |
| 1229 |
if ext == '.__1' |
| 1230 |
result = detector.detect(crypt(f.read())) |
| 1231 |
else |
| 1232 |
result = detector.detect(f.read()) |
| 1233 |
end |
| 1234 |
f.close() |
| 1235 |
if result[:confidence] > 98 and \ |
| 1236 |
result[:encoding] != 'ISO-8859-1' # XXX |
| 1237 |
@charset = result[:encoding] |
| 1238 |
if @charset == 'Shift_JIS' |
| 1239 |
@charset = 'CP932' # XXX |
| 1240 |
end |
| 1241 |
Logging::Logging.debug("CharlockHolmes(misaka.rb): '" + @charset.to_s + "'") |
| 1242 |
break |
| 1243 |
end |
| 1244 |
end |
| 1245 |
end |
| 1246 |
for filename in filelist |
| 1247 |
path = File.join(@misaka_dir, filename) |
| 1248 |
basename = File.basename(filename, ".*") |
| 1249 |
ext = File.extname(filename) |
| 1250 |
begin |
| 1251 |
if ext == '.__1' # should read lines as bytes |
| 1252 |
f = open(path, 'rb') |
| 1253 |
else |
| 1254 |
f = open(path, :encoding => @charset + ":utf-8") |
| 1255 |
end |
| 1256 |
rescue #except IOError: |
| 1257 |
Logging::Logging.debug('cannot read ' + filename.to_s) |
| 1258 |
next |
| 1259 |
end |
| 1260 |
if ext == '.__1' |
| 1261 |
f = StringIO.new(crypt(f.read())) |
| 1262 |
end |
| 1263 |
begin |
| 1264 |
variables, constants = read(f, :path => path) |
| 1265 |
rescue => error #except MisakaError as error: |
| 1266 |
Logging::Logging.debug(error) |
| 1267 |
next |
| 1268 |
end |
| 1269 |
global_variables |= variables |
| 1270 |
global_constants |= constants |
| 1271 |
end |
| 1272 |
eval_globals(global_variables, false) |
| 1273 |
load_database() |
| 1274 |
eval_globals(global_constants, true) |
| 1275 |
if @dict.include?('$_OnRandomTalk') |
| 1276 |
@random_talk = 0 |
| 1277 |
end |
| 1278 |
return 1 |
| 1279 |
end |
| 1280 |
|
| 1281 |
def crypt(data) |
| 1282 |
return data.chars.map {|c| (c.ord ^ 0xff).chr }.join('').force_encoding(@charset).encode("UTF-8", :invalid => :replace, :undef => :replace) |
| 1283 |
end |
| 1284 |
|
| 1285 |
def eval_globals(sentences, constant) |
| 1286 |
for sentence in sentences |
| 1287 |
for node in sentence |
| 1288 |
if node[0] == NODE_ASSIGNMENT |
| 1289 |
eval_assignment(node[1], node[2], node[3], :constant => constant) |
| 1290 |
elsif node[0] == NODE_FUNCTION |
| 1291 |
eval_function(node[1], node[2]) |
| 1292 |
end |
| 1293 |
end |
| 1294 |
end |
| 1295 |
end |
| 1296 |
|
| 1297 |
def read(f, path: nil) |
| 1298 |
parser = Parser.new(@charset) |
| 1299 |
parser.read(f, :path => path) |
| 1300 |
common, dic = parser.get_dict() |
| 1301 |
variables = [] |
| 1302 |
constants = [] |
| 1303 |
for name, parameters, sentences in dic |
| 1304 |
if sentences.nil? |
| 1305 |
next |
| 1306 |
elsif name == '$_Variable' |
| 1307 |
variables |= sentences |
| 1308 |
next |
| 1309 |
elsif name == '$_Constant' |
| 1310 |
constants |= sentences |
| 1311 |
next |
| 1312 |
end |
| 1313 |
_GroupClass = Group |
| 1314 |
conditions = [] |
| 1315 |
conditions << common unless common.nil? |
| 1316 |
for node in parameters |
| 1317 |
if node[0] == NODE_IF |
| 1318 |
conditions << node |
| 1319 |
elsif node[0] == NODE_TEXT and node[1] == 'nonoverlap' |
| 1320 |
_GroupClass = NonOverlapGroup |
| 1321 |
elsif node[0] == NODE_TEXT and node[1] == 'sequential' |
| 1322 |
_GroupClass = SequentialGroup |
| 1323 |
else |
| 1324 |
#pass # ignore unknown parameters |
| 1325 |
end |
| 1326 |
end |
| 1327 |
group = _GroupClass.new(self, name, :item_list => sentences) |
| 1328 |
if @dict.include?(name) |
| 1329 |
grouplist = @dict[name] |
| 1330 |
else |
| 1331 |
grouplist = @dict[name] = [] |
| 1332 |
end |
| 1333 |
grouplist << [group, conditions] |
| 1334 |
end |
| 1335 |
return variables, constants |
| 1336 |
end |
| 1337 |
|
| 1338 |
def strip_newline(line) |
| 1339 |
line.chomp |
| 1340 |
end |
| 1341 |
|
| 1342 |
def load_database |
| 1343 |
begin |
| 1344 |
open(@dbpath) do |f| |
| 1345 |
while true |
| 1346 |
begin |
| 1347 |
line = f.gets |
| 1348 |
rescue #except UnicodeError: |
| 1349 |
Logging::Logging.debug( |
| 1350 |
'misaka.rb: malformed database (ignored)') |
| 1351 |
break |
| 1352 |
end |
| 1353 |
break if line.nil? |
| 1354 |
header = strip_newline(line).split(nil, -1) |
| 1355 |
if header[0] == 'SCHOLAR' |
| 1356 |
name = header[1] |
| 1357 |
value = strip_newline(f.gets) |
| 1358 |
@variable[name] = [TYPE_SCHOLAR, value] |
| 1359 |
elsif header[0] == 'ARRAY' |
| 1360 |
begin |
| 1361 |
size = Integer(header[1]) |
| 1362 |
rescue #except ValueError: |
| 1363 |
Logging::Logging.debug( |
| 1364 |
'misaka.rb: malformed database (ignored)') |
| 1365 |
break |
| 1366 |
end |
| 1367 |
name = header[2] |
| 1368 |
array = MisakaArray.new(self, name) |
| 1369 |
for _ in 0..size-1 |
| 1370 |
value = strip_newline(f.gets) |
| 1371 |
array << value |
| 1372 |
end |
| 1373 |
@variable[name] = [TYPE_ARRAY, array] |
| 1374 |
else |
| 1375 |
fail RuntimeError.new('should not reach here') |
| 1376 |
end |
| 1377 |
end |
| 1378 |
end |
| 1379 |
rescue #except IOError: |
| 1380 |
return |
| 1381 |
end |
| 1382 |
end |
| 1383 |
|
| 1384 |
def save_database |
| 1385 |
begin |
| 1386 |
open(@dbpath, 'w') do |f| |
| 1387 |
for name in @variable.keys |
| 1388 |
value_type, value = @variable[name] |
| 1389 |
if value_type == TYPE_SCHOLAR |
| 1390 |
next if value == '' |
| 1391 |
f.write('SCHOLAR ' + name.to_s + "\n" + value.to_s + "\n") |
| 1392 |
elsif value_type == TYPE_ARRAY |
| 1393 |
f.write('ARRAY ' + value.length.to_s + ' ' + name.to_s + "\n") |
| 1394 |
for item in value |
| 1395 |
f.write(expand(item).to_s + "\n") |
| 1396 |
end |
| 1397 |
else |
| 1398 |
fail RuntimeError.new('should not reach here') |
| 1399 |
end |
| 1400 |
end |
| 1401 |
end |
| 1402 |
rescue #except IOError: |
| 1403 |
Logging::Logging.debug('misaka.rb: cannot write database (ignored)') |
| 1404 |
return |
| 1405 |
end |
| 1406 |
end |
| 1407 |
|
| 1408 |
def unload |
| 1409 |
save_database() |
| 1410 |
@saori_library.unload() |
| 1411 |
return 1 |
| 1412 |
end |
| 1413 |
|
| 1414 |
def reset_request |
| 1415 |
@req_command = '' |
| 1416 |
@req_protocol = '' |
| 1417 |
@req_key = [] |
| 1418 |
@req_header = {} |
| 1419 |
end |
| 1420 |
|
| 1421 |
def request(req_string) |
| 1422 |
header = req_string.split(/\r?\n/, 0) |
| 1423 |
line = header.shift |
| 1424 |
unless line.nil? |
| 1425 |
line = line.force_encoding(@charset).encode('utf-8', :invalid => :replace, :undef => :replace).strip() |
| 1426 |
req_list = line.split(nil, -1) |
| 1427 |
if req_list.length >= 2 |
| 1428 |
@req_command = req_list[0].strip() |
| 1429 |
@req_protocol = req_list[1].strip() |
| 1430 |
end |
| 1431 |
for line in header |
| 1432 |
line = line.force_encoding(@charset).encode('utf-8', :invalid => :replace, :undef => :replace).strip() |
| 1433 |
next if line.empty? |
| 1434 |
next unless line.include?(':') |
| 1435 |
key, value = line.split(':', 2) |
| 1436 |
key = key.strip() |
| 1437 |
begin |
| 1438 |
value = Integer(value.strip()) |
| 1439 |
rescue |
| 1440 |
value = value.strip() |
| 1441 |
end |
| 1442 |
@req_key << key |
| 1443 |
@req_header[key] = value |
| 1444 |
end |
| 1445 |
end |
| 1446 |
# FIXME |
| 1447 |
ref0 = get_ref(0) |
| 1448 |
ref1 = get_ref(1) |
| 1449 |
ref2 = get_ref(2) |
| 1450 |
ref3 = get_ref(3) |
| 1451 |
ref4 = get_ref(4) |
| 1452 |
ref5 = get_ref(5) |
| 1453 |
ref6 = get_ref(6) |
| 1454 |
ref7 = get_ref(7) |
| 1455 |
event = @req_header['ID'] |
| 1456 |
script = nil |
| 1457 |
if event == 'otherghostname' |
| 1458 |
n = 0 |
| 1459 |
refs = [] |
| 1460 |
while true |
| 1461 |
ref = get_ref(n) |
| 1462 |
unless ref.nil? |
| 1463 |
refs << ref |
| 1464 |
else |
| 1465 |
break |
| 1466 |
end |
| 1467 |
n += 1 |
| 1468 |
end |
| 1469 |
@otherghost = [] |
| 1470 |
for ref in refs |
| 1471 |
name, s0, s1 = ref.split(1.chr, 3) |
| 1472 |
@otherghost << [name, s0, s1] |
| 1473 |
end |
| 1474 |
end |
| 1475 |
if event == 'OnMouseMove' |
| 1476 |
key = [ref3, ref4] # side, part |
| 1477 |
if @mouse_move_count.include?(key) |
| 1478 |
count = @mouse_move_count[key] |
| 1479 |
else |
| 1480 |
count = 0 |
| 1481 |
end |
| 1482 |
@mouse_move_count[key] = count + 5 |
| 1483 |
end |
| 1484 |
if event == 'OnCommunicate' |
| 1485 |
@communicate = [ref0, ref1] |
| 1486 |
script = get('$_OnGhostCommunicateReceive', :default => nil) |
| 1487 |
elsif event == 'charset' |
| 1488 |
script = @charset |
| 1489 |
else |
| 1490 |
@reference = [ref0, ref1, ref2, ref3, ref4, ref5, ref6, ref7] |
| 1491 |
script = get(['$', event].join(''), :default => nil) |
| 1492 |
end |
| 1493 |
if event == 'OnSecondChange' |
| 1494 |
if @random_talk.zero? |
| 1495 |
reset_random_talk_interval() |
| 1496 |
elsif @random_talk > 0 |
| 1497 |
@random_talk -= 1 |
| 1498 |
if @random_talk.zero? |
| 1499 |
script = get('$_OnRandomTalk', :default => nil) |
| 1500 |
end |
| 1501 |
end |
| 1502 |
end |
| 1503 |
unless script.nil? |
| 1504 |
script = script.strip() |
| 1505 |
else |
| 1506 |
script = '' |
| 1507 |
end |
| 1508 |
reset_request() |
| 1509 |
response = ["SHIORI/3.0 200 OK\r\n", |
| 1510 |
"Sender: Misaka\r\n", |
| 1511 |
"Charset: ", |
| 1512 |
@charset.encode(@charset, :invalid => :replace, :undef => :replace), |
| 1513 |
"\r\n", |
| 1514 |
"Value: ", |
| 1515 |
script.encode(@charset, :invalid => :replace, :undef => :replace), |
| 1516 |
"\r\n"].join('') |
| 1517 |
to = eval_variable([[NODE_TEXT, '$to']]) |
| 1518 |
unless to.nil? |
| 1519 |
@variable.delete('$to') |
| 1520 |
response = [response, |
| 1521 |
"Reference0: ", |
| 1522 |
to.encode(@charset, :invalid => :replace, :undef => :replace), |
| 1523 |
"\r\n"].join('') |
| 1524 |
end |
| 1525 |
response = [response, "\r\n"].join('') |
| 1526 |
return response |
| 1527 |
end |
| 1528 |
|
| 1529 |
def get_ref(num) |
| 1530 |
key = ['Reference', num.to_s].join('') |
| 1531 |
return @req_header[key] |
| 1532 |
end |
| 1533 |
|
| 1534 |
# internal |
| 1535 |
def reset_random_talk_interval |
| 1536 |
interval = 0 |
| 1537 |
if @variable.include?('$_talkinterval') |
| 1538 |
value_type, value = @variable['$_talkinterval'] |
| 1539 |
else |
| 1540 |
value_type, value = TYPE_SCHOLAR, '' |
| 1541 |
end |
| 1542 |
if value_type == TYPE_SCHOLAR |
| 1543 |
begin |
| 1544 |
interval = [Integer(value), 0].max |
| 1545 |
rescue #except ValueError: |
| 1546 |
#pass |
| 1547 |
end |
| 1548 |
end |
| 1549 |
@random_talk = (interval * Array(5..15).sample / 10.0).to_i |
| 1550 |
end |
| 1551 |
|
| 1552 |
def get(name, default: '') |
| 1553 |
grouplist = @dict[name] |
| 1554 |
return default if grouplist.nil? |
| 1555 |
for group, conditions in grouplist |
| 1556 |
if eval_and_expr(conditions) == 'true' |
| 1557 |
return expand(group.get()) |
| 1558 |
end |
| 1559 |
end |
| 1560 |
return default |
| 1561 |
end |
| 1562 |
|
| 1563 |
def expand(nodelist) |
| 1564 |
return '' if nodelist.nil? |
| 1565 |
buf = [] |
| 1566 |
for node in nodelist |
| 1567 |
buf << eval_(node) |
| 1568 |
end |
| 1569 |
return buf.join('') |
| 1570 |
end |
| 1571 |
|
| 1572 |
def expand_args(nodelist) |
| 1573 |
tmp = expand(nodelist) |
| 1574 |
if tmp.start_with?('$') |
| 1575 |
return eval_variable(nodelist) |
| 1576 |
else |
| 1577 |
return tmp |
| 1578 |
end |
| 1579 |
end |
| 1580 |
|
| 1581 |
def eval_(node) |
| 1582 |
case node[0] |
| 1583 |
when NODE_TEXT |
| 1584 |
result = node[1] |
| 1585 |
when NODE_STRING |
| 1586 |
result = node[1] |
| 1587 |
when NODE_VARIABLE |
| 1588 |
result = eval_variable(node[1]) |
| 1589 |
when NODE_INDEXED |
| 1590 |
result = eval_indexed(node[1], node[2]) |
| 1591 |
when NODE_ASSIGNMENT |
| 1592 |
result = eval_assignment(node[1], node[2], node[3]) |
| 1593 |
when NODE_FUNCTION |
| 1594 |
result = eval_function(node[1], node[2]) |
| 1595 |
when NODE_IF |
| 1596 |
result = eval_if(node[1], node[2], node[3]) |
| 1597 |
when NODE_CALC |
| 1598 |
result = eval_calc(node[1]) |
| 1599 |
when NODE_COMP_EXPR |
| 1600 |
result = eval_comp_expr(node[1], node[2], node[3]) |
| 1601 |
when NODE_AND_EXPR |
| 1602 |
result = eval_and_expr(node[1..-1]) |
| 1603 |
when NODE_OR_EXPR |
| 1604 |
result = eval_or_expr(node[1..-1]) |
| 1605 |
when NODE_ADD_EXPR |
| 1606 |
result = eval_add_expr(node[1..-1]) |
| 1607 |
when NODE_MUL_EXPR |
| 1608 |
result = eval_mul_expr(node[1..-1]) |
| 1609 |
when NODE_POW_EXPR |
| 1610 |
result = eval_pow_expr(node[1..-1]) |
| 1611 |
else |
| 1612 |
##print(node) |
| 1613 |
fail RuntimeError.new('should not reach here') |
| 1614 |
end |
| 1615 |
return result |
| 1616 |
end |
| 1617 |
|
| 1618 |
SYSTEM_VARIABLES = [ |
| 1619 |
# current date and time |
| 1620 |
'$year', '$month', '$day', '$hour', '$minute', '$second', |
| 1621 |
# day of week (0 = Sunday) |
| 1622 |
'$dayofweek', |
| 1623 |
# ghost uptime |
| 1624 |
'$elapsedhour', '$elapsedminute', '$elapsedsecond', |
| 1625 |
# OS uptime |
| 1626 |
'$elapsedhouros', '$elapsedminuteos', '$elapsedsecondos', |
| 1627 |
# OS accumlative uptime |
| 1628 |
'$elapsedhourtotal', '$elapsedminutetotal', '$elapsedsecondtotal', |
| 1629 |
# system information |
| 1630 |
'$os.version', |
| 1631 |
'$os.name', |
| 1632 |
'$os.phisicalmemorysize', |
| 1633 |
'$os.freememorysize', |
| 1634 |
'$os.totalmemorysize', |
| 1635 |
'$cpu.vendorname', |
| 1636 |
'$cpu.name', |
| 1637 |
'$cpu.clockcycle', |
| 1638 |
# number of days since the last network update |
| 1639 |
'$daysfromlastupdate', |
| 1640 |
# number of days since the first boot |
| 1641 |
'$daysfromfirstboot', |
| 1642 |
# name of the ghost with which it has communicated |
| 1643 |
##'$to', |
| 1644 |
'$sender', |
| 1645 |
'$lastsentence', |
| 1646 |
'$otherghostlist', |
| 1647 |
] |
| 1648 |
|
| 1649 |
def eval_variable(name) |
| 1650 |
now = Time.now |
| 1651 |
name = expand(name) |
| 1652 |
case name |
| 1653 |
when '$year' |
| 1654 |
result = now.year |
| 1655 |
when '$month' |
| 1656 |
result = now.month |
| 1657 |
when '$day' |
| 1658 |
result = now.day |
| 1659 |
when '$hour' |
| 1660 |
result = now.hour |
| 1661 |
when '$minute' |
| 1662 |
result = now.min |
| 1663 |
when '$second' |
| 1664 |
result = now.sec |
| 1665 |
when '$dayofweek' |
| 1666 |
result = now.wday |
| 1667 |
when '$lastsentence' |
| 1668 |
result = @communicate[1] |
| 1669 |
when '$otherghostlist' |
| 1670 |
unless @otherghost.empty? |
| 1671 |
ghost = @otherghost.sample |
| 1672 |
result = ghost[0] |
| 1673 |
else |
| 1674 |
result = '' |
| 1675 |
end |
| 1676 |
when '$sender' |
| 1677 |
result = @communicate[0] |
| 1678 |
when *SYSTEM_VARIABLES |
| 1679 |
result = '' |
| 1680 |
when *@dict.keys |
| 1681 |
result = get(name) |
| 1682 |
when *@constant.keys |
| 1683 |
value_type, value = @constant[name] |
| 1684 |
if value_type == TYPE_ARRAY |
| 1685 |
result = expand(value.get()) |
| 1686 |
else |
| 1687 |
result = value.to_s |
| 1688 |
end |
| 1689 |
when *@variable.keys |
| 1690 |
value_type, value = @variable[name] |
| 1691 |
if value_type == TYPE_ARRAY |
| 1692 |
result = expand(value.get()) |
| 1693 |
else |
| 1694 |
result = value.to_s |
| 1695 |
end |
| 1696 |
else |
| 1697 |
result = '' |
| 1698 |
end |
| 1699 |
return result |
| 1700 |
end |
| 1701 |
|
| 1702 |
def eval_indexed(name, index) |
| 1703 |
name = expand(name) |
| 1704 |
index = expand(index) |
| 1705 |
begin |
| 1706 |
index = Integer(index) |
| 1707 |
rescue #except ValueError: |
| 1708 |
index = 0 |
| 1709 |
end |
| 1710 |
case name |
| 1711 |
when '$otherghostlist' |
| 1712 |
group = [] |
| 1713 |
for ghost in @otherghost |
| 1714 |
group << ghost[0] |
| 1715 |
end |
| 1716 |
when *SYSTEM_VARIABLES |
| 1717 |
return '' |
| 1718 |
when *@dict.keys |
| 1719 |
grouplist = @dict[name] |
| 1720 |
group, constraints = grouplist[0] |
| 1721 |
when *@constant.keys |
| 1722 |
return '' |
| 1723 |
when *@variable.keys |
| 1724 |
value_type, group = @variable[name] |
| 1725 |
return '' if value_type != TYPE_ARRAY |
| 1726 |
else |
| 1727 |
return '' |
| 1728 |
end |
| 1729 |
return '' if index < 0 or index >= group.length |
| 1730 |
return expand(group[index]) |
| 1731 |
end |
| 1732 |
|
| 1733 |
def eval_assignment(name, operator, value, constant: false) |
| 1734 |
name = expand(name) |
| 1735 |
value = expand(value) |
| 1736 |
if ['+=', '-=', '*=', '/='].include?(operator) |
| 1737 |
begin |
| 1738 |
operand = Integer(value) |
| 1739 |
rescue #except ValueError: |
| 1740 |
operand = 0 |
| 1741 |
end |
| 1742 |
if @constant.include?(name) |
| 1743 |
value_type, value = @constant[name] |
| 1744 |
else |
| 1745 |
value_type, value = nil, '' |
| 1746 |
end |
| 1747 |
if value_type.nil? |
| 1748 |
if @variable.include?(name) |
| 1749 |
value_type, value = @variable[name] |
| 1750 |
else |
| 1751 |
value_type, value = TYPE_SCHOLAR, '' |
| 1752 |
end |
| 1753 |
end |
| 1754 |
return '' if value_type == TYPE_ARRAY |
| 1755 |
begin |
| 1756 |
value = Integer(value) |
| 1757 |
rescue #except ValueError: |
| 1758 |
value = 0 |
| 1759 |
end |
| 1760 |
if operator == '+=' |
| 1761 |
value += operand |
| 1762 |
elsif operator == '-=' |
| 1763 |
value -= operand |
| 1764 |
elsif operator == '*=' |
| 1765 |
value *= operand |
| 1766 |
elsif operator == '/=' and not operand.zero? |
| 1767 |
value /= operand |
| 1768 |
value = value.to_i |
| 1769 |
end |
| 1770 |
end |
| 1771 |
if constant or @constant.include?(name) |
| 1772 |
@constant[name] = [TYPE_SCHOLAR, value] |
| 1773 |
else |
| 1774 |
@variable[name] = [TYPE_SCHOLAR, value] |
| 1775 |
end |
| 1776 |
if name == '$_talkinterval' |
| 1777 |
reset_random_talk_interval() |
| 1778 |
end |
| 1779 |
return '' |
| 1780 |
end |
| 1781 |
|
| 1782 |
def eval_comp_expr(operand1, operator, operand2) |
| 1783 |
value1 = expand(operand1) |
| 1784 |
value2 = expand(operand2) |
| 1785 |
begin |
| 1786 |
operand1 = Integer(value1) |
| 1787 |
operand2 = Integer(value2) |
| 1788 |
rescue #except ValueError: |
| 1789 |
operand1 = value1 |
| 1790 |
operand2 = value2 |
| 1791 |
end |
| 1792 |
if (operator == '==' and operand1 == operand2) or \ |
| 1793 |
(operator == '!=' and operand1 != operand2) or \ |
| 1794 |
(operator == '<' and operand1 < operand2) or \ |
| 1795 |
(operator == '<=' and operand1 <= operand2) or \ |
| 1796 |
(operator == '>' and operand1 > operand2) or \ |
| 1797 |
(operator == '>=' and operand1 >= operand2) |
| 1798 |
return 'true' |
| 1799 |
end |
| 1800 |
return 'false' |
| 1801 |
end |
| 1802 |
|
| 1803 |
def eval_and_expr(conditions) |
| 1804 |
for condition in conditions |
| 1805 |
boolean = eval_(condition) |
| 1806 |
if boolean.strip() != 'true' |
| 1807 |
return 'false' |
| 1808 |
end |
| 1809 |
end |
| 1810 |
return 'true' |
| 1811 |
end |
| 1812 |
|
| 1813 |
def eval_or_expr(conditions) |
| 1814 |
for condition in conditions |
| 1815 |
boolean = eval_(condition) |
| 1816 |
if boolean.strip() == 'true' |
| 1817 |
return 'true' |
| 1818 |
end |
| 1819 |
end |
| 1820 |
return 'false' |
| 1821 |
end |
| 1822 |
|
| 1823 |
def eval_add_expr(expression) |
| 1824 |
value = expand(expression[0]) |
| 1825 |
begin |
| 1826 |
value = Integer(value) |
| 1827 |
rescue #except ValueError: |
| 1828 |
value = 0 |
| 1829 |
end |
| 1830 |
for i in 1.step(expression.length-1, 2) |
| 1831 |
operand = expand(expression[i + 1]) |
| 1832 |
begin |
| 1833 |
operand = Integer(operand) |
| 1834 |
rescue #except ValueError: |
| 1835 |
operand = 0 |
| 1836 |
end |
| 1837 |
if expression[i] == '+' |
| 1838 |
value += operand |
| 1839 |
elsif expression[i] == '-' |
| 1840 |
value -= operand |
| 1841 |
end |
| 1842 |
end |
| 1843 |
return value.to_s |
| 1844 |
end |
| 1845 |
|
| 1846 |
def eval_mul_expr(expression) |
| 1847 |
value = expand(expression[0]) |
| 1848 |
begin |
| 1849 |
value = Integer(value) |
| 1850 |
rescue #except ValueError: |
| 1851 |
value = 0 |
| 1852 |
end |
| 1853 |
for i in 1.step(expression.length-1, 2) |
| 1854 |
operand = expand(expression[i + 1]) |
| 1855 |
begin |
| 1856 |
operand = Integer(operand) |
| 1857 |
rescue #except ValueError: |
| 1858 |
operand = 0 |
| 1859 |
end |
| 1860 |
if expression[i] == '*' |
| 1861 |
value *= operand |
| 1862 |
elsif expression[i] == '/' and not operand.zero? |
| 1863 |
value /= operand |
| 1864 |
value = value.to_i |
| 1865 |
elsif expression[i] == '%' and not operand.zero? |
| 1866 |
value = (value % operand) |
| 1867 |
end |
| 1868 |
end |
| 1869 |
return value.to_s |
| 1870 |
end |
| 1871 |
|
| 1872 |
def eval_pow_expr(expression) |
| 1873 |
value = expand(expression[-1]) |
| 1874 |
begin |
| 1875 |
value = Integer(value) |
| 1876 |
rescue #except ValueError: |
| 1877 |
value = 0 |
| 1878 |
end |
| 1879 |
for i in 1..expression.length-1 |
| 1880 |
operand = expand(expression[-i - 1]) |
| 1881 |
begin |
| 1882 |
operand = Integer(operand) |
| 1883 |
rescue #except ValueError: |
| 1884 |
operand = 0 |
| 1885 |
end |
| 1886 |
value = (operand**value) |
| 1887 |
end |
| 1888 |
return value.to_s |
| 1889 |
end |
| 1890 |
|
| 1891 |
def eval_if(condition, then_clause, else_clause) |
| 1892 |
boolean = eval_(condition) |
| 1893 |
if boolean.strip() == 'true' |
| 1894 |
return expand(then_clause) |
| 1895 |
else |
| 1896 |
return expand(else_clause) |
| 1897 |
end |
| 1898 |
end |
| 1899 |
|
| 1900 |
def eval_calc(expression) |
| 1901 |
expand(expression) |
| 1902 |
end |
| 1903 |
|
| 1904 |
def eval_function(name, args) |
| 1905 |
function = SYSTEM_FUNCTIONS[name] |
| 1906 |
return '' if function.nil? |
| 1907 |
return method(function).call(args) |
| 1908 |
end |
| 1909 |
|
| 1910 |
def is_number(s) |
| 1911 |
return (s and s.chars.map {|c| '0123456789'.include?(c) }.all?) |
| 1912 |
end |
| 1913 |
|
| 1914 |
def split(s) |
| 1915 |
buf = [] |
| 1916 |
i, j = 0, s.length |
| 1917 |
while i < j |
| 1918 |
if s[i].ord < 0x80 |
| 1919 |
buf << s[i] |
| 1920 |
i += 1 |
| 1921 |
else |
| 1922 |
buf << s[i..i + 1] |
| 1923 |
i += 2 |
| 1924 |
end |
| 1925 |
end |
| 1926 |
return buf |
| 1927 |
end |
| 1928 |
|
| 1929 |
def exec_reference(args) |
| 1930 |
return '' if args.length != 1 |
| 1931 |
n = expand_args(args[0]) |
| 1932 |
if '01234567'.include?(n) |
| 1933 |
value = @reference[n.to_i] |
| 1934 |
return value.to_s unless value.nil? |
| 1935 |
end |
| 1936 |
return '' |
| 1937 |
end |
| 1938 |
|
| 1939 |
def exec_random(args) |
| 1940 |
return '' if args.length != 1 |
| 1941 |
n = expand_args(args[0]) |
| 1942 |
begin |
| 1943 |
return Array(0..Integer(n)-1).sample.to_s |
| 1944 |
rescue #except ValueError: |
| 1945 |
return '' # n < 1 or not a number |
| 1946 |
end |
| 1947 |
end |
| 1948 |
|
| 1949 |
def exec_choice(args) |
| 1950 |
return '' if args.empty? |
| 1951 |
return expand_args(args.sample) |
| 1952 |
end |
| 1953 |
|
| 1954 |
def exec_getvalue(args) |
| 1955 |
return '' if args.length != 2 |
| 1956 |
begin |
| 1957 |
n = Integer(expand_args(args[1])) |
| 1958 |
rescue #except ValueError: |
| 1959 |
return '' |
| 1960 |
end |
| 1961 |
if n < 0 |
| 1962 |
return '' |
| 1963 |
end |
| 1964 |
value_list = expand_args(args[0]).split(',', 0) |
| 1965 |
begin |
| 1966 |
return value_list[n] |
| 1967 |
rescue #except IndexError: |
| 1968 |
return '' |
| 1969 |
end |
| 1970 |
end |
| 1971 |
|
| 1972 |
def exec_search(args) |
| 1973 |
namelist = [] |
| 1974 |
for arg in args |
| 1975 |
name = expand_args(arg) |
| 1976 |
if name.strip() |
| 1977 |
namelist << name |
| 1978 |
end |
| 1979 |
end |
| 1980 |
return '' if namelist.empty? |
| 1981 |
keylist = [] |
| 1982 |
for key in keys() # dict, variable, constant |
| 1983 |
break_flag = false |
| 1984 |
for name in namelist |
| 1985 |
unless key.include?(name) |
| 1986 |
break_flag = true |
| 1987 |
break |
| 1988 |
end |
| 1989 |
end |
| 1990 |
unless break_flag |
| 1991 |
keylist << key |
| 1992 |
end |
| 1993 |
end |
| 1994 |
return '' if keylist.empty? |
| 1995 |
return eval_variable([[NODE_TEXT, keylist.sample]]) |
| 1996 |
end |
| 1997 |
|
| 1998 |
def keys |
| 1999 |
buf = @dict.keys() |
| 2000 |
buf |= @variable.keys() |
| 2001 |
buf |= @constant.keys() |
| 2002 |
return buf |
| 2003 |
end |
| 2004 |
|
| 2005 |
def exec_backup(args) |
| 2006 |
if args.empty? |
| 2007 |
save_database() |
| 2008 |
end |
| 2009 |
return '' |
| 2010 |
end |
| 2011 |
|
| 2012 |
def exec_getmousemovecount(args) |
| 2013 |
if args.length == 2 |
| 2014 |
side = expand_args(args[0]) |
| 2015 |
part = expand_args(args[1]) |
| 2016 |
begin |
| 2017 |
key = [Integer(side), part] |
| 2018 |
rescue #except ValueError: |
| 2019 |
#pass |
| 2020 |
else |
| 2021 |
if @mouse_move_count.include?(key) |
| 2022 |
return @mouse_move_count[key].to_s |
| 2023 |
else |
| 2024 |
return 0 |
| 2025 |
end |
| 2026 |
end |
| 2027 |
end |
| 2028 |
return '' |
| 2029 |
end |
| 2030 |
|
| 2031 |
def exec_resetmousemovecount(args) |
| 2032 |
if args.length == 2 |
| 2033 |
side = expand_args(args[0]) |
| 2034 |
part = expand_args(args[1]) |
| 2035 |
begin |
| 2036 |
key = [Integer(side), part] |
| 2037 |
rescue #except ValueError: |
| 2038 |
pass |
| 2039 |
else |
| 2040 |
@mouse_move_count[key] = 0 |
| 2041 |
end |
| 2042 |
end |
| 2043 |
return '' |
| 2044 |
end |
| 2045 |
|
| 2046 |
def exec_substring(args) |
| 2047 |
return '' if args.length != 3 |
| 2048 |
value = expand_args(args[2]) |
| 2049 |
begin |
| 2050 |
count = Integer(value) |
| 2051 |
rescue #except ValueError: |
| 2052 |
return '' |
| 2053 |
end |
| 2054 |
return '' if count < 0 |
| 2055 |
value = expand_args(args[1]) |
| 2056 |
begin |
| 2057 |
offset = Integer(value) |
| 2058 |
rescue #except ValueError: |
| 2059 |
return '' |
| 2060 |
end |
| 2061 |
return '' if offset < 0 |
| 2062 |
s = expand_args(args[0]).encode(@charset, :invalid => :replace, :undef => :replace) |
| 2063 |
return s[offset..offset + count - 1].force_encoding(@charset).encode('utf-8', :invalid => :replace, :undef => :replace) |
| 2064 |
end |
| 2065 |
|
| 2066 |
def exec_substringl(args) |
| 2067 |
return '' if args.length != 2 |
| 2068 |
value = expand_args(args[1]) |
| 2069 |
begin |
| 2070 |
count = Integer(value) |
| 2071 |
rescue #except ValueError: |
| 2072 |
return '' |
| 2073 |
end |
| 2074 |
return '' if count < 0 |
| 2075 |
s = expand_args(args[0]).encode(@charset, :invalid => :replace, :undef => :replace) |
| 2076 |
return s[0..count-1].force_encoding(@charset).encode('utf-8', :invalid => :replace, :undef => :replace) |
| 2077 |
end |
| 2078 |
|
| 2079 |
def exec_substringr(args) |
| 2080 |
return '' if args.length != 2 |
| 2081 |
value = expand_args(args[1]) |
| 2082 |
begin |
| 2083 |
count = Integer(value) |
| 2084 |
rescue #except ValueError: |
| 2085 |
return '' |
| 2086 |
end |
| 2087 |
return '' if count < 0 |
| 2088 |
s = expand_args(args[0]).encode(@charset, :invalid => :replace, :undef => :replace) |
| 2089 |
return s[s.length - count..-1].force_encoding(@charset).encode('utf-8', :invalid => :replace, :undef => :replace) |
| 2090 |
end |
| 2091 |
|
| 2092 |
def exec_substringfirst(args) |
| 2093 |
return '' if args.length != 1 |
| 2094 |
buf = expand_args(args[0]) |
| 2095 |
return '' if buf.empty? |
| 2096 |
return buf[0] |
| 2097 |
end |
| 2098 |
|
| 2099 |
def exec_substringlast(args) |
| 2100 |
return '' if args.length != 1 |
| 2101 |
buf = expand_args(args[0]) |
| 2102 |
return '' if buf.empty? |
| 2103 |
return buf[-1] |
| 2104 |
end |
| 2105 |
|
| 2106 |
def exec_length(args) |
| 2107 |
return '' if args.length != 1 |
| 2108 |
return expand_args(args[0]).encode(@charset, :invalid => :replace, :undef => :replace).length.to_s |
| 2109 |
end |
| 2110 |
|
| 2111 |
Katakana2hiragana = { |
| 2112 |
'ァ' => 'ぁ', 'ア' => 'あ', 'ィ' => 'ぃ', 'イ' => 'い', 'ゥ' => 'ぅ', |
| 2113 |
'ウ' => 'う', 'ェ' => 'ぇ', 'エ' => 'え', 'ォ' => 'ぉ', 'オ' => 'お', |
| 2114 |
'カ' => 'か', 'ガ' => 'が', 'キ' => 'き', 'ギ' => 'ぎ', 'ク' => 'く', |
| 2115 |
'グ' => 'ぐ', 'ケ' => 'け', 'ゲ' => 'げ', 'コ' => 'こ', 'ゴ' => 'ご', |
| 2116 |
'サ' => 'さ', 'ザ' => 'ざ', 'シ' => 'し', 'ジ' => 'じ', 'ス' => 'す', |
| 2117 |
'ズ' => 'ず', 'セ' => 'せ', 'ゼ' => 'ぜ', 'ソ' => 'そ', 'ゾ' => 'ぞ', |
| 2118 |
'タ' => 'た', 'ダ' => 'だ', 'チ' => 'ち', 'ヂ' => 'ぢ', 'ッ' => 'っ', |
| 2119 |
'ツ' => 'つ', 'ヅ' => 'づ', 'テ' => 'て', 'デ' => 'で', 'ト' => 'と', |
| 2120 |
'ド' => 'ど', 'ナ' => 'な', 'ニ' => 'に', 'ヌ' => 'ぬ', 'ネ' => 'ね', |
| 2121 |
'ノ' => 'の', 'ハ' => 'は', 'バ' => 'ば', 'パ' => 'ぱ', 'ヒ' => 'ひ', |
| 2122 |
'ビ' => 'び', 'ピ' => 'ぴ', 'フ' => 'ふ', 'ブ' => 'ぶ', 'プ' => 'ぷ', |
| 2123 |
'ヘ' => 'へ', 'ベ' => 'べ', 'ペ' => 'ぺ', 'ホ' => 'ほ', 'ボ' => 'ぼ', |
| 2124 |
'ポ' => 'ぽ', 'マ' => 'ま', 'ミ' => 'み', 'ム' => 'む', 'メ' => 'め', |
| 2125 |
'モ' => 'も', 'ャ' => 'ゃ', 'ヤ' => 'や', 'ュ' => 'ゅ', 'ユ' => 'ゆ', |
| 2126 |
'ョ' => 'ょ', 'ヨ' => 'よ', 'ラ' => 'ら', 'リ' => 'り', 'ル' => 'る', |
| 2127 |
'レ' => 'れ', 'ロ' => 'ろ', 'ヮ' => 'ゎ', 'ワ' => 'わ', 'ヰ' => 'ゐ', |
| 2128 |
'ヱ' => 'ゑ', 'ヲ' => 'を', 'ン' => 'ん', 'ヴ' => 'う゛', |
| 2129 |
} |
| 2130 |
|
| 2131 |
def exec_hiraganacase(args) |
| 2132 |
return '' if args.length != 1 |
| 2133 |
buf = [] |
| 2134 |
string = expand_args(args[0]).force_encoding(@charset).encode('utf-8', :invalid => :replace, :undef => :replace) |
| 2135 |
#string = args[0].force_encoding(@charset).encode('utf-8', :invalid => :replace, :undef => :replace) |
| 2136 |
for c in string.chars |
| 2137 |
if Katakana2hiragana.include?(c) |
| 2138 |
c = Katakana2hiragana[c] |
| 2139 |
else |
| 2140 |
#pass |
| 2141 |
end |
| 2142 |
buf << c |
| 2143 |
end |
| 2144 |
return buf.join('') |
| 2145 |
end |
| 2146 |
|
| 2147 |
def exec_isequallastandfirst(args) |
| 2148 |
return 'false' if args.length != 2 |
| 2149 |
buf0 = expand_args(args[0]) |
| 2150 |
buf1 = expand_args(args[1]) |
| 2151 |
if not buf0.empty? and not buf1.empty? and buf0[-1] == buf1[0] |
| 2152 |
return 'true' |
| 2153 |
else |
| 2154 |
return 'false' |
| 2155 |
end |
| 2156 |
end |
| 2157 |
|
| 2158 |
def exec_append(args) |
| 2159 |
if args.length == 2 |
| 2160 |
name = expand_args(args[0]) |
| 2161 |
if not name.empty? and name.start_with?('$') |
| 2162 |
if @variable.include?(name) |
| 2163 |
value_type, value = @variable[name] |
| 2164 |
else |
| 2165 |
value_type, value = TYPE_SCHOLAR, '' |
| 2166 |
end |
| 2167 |
if value_type == TYPE_SCHOLAR |
| 2168 |
value_type = TYPE_ARRAY |
| 2169 |
array = MisakaArray.new(self, name) |
| 2170 |
if value != '' |
| 2171 |
array.append(value) |
| 2172 |
end |
| 2173 |
value = array |
| 2174 |
end |
| 2175 |
member_list = expand_args(args[1]).split(1.chr, 0) |
| 2176 |
for member in member_list |
| 2177 |
value.append(member) |
| 2178 |
end |
| 2179 |
@variable[name] = [value_type, value] |
| 2180 |
end |
| 2181 |
end |
| 2182 |
return '' |
| 2183 |
end |
| 2184 |
|
| 2185 |
def exec_stringexists(args) |
| 2186 |
return '' if args.length != 2 |
| 2187 |
name = expand_args(args[0]) |
| 2188 |
if @variable.include?(name) |
| 2189 |
value_type, value = @variable[name] |
| 2190 |
else |
| 2191 |
value_type, value = TYPE_SCHOLAR, '' |
| 2192 |
end |
| 2193 |
return '' if value_type == TYPE_SCHOLAR |
| 2194 |
if value.index(expand_args(args[1])).nil? # value.index(expand_args(args[1])) < 0 |
| 2195 |
return 'false' |
| 2196 |
else |
| 2197 |
return 'true' |
| 2198 |
end |
| 2199 |
end |
| 2200 |
|
| 2201 |
def exec_copy(args) |
| 2202 |
return '' if args.length != 2 |
| 2203 |
src_name = expand_args(args[0]) |
| 2204 |
return '' unless not src_name.empty? and src_name.start_with?('$') |
| 2205 |
new_name = expand_args(args[1]) |
| 2206 |
return '' unless not new_name.empty? and new_name.start_with?('$') |
| 2207 |
source = nil |
| 2208 |
if @dict.include?(src_name) |
| 2209 |
grouplist = @dict[src_name] |
| 2210 |
source, conditions = grouplist[0] |
| 2211 |
elsif @variable.include?(src_name) |
| 2212 |
value_type, value = @variable[src_name] |
| 2213 |
if value_type == TYPE_ARRAY |
| 2214 |
source = value |
| 2215 |
end |
| 2216 |
end |
| 2217 |
if source.nil? |
| 2218 |
value = MisakaArray.new(self, new_name) |
| 2219 |
else |
| 2220 |
value = source.copy(new_name) |
| 2221 |
end |
| 2222 |
@variable[new_name] = [TYPE_ARRAY, value] |
| 2223 |
return '' |
| 2224 |
end |
| 2225 |
|
| 2226 |
def exec_pop(args) |
| 2227 |
if args.length == 1 |
| 2228 |
name = expand_args(args[0]) |
| 2229 |
if @variable.include?(name) |
| 2230 |
value_type, value = @variable[name] |
| 2231 |
else |
| 2232 |
value_type, value = TYPE_SCHOLAR, '' |
| 2233 |
end |
| 2234 |
if value_type == TYPE_ARRAY |
| 2235 |
return expand(value.pop()) |
| 2236 |
end |
| 2237 |
end |
| 2238 |
return '' |
| 2239 |
end |
| 2240 |
|
| 2241 |
def exec_popmatchl(args) |
| 2242 |
if args.length == 2 |
| 2243 |
name = expand_args(args[0]) |
| 2244 |
if @variable.include?(name) |
| 2245 |
value_type, value = @variable[name] |
| 2246 |
else |
| 2247 |
value_type, value = TYPE_SCHOLAR, '' |
| 2248 |
end |
| 2249 |
if value_type == TYPE_ARRAY |
| 2250 |
return expand(value.popmatchl(expand_args(args[1]))) |
| 2251 |
end |
| 2252 |
end |
| 2253 |
return '' |
| 2254 |
end |
| 2255 |
|
| 2256 |
def exec_index(args) |
| 2257 |
if args.length == 2 |
| 2258 |
pos = expand_args(args[1]).find(expand_args(args[0])) |
| 2259 |
if pos > 0 |
| 2260 |
s = expand_args(args[0])[pos] |
| 2261 |
pos = s.encode(@charset, :invalid => :replace, :undef => :replace).length |
| 2262 |
end |
| 2263 |
return pos.to_s |
| 2264 |
end |
| 2265 |
return '' |
| 2266 |
end |
| 2267 |
|
| 2268 |
def exec_insentence(args) |
| 2269 |
return '' if args.empty? |
| 2270 |
s = expand_args(args[0]) |
| 2271 |
for i in 1..args.length-1 |
| 2272 |
unless s.include?(expand_args(args[i])) |
| 2273 |
return 'false' |
| 2274 |
end |
| 2275 |
end |
| 2276 |
return 'true' |
| 2277 |
end |
| 2278 |
|
| 2279 |
def exec_substringw(args) |
| 2280 |
return '' if args.length != 3 |
| 2281 |
value = expand_args(args[2]) |
| 2282 |
begin |
| 2283 |
count = Integer(value) |
| 2284 |
rescue #except ValueError: |
| 2285 |
return '' |
| 2286 |
end |
| 2287 |
if count < 0 |
| 2288 |
return '' |
| 2289 |
end |
| 2290 |
value = expand_args(args[1]) |
| 2291 |
begin |
| 2292 |
offset = Integer(value) |
| 2293 |
rescue #except ValueError: |
| 2294 |
return '' |
| 2295 |
end |
| 2296 |
return '' if offset < 0 |
| 2297 |
buf = expand_args(args[0]) |
| 2298 |
return buf[offset..offset + count - 1] |
| 2299 |
end |
| 2300 |
|
| 2301 |
def exec_substringwl(args) |
| 2302 |
return '' if args.length != 2 |
| 2303 |
value = expand_args(args[1]) |
| 2304 |
begin |
| 2305 |
count = Integer(value) |
| 2306 |
rescue #except ValueError: |
| 2307 |
return '' |
| 2308 |
end |
| 2309 |
return '' if count < 0 |
| 2310 |
buf = expand_args(args[0]) |
| 2311 |
return buf[0..count-1] |
| 2312 |
end |
| 2313 |
|
| 2314 |
def exec_substringwr(args) |
| 2315 |
return '' if args.length != 2 |
| 2316 |
value = expand_args(args[1]) |
| 2317 |
begin |
| 2318 |
count = Integer(value) |
| 2319 |
rescue #except ValueError: |
| 2320 |
return '' |
| 2321 |
end |
| 2322 |
return '' if count < 0 |
| 2323 |
buf = expand_args(args[0]) |
| 2324 |
return buf[buf.length - count..-1] |
| 2325 |
end |
| 2326 |
|
| 2327 |
Prefixes = ['さん', 'ちゃん', '君', 'くん', '様', 'さま', '氏', '先生'] |
| 2328 |
|
| 2329 |
def exec_adjustprefix(args) |
| 2330 |
return '' if args.length != 2 |
| 2331 |
s = expand_args(args[0]) |
| 2332 |
for prefix in Prefixes |
| 2333 |
if s.end_with?(prefix) |
| 2334 |
s = [s[0..-prefix.length-1], expand_args(args[1])].join('') |
| 2335 |
break |
| 2336 |
end |
| 2337 |
end |
| 2338 |
return s |
| 2339 |
end |
| 2340 |
|
| 2341 |
def exec_count(args) |
| 2342 |
return '' if args.length != 1 |
| 2343 |
name = expand_args(args[0]) |
| 2344 |
begin |
| 2345 |
value_type, value = @variable[name] |
| 2346 |
if value_type == TYPE_SCHOLAR |
| 2347 |
return '1' |
| 2348 |
end |
| 2349 |
return value.length.to_s |
| 2350 |
rescue #except KeyError: |
| 2351 |
return '-1' |
| 2352 |
end |
| 2353 |
end |
| 2354 |
|
| 2355 |
def exec_inlastsentence(args) |
| 2356 |
return '' if args.empty? |
| 2357 |
s = @communicate[1] |
| 2358 |
for i in 1..args.length-1 |
| 2359 |
unless s.include?(expand_args(args[i])) |
| 2360 |
return 'false' |
| 2361 |
end |
| 2362 |
end |
| 2363 |
return 'true' |
| 2364 |
end |
| 2365 |
|
| 2366 |
def saori(args) |
| 2367 |
saori_statuscode = '' |
| 2368 |
saori_header = [] |
| 2369 |
saori_value = {} |
| 2370 |
saori_protocol = '' |
| 2371 |
req = ["EXECUTE SAORI/1.0\r\n", |
| 2372 |
"Sender: MISAKA\r\n", |
| 2373 |
"SecurityLevel: local\r\n", |
| 2374 |
"Charset: ", |
| 2375 |
@charset.encode(@charset, :invalid => :replace, :undef => :replace), |
| 2376 |
"\r\n"].join('') |
| 2377 |
for i in 1..args.length-1 |
| 2378 |
req = [req, |
| 2379 |
'Argument', i.to_s.encode(@charset, :invalid => :replace, :undef => :replace), ': ', |
| 2380 |
expand_args(args[i]).encode(@charset, :invalid => :replace, :undef => :replace), |
| 2381 |
"\r\n"].join('') |
| 2382 |
end |
| 2383 |
req = [req, "\r\n"].join('') |
| 2384 |
response = @saori_library.request(expand_args(args[0]), req) |
| 2385 |
header = response.split(/\r?\n/, 0) |
| 2386 |
unless header.empty? |
| 2387 |
line = header.shift |
| 2388 |
line = line.force_encoding(@charset).encode('utf-8', :invalid => :replace, :undef => :replace).strip() |
| 2389 |
if line.include?(' ') |
| 2390 |
saori_protocol, saori_statuscode = line.split(' ', 2) |
| 2391 |
saori_protocol.strip! |
| 2392 |
saori_statuscode.strip! |
| 2393 |
end |
| 2394 |
for line in header |
| 2395 |
line = line.force_encoding(@charset).encode('utf-8', :invalid => :replace, :undef => :replace).strip() |
| 2396 |
next if line.empty? |
| 2397 |
nexy unless line.include?(':') |
| 2398 |
key, value = line.split(':', 2) |
| 2399 |
key.strip! |
| 2400 |
value.strip! |
| 2401 |
unless key.empty? |
| 2402 |
saori_header << key |
| 2403 |
saori_value[key] = value |
| 2404 |
end |
| 2405 |
end |
| 2406 |
end |
| 2407 |
if saori_value.include?('Result') |
| 2408 |
return saori_value['Result'] |
| 2409 |
else |
| 2410 |
return '' |
| 2411 |
end |
| 2412 |
end |
| 2413 |
|
| 2414 |
def load_saori(args) |
| 2415 |
return '' if args.empty? |
| 2416 |
result = @saori_library.load(expand_args(args[0]), |
| 2417 |
@misaka_dir) |
| 2418 |
if result.zero? |
| 2419 |
return '' |
| 2420 |
else |
| 2421 |
result.to_s |
| 2422 |
end |
| 2423 |
end |
| 2424 |
|
| 2425 |
def unload_saori(args) |
| 2426 |
return '' if args.empty? |
| 2427 |
result = @saori_library.unload(:name => expand_args(args[0])) |
| 2428 |
if result.zero? |
| 2429 |
return '' |
| 2430 |
else |
| 2431 |
return result.to_s |
| 2432 |
end |
| 2433 |
end |
| 2434 |
|
| 2435 |
def exec_isghostexists(args) |
| 2436 |
result = 'false' |
| 2437 |
if args.length != 1 |
| 2438 |
if @otherghost.length > 0 |
| 2439 |
result = 'true' |
| 2440 |
@variable['$to'] = [TYPE_SCHOLAR, |
| 2441 |
@otherghost.sample[0]] |
| 2442 |
end |
| 2443 |
else |
| 2444 |
for ghost in @otherghost |
| 2445 |
if ghost[0] == expand_args(args[0]) |
| 2446 |
result = 'true' |
| 2447 |
break |
| 2448 |
end |
| 2449 |
end |
| 2450 |
end |
| 2451 |
return result |
| 2452 |
end |
| 2453 |
end |
| 2454 |
|
| 2455 |
SYSTEM_FUNCTIONS = { |
| 2456 |
'$reference' => 'exec_reference', |
| 2457 |
'$random' => 'exec_random', |
| 2458 |
'$choice' => 'exec_choice', |
| 2459 |
'$getvalue' => 'exec_getvalue', |
| 2460 |
'$inlastsentence' => 'exec_inlastsentence', |
| 2461 |
'$isghostexists' => 'exec_isghostexists', |
| 2462 |
'$search' => 'exec_search', |
| 2463 |
'$backup' => 'exec_backup', |
| 2464 |
'$getmousemovecount' => 'exec_getmousemovecount', |
| 2465 |
'$resetmousemovecount' => 'exec_resetmousemovecount', |
| 2466 |
'$substring' => 'exec_substring', |
| 2467 |
'$substringl' => 'exec_substringl', |
| 2468 |
'$substringr' => 'exec_substringr', |
| 2469 |
'$substringfirst' => 'exec_substringfirst', |
| 2470 |
'$substringlast' => 'exec_substringlast', |
| 2471 |
'$length' => 'exec_length', |
| 2472 |
'$hiraganacase' => 'exec_hiraganacase', |
| 2473 |
'$isequallastandfirst' => 'exec_isequallastandfirst', |
| 2474 |
'$append' => 'exec_append', |
| 2475 |
'$stringexists' => 'exec_stringexists', |
| 2476 |
'$copy' => 'exec_copy', |
| 2477 |
'$pop' => 'exec_pop', |
| 2478 |
'$popmatchl' => 'exec_popmatchl', |
| 2479 |
'$index' => 'exec_index', |
| 2480 |
'$insentence' => 'exec_insentence', |
| 2481 |
'$substringw' => 'exec_substringw', |
| 2482 |
'$substringwl' => 'exec_substringwl', |
| 2483 |
'$substringwr' => 'exec_substringwr', |
| 2484 |
'$adjustprefix' => 'exec_adjustprefix', |
| 2485 |
'$count' => 'exec_count', |
| 2486 |
'$saori' => 'saori', |
| 2487 |
'$loadsaori' => 'load_saori', |
| 2488 |
'$unloadsaori' => 'unload_saori', |
| 2489 |
} |
| 2490 |
|
| 2491 |
|
| 2492 |
class SaoriLibrary |
| 2493 |
|
| 2494 |
def initialize(saori, top_dir) |
| 2495 |
@saori_list = {} |
| 2496 |
@saori = saori |
| 2497 |
end |
| 2498 |
|
| 2499 |
def load(name, top_dir) |
| 2500 |
result = 0 |
| 2501 |
head, name = File.split(name.gsub('\\', '/')) # XXX: don't encode here |
| 2502 |
top_dir = File.join(top_dir, head) |
| 2503 |
unless @saori.nil? or @saori_list.include?(name) |
| 2504 |
module_ = @saori.request(name) |
| 2505 |
unless module_.nil? |
| 2506 |
@saori_list[name] = module_ |
| 2507 |
end |
| 2508 |
end |
| 2509 |
if @saori_list.include?(name) |
| 2510 |
result = @saori_list[name].load(:dir => top_dir) |
| 2511 |
end |
| 2512 |
return result |
| 2513 |
end |
| 2514 |
|
| 2515 |
def unload(name: nil) |
| 2516 |
unless name.nil? |
| 2517 |
name = File.split(name.gsub('\\', '/'))[-1] # XXX: don't encode here |
| 2518 |
if @saori_list.include?(name) |
| 2519 |
@saori_list[name].unload() |
| 2520 |
@saori_list.delete(name) |
| 2521 |
end |
| 2522 |
else |
| 2523 |
for key in @saori_list.keys |
| 2524 |
@saori_list[key].unload() |
| 2525 |
end |
| 2526 |
end |
| 2527 |
return nil |
| 2528 |
end |
| 2529 |
|
| 2530 |
def request(name, req) |
| 2531 |
result = '' # FIXME |
| 2532 |
name = File.split(name.gsub('\\', '/'))[-1] # XXX: don't encode here |
| 2533 |
if not name.empty? and @saori_list.include?(name) |
| 2534 |
result = @saori_list[name].request(req) |
| 2535 |
end |
| 2536 |
return result |
| 2537 |
end |
| 2538 |
end |
| 2539 |
end |