nadesiko3 3.3.45 → 3.3.49
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +143 -132
- package/batch/browsers.template.md +3 -3
- package/batch/build_browsers.nako3 +72 -72
- package/batch/build_command.nako3 +44 -44
- package/batch/build_nako_version.nako3 +42 -42
- package/batch/calc_hash.nako3 +29 -29
- package/batch/cmd_txt2json.nako3 +74 -74
- package/batch/command.txt +270 -338
- package/batch/command_nakopad.txt +9 -69
- package/batch/download-extlib.nako3 +43 -43
- package/batch/gen_command_nakopad.nako3 +57 -57
- package/batch/jsplugin2text.nako3 +285 -285
- package/batch/pickup_command.nako3 +110 -110
- package/batch/pickup_reserved_words.nako3 +11 -11
- package/batch/publish_version.nako3 +46 -46
- package/batch/show_agents.js +14 -14
- package/batch/turtle2js.nako3 +21 -21
- package/bin/cnako3 +10 -10
- package/core/.editorconfig +6 -0
- package/core/.eslintrc.cjs +33 -0
- package/core/.github/dependabot.yml +7 -0
- package/core/.github/workflows/nodejs.yml +37 -0
- package/core/.github/workflows/super-linter.yml +61 -0
- package/core/.github/workflows/textlint.yml +199 -0
- package/core/LICENSE +21 -0
- package/core/README.md +66 -0
- package/core/batch/build_nako_version.nako3 +42 -0
- package/core/command/snako.mjs +105 -0
- package/core/command/snako.mts +116 -0
- package/core/index.mjs +21 -0
- package/core/index.mts +21 -0
- package/core/package.json +47 -0
- package/core/sample/hello.nako3 +7 -0
- package/core/sample/hoge.mjs +4 -0
- package/core/sample/hoge.mts +6 -0
- package/core/src/nako3.mjs +858 -0
- package/core/src/nako3.mts +967 -0
- package/core/src/nako_colors.mjs +78 -0
- package/core/src/nako_colors.mts +86 -0
- package/core/src/nako_core_version.mjs +8 -0
- package/core/src/nako_core_version.mts +19 -0
- package/core/src/nako_csv.mjs +185 -0
- package/core/src/nako_csv.mts +188 -0
- package/core/src/nako_errors.mjs +173 -0
- package/core/src/nako_errors.mts +197 -0
- package/core/src/nako_from_dncl.mjs +255 -0
- package/core/src/nako_from_dncl.mts +250 -0
- package/core/src/nako_gen.mjs +1648 -0
- package/core/src/nako_gen.mts +1719 -0
- package/core/src/nako_gen_async.mjs +1659 -0
- package/core/src/nako_gen_async.mts +1732 -0
- package/core/src/nako_global.mjs +107 -0
- package/core/src/nako_global.mts +138 -0
- package/core/src/nako_indent.mjs +445 -0
- package/core/src/nako_indent.mts +492 -0
- package/core/src/nako_josi_list.mjs +38 -0
- package/core/src/nako_josi_list.mts +45 -0
- package/core/src/nako_lex_rules.mjs +253 -0
- package/core/src/nako_lex_rules.mts +260 -0
- package/core/src/nako_lexer.mjs +609 -0
- package/core/src/nako_lexer.mts +612 -0
- package/core/src/nako_logger.mjs +199 -0
- package/core/src/nako_logger.mts +232 -0
- package/core/src/nako_parser3.mjs +2439 -0
- package/core/src/nako_parser3.mts +2195 -0
- package/core/src/nako_parser_base.mjs +370 -0
- package/core/src/nako_parser_base.mts +370 -0
- package/core/src/nako_parser_const.mjs +37 -0
- package/core/src/nako_parser_const.mts +37 -0
- package/core/src/nako_prepare.mjs +304 -0
- package/core/src/nako_prepare.mts +315 -0
- package/core/src/nako_reserved_words.mjs +38 -0
- package/core/src/nako_reserved_words.mts +38 -0
- package/core/src/nako_source_mapping.mjs +207 -0
- package/core/src/nako_source_mapping.mts +262 -0
- package/core/src/nako_test.mjs +37 -0
- package/core/src/nako_types.mjs +25 -0
- package/core/src/nako_types.mts +151 -0
- package/core/src/plugin_csv.mjs +49 -0
- package/core/src/plugin_csv.mts +50 -0
- package/core/src/plugin_math.mjs +328 -0
- package/core/src/plugin_math.mts +326 -0
- package/core/src/plugin_promise.mjs +91 -0
- package/core/src/plugin_promise.mts +91 -0
- package/core/src/plugin_system.mjs +2832 -0
- package/core/src/plugin_system.mts +2690 -0
- package/core/src/plugin_test.mjs +34 -0
- package/core/src/plugin_test.mts +34 -0
- package/core/test/array_test.mjs +34 -0
- package/core/test/basic_test.mjs +344 -0
- package/core/test/calc_test.mjs +140 -0
- package/core/test/core_module_test.mjs +23 -0
- package/core/test/debug_test.mjs +16 -0
- package/core/test/dncl_test.mjs +94 -0
- package/core/test/error_message_test.mjs +210 -0
- package/core/test/error_test.mjs +16 -0
- package/core/test/flow_test.mjs +373 -0
- package/core/test/func_call.mjs +160 -0
- package/core/test/func_test.mjs +149 -0
- package/core/test/indent_test.mjs +364 -0
- package/core/test/lex_test.mjs +168 -0
- package/core/test/literal_test.mjs +73 -0
- package/core/test/nako_lexer_test.mjs +35 -0
- package/core/test/nako_logger_test.mjs +76 -0
- package/core/test/nako_logger_test.mts +78 -0
- package/core/test/plugin_csv_test.mjs +38 -0
- package/core/test/plugin_promise_test.mjs +18 -0
- package/core/test/plugin_system_test.mjs +630 -0
- package/core/test/prepare_test.mjs +96 -0
- package/core/test/re_test.mjs +22 -0
- package/core/test/side_effects_test.mjs +92 -0
- package/core/test/variable_scope_test.mjs +149 -0
- package/core/tsconfig.json +101 -0
- package/demo/ace_editor.html +89 -89
- package/demo/ace_editor_tabs.html +161 -161
- package/demo/basic.html +71 -71
- package/demo/browsers.html +9 -10
- package/demo/css/basic.css +3 -3
- package/demo/css/common.css +157 -157
- package/demo/css/editor.css +8 -8
- package/demo/css/flow.css +3 -3
- package/demo/css/index.css +3 -3
- package/demo/flow.html +98 -98
- package/demo/graph.html +53 -53
- package/demo/image/nakopad-icon256.png +0 -0
- package/demo/index.html +133 -133
- package/demo/js/common.js +17 -17
- package/demo/js/turtle3d_test.js +44 -44
- package/demo/js/turtle_test.js +45 -45
- package/demo/nako3/calc.nako3 +4 -4
- package/demo/runscript.html +47 -47
- package/demo/runscript2.html +33 -33
- package/demo/runscript3.html +35 -35
- package/demo/runscript4.html +33 -33
- package/demo/turtle.html +58 -58
- package/demo/turtle2.html +141 -141
- package/demo/turtle3.html +279 -279
- package/demo/turtle3d.html +58 -58
- package/demo/turtle3d2.html +107 -107
- package/demo/version.html +24 -24
- package/doc/SETUP.md +157 -157
- package/doc/about.md +17 -17
- package/doc/browsers.md +26 -26
- package/doc/docgen.md +21 -21
- package/doc/editor.md +44 -44
- package/doc/files.md +39 -39
- package/doc/plugins.md +234 -234
- package/doc/release.md +79 -79
- package/doc/textlint.md +43 -43
- package/doc/win32.md +57 -57
- package/package.json +195 -192
- package/release/_hash.txt +28 -28
- package/release/_script-tags.txt +14 -14
- package/release/command.json +1 -1
- package/release/command.json.js +1 -1
- package/release/command_cnako3.json +1 -1
- package/release/command_list.json +1 -1
- package/release/editor.js +1 -1
- package/release/nako_gen_async.js +1 -1
- package/release/plugin_csv.js +1 -1
- package/release/stats.json +1 -1
- package/release/version.js +1 -1
- package/release/wnako3.js +1 -1
- package/release/wnako3webworker.js +1 -1
- package/src/browsers.txt +11 -12
- package/src/browsers_agents.json +2 -2
- package/src/browsers_agents.mjs +1 -1
- package/src/cnako3.mjs +17 -10
- package/src/cnako3.mts +18 -12
- package/src/cnako3mod.mjs +707 -687
- package/src/cnako3mod.mts +712 -696
- package/src/commander_ja.mjs +164 -164
- package/src/commander_ja.mts +161 -161
- package/src/enako3.mjs +68 -69
- package/src/era.mjs +22 -22
- package/src/image_turtle-elephant.mjs +2 -5
- package/src/image_turtle-panda.mjs +2 -5
- package/src/image_turtle64.mjs +2 -5
- package/src/index.mjs +9 -9
- package/src/index.mts +10 -11
- package/src/nako3editorfix.sfd +106 -106
- package/src/nako_version.mjs +8 -8
- package/src/nako_version.mts +2 -2
- package/src/plugin_browser.mjs +213 -212
- package/src/plugin_browser.mts +206 -205
- package/src/plugin_browser_ajax.mjs +399 -399
- package/src/plugin_browser_audio.mjs +109 -109
- package/src/plugin_browser_canvas.mjs +449 -449
- package/src/plugin_browser_chart.mjs +294 -294
- package/src/plugin_browser_color.mjs +49 -49
- package/src/plugin_browser_crypto.mjs +26 -26
- package/src/plugin_browser_dialog.mjs +53 -53
- package/src/plugin_browser_dom_basic.mjs +336 -336
- package/src/plugin_browser_dom_event.mjs +193 -193
- package/src/plugin_browser_dom_parts.mjs +396 -396
- package/src/plugin_browser_geolocation.mjs +51 -51
- package/src/plugin_browser_hotkey.mjs +25 -25
- package/src/plugin_browser_html.mjs +59 -59
- package/src/plugin_browser_in_worker.mjs +45 -45
- package/src/plugin_browser_location.mjs +21 -21
- package/src/plugin_browser_speech.mjs +111 -111
- package/src/plugin_browser_storage.mjs +121 -121
- package/src/plugin_browser_system.mjs +31 -31
- package/src/plugin_browser_websocket.mjs +73 -73
- package/src/plugin_caniuse.mjs +29 -29
- package/src/plugin_datetime.mjs +394 -394
- package/src/plugin_httpserver.mjs +277 -0
- package/src/plugin_httpserver.mts +286 -0
- package/src/plugin_kansuji.mjs +224 -224
- package/src/plugin_keigo.mjs +55 -55
- package/src/plugin_markup.mjs +32 -32
- package/src/plugin_node.mjs +1047 -1047
- package/src/plugin_node.mts +980 -980
- package/src/plugin_turtle.mjs +647 -647
- package/src/plugin_webworker.mjs +334 -334
- package/src/plugin_weykturtle3d.mjs +1214 -1214
- package/src/plugin_worker.mjs +95 -95
- package/src/repl.nako3 +63 -63
- package/src/wnako3.mjs +12 -12
- package/src/wnako3.mts +11 -11
- package/src/wnako3_editor.css +215 -215
- package/src/wnako3_editor.mjs +1542 -1542
- package/src/wnako3_editor.mts +1657 -1656
- package/src/wnako3mod.mjs +213 -213
- package/src/wnako3mod.mts +214 -214
- package/src/wnako3webworker.mjs +69 -68
- package/test/ace_editor/karma.config.js +94 -94
- package/test/ace_editor/test/.babelrc.json +3 -3
- package/test/ace_editor/test/ace_editor_test.js +178 -178
- package/test/ace_editor/test/html/custom_context.html +139 -139
- package/test/async/async_basic_test.mjs +122 -122
- package/test/browser/karma.config.js +221 -221
- package/test/browser/test/.babelrc.json +3 -3
- package/test/browser/test/compare_util.js +50 -50
- package/test/browser/test/html/div_basic.html +2 -2
- package/test/browser/test/html/event_dom_form.html +4 -4
- package/test/browser/test/html/event_dom_scrolldiv.html +5 -5
- package/test/browser/test/import_plugin_checker.js +24 -24
- package/test/browser/test/plugin_browser_test.js +51 -51
- package/test/browser/test/plugin_browser_test_ajax.js +123 -123
- package/test/browser/test/plugin_browser_test_color.js +18 -18
- package/test/browser/test/plugin_browser_test_dialog.js +72 -72
- package/test/browser/test/plugin_browser_test_dom_event.js +598 -598
- package/test/browser/test/plugin_browser_test_dom_parts.js +125 -125
- package/test/browser/test/plugin_browser_test_system.js +9 -9
- package/test/browser/test/plugin_turtle_test.js +817 -817
- package/test/browser/test/plugin_webworker_test.js +86 -86
- package/test/browser/test/require_test.js +68 -68
- package/test/bundled/karma.config.base.js +117 -117
- package/test/bundled/karma.config.js +86 -86
- package/test/bundled/test/.babelrc.json +3 -3
- package/test/bundled/test/bundled_test.js +69 -69
- package/test/bundled/test/html/custom_context.html +65 -65
- package/test/bundled/test/html/custom_debug.html +66 -66
- package/test/bundled/test4b.cmd +52 -52
- package/test/bundled/test_base/.babelrc.json +3 -3
- package/test/bundled/test_base/_checktool_test.js +25 -25
- package/test/bundled/test_base/basic_ajax_test.js +56 -56
- package/test/bundled/test_base/basic_async_test.js +18 -18
- package/test/bundled/test_base/basic_test.js +153 -153
- package/test/bundled/test_base/calc_test.js +132 -132
- package/test/bundled/test_base/css/browsers_box.css +114 -114
- package/test/bundled/test_base/html/custom_context.html +69 -69
- package/test/bundled/test_base/html/custom_debug.html +71 -71
- package/test/bundled/test_base/js/browsers_box.js +72 -72
- package/test/bundled/test_base/plugin_csv_test.js +37 -37
- package/test/bundled/test_base/plugin_datetime_test.js +115 -115
- package/test/bundled/test_base/plugin_kansuji_test.js +49 -49
- package/test/bundled/test_base/plugin_system_test.js +410 -410
- package/test/bundled/test_base/plugin_webworker_test.js +53 -53
- package/test/bundled/test_base/test_utils.js +191 -191
- package/test/common/plugin_browser_test.mjs +22 -24
- package/test/common/plugin_browser_ut_audio_test.mjs +108 -108
- package/test/common/plugin_browser_ut_color_test.mjs +21 -21
- package/test/common/plugin_browser_ut_dialog_test.mjs +100 -100
- package/test/common/plugin_browser_ut_html_test.mjs +13 -13
- package/test/common/plugin_browser_ut_system_test.mjs +10 -10
- package/test/common/plugin_markup_test.mjs +23 -23
- package/test/jsconfig.json +19 -19
- package/test/karma.config.js +91 -91
- package/test/node/async_test.mjs +96 -82
- package/test/node/commander_ja_test.mjs +89 -89
- package/test/node/error_message_test.mjs +243 -253
- package/test/node/kai_test.nako3 +6 -6
- package/test/node/node_test.mjs +60 -60
- package/test/node/plugin_broken.js.txt +3 -3
- package/test/node/plugin_browser_ut_ajax_test.mjs.todo +357 -357
- package/test/node/plugin_browser_ut_location_test.mjs +42 -42
- package/test/node/plugin_markup_test.mjs +47 -46
- package/test/node/plugin_math_test.mjs +45 -44
- package/test/node/plugin_node_test.mjs +98 -97
- package/test/node/plugin_test.mjs +44 -43
- package/test/node/relative_import_test_1.nako3 +1 -1
- package/test/node/relative_import_test_2.nako3 +2 -2
- package/test/node/require_nako3_test.mjs +67 -66
- package/test/node/requiretest.nako3 +4 -4
- package/test/node/requiretest_indirect.nako3 +1 -1
- package/test/node/requiretest_name.nako3 +5 -5
- package/test/node/runtime_error.nako3 +2 -2
- package/test/node/scope1.nako3 +10 -10
- package/test/node/scope2.nako3 +12 -12
- package/test/node/side_effects_test.mjs +39 -119
- package/test/node/sjis.txt +5 -5
- package/test/node/syntax_error.nako3 +1 -1
- package/test/node/wnako3_editor_test.mjs +384 -384
- package/tools/README.md +12 -7
- package/tools/check_new_version.nako3 +25 -25
- package/tools/nako3edit/html/daisyui/LICENSE +22 -22
- package/tools/nako3edit/html/daisyui/version_2.14.1 +1 -1
- package/tools/nako3edit/html/edit.html +170 -170
- package/tools/nako3edit/html/edit_plugin.js +6 -6
- package/tools/nako3edit/html/files.html +125 -125
- package/tools/nako3edit/html/nako3edit.css +65 -65
- package/tools/nako3edit/index.mjs +248 -244
- package/tools/nako3server/index.html +10 -0
- package/tools/nako3server/index.mjs +116 -116
- package/tools/nako3server/index.nako3 +34 -0
- package/release/nako_gen_async.js.LICENSE.txt +0 -35
- package/release/plugin_caniuse.js.LICENSE.txt +0 -11
- package/release/plugin_csv.js.LICENSE.txt +0 -15
- package/release/plugin_datetime.js.LICENSE.txt +0 -15
- package/release/plugin_kansuji.js.LICENSE.txt +0 -3
- package/release/plugin_markup.js.LICENSE.txt +0 -11
- package/release/plugin_turtle.js.LICENSE.txt +0 -15
- package/release/plugin_webworker.js.LICENSE.txt +0 -3
- package/release/plugin_weykturtle3d.js.LICENSE.txt +0 -3
- package/release/wnako3webworker.js.LICENSE.txt +0 -131
- package/tools/nako3edit/a.sqlite3 +0 -0
|
@@ -0,0 +1,1648 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* パーサーが生成した中間オブジェクトを実際のJavaScriptのコードに変換する。
|
|
3
|
+
* なお速度優先で忠実にJavaScriptのコードを生成する。
|
|
4
|
+
*/
|
|
5
|
+
import { NakoRuntimeError, NakoSyntaxError, NakoError } from './nako_errors.mjs';
|
|
6
|
+
import { NakoLexer } from './nako_lexer.mjs';
|
|
7
|
+
// なでしこで定義した関数の開始コードと終了コード
|
|
8
|
+
const topOfFunction = '(function(){\n';
|
|
9
|
+
const endOfFunction = '})';
|
|
10
|
+
const topOfFunctionAsync = '(async function(){\n';
|
|
11
|
+
/** コード生成オプション */
|
|
12
|
+
export class NakoGenOptions {
|
|
13
|
+
constructor(isTest = false, importFiles = [], codeStandalone = '', convEnv = '') {
|
|
14
|
+
this.isTest = isTest;
|
|
15
|
+
this.codeStandalone = codeStandalone;
|
|
16
|
+
this.codeEnv = convEnv;
|
|
17
|
+
this.importFiles = ['plugin_system.mjs', 'plugin_math.mjs', 'plugin_csv.mjs', 'plugin_promise.mjs', 'plugin_test.mjs'];
|
|
18
|
+
for (const fname of importFiles) {
|
|
19
|
+
this.importFiles.push(fname);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* 構文木からJSのコードを生成するクラス
|
|
25
|
+
*/
|
|
26
|
+
export class NakoGen {
|
|
27
|
+
/** constructor
|
|
28
|
+
* @param com コンパイラのインスタンス
|
|
29
|
+
*/
|
|
30
|
+
constructor(com) {
|
|
31
|
+
/**
|
|
32
|
+
* 出力するJavaScriptコードのヘッダー部分で定義する必要のある関数。fnはjsのコード。
|
|
33
|
+
* プラグイン関数は含まれない。
|
|
34
|
+
*/
|
|
35
|
+
this.nakoFuncList = { ...com.getNakoFuncList() };
|
|
36
|
+
/**
|
|
37
|
+
* なでしこで定義したテストの一覧
|
|
38
|
+
*/
|
|
39
|
+
this.nakoTestFuncs = {};
|
|
40
|
+
/**
|
|
41
|
+
* プログラム内で参照された関数のリスト。プラグインの命令を含む。
|
|
42
|
+
* JavaScript単体で実行するとき、このリストにある関数の定義をJavaScriptコードの先頭に付け足す。
|
|
43
|
+
*/
|
|
44
|
+
this.usedFuncSet = new Set();
|
|
45
|
+
/**
|
|
46
|
+
* ループ時の一時変数が被らないようにIDで管理
|
|
47
|
+
*/
|
|
48
|
+
this.loopId = 1;
|
|
49
|
+
/**
|
|
50
|
+
* 非同関数を何回使ったか
|
|
51
|
+
*/
|
|
52
|
+
this.numAsyncFn = 0;
|
|
53
|
+
/**
|
|
54
|
+
* 関数定義の際、関数の中でasyncFn=trueの関数を呼び出したかどうかを調べる @see convDefFuncCommon
|
|
55
|
+
*/
|
|
56
|
+
this.usedAsyncFn = false;
|
|
57
|
+
/** 変換中の処理が、ループの中かどうかを判定する */
|
|
58
|
+
this.flagLoop = false;
|
|
59
|
+
this.__self = com;
|
|
60
|
+
/** コードジェネレータの種類 */
|
|
61
|
+
this.genMode = 'sync';
|
|
62
|
+
/** 行番号とファイル名が分かるときは `l123:main.nako3`、行番号だけ分かるときは `l123`、そうでなければ任意の文字列。 */
|
|
63
|
+
this.lastLineNo = null;
|
|
64
|
+
/** スタック */
|
|
65
|
+
this.varslistSet = com.__varslist.map((v) => ({ isFunction: false, names: new Set(Object.keys(v)), readonly: new Set() }));
|
|
66
|
+
/** スタックトップ */
|
|
67
|
+
this.varsSet = { isFunction: false, names: new Set(), readonly: new Set() };
|
|
68
|
+
this.varslistSet[2] = this.varsSet;
|
|
69
|
+
// 1以上のとき高速化する。
|
|
70
|
+
// 実行速度優先ブロック内で1増える。
|
|
71
|
+
this.speedMode = {
|
|
72
|
+
lineNumbers: 0,
|
|
73
|
+
implicitTypeCasting: 0,
|
|
74
|
+
invalidSore: 0,
|
|
75
|
+
forcePure: 0 // 全てのシステム命令をpureとして扱う。命令からローカル変数への参照が出来なくなる。
|
|
76
|
+
};
|
|
77
|
+
// 1以上のとき測定をinjectする。
|
|
78
|
+
// パフォーマンスモニタのブロック内で1増える。
|
|
79
|
+
this.performanceMonitor = {
|
|
80
|
+
userFunction: 0,
|
|
81
|
+
systemFunction: 0,
|
|
82
|
+
systemFunctionBody: 0,
|
|
83
|
+
mumeiId: 0
|
|
84
|
+
};
|
|
85
|
+
/**
|
|
86
|
+
* 未定義の変数の警告を行う
|
|
87
|
+
*/
|
|
88
|
+
this.warnUndefinedVar = true;
|
|
89
|
+
// 暫定変数
|
|
90
|
+
this.warnUndefinedReturnUserFunc = 1;
|
|
91
|
+
this.warnUndefinedCallingUserFunc = 1;
|
|
92
|
+
this.warnUndefinedCallingSystemFunc = 1;
|
|
93
|
+
this.warnUndefinedCalledUserFuncArgs = 1;
|
|
94
|
+
}
|
|
95
|
+
static isValidIdentifier(name) {
|
|
96
|
+
// TODO: いらなそうな部分は削る
|
|
97
|
+
// https://stackoverflow.com/a/9337047
|
|
98
|
+
// eslint-disable-next-line no-misleading-character-class
|
|
99
|
+
return /^(?!(?:do|if|in|for|let|new|try|var|case|else|enum|eval|false|null|this|true|void|with|break|catch|class|const|super|throw|while|yield|delete|export|import|public|return|static|switch|typeof|default|extends|finally|package|private|continue|debugger|function|arguments|interface|protected|implements|instanceof)$)[$A-Z_a-z\xaa\xb5\xba\xc0-\xd6\xd8-\xf6\xf8-\u02c1\u02c6-\u02d1\u02e0-\u02e4\u02ec\u02ee\u0370-\u0374\u0376\u0377\u037a-\u037d\u0386\u0388-\u038a\u038c\u038e-\u03a1\u03a3-\u03f5\u03f7-\u0481\u048a-\u0527\u0531-\u0556\u0559\u0561-\u0587\u05d0-\u05ea\u05f0-\u05f2\u0620-\u064a\u066e\u066f\u0671-\u06d3\u06d5\u06e5\u06e6\u06ee\u06ef\u06fa-\u06fc\u06ff\u0710\u0712-\u072f\u074d-\u07a5\u07b1\u07ca-\u07ea\u07f4\u07f5\u07fa\u0800-\u0815\u081a\u0824\u0828\u0840-\u0858\u08a0\u08a2-\u08ac\u0904-\u0939\u093d\u0950\u0958-\u0961\u0971-\u0977\u0979-\u097f\u0985-\u098c\u098f\u0990\u0993-\u09a8\u09aa-\u09b0\u09b2\u09b6-\u09b9\u09bd\u09ce\u09dc\u09dd\u09df-\u09e1\u09f0\u09f1\u0a05-\u0a0a\u0a0f\u0a10\u0a13-\u0a28\u0a2a-\u0a30\u0a32\u0a33\u0a35\u0a36\u0a38\u0a39\u0a59-\u0a5c\u0a5e\u0a72-\u0a74\u0a85-\u0a8d\u0a8f-\u0a91\u0a93-\u0aa8\u0aaa-\u0ab0\u0ab2\u0ab3\u0ab5-\u0ab9\u0abd\u0ad0\u0ae0\u0ae1\u0b05-\u0b0c\u0b0f\u0b10\u0b13-\u0b28\u0b2a-\u0b30\u0b32\u0b33\u0b35-\u0b39\u0b3d\u0b5c\u0b5d\u0b5f-\u0b61\u0b71\u0b83\u0b85-\u0b8a\u0b8e-\u0b90\u0b92-\u0b95\u0b99\u0b9a\u0b9c\u0b9e\u0b9f\u0ba3\u0ba4\u0ba8-\u0baa\u0bae-\u0bb9\u0bd0\u0c05-\u0c0c\u0c0e-\u0c10\u0c12-\u0c28\u0c2a-\u0c33\u0c35-\u0c39\u0c3d\u0c58\u0c59\u0c60\u0c61\u0c85-\u0c8c\u0c8e-\u0c90\u0c92-\u0ca8\u0caa-\u0cb3\u0cb5-\u0cb9\u0cbd\u0cde\u0ce0\u0ce1\u0cf1\u0cf2\u0d05-\u0d0c\u0d0e-\u0d10\u0d12-\u0d3a\u0d3d\u0d4e\u0d60\u0d61\u0d7a-\u0d7f\u0d85-\u0d96\u0d9a-\u0db1\u0db3-\u0dbb\u0dbd\u0dc0-\u0dc6\u0e01-\u0e30\u0e32\u0e33\u0e40-\u0e46\u0e81\u0e82\u0e84\u0e87\u0e88\u0e8a\u0e8d\u0e94-\u0e97\u0e99-\u0e9f\u0ea1-\u0ea3\u0ea5\u0ea7\u0eaa\u0eab\u0ead-\u0eb0\u0eb2\u0eb3\u0ebd\u0ec0-\u0ec4\u0ec6\u0edc-\u0edf\u0f00\u0f40-\u0f47\u0f49-\u0f6c\u0f88-\u0f8c\u1000-\u102a\u103f\u1050-\u1055\u105a-\u105d\u1061\u1065\u1066\u106e-\u1070\u1075-\u1081\u108e\u10a0-\u10c5\u10c7\u10cd\u10d0-\u10fa\u10fc-\u1248\u124a-\u124d\u1250-\u1256\u1258\u125a-\u125d\u1260-\u1288\u128a-\u128d\u1290-\u12b0\u12b2-\u12b5\u12b8-\u12be\u12c0\u12c2-\u12c5\u12c8-\u12d6\u12d8-\u1310\u1312-\u1315\u1318-\u135a\u1380-\u138f\u13a0-\u13f4\u1401-\u166c\u166f-\u167f\u1681-\u169a\u16a0-\u16ea\u16ee-\u16f0\u1700-\u170c\u170e-\u1711\u1720-\u1731\u1740-\u1751\u1760-\u176c\u176e-\u1770\u1780-\u17b3\u17d7\u17dc\u1820-\u1877\u1880-\u18a8\u18aa\u18b0-\u18f5\u1900-\u191c\u1950-\u196d\u1970-\u1974\u1980-\u19ab\u19c1-\u19c7\u1a00-\u1a16\u1a20-\u1a54\u1aa7\u1b05-\u1b33\u1b45-\u1b4b\u1b83-\u1ba0\u1bae\u1baf\u1bba-\u1be5\u1c00-\u1c23\u1c4d-\u1c4f\u1c5a-\u1c7d\u1ce9-\u1cec\u1cee-\u1cf1\u1cf5\u1cf6\u1d00-\u1dbf\u1e00-\u1f15\u1f18-\u1f1d\u1f20-\u1f45\u1f48-\u1f4d\u1f50-\u1f57\u1f59\u1f5b\u1f5d\u1f5f-\u1f7d\u1f80-\u1fb4\u1fb6-\u1fbc\u1fbe\u1fc2-\u1fc4\u1fc6-\u1fcc\u1fd0-\u1fd3\u1fd6-\u1fdb\u1fe0-\u1fec\u1ff2-\u1ff4\u1ff6-\u1ffc\u2071\u207f\u2090-\u209c\u2102\u2107\u210a-\u2113\u2115\u2119-\u211d\u2124\u2126\u2128\u212a-\u212d\u212f-\u2139\u213c-\u213f\u2145-\u2149\u214e\u2160-\u2188\u2c00-\u2c2e\u2c30-\u2c5e\u2c60-\u2ce4\u2ceb-\u2cee\u2cf2\u2cf3\u2d00-\u2d25\u2d27\u2d2d\u2d30-\u2d67\u2d6f\u2d80-\u2d96\u2da0-\u2da6\u2da8-\u2dae\u2db0-\u2db6\u2db8-\u2dbe\u2dc0-\u2dc6\u2dc8-\u2dce\u2dd0-\u2dd6\u2dd8-\u2dde\u2e2f\u3005-\u3007\u3021-\u3029\u3031-\u3035\u3038-\u303c\u3041-\u3096\u309d-\u309f\u30a1-\u30fa\u30fc-\u30ff\u3105-\u312d\u3131-\u318e\u31a0-\u31ba\u31f0-\u31ff\u3400-\u4db5\u4e00-\u9fcc\ua000-\ua48c\ua4d0-\ua4fd\ua500-\ua60c\ua610-\ua61f\ua62a\ua62b\ua640-\ua66e\ua67f-\ua697\ua6a0-\ua6ef\ua717-\ua71f\ua722-\ua788\ua78b-\ua78e\ua790-\ua793\ua7a0-\ua7aa\ua7f8-\ua801\ua803-\ua805\ua807-\ua80a\ua80c-\ua822\ua840-\ua873\ua882-\ua8b3\ua8f2-\ua8f7\ua8fb\ua90a-\ua925\ua930-\ua946\ua960-\ua97c\ua984-\ua9b2\ua9cf\uaa00-\uaa28\uaa40-\uaa42\uaa44-\uaa4b\uaa60-\uaa76\uaa7a\uaa80-\uaaaf\uaab1\uaab5\uaab6\uaab9-\uaabd\uaac0\uaac2\uaadb-\uaadd\uaae0-\uaaea\uaaf2-\uaaf4\uab01-\uab06\uab09-\uab0e\uab11-\uab16\uab20-\uab26\uab28-\uab2e\uabc0-\uabe2\uac00-\ud7a3\ud7b0-\ud7c6\ud7cb-\ud7fb\uf900-\ufa6d\ufa70-\ufad9\ufb00-\ufb06\ufb13-\ufb17\ufb1d\ufb1f-\ufb28\ufb2a-\ufb36\ufb38-\ufb3c\ufb3e\ufb40\ufb41\ufb43\ufb44\ufb46-\ufbb1\ufbd3-\ufd3d\ufd50-\ufd8f\ufd92-\ufdc7\ufdf0-\ufdfb\ufe70-\ufe74\ufe76-\ufefc\uff21-\uff3a\uff41-\uff5a\uff66-\uffbe\uffc2-\uffc7\uffca-\uffcf\uffd2-\uffd7\uffda-\uffdc][$A-Z_a-z\xaa\xb5\xba\xc0-\xd6\xd8-\xf6\xf8-\u02c1\u02c6-\u02d1\u02e0-\u02e4\u02ec\u02ee\u0370-\u0374\u0376\u0377\u037a-\u037d\u0386\u0388-\u038a\u038c\u038e-\u03a1\u03a3-\u03f5\u03f7-\u0481\u048a-\u0527\u0531-\u0556\u0559\u0561-\u0587\u05d0-\u05ea\u05f0-\u05f2\u0620-\u064a\u066e\u066f\u0671-\u06d3\u06d5\u06e5\u06e6\u06ee\u06ef\u06fa-\u06fc\u06ff\u0710\u0712-\u072f\u074d-\u07a5\u07b1\u07ca-\u07ea\u07f4\u07f5\u07fa\u0800-\u0815\u081a\u0824\u0828\u0840-\u0858\u08a0\u08a2-\u08ac\u0904-\u0939\u093d\u0950\u0958-\u0961\u0971-\u0977\u0979-\u097f\u0985-\u098c\u098f\u0990\u0993-\u09a8\u09aa-\u09b0\u09b2\u09b6-\u09b9\u09bd\u09ce\u09dc\u09dd\u09df-\u09e1\u09f0\u09f1\u0a05-\u0a0a\u0a0f\u0a10\u0a13-\u0a28\u0a2a-\u0a30\u0a32\u0a33\u0a35\u0a36\u0a38\u0a39\u0a59-\u0a5c\u0a5e\u0a72-\u0a74\u0a85-\u0a8d\u0a8f-\u0a91\u0a93-\u0aa8\u0aaa-\u0ab0\u0ab2\u0ab3\u0ab5-\u0ab9\u0abd\u0ad0\u0ae0\u0ae1\u0b05-\u0b0c\u0b0f\u0b10\u0b13-\u0b28\u0b2a-\u0b30\u0b32\u0b33\u0b35-\u0b39\u0b3d\u0b5c\u0b5d\u0b5f-\u0b61\u0b71\u0b83\u0b85-\u0b8a\u0b8e-\u0b90\u0b92-\u0b95\u0b99\u0b9a\u0b9c\u0b9e\u0b9f\u0ba3\u0ba4\u0ba8-\u0baa\u0bae-\u0bb9\u0bd0\u0c05-\u0c0c\u0c0e-\u0c10\u0c12-\u0c28\u0c2a-\u0c33\u0c35-\u0c39\u0c3d\u0c58\u0c59\u0c60\u0c61\u0c85-\u0c8c\u0c8e-\u0c90\u0c92-\u0ca8\u0caa-\u0cb3\u0cb5-\u0cb9\u0cbd\u0cde\u0ce0\u0ce1\u0cf1\u0cf2\u0d05-\u0d0c\u0d0e-\u0d10\u0d12-\u0d3a\u0d3d\u0d4e\u0d60\u0d61\u0d7a-\u0d7f\u0d85-\u0d96\u0d9a-\u0db1\u0db3-\u0dbb\u0dbd\u0dc0-\u0dc6\u0e01-\u0e30\u0e32\u0e33\u0e40-\u0e46\u0e81\u0e82\u0e84\u0e87\u0e88\u0e8a\u0e8d\u0e94-\u0e97\u0e99-\u0e9f\u0ea1-\u0ea3\u0ea5\u0ea7\u0eaa\u0eab\u0ead-\u0eb0\u0eb2\u0eb3\u0ebd\u0ec0-\u0ec4\u0ec6\u0edc-\u0edf\u0f00\u0f40-\u0f47\u0f49-\u0f6c\u0f88-\u0f8c\u1000-\u102a\u103f\u1050-\u1055\u105a-\u105d\u1061\u1065\u1066\u106e-\u1070\u1075-\u1081\u108e\u10a0-\u10c5\u10c7\u10cd\u10d0-\u10fa\u10fc-\u1248\u124a-\u124d\u1250-\u1256\u1258\u125a-\u125d\u1260-\u1288\u128a-\u128d\u1290-\u12b0\u12b2-\u12b5\u12b8-\u12be\u12c0\u12c2-\u12c5\u12c8-\u12d6\u12d8-\u1310\u1312-\u1315\u1318-\u135a\u1380-\u138f\u13a0-\u13f4\u1401-\u166c\u166f-\u167f\u1681-\u169a\u16a0-\u16ea\u16ee-\u16f0\u1700-\u170c\u170e-\u1711\u1720-\u1731\u1740-\u1751\u1760-\u176c\u176e-\u1770\u1780-\u17b3\u17d7\u17dc\u1820-\u1877\u1880-\u18a8\u18aa\u18b0-\u18f5\u1900-\u191c\u1950-\u196d\u1970-\u1974\u1980-\u19ab\u19c1-\u19c7\u1a00-\u1a16\u1a20-\u1a54\u1aa7\u1b05-\u1b33\u1b45-\u1b4b\u1b83-\u1ba0\u1bae\u1baf\u1bba-\u1be5\u1c00-\u1c23\u1c4d-\u1c4f\u1c5a-\u1c7d\u1ce9-\u1cec\u1cee-\u1cf1\u1cf5\u1cf6\u1d00-\u1dbf\u1e00-\u1f15\u1f18-\u1f1d\u1f20-\u1f45\u1f48-\u1f4d\u1f50-\u1f57\u1f59\u1f5b\u1f5d\u1f5f-\u1f7d\u1f80-\u1fb4\u1fb6-\u1fbc\u1fbe\u1fc2-\u1fc4\u1fc6-\u1fcc\u1fd0-\u1fd3\u1fd6-\u1fdb\u1fe0-\u1fec\u1ff2-\u1ff4\u1ff6-\u1ffc\u2071\u207f\u2090-\u209c\u2102\u2107\u210a-\u2113\u2115\u2119-\u211d\u2124\u2126\u2128\u212a-\u212d\u212f-\u2139\u213c-\u213f\u2145-\u2149\u214e\u2160-\u2188\u2c00-\u2c2e\u2c30-\u2c5e\u2c60-\u2ce4\u2ceb-\u2cee\u2cf2\u2cf3\u2d00-\u2d25\u2d27\u2d2d\u2d30-\u2d67\u2d6f\u2d80-\u2d96\u2da0-\u2da6\u2da8-\u2dae\u2db0-\u2db6\u2db8-\u2dbe\u2dc0-\u2dc6\u2dc8-\u2dce\u2dd0-\u2dd6\u2dd8-\u2dde\u2e2f\u3005-\u3007\u3021-\u3029\u3031-\u3035\u3038-\u303c\u3041-\u3096\u309d-\u309f\u30a1-\u30fa\u30fc-\u30ff\u3105-\u312d\u3131-\u318e\u31a0-\u31ba\u31f0-\u31ff\u3400-\u4db5\u4e00-\u9fcc\ua000-\ua48c\ua4d0-\ua4fd\ua500-\ua60c\ua610-\ua61f\ua62a\ua62b\ua640-\ua66e\ua67f-\ua697\ua6a0-\ua6ef\ua717-\ua71f\ua722-\ua788\ua78b-\ua78e\ua790-\ua793\ua7a0-\ua7aa\ua7f8-\ua801\ua803-\ua805\ua807-\ua80a\ua80c-\ua822\ua840-\ua873\ua882-\ua8b3\ua8f2-\ua8f7\ua8fb\ua90a-\ua925\ua930-\ua946\ua960-\ua97c\ua984-\ua9b2\ua9cf\uaa00-\uaa28\uaa40-\uaa42\uaa44-\uaa4b\uaa60-\uaa76\uaa7a\uaa80-\uaaaf\uaab1\uaab5\uaab6\uaab9-\uaabd\uaac0\uaac2\uaadb-\uaadd\uaae0-\uaaea\uaaf2-\uaaf4\uab01-\uab06\uab09-\uab0e\uab11-\uab16\uab20-\uab26\uab28-\uab2e\uabc0-\uabe2\uac00-\ud7a3\ud7b0-\ud7c6\ud7cb-\ud7fb\uf900-\ufa6d\ufa70-\ufad9\ufb00-\ufb06\ufb13-\ufb17\ufb1d\ufb1f-\ufb28\ufb2a-\ufb36\ufb38-\ufb3c\ufb3e\ufb40\ufb41\ufb43\ufb44\ufb46-\ufbb1\ufbd3-\ufd3d\ufd50-\ufd8f\ufd92-\ufdc7\ufdf0-\ufdfb\ufe70-\ufe74\ufe76-\ufefc\uff21-\uff3a\uff41-\uff5a\uff66-\uffbe\uffc2-\uffc7\uffca-\uffcf\uffd2-\uffd7\uffda-\uffdc0-9\u0300-\u036f\u0483-\u0487\u0591-\u05bd\u05bf\u05c1\u05c2\u05c4\u05c5\u05c7\u0610-\u061a\u064b-\u0669\u0670\u06d6-\u06dc\u06df-\u06e4\u06e7\u06e8\u06ea-\u06ed\u06f0-\u06f9\u0711\u0730-\u074a\u07a6-\u07b0\u07c0-\u07c9\u07eb-\u07f3\u0816-\u0819\u081b-\u0823\u0825-\u0827\u0829-\u082d\u0859-\u085b\u08e4-\u08fe\u0900-\u0903\u093a-\u093c\u093e-\u094f\u0951-\u0957\u0962\u0963\u0966-\u096f\u0981-\u0983\u09bc\u09be-\u09c4\u09c7\u09c8\u09cb-\u09cd\u09d7\u09e2\u09e3\u09e6-\u09ef\u0a01-\u0a03\u0a3c\u0a3e-\u0a42\u0a47\u0a48\u0a4b-\u0a4d\u0a51\u0a66-\u0a71\u0a75\u0a81-\u0a83\u0abc\u0abe-\u0ac5\u0ac7-\u0ac9\u0acb-\u0acd\u0ae2\u0ae3\u0ae6-\u0aef\u0b01-\u0b03\u0b3c\u0b3e-\u0b44\u0b47\u0b48\u0b4b-\u0b4d\u0b56\u0b57\u0b62\u0b63\u0b66-\u0b6f\u0b82\u0bbe-\u0bc2\u0bc6-\u0bc8\u0bca-\u0bcd\u0bd7\u0be6-\u0bef\u0c01-\u0c03\u0c3e-\u0c44\u0c46-\u0c48\u0c4a-\u0c4d\u0c55\u0c56\u0c62\u0c63\u0c66-\u0c6f\u0c82\u0c83\u0cbc\u0cbe-\u0cc4\u0cc6-\u0cc8\u0cca-\u0ccd\u0cd5\u0cd6\u0ce2\u0ce3\u0ce6-\u0cef\u0d02\u0d03\u0d3e-\u0d44\u0d46-\u0d48\u0d4a-\u0d4d\u0d57\u0d62\u0d63\u0d66-\u0d6f\u0d82\u0d83\u0dca\u0dcf-\u0dd4\u0dd6\u0dd8-\u0ddf\u0df2\u0df3\u0e31\u0e34-\u0e3a\u0e47-\u0e4e\u0e50-\u0e59\u0eb1\u0eb4-\u0eb9\u0ebb\u0ebc\u0ec8-\u0ecd\u0ed0-\u0ed9\u0f18\u0f19\u0f20-\u0f29\u0f35\u0f37\u0f39\u0f3e\u0f3f\u0f71-\u0f84\u0f86\u0f87\u0f8d-\u0f97\u0f99-\u0fbc\u0fc6\u102b-\u103e\u1040-\u1049\u1056-\u1059\u105e-\u1060\u1062-\u1064\u1067-\u106d\u1071-\u1074\u1082-\u108d\u108f-\u109d\u135d-\u135f\u1712-\u1714\u1732-\u1734\u1752\u1753\u1772\u1773\u17b4-\u17d3\u17dd\u17e0-\u17e9\u180b-\u180d\u1810-\u1819\u18a9\u1920-\u192b\u1930-\u193b\u1946-\u194f\u19b0-\u19c0\u19c8\u19c9\u19d0-\u19d9\u1a17-\u1a1b\u1a55-\u1a5e\u1a60-\u1a7c\u1a7f-\u1a89\u1a90-\u1a99\u1b00-\u1b04\u1b34-\u1b44\u1b50-\u1b59\u1b6b-\u1b73\u1b80-\u1b82\u1ba1-\u1bad\u1bb0-\u1bb9\u1be6-\u1bf3\u1c24-\u1c37\u1c40-\u1c49\u1c50-\u1c59\u1cd0-\u1cd2\u1cd4-\u1ce8\u1ced\u1cf2-\u1cf4\u1dc0-\u1de6\u1dfc-\u1dff\u200c\u200d\u203f\u2040\u2054\u20d0-\u20dc\u20e1\u20e5-\u20f0\u2cef-\u2cf1\u2d7f\u2de0-\u2dff\u302a-\u302f\u3099\u309a\ua620-\ua629\ua66f\ua674-\ua67d\ua69f\ua6f0\ua6f1\ua802\ua806\ua80b\ua823-\ua827\ua880\ua881\ua8b4-\ua8c4\ua8d0-\ua8d9\ua8e0-\ua8f1\ua900-\ua909\ua926-\ua92d\ua947-\ua953\ua980-\ua983\ua9b3-\ua9c0\ua9d0-\ua9d9\uaa29-\uaa36\uaa43\uaa4c\uaa4d\uaa50-\uaa59\uaa7b\uaab0\uaab2-\uaab4\uaab7\uaab8\uaabe\uaabf\uaac1\uaaeb-\uaaef\uaaf5\uaaf6\uabe3-\uabea\uabec\uabed\uabf0-\uabf9\ufb1e\ufe00-\ufe0f\ufe20-\ufe26\ufe33\ufe34\ufe4d-\ufe4f\uff10-\uff19\uff3f]*$/.test(name);
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* @param {Ast} node
|
|
103
|
+
* @param {boolean} forceUpdate
|
|
104
|
+
*/
|
|
105
|
+
convLineno(node, forceUpdate = false) {
|
|
106
|
+
if (this.speedMode.lineNumbers > 0) {
|
|
107
|
+
return '';
|
|
108
|
+
}
|
|
109
|
+
let lineNo;
|
|
110
|
+
if (typeof node.line !== 'number') {
|
|
111
|
+
lineNo = 'unknown';
|
|
112
|
+
}
|
|
113
|
+
else if (typeof node.file !== 'string') {
|
|
114
|
+
lineNo = `l${node.line}`;
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
lineNo = `l${node.line}:${node.file}`;
|
|
118
|
+
}
|
|
119
|
+
// 強制的に行番号をアップデートするか
|
|
120
|
+
if (!forceUpdate) {
|
|
121
|
+
if (lineNo === this.lastLineNo) {
|
|
122
|
+
return '';
|
|
123
|
+
}
|
|
124
|
+
this.lastLineNo = lineNo;
|
|
125
|
+
}
|
|
126
|
+
// 例: __v0.line='l1:main.nako3'
|
|
127
|
+
return `__v0.line=${JSON.stringify(lineNo)};`;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* ローカル変数のJavaScriptコードを生成する。
|
|
131
|
+
* @param {string} name
|
|
132
|
+
*/
|
|
133
|
+
varname(name) {
|
|
134
|
+
if (this.varslistSet.length === 3) {
|
|
135
|
+
// グローバル
|
|
136
|
+
return `__varslist[${2}][${JSON.stringify(name)}]`;
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
// 関数内
|
|
140
|
+
if (NakoGen.isValidIdentifier(name)) {
|
|
141
|
+
return name;
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
return `__vars[${JSON.stringify(name)}]`;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* @param {string} name
|
|
150
|
+
* @returns {string}
|
|
151
|
+
*/
|
|
152
|
+
static getFuncName(name) {
|
|
153
|
+
if (name.indexOf('__') >= 0) { // スコープがある場合
|
|
154
|
+
const a = name.split('__');
|
|
155
|
+
const scope = a[0];
|
|
156
|
+
const name3 = NakoGen.getFuncName(a[1]);
|
|
157
|
+
return `${scope}__${name3}`;
|
|
158
|
+
}
|
|
159
|
+
let name2 = name.replace(/[ぁ-ん]+$/, '');
|
|
160
|
+
if (name2 === '') {
|
|
161
|
+
name2 = name;
|
|
162
|
+
}
|
|
163
|
+
return name2;
|
|
164
|
+
}
|
|
165
|
+
/** @param {Ast} node */
|
|
166
|
+
static convPrint(node) {
|
|
167
|
+
return `__print(${node});`;
|
|
168
|
+
}
|
|
169
|
+
/** @param {Ast} node */
|
|
170
|
+
convRequire(node) {
|
|
171
|
+
const moduleName = node.value;
|
|
172
|
+
return this.convLineno(node, false) +
|
|
173
|
+
`__module['${moduleName}'] = require('${moduleName}');\n`;
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* プログラムの実行に必要な関数定義を書き出す(グローバル領域)
|
|
177
|
+
* convGenの結果を利用するため、convGenの後に呼び出すこと。
|
|
178
|
+
* @param com
|
|
179
|
+
* @param opt
|
|
180
|
+
*/
|
|
181
|
+
getDefFuncCode(com, opt) {
|
|
182
|
+
let code = '';
|
|
183
|
+
// よく使う変数のショートカット
|
|
184
|
+
code += `const nakoVersion = { version: ${JSON.stringify(com.version)} }\n`;
|
|
185
|
+
code += 'const __self = self;\n';
|
|
186
|
+
code += 'self.__self = self;\n';
|
|
187
|
+
code += 'const __varslist = self.__varslist;\n';
|
|
188
|
+
code += 'const __module = self.__module;\n';
|
|
189
|
+
code += 'const __v0 = self.__v0 = self.__varslist[0];\n';
|
|
190
|
+
code += 'const __v1 = self.__v1 = self.__varslist[1];\n';
|
|
191
|
+
code += 'const __vars = self.__vars = self.__varslist[2];\n';
|
|
192
|
+
// なでしこの関数定義を行う
|
|
193
|
+
let nakoFuncCode = '';
|
|
194
|
+
for (const key in this.nakoFuncList) {
|
|
195
|
+
const f = this.nakoFuncList[key].fn;
|
|
196
|
+
const isAsync = this.nakoFuncList[key].asyncFn ? 'true' : 'false';
|
|
197
|
+
nakoFuncCode += '' +
|
|
198
|
+
`//[DEF_FUNC name='${key}' asyncFn=${isAsync}]\n` +
|
|
199
|
+
`self.__varslist[1]["${key}"]=${f};\n;` +
|
|
200
|
+
`//[/DEF_FUNC name='${key}']\n`;
|
|
201
|
+
}
|
|
202
|
+
if (nakoFuncCode !== '') {
|
|
203
|
+
code += '__v0.line=\'関数の定義\';\n' + nakoFuncCode;
|
|
204
|
+
}
|
|
205
|
+
// テストの定義を行う
|
|
206
|
+
if (opt.isTest) {
|
|
207
|
+
let testCode = 'const __tests = [];\n';
|
|
208
|
+
for (const key in this.nakoTestFuncs) {
|
|
209
|
+
const f = this.nakoTestFuncs[key].fn;
|
|
210
|
+
testCode += `${f};\n;`;
|
|
211
|
+
}
|
|
212
|
+
if (testCode !== '') {
|
|
213
|
+
code += '__v0.line=\'テストの定義\';\n';
|
|
214
|
+
code += testCode + '\n';
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
return code;
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* プラグイン・オブジェクトを追加
|
|
221
|
+
* @param po プラグイン・オブジェクト
|
|
222
|
+
*/
|
|
223
|
+
addPlugin(po) {
|
|
224
|
+
return this.__self.addPlugin(po);
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* プラグイン・オブジェクトを追加(ブラウザ向け)
|
|
228
|
+
* @param name オブジェクト名
|
|
229
|
+
* @param po 関数リスト
|
|
230
|
+
*/
|
|
231
|
+
addPluginObject(name, po) {
|
|
232
|
+
this.__self.addPluginObject(name, po);
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* プラグイン・ファイルを追加(Node.js向け)
|
|
236
|
+
* @param objName オブジェクト名
|
|
237
|
+
* @param path ファイルパス
|
|
238
|
+
* @param po 登録するオブジェクト
|
|
239
|
+
*/
|
|
240
|
+
addPluginFile(objName, path, po) {
|
|
241
|
+
this.__self.addPluginFile(objName, path, po);
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* 関数を追加する
|
|
245
|
+
* @param key 関数名
|
|
246
|
+
* @param josi 助詞
|
|
247
|
+
* @param fn 関数
|
|
248
|
+
*/
|
|
249
|
+
addFunc(key, josi, fn) {
|
|
250
|
+
this.__self.addFunc(key, josi, fn);
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* プラグイン関数を参照する
|
|
254
|
+
* @param key プラグイン関数の関数名
|
|
255
|
+
* @returns プラグイン・オブジェクト
|
|
256
|
+
*/
|
|
257
|
+
getFunc(key) {
|
|
258
|
+
return this.__self.getFunc(key);
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* 関数を先に登録してしまう
|
|
262
|
+
*/
|
|
263
|
+
registerFunction(ast) {
|
|
264
|
+
if (ast.type !== 'block') {
|
|
265
|
+
throw NakoSyntaxError.fromNode('構文解析に失敗しています。構文は必ずblockが先頭になります', ast);
|
|
266
|
+
}
|
|
267
|
+
/** 関数一覧 */
|
|
268
|
+
const funcList = [];
|
|
269
|
+
// なでしこ関数を定義して this.nako_func[name] に定義する
|
|
270
|
+
const registFunc = (node) => {
|
|
271
|
+
if (!node.block) {
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
const blockList = (node.block instanceof Array) ? node.block : [node.block];
|
|
275
|
+
for (let i = 0; i < blockList.length; i++) {
|
|
276
|
+
const t = blockList[i];
|
|
277
|
+
if (t.type === 'def_func') {
|
|
278
|
+
if (!t.name) {
|
|
279
|
+
throw new Error('[System Error] 関数の定義で関数名が指定されていない');
|
|
280
|
+
}
|
|
281
|
+
const name = t.name.value;
|
|
282
|
+
this.usedFuncSet.add(name);
|
|
283
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
284
|
+
this.__self.__varslist[1][name] = function () { }; // 事前に適当な値を設定
|
|
285
|
+
this.varslistSet[1].names.add(name); // global
|
|
286
|
+
const meta = (t.name).meta; // todo: 強制変換したが正しいかチェック
|
|
287
|
+
this.nakoFuncList[name] = {
|
|
288
|
+
josi: meta.josi,
|
|
289
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
290
|
+
fn: () => { },
|
|
291
|
+
type: 'func',
|
|
292
|
+
asyncFn: t.asyncFn
|
|
293
|
+
};
|
|
294
|
+
funcList.push({ name, node: t });
|
|
295
|
+
// eslint-disable-next-line brace-style
|
|
296
|
+
}
|
|
297
|
+
// 実行速度優先 などのオプションが付いている場合の処理
|
|
298
|
+
else if (t.type === 'speed_mode') {
|
|
299
|
+
if (!t.block) {
|
|
300
|
+
continue;
|
|
301
|
+
}
|
|
302
|
+
if (t.block.type === 'block') {
|
|
303
|
+
registFunc(t.block);
|
|
304
|
+
}
|
|
305
|
+
else {
|
|
306
|
+
registFunc(t);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
else if (t.type === 'performance_monitor') {
|
|
310
|
+
if (!t.block) {
|
|
311
|
+
continue;
|
|
312
|
+
}
|
|
313
|
+
if (t.block.type === 'block') {
|
|
314
|
+
registFunc(t.block);
|
|
315
|
+
}
|
|
316
|
+
else {
|
|
317
|
+
registFunc(t);
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
};
|
|
322
|
+
// 関数の登録
|
|
323
|
+
registFunc(ast);
|
|
324
|
+
// __self.__varslistの変更を反映
|
|
325
|
+
const initialNames = new Set();
|
|
326
|
+
if (this.speedMode.invalidSore === 0) {
|
|
327
|
+
initialNames.add('それ');
|
|
328
|
+
}
|
|
329
|
+
this.varsSet = { isFunction: false, names: initialNames, readonly: new Set() };
|
|
330
|
+
this.varslistSet = this.__self.__varslist.map((v) => ({ isFunction: false, names: new Set(Object.keys(v)), readonly: new Set() }));
|
|
331
|
+
this.varslistSet[2] = this.varsSet;
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* @param node
|
|
335
|
+
* @param opt
|
|
336
|
+
*/
|
|
337
|
+
convGen(node, opt) {
|
|
338
|
+
const result = this.convLineno(node, false) + this._convGen(node, true);
|
|
339
|
+
if (opt.isTest) {
|
|
340
|
+
return '';
|
|
341
|
+
}
|
|
342
|
+
else {
|
|
343
|
+
return result;
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* @param {Ast} node
|
|
348
|
+
* @param {boolean} isExpression
|
|
349
|
+
*/
|
|
350
|
+
_convGen(node, isExpression) {
|
|
351
|
+
if (!node) {
|
|
352
|
+
return '';
|
|
353
|
+
}
|
|
354
|
+
let code = '';
|
|
355
|
+
if (node instanceof Array) {
|
|
356
|
+
for (let i = 0; i < node.length; i++) {
|
|
357
|
+
const n = node[i];
|
|
358
|
+
code += this._convGen(n, isExpression);
|
|
359
|
+
}
|
|
360
|
+
return code;
|
|
361
|
+
}
|
|
362
|
+
if (node === null) {
|
|
363
|
+
return 'null';
|
|
364
|
+
}
|
|
365
|
+
if (node === undefined) {
|
|
366
|
+
return 'undefined';
|
|
367
|
+
}
|
|
368
|
+
if (typeof (node) !== 'object') {
|
|
369
|
+
return '' + node;
|
|
370
|
+
}
|
|
371
|
+
// switch
|
|
372
|
+
switch (node.type) {
|
|
373
|
+
case 'nop':
|
|
374
|
+
break;
|
|
375
|
+
case 'block':
|
|
376
|
+
// eslint-disable-next-line no-case-declarations
|
|
377
|
+
const modName = NakoLexer.filenameToModName(node.file || '');
|
|
378
|
+
code += `;__self.__modName='${modName}';\n`;
|
|
379
|
+
if (!node.block) {
|
|
380
|
+
return code;
|
|
381
|
+
}
|
|
382
|
+
// eslint-disable-next-line no-case-declarations
|
|
383
|
+
const blocks = (node.block instanceof Array) ? node.block : [node.block];
|
|
384
|
+
for (let i = 0; i < blocks.length; i++) {
|
|
385
|
+
const b = blocks[i];
|
|
386
|
+
code += this._convGen(b, false);
|
|
387
|
+
}
|
|
388
|
+
break;
|
|
389
|
+
case 'comment':
|
|
390
|
+
case 'eol':
|
|
391
|
+
code += this.convComment(node);
|
|
392
|
+
break;
|
|
393
|
+
case 'break':
|
|
394
|
+
code += this.convCheckLoop(node, 'break');
|
|
395
|
+
break;
|
|
396
|
+
case 'continue':
|
|
397
|
+
code += this.convCheckLoop(node, 'continue');
|
|
398
|
+
break;
|
|
399
|
+
case 'end':
|
|
400
|
+
code += '__varslist[0][\'終\']();';
|
|
401
|
+
break;
|
|
402
|
+
case 'number':
|
|
403
|
+
code += node.value;
|
|
404
|
+
break;
|
|
405
|
+
case 'string':
|
|
406
|
+
code += this.convString(node);
|
|
407
|
+
break;
|
|
408
|
+
case 'def_local_var':
|
|
409
|
+
code += this.convDefLocalVar(node);
|
|
410
|
+
break;
|
|
411
|
+
case 'def_local_varlist':
|
|
412
|
+
code += this.convDefLocalVarlist(node);
|
|
413
|
+
break;
|
|
414
|
+
case 'let':
|
|
415
|
+
code += this.convLet(node);
|
|
416
|
+
break;
|
|
417
|
+
case 'inc':
|
|
418
|
+
code += this.convInc(node);
|
|
419
|
+
break;
|
|
420
|
+
case 'word':
|
|
421
|
+
case 'variable':
|
|
422
|
+
code += this.convGetVar(node);
|
|
423
|
+
break;
|
|
424
|
+
case 'op':
|
|
425
|
+
case 'calc':
|
|
426
|
+
code += this.convOp(node);
|
|
427
|
+
break;
|
|
428
|
+
case 'renbun':
|
|
429
|
+
code += this.convRenbun(node);
|
|
430
|
+
break;
|
|
431
|
+
case 'not':
|
|
432
|
+
code += '((' + this._convGen(node.value, true) + ')?0:1)';
|
|
433
|
+
break;
|
|
434
|
+
case 'func':
|
|
435
|
+
case 'func_pointer':
|
|
436
|
+
case 'calc_func':
|
|
437
|
+
code += this.convCallFunc(node, isExpression);
|
|
438
|
+
break;
|
|
439
|
+
case 'if':
|
|
440
|
+
code += this.convIf(node);
|
|
441
|
+
break;
|
|
442
|
+
case 'tikuji':
|
|
443
|
+
code += this.convTikuji(node);
|
|
444
|
+
break;
|
|
445
|
+
case 'for':
|
|
446
|
+
code += this.convFor(node);
|
|
447
|
+
break;
|
|
448
|
+
case 'foreach':
|
|
449
|
+
code += this.convForeach(node);
|
|
450
|
+
break;
|
|
451
|
+
case 'repeat_times':
|
|
452
|
+
code += this.convRepeatTimes(node);
|
|
453
|
+
break;
|
|
454
|
+
case 'speed_mode':
|
|
455
|
+
code += this.convSpeedMode(node, isExpression);
|
|
456
|
+
break;
|
|
457
|
+
case 'performance_monitor':
|
|
458
|
+
code += this.convPerformanceMonitor(node, isExpression);
|
|
459
|
+
break;
|
|
460
|
+
case 'while':
|
|
461
|
+
code += this.convWhile(node);
|
|
462
|
+
break;
|
|
463
|
+
case 'atohantei':
|
|
464
|
+
code += this.convAtohantei(node);
|
|
465
|
+
break;
|
|
466
|
+
case 'switch':
|
|
467
|
+
code += this.convSwitch(node);
|
|
468
|
+
break;
|
|
469
|
+
case 'let_array':
|
|
470
|
+
code += this.convLetArray(node);
|
|
471
|
+
break;
|
|
472
|
+
case '配列参照':
|
|
473
|
+
code += this.convRefArray(node);
|
|
474
|
+
break;
|
|
475
|
+
case 'json_array':
|
|
476
|
+
code += this.convJsonArray(node);
|
|
477
|
+
break;
|
|
478
|
+
case 'json_obj':
|
|
479
|
+
code += this.convJsonObj(node);
|
|
480
|
+
break;
|
|
481
|
+
case 'func_obj':
|
|
482
|
+
code += this.convFuncObj(node);
|
|
483
|
+
break;
|
|
484
|
+
case 'bool':
|
|
485
|
+
code += (node.value) ? 'true' : 'false';
|
|
486
|
+
break;
|
|
487
|
+
case 'null':
|
|
488
|
+
code += 'null';
|
|
489
|
+
break;
|
|
490
|
+
case 'def_test':
|
|
491
|
+
code += this.convDefTest(node);
|
|
492
|
+
break;
|
|
493
|
+
case 'def_func':
|
|
494
|
+
code += this.convDefFunc(node);
|
|
495
|
+
break;
|
|
496
|
+
case 'return':
|
|
497
|
+
code += this.convReturn(node);
|
|
498
|
+
break;
|
|
499
|
+
case 'try_except':
|
|
500
|
+
code += this.convTryExcept(node);
|
|
501
|
+
break;
|
|
502
|
+
case 'require':
|
|
503
|
+
code += this.convRequire(node);
|
|
504
|
+
break;
|
|
505
|
+
default:
|
|
506
|
+
throw new Error('System Error: unknown_type=' + node.type);
|
|
507
|
+
}
|
|
508
|
+
return code;
|
|
509
|
+
}
|
|
510
|
+
/** 変数を検索 */
|
|
511
|
+
findVar(name) {
|
|
512
|
+
// __vars ? (ローカル変数)
|
|
513
|
+
if (this.varslistSet.length > 3 && this.varsSet.names.has(name)) {
|
|
514
|
+
return { i: this.varslistSet.length - 1, name, isTop: true, js: this.varname(name) };
|
|
515
|
+
}
|
|
516
|
+
// __varslist ?
|
|
517
|
+
for (let i = 2; i >= 0; i--) {
|
|
518
|
+
if (this.varslistSet[i].names.has(name)) {
|
|
519
|
+
// ユーザーの定義したグローバル変数 (__varslist[2]) は、変数展開されている(そのままの名前で定義されている)可能性がある。
|
|
520
|
+
// それ以外の変数は、必ず__varslistに入っている。
|
|
521
|
+
return { i, name, isTop: false, js: `__varslist[${i}][${JSON.stringify(name)}]` };
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
return null;
|
|
525
|
+
}
|
|
526
|
+
/**
|
|
527
|
+
* 定義済みの変数の参照
|
|
528
|
+
* @param {string} name
|
|
529
|
+
* @param {Ast} position
|
|
530
|
+
*/
|
|
531
|
+
genVar(name, position) {
|
|
532
|
+
const res = this.findVar(name);
|
|
533
|
+
const lno = position.line;
|
|
534
|
+
if (res === null) {
|
|
535
|
+
// 定義されていない名前の参照は変数の定義とみなす。
|
|
536
|
+
// 多くの場合はundefined値を持つ変数であり分かりづらいバグを引き起こすが、
|
|
537
|
+
// 「ナデシコする」などの命令の中で定義された変数の参照の場合があるため警告に留める。
|
|
538
|
+
// ただし、自動的に定義される変数『引数』『それ』などは例外 #952
|
|
539
|
+
if (name === '引数' || name === 'それ' || name === '対象' || name === '対象キー') {
|
|
540
|
+
// デフォルト定義されている変数名
|
|
541
|
+
}
|
|
542
|
+
else {
|
|
543
|
+
if (this.warnUndefinedVar) {
|
|
544
|
+
// main__は省略して表示するように。 #1223
|
|
545
|
+
const dispName = name.replace(/^main__(.+)$/, '$1');
|
|
546
|
+
this.__self.getLogger().warn(`変数『${dispName}』は定義されていません。`, position);
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
this.varsSet.names.add(name);
|
|
550
|
+
return this.varname(name);
|
|
551
|
+
}
|
|
552
|
+
const i = res.i;
|
|
553
|
+
// システム関数・変数の場合
|
|
554
|
+
if (i === 0) {
|
|
555
|
+
const pv = this.__self.getNakoFunc(name);
|
|
556
|
+
if (!pv) {
|
|
557
|
+
return `${res.js}/*err:${lno}*/`;
|
|
558
|
+
}
|
|
559
|
+
if (pv.type === 'const' || pv.type === 'var') {
|
|
560
|
+
return res.js;
|
|
561
|
+
}
|
|
562
|
+
if (pv.type === 'func') {
|
|
563
|
+
if (!pv.josi || pv.josi.length === 0) {
|
|
564
|
+
return `(${res.js}())`;
|
|
565
|
+
}
|
|
566
|
+
throw NakoSyntaxError.fromNode(`『${name}』が複文で使われました。単文で記述してください。(v1非互換)`, position);
|
|
567
|
+
}
|
|
568
|
+
throw NakoSyntaxError.fromNode(`『${name}』は関数であり参照できません。`, position);
|
|
569
|
+
}
|
|
570
|
+
return res.js;
|
|
571
|
+
}
|
|
572
|
+
convGetVar(node) {
|
|
573
|
+
const name = node.value;
|
|
574
|
+
return this.genVar(name, node);
|
|
575
|
+
}
|
|
576
|
+
convComment(node) {
|
|
577
|
+
let commentSrc = String(node.value);
|
|
578
|
+
commentSrc = commentSrc.replace(/\n/g, '¶');
|
|
579
|
+
const lineNo = this.convLineno(node, false);
|
|
580
|
+
if (commentSrc === '' && lineNo === '') {
|
|
581
|
+
return ';';
|
|
582
|
+
}
|
|
583
|
+
if (commentSrc === '') {
|
|
584
|
+
return ';' + lineNo + '\n';
|
|
585
|
+
}
|
|
586
|
+
return ';' + lineNo + '//' + commentSrc + '\n';
|
|
587
|
+
}
|
|
588
|
+
convReturn(node) {
|
|
589
|
+
// 関数の中であれば利用可能
|
|
590
|
+
if (this.varsSet.names.has('!関数')) {
|
|
591
|
+
throw NakoSyntaxError.fromNode('『戻る』がありますが、関数定義内のみで使用可能です。', node);
|
|
592
|
+
}
|
|
593
|
+
const lno = this.convLineno(node, false);
|
|
594
|
+
let value;
|
|
595
|
+
if (node.value) {
|
|
596
|
+
value = this._convGen(node.value, true);
|
|
597
|
+
}
|
|
598
|
+
else if (this.speedMode.invalidSore === 0) {
|
|
599
|
+
value = this.varname('それ');
|
|
600
|
+
}
|
|
601
|
+
else {
|
|
602
|
+
return lno + 'return;';
|
|
603
|
+
}
|
|
604
|
+
if (this.warnUndefinedReturnUserFunc === 0) {
|
|
605
|
+
return lno + `return ${value};`;
|
|
606
|
+
}
|
|
607
|
+
else {
|
|
608
|
+
return lno + `return (function(a){if(a===undefined){__self.logger.warn('ユーザ関数からundefinedが返されています',{file:'${node.file}',line:${node.line}});};return a;})(${value});`;
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
convCheckLoop(node, cmd) {
|
|
612
|
+
// ループの中であれば利用可能
|
|
613
|
+
if (!this.flagLoop) {
|
|
614
|
+
const cmdj = (cmd === 'continue') ? '続ける' : '抜ける';
|
|
615
|
+
throw NakoSyntaxError.fromNode(`『${cmdj}』文がありますが、それは繰り返しの中で利用してください。`, node);
|
|
616
|
+
}
|
|
617
|
+
return this.convLineno(node) + cmd + ';';
|
|
618
|
+
}
|
|
619
|
+
convDefFuncCommon(node, name) {
|
|
620
|
+
// パフォーマンスモニタ:ユーザ関数のinjectの定義
|
|
621
|
+
let performanceMonitorInjectAtStart = '';
|
|
622
|
+
let performanceMonitorInjectAtEnd = '';
|
|
623
|
+
if (this.performanceMonitor.userFunction !== 0) {
|
|
624
|
+
let key = name;
|
|
625
|
+
if (!key) {
|
|
626
|
+
if (typeof this.performanceMonitor.mumeiId === 'undefined') {
|
|
627
|
+
this.performanceMonitor.mumeiId = 0;
|
|
628
|
+
}
|
|
629
|
+
this.performanceMonitor.mumeiId++;
|
|
630
|
+
key = `anous_${this.performanceMonitor.mumeiId}`;
|
|
631
|
+
}
|
|
632
|
+
performanceMonitorInjectAtStart = 'const performanceMonitorEnd = (function (key, type) {\n' +
|
|
633
|
+
'const uf_start = performance.now() * 1000;\n' +
|
|
634
|
+
'return function () {\n' +
|
|
635
|
+
'const el_time = performance.now() * 1000 - uf_start;\n' +
|
|
636
|
+
'if (!__self.__performance_monitor) {\n' +
|
|
637
|
+
'__self.__performance_monitor={};\n' +
|
|
638
|
+
'__self.__performance_monitor[key] = { called:1, totel_usec: el_time, min_usec: el_time, max_usec: el_time, type: type };\n' +
|
|
639
|
+
'} else if (!__self.__performance_monitor[key]) {\n' +
|
|
640
|
+
'__self.__performance_monitor[key] = { called:1, totel_usec: el_time, min_usec: el_time, max_usec: el_time, type: type };\n' +
|
|
641
|
+
'} else {\n' +
|
|
642
|
+
'__self.__performance_monitor[key].called++;\n' +
|
|
643
|
+
'__self.__performance_monitor[key].totel_usec+=el_time;\n' +
|
|
644
|
+
'if(__self.__performance_monitor[key].min_usec>el_time){__self.__performance_monitor[key].min_usec=el_time;}\n' +
|
|
645
|
+
'if(__self.__performance_monitor[key].max_usec<el_time){__self.__performance_monitor[key].max_usec=el_time;}\n' +
|
|
646
|
+
`}};})('${key}', 'user');` +
|
|
647
|
+
'try {\n';
|
|
648
|
+
performanceMonitorInjectAtEnd = '} finally { performanceMonitorEnd(); }\n';
|
|
649
|
+
}
|
|
650
|
+
let variableDeclarations = '';
|
|
651
|
+
const popStack = '';
|
|
652
|
+
const initialNames = new Set();
|
|
653
|
+
if (this.speedMode.invalidSore === 0) {
|
|
654
|
+
initialNames.add('それ');
|
|
655
|
+
}
|
|
656
|
+
this.varsSet = { isFunction: true, names: initialNames, readonly: new Set() };
|
|
657
|
+
// ローカル変数をPUSHする
|
|
658
|
+
this.varslistSet.push(this.varsSet);
|
|
659
|
+
// JSの引数と引数をバインド
|
|
660
|
+
variableDeclarations += ' var 引数 = arguments;\n';
|
|
661
|
+
// ローカル変数を生成
|
|
662
|
+
variableDeclarations += ' var __vars = {};\n';
|
|
663
|
+
// 宣言済みの名前を保存
|
|
664
|
+
const varsDeclared = Array.from(this.varsSet.names.values());
|
|
665
|
+
let code = '';
|
|
666
|
+
// 引数をローカル変数に設定
|
|
667
|
+
const meta = (!name) ? node.meta : node.name.meta;
|
|
668
|
+
for (let i = 0; i < meta.varnames.length; i++) {
|
|
669
|
+
const word = meta.varnames[i];
|
|
670
|
+
if (this.warnUndefinedCalledUserFuncArgs === 0) {
|
|
671
|
+
code += ` ${this.varname(word)} = arguments[${i}];\n`;
|
|
672
|
+
}
|
|
673
|
+
else if (name) {
|
|
674
|
+
code += ` ${this.varname(word)} = (function(a){if(a===undefined){__self.logger.warn('ユーザ関数(${name})の引数(${this.varname(word)})にundefinedが渡されました',{file:'${node.file}',line:${node.line}});};return a;})(arguments[${i}]);\n`;
|
|
675
|
+
}
|
|
676
|
+
else {
|
|
677
|
+
code += ` ${this.varname(word)} = (function(a){if(a===undefined){__self.logger.warn('匿名関数の引数(${this.varname(word)})にundefinedが渡されました',{file:'${node.file}',line:${node.line}});};return a;})(arguments[${i}]);\n`;
|
|
678
|
+
}
|
|
679
|
+
this.varsSet.names.add(word);
|
|
680
|
+
}
|
|
681
|
+
// 関数定義は、グローバル領域で。
|
|
682
|
+
if (name) {
|
|
683
|
+
this.usedFuncSet.add(name);
|
|
684
|
+
this.varslistSet[1].names.add(name);
|
|
685
|
+
if (this.nakoFuncList[name] === undefined) {
|
|
686
|
+
// 既に generate で作成済みのはず(念のため)
|
|
687
|
+
this.nakoFuncList[name] = {
|
|
688
|
+
josi: node.name.meta.josi,
|
|
689
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
690
|
+
fn: () => { },
|
|
691
|
+
type: 'func',
|
|
692
|
+
asyncFn: false
|
|
693
|
+
};
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
// ブロックを解析
|
|
697
|
+
const oldUsedAsyncFn = this.usedAsyncFn;
|
|
698
|
+
this.usedAsyncFn = false;
|
|
699
|
+
const block = this._convGen(node.block, false);
|
|
700
|
+
code += block.split('\n').map((line) => ' ' + line).join('\n') + '\n';
|
|
701
|
+
// 関数の最後に、変数「それ」をreturnするようにする
|
|
702
|
+
if (this.speedMode.invalidSore === 0) {
|
|
703
|
+
code += ` return (${this.varname('それ')});\n`;
|
|
704
|
+
}
|
|
705
|
+
// パフォーマンスモニタ:ユーザ関数のinject
|
|
706
|
+
code += performanceMonitorInjectAtEnd;
|
|
707
|
+
// ブロックでasyncFnを使ったか
|
|
708
|
+
if (name && this.usedAsyncFn) {
|
|
709
|
+
this.nakoFuncList[name].asyncFn = true;
|
|
710
|
+
}
|
|
711
|
+
// 関数の末尾に、ローカル変数をPOP
|
|
712
|
+
// 関数内で定義されたローカル変数の宣言
|
|
713
|
+
for (const name of Array.from(this.varsSet.names.values())) {
|
|
714
|
+
if (!varsDeclared.includes(name)) {
|
|
715
|
+
if (NakoGen.isValidIdentifier(name)) {
|
|
716
|
+
variableDeclarations += ` var ${name};\n`;
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
if (this.speedMode.invalidSore === 0) {
|
|
721
|
+
if (NakoGen.isValidIdentifier('それ')) {
|
|
722
|
+
variableDeclarations += ' var それ = \'\';\n';
|
|
723
|
+
}
|
|
724
|
+
else {
|
|
725
|
+
variableDeclarations += ` ${this.varname('それ')} = '';`;
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
// usedAsyncFnの値に応じて関数定義の方法を変更
|
|
729
|
+
const tof = (this.usedAsyncFn) ? topOfFunctionAsync : topOfFunction;
|
|
730
|
+
// 関数コード全体を構築
|
|
731
|
+
code = tof + performanceMonitorInjectAtStart + variableDeclarations + code + popStack;
|
|
732
|
+
code += endOfFunction;
|
|
733
|
+
// 名前があれば、関数を登録する
|
|
734
|
+
if (name) {
|
|
735
|
+
this.nakoFuncList[name].fn = code;
|
|
736
|
+
this.nakoFuncList[name].asyncFn = this.usedAsyncFn;
|
|
737
|
+
meta.asyncFn = this.usedAsyncFn;
|
|
738
|
+
}
|
|
739
|
+
this.usedAsyncFn = oldUsedAsyncFn; // 以前の値を戻す
|
|
740
|
+
this.varslistSet.pop();
|
|
741
|
+
this.varsSet = this.varslistSet[this.varslistSet.length - 1];
|
|
742
|
+
if (name) {
|
|
743
|
+
this.__self.__varslist[1][name] = code;
|
|
744
|
+
}
|
|
745
|
+
return code;
|
|
746
|
+
}
|
|
747
|
+
convDefTest(node) {
|
|
748
|
+
const name = node.name.value;
|
|
749
|
+
let code = `__tests.push({ name: '${name}', f: () => {\n`;
|
|
750
|
+
// ブロックを解析
|
|
751
|
+
const block = this._convGen(node.block, false);
|
|
752
|
+
code += ` ${block}\n` +
|
|
753
|
+
'}});';
|
|
754
|
+
this.nakoTestFuncs[name] = {
|
|
755
|
+
josi: node.name.meta.josi,
|
|
756
|
+
fn: code,
|
|
757
|
+
type: 'test_func'
|
|
758
|
+
};
|
|
759
|
+
// ★この時点ではテストコードを生成しない★
|
|
760
|
+
// プログラム冒頭でコード生成時にテストの定義を行う
|
|
761
|
+
return '';
|
|
762
|
+
}
|
|
763
|
+
convDefFunc(node) {
|
|
764
|
+
// ※ [関数定義のメモ]
|
|
765
|
+
// ※ 関数の定義はプログラムの冒頭に移される。
|
|
766
|
+
// ※ そのため、生成されたコードはここでは返さない
|
|
767
|
+
// ※ registerFunction を参照
|
|
768
|
+
if (!node.name) {
|
|
769
|
+
return '';
|
|
770
|
+
}
|
|
771
|
+
const name = NakoGen.getFuncName(node.name.value);
|
|
772
|
+
this.convDefFuncCommon(node, name);
|
|
773
|
+
return '';
|
|
774
|
+
}
|
|
775
|
+
convFuncObj(node) {
|
|
776
|
+
return this.convDefFuncCommon(node, '');
|
|
777
|
+
}
|
|
778
|
+
convJsonObj(node) {
|
|
779
|
+
const list = node.value;
|
|
780
|
+
const codelist = list.map((e) => {
|
|
781
|
+
const key = this._convGen(e.key, true);
|
|
782
|
+
const val = this._convGen(e.value, true);
|
|
783
|
+
return `${key}:${val}`;
|
|
784
|
+
});
|
|
785
|
+
return '{' + codelist.join(',') + '}';
|
|
786
|
+
}
|
|
787
|
+
convJsonArray(node) {
|
|
788
|
+
const list = node.value;
|
|
789
|
+
const codelist = list.map((e) => {
|
|
790
|
+
return this._convGen(e, true);
|
|
791
|
+
});
|
|
792
|
+
return '[' + codelist.join(',') + ']';
|
|
793
|
+
}
|
|
794
|
+
convRefArray(node) {
|
|
795
|
+
const name = this._convGen(node.name, true);
|
|
796
|
+
const list = node.index;
|
|
797
|
+
let code = name;
|
|
798
|
+
if (!list) {
|
|
799
|
+
return code;
|
|
800
|
+
}
|
|
801
|
+
for (let i = 0; i < list.length; i++) {
|
|
802
|
+
const idx = this._convGen(list[i], true);
|
|
803
|
+
code += '[' + idx + ']';
|
|
804
|
+
}
|
|
805
|
+
return code;
|
|
806
|
+
}
|
|
807
|
+
convLetArray(node) {
|
|
808
|
+
const name = this._convGen(node.name, true);
|
|
809
|
+
const list = node.index || [];
|
|
810
|
+
let codeInit = '';
|
|
811
|
+
let code = name;
|
|
812
|
+
let codeArray = '';
|
|
813
|
+
// codeInit?
|
|
814
|
+
if (node.checkInit) {
|
|
815
|
+
const arrayDefCode = '[0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0]';
|
|
816
|
+
codeInit += `\n/*配列初期化*/if (!(${name} instanceof Array)) { ${name} = ${arrayDefCode}; console.log('初期化:${name}') };`;
|
|
817
|
+
for (let i = 0; i < list.length - 1; i++) {
|
|
818
|
+
const idx = this._convGen(list[i], true);
|
|
819
|
+
codeArray += `[${idx}]`;
|
|
820
|
+
codeInit += `\n/*配列初期化${i}*/if (!(${name}${codeArray} instanceof Array)) { ${name}${codeArray} = ${arrayDefCode}; };`;
|
|
821
|
+
// codeInit += `\n/*配列初期化${i}*/if (!(${name}${codeArray} instanceof Array)) { ${name}${codeArray} = ${arrayDefCode}; console.log('初期化:${i}:${name}${codeArray}',JSON.stringify(${name})) }; `
|
|
822
|
+
}
|
|
823
|
+
codeInit += '\n';
|
|
824
|
+
}
|
|
825
|
+
// array
|
|
826
|
+
for (let i = 0; i < list.length; i++) {
|
|
827
|
+
const idx = this._convGen(list[i], true);
|
|
828
|
+
code += '[' + idx + ']';
|
|
829
|
+
}
|
|
830
|
+
const value = this._convGen(node.value, true);
|
|
831
|
+
code += ' = ' + value + ';\n';
|
|
832
|
+
// generate code
|
|
833
|
+
const src = this.convLineno(node, false) + codeInit + code;
|
|
834
|
+
return src;
|
|
835
|
+
}
|
|
836
|
+
convGenLoop(node) {
|
|
837
|
+
const tmpflag = this.flagLoop;
|
|
838
|
+
this.flagLoop = true;
|
|
839
|
+
try {
|
|
840
|
+
return this._convGen(node, false);
|
|
841
|
+
}
|
|
842
|
+
finally {
|
|
843
|
+
this.flagLoop = tmpflag;
|
|
844
|
+
}
|
|
845
|
+
}
|
|
846
|
+
convFor(node) {
|
|
847
|
+
// ループ変数について
|
|
848
|
+
let word;
|
|
849
|
+
if (node.word !== null) { // ループ変数を使う時
|
|
850
|
+
const varName = node.word.value; // todo: Forの最初のパラメータが Token か Astか確認
|
|
851
|
+
this.varsSet.names.add(varName);
|
|
852
|
+
word = this.varname(varName);
|
|
853
|
+
}
|
|
854
|
+
else {
|
|
855
|
+
this.varsSet.names.add('dummy');
|
|
856
|
+
word = this.varname('dummy');
|
|
857
|
+
}
|
|
858
|
+
const idLoop = this.loopId++;
|
|
859
|
+
const varI = `$nako_i${idLoop}`;
|
|
860
|
+
// ループ条件を確認
|
|
861
|
+
const kara = this._convGen(node.from, true);
|
|
862
|
+
const made = this._convGen(node.to, true);
|
|
863
|
+
let inc = '1';
|
|
864
|
+
if (node.inc !== null || node.inc === undefined || node.inc === 'null') {
|
|
865
|
+
inc = this._convGen(node.inc, true);
|
|
866
|
+
}
|
|
867
|
+
// ループ内のブロック内容を得る
|
|
868
|
+
const block = this.convGenLoop(node.block);
|
|
869
|
+
// ループ条件を変数に入れる用
|
|
870
|
+
const varFrom = `$nako_from${idLoop}`;
|
|
871
|
+
const varTo = `$nako_to${idLoop}`;
|
|
872
|
+
let sorePrefex = '';
|
|
873
|
+
if (this.speedMode.invalidSore === 0) {
|
|
874
|
+
sorePrefex = `${this.varname('それ')} = `;
|
|
875
|
+
}
|
|
876
|
+
const code = `\n//[FOR id=${idLoop}]\n` +
|
|
877
|
+
`const ${varFrom} = ${kara};\n` +
|
|
878
|
+
`const ${varTo} = ${made};\n` +
|
|
879
|
+
`if (${varFrom} <= ${varTo}) { // up\n` +
|
|
880
|
+
` for (let ${varI} = ${varFrom}; ${varI} <= ${varTo}; ${varI}+= ${inc}) {\n` +
|
|
881
|
+
` ${sorePrefex}${word} = ${varI};\n` +
|
|
882
|
+
` ${block}\n` +
|
|
883
|
+
' };\n' +
|
|
884
|
+
'} else { // down\n' +
|
|
885
|
+
` for (let ${varI} = ${varFrom}; ${varI} >= ${varTo}; ${varI}-= ${inc}) {\n` +
|
|
886
|
+
` ${sorePrefex}${word} = ${varI};` + '\n' +
|
|
887
|
+
` ${block}\n` +
|
|
888
|
+
' };\n' +
|
|
889
|
+
`};\n//[/FOR id=${idLoop}]\n`;
|
|
890
|
+
return this.convLineno(node, false) + code;
|
|
891
|
+
}
|
|
892
|
+
convForeach(node) {
|
|
893
|
+
let target;
|
|
894
|
+
if (node.target === null) {
|
|
895
|
+
if (this.speedMode.invalidSore === 0) {
|
|
896
|
+
target = this.varname('それ');
|
|
897
|
+
}
|
|
898
|
+
else {
|
|
899
|
+
throw NakoSyntaxError.fromNode('『反復』の対象がありません。', node);
|
|
900
|
+
}
|
|
901
|
+
}
|
|
902
|
+
else {
|
|
903
|
+
target = this._convGen(node.target, true);
|
|
904
|
+
}
|
|
905
|
+
// blockより早く変数を定義する必要がある
|
|
906
|
+
let nameS = '__v0["対象"]';
|
|
907
|
+
if (node.name) {
|
|
908
|
+
nameS = this.varname(node.name.value);
|
|
909
|
+
this.varsSet.names.add(node.name.value);
|
|
910
|
+
}
|
|
911
|
+
const block = this.convGenLoop(node.block);
|
|
912
|
+
const id = this.loopId++;
|
|
913
|
+
const key = '__v0["対象キー"]';
|
|
914
|
+
let sorePrefex = '';
|
|
915
|
+
if (this.speedMode.invalidSore === 0) {
|
|
916
|
+
sorePrefex = `${this.varname('それ')} = `;
|
|
917
|
+
}
|
|
918
|
+
const code = `let $nako_foreach_v${id}=${target};\n` +
|
|
919
|
+
`for (let $nako_i${id} in $nako_foreach_v${id})` + '{\n' +
|
|
920
|
+
` if ($nako_foreach_v${id}.hasOwnProperty($nako_i${id})) {\n` +
|
|
921
|
+
` ${nameS} = ${sorePrefex}$nako_foreach_v${id}[$nako_i${id}];` + '\n' +
|
|
922
|
+
` ${key} = $nako_i${id};\n` +
|
|
923
|
+
` ${block}\n` +
|
|
924
|
+
' }\n' +
|
|
925
|
+
'};\n';
|
|
926
|
+
return this.convLineno(node, false) + code;
|
|
927
|
+
}
|
|
928
|
+
convRepeatTimes(node) {
|
|
929
|
+
const id = this.loopId++;
|
|
930
|
+
const value = this._convGen(node.value, true);
|
|
931
|
+
const block = this.convGenLoop(node.block);
|
|
932
|
+
const kaisu = '__v0["回数"]';
|
|
933
|
+
let sorePrefex = '';
|
|
934
|
+
if (this.speedMode.invalidSore === 0) {
|
|
935
|
+
sorePrefex = `${this.varname('それ')} = `;
|
|
936
|
+
}
|
|
937
|
+
const code = `let $nako_times_v${id} = ${value};\n` +
|
|
938
|
+
`for(var $nako_i${id} = 1; $nako_i${id} <= $nako_times_v${id}; $nako_i${id}++)` + '{\n' +
|
|
939
|
+
` ${sorePrefex}${kaisu} = $nako_i${id};` + '\n' +
|
|
940
|
+
' ' + block + '\n}\n';
|
|
941
|
+
return this.convLineno(node, false) + code;
|
|
942
|
+
}
|
|
943
|
+
/**
|
|
944
|
+
* @param {Ast} node
|
|
945
|
+
* @param {boolean} isExpression
|
|
946
|
+
*/
|
|
947
|
+
convSpeedMode(node, isExpression) {
|
|
948
|
+
if (!node.options) {
|
|
949
|
+
return '';
|
|
950
|
+
}
|
|
951
|
+
const prev = { ...this.speedMode };
|
|
952
|
+
if (node.options['行番号無し']) {
|
|
953
|
+
this.speedMode.lineNumbers++;
|
|
954
|
+
}
|
|
955
|
+
if (node.options['暗黙の型変換無し']) {
|
|
956
|
+
this.speedMode.implicitTypeCasting++;
|
|
957
|
+
}
|
|
958
|
+
if (node.options['強制ピュア']) {
|
|
959
|
+
this.speedMode.forcePure++;
|
|
960
|
+
}
|
|
961
|
+
if (node.options['それ無効']) {
|
|
962
|
+
this.speedMode.invalidSore++;
|
|
963
|
+
}
|
|
964
|
+
try {
|
|
965
|
+
return this._convGen(node.block, isExpression);
|
|
966
|
+
}
|
|
967
|
+
finally {
|
|
968
|
+
this.speedMode = prev;
|
|
969
|
+
}
|
|
970
|
+
}
|
|
971
|
+
/**
|
|
972
|
+
* @param {Ast} node
|
|
973
|
+
* @param {boolean} isExpression
|
|
974
|
+
*/
|
|
975
|
+
convPerformanceMonitor(node, isExpression) {
|
|
976
|
+
const prev = { ...this.performanceMonitor };
|
|
977
|
+
if (!node.options) {
|
|
978
|
+
return '';
|
|
979
|
+
}
|
|
980
|
+
if (node.options['ユーザ関数']) {
|
|
981
|
+
this.performanceMonitor.userFunction++;
|
|
982
|
+
}
|
|
983
|
+
if (node.options['システム関数本体']) {
|
|
984
|
+
this.performanceMonitor.systemFunctionBody++;
|
|
985
|
+
}
|
|
986
|
+
if (node.options['システム関数']) {
|
|
987
|
+
this.performanceMonitor.systemFunction++;
|
|
988
|
+
}
|
|
989
|
+
try {
|
|
990
|
+
return this._convGen(node.block, isExpression);
|
|
991
|
+
}
|
|
992
|
+
finally {
|
|
993
|
+
this.performanceMonitor = prev;
|
|
994
|
+
}
|
|
995
|
+
}
|
|
996
|
+
convWhile(node) {
|
|
997
|
+
const cond = this._convGen(node.cond, true);
|
|
998
|
+
const block = this.convGenLoop(node.block);
|
|
999
|
+
const code = `while (${cond})` + '{\n' +
|
|
1000
|
+
` ${block}` + '\n' +
|
|
1001
|
+
'}\n';
|
|
1002
|
+
return this.convLineno(node, false) + code;
|
|
1003
|
+
}
|
|
1004
|
+
convAtohantei(node) {
|
|
1005
|
+
const id = this.loopId++;
|
|
1006
|
+
const varId = `$nako_i${id}`;
|
|
1007
|
+
const cond = this._convGen(node.cond, true);
|
|
1008
|
+
const block = this.convGenLoop(node.block);
|
|
1009
|
+
const code = 'for(;;) {\n' +
|
|
1010
|
+
` ${block}\n` +
|
|
1011
|
+
` let ${varId} = ${cond};\n` +
|
|
1012
|
+
` if (${varId}) { continue } else { break }\n` +
|
|
1013
|
+
'}\n\n';
|
|
1014
|
+
return this.convLineno(node, false) + code;
|
|
1015
|
+
}
|
|
1016
|
+
convSwitch(node) {
|
|
1017
|
+
const value = this._convGen(node.value, true);
|
|
1018
|
+
const cases = node.cases || [];
|
|
1019
|
+
let body = '';
|
|
1020
|
+
for (let i = 0; i < cases.length; i++) {
|
|
1021
|
+
const cvalue = cases[i][0];
|
|
1022
|
+
const cblock = this.convGenLoop(cases[i][1]);
|
|
1023
|
+
if (cvalue.type === '違えば') {
|
|
1024
|
+
body += ' default:\n';
|
|
1025
|
+
}
|
|
1026
|
+
else {
|
|
1027
|
+
const cvalueCode = this._convGen(cvalue, true);
|
|
1028
|
+
body += ` case ${cvalueCode}:\n`;
|
|
1029
|
+
}
|
|
1030
|
+
body += ` ${cblock}\n` +
|
|
1031
|
+
' break\n';
|
|
1032
|
+
}
|
|
1033
|
+
const code = `switch (${value})` + '{\n' +
|
|
1034
|
+
`${body}` + '\n' +
|
|
1035
|
+
'}\n';
|
|
1036
|
+
return this.convLineno(node, false) + code;
|
|
1037
|
+
}
|
|
1038
|
+
convIf(node) {
|
|
1039
|
+
const expr = this._convGen(node.expr, true);
|
|
1040
|
+
const block = this._convGen(node.block, false);
|
|
1041
|
+
const falseBlock = (node.false_block === null)
|
|
1042
|
+
? ''
|
|
1043
|
+
: 'else {' + this._convGen(node.false_block, false) + '};\n';
|
|
1044
|
+
return this.convLineno(node, false) +
|
|
1045
|
+
`if (${expr}) {\n ${block}\n}` + falseBlock + ';\n';
|
|
1046
|
+
}
|
|
1047
|
+
convTikuji(node) {
|
|
1048
|
+
const pid = this.loopId++;
|
|
1049
|
+
// gen tikuji blocks
|
|
1050
|
+
const curName = `__tikuji${pid}`;
|
|
1051
|
+
let code = `const ${curName} = []\n`;
|
|
1052
|
+
const blocks = (node.blocks) ? node.blocks : [];
|
|
1053
|
+
for (let i = 0; i < blocks.length; i++) {
|
|
1054
|
+
const block = this._convGen(blocks[i], false).replace(/\s+$/, '') + '\n';
|
|
1055
|
+
const blockLineNo = this.convLineno(blocks[i], true);
|
|
1056
|
+
const blockCode = `${curName}.push(function(resolve, reject) {\n` +
|
|
1057
|
+
' __self.resolve = resolve;\n' +
|
|
1058
|
+
' __self.reject = reject;\n' +
|
|
1059
|
+
' __self.resolveCount = 0;\n' +
|
|
1060
|
+
` ${blockLineNo}\n` +
|
|
1061
|
+
` ${block}` +
|
|
1062
|
+
' if (__self.resolveCount === 0) resolve();\n' +
|
|
1063
|
+
// eslint-disable-next-line no-template-curly-in-string
|
|
1064
|
+
'}); // end of tikuji__${pid}[{$i}]\n';
|
|
1065
|
+
code += blockCode;
|
|
1066
|
+
}
|
|
1067
|
+
code += `// end of ${curName} \n`;
|
|
1068
|
+
// gen error block
|
|
1069
|
+
let errorCode = ` ${curName}.splice(0);\n` + // clear
|
|
1070
|
+
' __v0["エラーメッセージ"]=errMsg;\n';
|
|
1071
|
+
if (node.errorBlock != null) {
|
|
1072
|
+
const errBlock = this._convGen(node.errorBlock, false).replace(/\s+$/, '') + '\n';
|
|
1073
|
+
errorCode += errBlock;
|
|
1074
|
+
}
|
|
1075
|
+
code += `const ${curName}__reject = function(errMsg){\n${errorCode}};\n`;
|
|
1076
|
+
// gen run block
|
|
1077
|
+
code += '__self.resolve = undefined;\n';
|
|
1078
|
+
code += `const ${curName}__resolve = function(){\n`;
|
|
1079
|
+
code += ' setTimeout(function(){\n';
|
|
1080
|
+
code += ` if (${curName}.length == 0) {return}\n`;
|
|
1081
|
+
code += ` const f = ${curName}.shift()\n`;
|
|
1082
|
+
code += ` f(${curName}__resolve, ${curName}__reject);\n`;
|
|
1083
|
+
code += ' }, 0);\n';
|
|
1084
|
+
code += '};\n';
|
|
1085
|
+
code += `${curName}__resolve()\n`;
|
|
1086
|
+
return this.convLineno(node, false) + code;
|
|
1087
|
+
}
|
|
1088
|
+
convFuncGetArgsCalcType(_funcName, _func, node) {
|
|
1089
|
+
const args = [];
|
|
1090
|
+
const opts = {};
|
|
1091
|
+
const nodeArgs = (node.args) ? node.args : [];
|
|
1092
|
+
for (let i = 0; i < nodeArgs.length; i++) {
|
|
1093
|
+
const arg = nodeArgs[i];
|
|
1094
|
+
if (i === 0 && arg === null && this.speedMode.invalidSore === 0) {
|
|
1095
|
+
args.push(this.varname('それ'));
|
|
1096
|
+
opts.sore = true;
|
|
1097
|
+
}
|
|
1098
|
+
else {
|
|
1099
|
+
args.push(this._convGen(arg, true));
|
|
1100
|
+
}
|
|
1101
|
+
}
|
|
1102
|
+
return [args, opts];
|
|
1103
|
+
}
|
|
1104
|
+
getPluginList() {
|
|
1105
|
+
const r = [];
|
|
1106
|
+
for (const name in this.__self.__module) {
|
|
1107
|
+
r.push(name);
|
|
1108
|
+
}
|
|
1109
|
+
return r;
|
|
1110
|
+
}
|
|
1111
|
+
/**
|
|
1112
|
+
* 関数の呼び出し
|
|
1113
|
+
* @param {Ast} node
|
|
1114
|
+
* @param {boolean} isExpression
|
|
1115
|
+
* @returns string コード
|
|
1116
|
+
*/
|
|
1117
|
+
convCallFunc(node, isExpression) {
|
|
1118
|
+
const funcName = NakoGen.getFuncName(node.name);
|
|
1119
|
+
const res = this.findVar(funcName);
|
|
1120
|
+
if (res === null) {
|
|
1121
|
+
throw NakoSyntaxError.fromNode(`関数『${funcName}』が見当たりません。有効プラグイン=[` + this.getPluginList().join(', ') + ']', node);
|
|
1122
|
+
}
|
|
1123
|
+
// どの関数を呼び出すのか関数を特定する
|
|
1124
|
+
let func;
|
|
1125
|
+
if (res.i === 0) { // plugin function
|
|
1126
|
+
func = this.__self.getFunc(funcName);
|
|
1127
|
+
if (!func) {
|
|
1128
|
+
throw new Error(`[System Error] 関数「${funcName}」NakoCompiler.nakoFuncList の不整合があります。`);
|
|
1129
|
+
}
|
|
1130
|
+
if (func.type !== 'func') {
|
|
1131
|
+
throw NakoSyntaxError.fromNode(`『${funcName}』は関数ではありません。`, node);
|
|
1132
|
+
}
|
|
1133
|
+
}
|
|
1134
|
+
else {
|
|
1135
|
+
func = this.nakoFuncList[funcName];
|
|
1136
|
+
// 無名関数の可能性
|
|
1137
|
+
if (func === undefined) {
|
|
1138
|
+
func = { return_none: false };
|
|
1139
|
+
}
|
|
1140
|
+
}
|
|
1141
|
+
// 関数の参照渡しか?
|
|
1142
|
+
if (node.type === 'func_pointer') {
|
|
1143
|
+
return res.js;
|
|
1144
|
+
}
|
|
1145
|
+
// 関数の参照渡しでない場合
|
|
1146
|
+
// 関数定義より助詞を一つずつ調べる
|
|
1147
|
+
const argsInfo = this.convFuncGetArgsCalcType(funcName, func, node);
|
|
1148
|
+
const args = argsInfo[0];
|
|
1149
|
+
const argsOpts = argsInfo[1];
|
|
1150
|
+
// function
|
|
1151
|
+
this.usedFuncSet.add(funcName);
|
|
1152
|
+
// 関数呼び出しで、引数の末尾にthisを追加する-システム情報を参照するため
|
|
1153
|
+
args.push('__self');
|
|
1154
|
+
let funcDef = 'function';
|
|
1155
|
+
let funcBegin = '';
|
|
1156
|
+
let funcEnd = '';
|
|
1157
|
+
// setter?
|
|
1158
|
+
if (node.setter) {
|
|
1159
|
+
funcBegin += ';__self.isSetter = true;\n';
|
|
1160
|
+
funcEnd += ';__self.isSetter = false;\n';
|
|
1161
|
+
}
|
|
1162
|
+
// 関数内 (__varslist.length > 3) からプラグイン関数 (res.i === 0) を呼び出すとき、 そのプラグイン関数がpureでなければ
|
|
1163
|
+
// 呼び出しの直前に全てのローカル変数をthis.__localsに入れる。
|
|
1164
|
+
if (res.i === 0 && this.varslistSet.length > 3 && func.pure !== true && this.speedMode.forcePure === 0) { // undefinedはfalseとみなす
|
|
1165
|
+
// 展開されたローカル変数の列挙
|
|
1166
|
+
const localVars = [];
|
|
1167
|
+
for (const name of Array.from(this.varsSet.names.values())) {
|
|
1168
|
+
if (NakoGen.isValidIdentifier(name)) {
|
|
1169
|
+
localVars.push({ str: JSON.stringify(name), js: this.varname(name) });
|
|
1170
|
+
}
|
|
1171
|
+
}
|
|
1172
|
+
// --- 実行前 ---
|
|
1173
|
+
// 全ての展開されていないローカル変数を __self.__locals にコピーする
|
|
1174
|
+
funcBegin += '__self.__locals = __vars;\n';
|
|
1175
|
+
// 全ての展開されたローカル変数を __self.__locals に保存する
|
|
1176
|
+
for (const v of localVars) {
|
|
1177
|
+
funcBegin += `__self.__locals[${v.str}] = ${v.js};\n`;
|
|
1178
|
+
}
|
|
1179
|
+
// --- 実行後 ---
|
|
1180
|
+
// 全ての展開されたローカル変数を __self.__locals から受け取る
|
|
1181
|
+
// 「それ」は関数の実行結果を受け取るために使うためスキップ。
|
|
1182
|
+
for (const v of localVars) {
|
|
1183
|
+
if (v.js !== 'それ') {
|
|
1184
|
+
funcEnd += `${v.js} = __self.__locals[${v.str}];\n`;
|
|
1185
|
+
}
|
|
1186
|
+
}
|
|
1187
|
+
}
|
|
1188
|
+
// 変数「それ」が補完されていることをヒントとして出力
|
|
1189
|
+
if (argsOpts.sore) {
|
|
1190
|
+
funcBegin += '/*[sore]*/';
|
|
1191
|
+
}
|
|
1192
|
+
const indent = (text, n) => {
|
|
1193
|
+
let result = '';
|
|
1194
|
+
for (const line of text.split('\n')) {
|
|
1195
|
+
if (line !== '') {
|
|
1196
|
+
result += ' '.repeat(n) + line + '\n';
|
|
1197
|
+
}
|
|
1198
|
+
}
|
|
1199
|
+
return result;
|
|
1200
|
+
};
|
|
1201
|
+
// 関数呼び出しコードの構築
|
|
1202
|
+
let argsCode;
|
|
1203
|
+
if ((this.warnUndefinedCallingUserFunc === 0 && res.i !== 0) || (this.warnUndefinedCallingSystemFunc === 0 && res.i === 0)) {
|
|
1204
|
+
argsCode = args.join(',');
|
|
1205
|
+
}
|
|
1206
|
+
else {
|
|
1207
|
+
argsCode = '';
|
|
1208
|
+
args.forEach((arg) => {
|
|
1209
|
+
if (arg === '__self') {
|
|
1210
|
+
argsCode += `,${arg}`;
|
|
1211
|
+
}
|
|
1212
|
+
else {
|
|
1213
|
+
if (res.i === 0) {
|
|
1214
|
+
argsCode += `,(function(a){if(a===undefined){__self.logger.warn('命令『${funcName}』の引数にundefinedを渡しています。',{file:'${node.file}',line:${node.line}});};return a;})(${arg})`;
|
|
1215
|
+
}
|
|
1216
|
+
else {
|
|
1217
|
+
argsCode += `,(function(a){if(a===undefined){__self.logger.warn('ユーザ関数『${funcName}』の引数にundefinedを渡しています。',{file:'${node.file}',line:${node.line}});};return a;})(${arg})`;
|
|
1218
|
+
}
|
|
1219
|
+
}
|
|
1220
|
+
});
|
|
1221
|
+
argsCode = argsCode.substring(1);
|
|
1222
|
+
}
|
|
1223
|
+
let funcCall = `${res.js}(${argsCode})`;
|
|
1224
|
+
if (func.asyncFn) {
|
|
1225
|
+
funcDef = `async ${funcDef}`;
|
|
1226
|
+
funcCall = `await ${funcCall}`;
|
|
1227
|
+
this.numAsyncFn++;
|
|
1228
|
+
this.usedAsyncFn = true;
|
|
1229
|
+
}
|
|
1230
|
+
if (res.i === 0 && this.performanceMonitor.systemFunctionBody !== 0) {
|
|
1231
|
+
let key = funcName;
|
|
1232
|
+
if (!key) {
|
|
1233
|
+
if (typeof this.performanceMonitor.mumeiId === 'undefined') {
|
|
1234
|
+
this.performanceMonitor.mumeiId = 0;
|
|
1235
|
+
}
|
|
1236
|
+
this.performanceMonitor.mumeiId++;
|
|
1237
|
+
key = `anous_${this.performanceMonitor.mumeiId}`;
|
|
1238
|
+
}
|
|
1239
|
+
funcCall = `(${funcDef} (key, type) {\n` +
|
|
1240
|
+
'const sbf_start = performance.now() * 1000;\n' +
|
|
1241
|
+
'try {\n' +
|
|
1242
|
+
'return ' + funcCall + ';\n' +
|
|
1243
|
+
'} finally {\n' +
|
|
1244
|
+
'const sbl_time = performance.now() * 1000 - sbf_start;\n' +
|
|
1245
|
+
'if (!__self.__performance_monitor) {\n' +
|
|
1246
|
+
'__self.__performance_monitor={};\n' +
|
|
1247
|
+
'__self.__performance_monitor[key] = { called:1, totel_usec: sbl_time, min_usec: sbl_time, max_usec: sbl_time, type: type };\n' +
|
|
1248
|
+
'} else if (!__self.__performance_monitor[key]) {\n' +
|
|
1249
|
+
'__self.__performance_monitor[key] = { called:1, totel_usec: sbl_time, min_usec: sbl_time, max_usec: sbl_time, type: type };\n' +
|
|
1250
|
+
'} else {\n' +
|
|
1251
|
+
'__self.__performance_monitor[key].called++;\n' +
|
|
1252
|
+
'__self.__performance_monitor[key].totel_usec+=sbl_time;\n' +
|
|
1253
|
+
'if(__self.__performance_monitor[key].min_usec>sbl_time){__self.__performance_monitor[key].min_usec=sbl_time;}\n' +
|
|
1254
|
+
'if(__self.__performance_monitor[key].max_usec<sbl_time){__self.__performance_monitor[key].max_usec=sbl_time;}\n' +
|
|
1255
|
+
`}}})('${funcName}_body', 'sysbody')\n`;
|
|
1256
|
+
}
|
|
1257
|
+
let code = '';
|
|
1258
|
+
if (func.return_none) {
|
|
1259
|
+
// 戻り値のない関数の場合
|
|
1260
|
+
if (funcEnd === '') {
|
|
1261
|
+
if (funcBegin === '') {
|
|
1262
|
+
code = `${funcCall};\n`;
|
|
1263
|
+
}
|
|
1264
|
+
else {
|
|
1265
|
+
code = `${funcBegin} ${funcCall};\n`;
|
|
1266
|
+
}
|
|
1267
|
+
}
|
|
1268
|
+
else {
|
|
1269
|
+
code = `${funcBegin}try {\n${indent(funcCall, 1)};\n} finally {\n${indent(funcEnd, 1)}}\n`;
|
|
1270
|
+
}
|
|
1271
|
+
}
|
|
1272
|
+
else {
|
|
1273
|
+
// 戻り値のある関数の場合
|
|
1274
|
+
let sorePrefex = '';
|
|
1275
|
+
if (this.speedMode.invalidSore === 0) {
|
|
1276
|
+
sorePrefex = `${this.varname('それ')} = `;
|
|
1277
|
+
}
|
|
1278
|
+
if (funcBegin === '' && funcEnd === '') {
|
|
1279
|
+
code = `(${sorePrefex}${funcCall})`;
|
|
1280
|
+
}
|
|
1281
|
+
else {
|
|
1282
|
+
if (funcEnd === '') {
|
|
1283
|
+
code = `(${funcDef}(){\n${indent(`${funcBegin};\nreturn ${sorePrefex} ${funcCall}`, 1)}}).call(this)`;
|
|
1284
|
+
}
|
|
1285
|
+
else {
|
|
1286
|
+
code = `(${funcDef}(){\n${indent(`${funcBegin}try {\n${indent(`return ${sorePrefex}${funcCall};`, 1)}\n} finally {\n${indent(funcEnd, 1)}}`, 1)}}).call(this)`;
|
|
1287
|
+
}
|
|
1288
|
+
}
|
|
1289
|
+
// ...して
|
|
1290
|
+
if (node.josi === 'して' || (node.josi === '' && !isExpression)) {
|
|
1291
|
+
code += ';\n';
|
|
1292
|
+
}
|
|
1293
|
+
}
|
|
1294
|
+
if (res.i === 0 && this.performanceMonitor.systemFunction !== 0) {
|
|
1295
|
+
code = '(function (key, type) {\n' +
|
|
1296
|
+
'const sf_start = performance.now() * 1000;\n' +
|
|
1297
|
+
'try {\n' +
|
|
1298
|
+
'return ' + code + ';\n' +
|
|
1299
|
+
'} finally {\n' +
|
|
1300
|
+
'const sl_time = performance.now() * 1000 - sf_start;\n' +
|
|
1301
|
+
'if (!__self.__performance_monitor) {\n' +
|
|
1302
|
+
'__self.__performance_monitor={};\n' +
|
|
1303
|
+
'__self.__performance_monitor[key] = { called:1, totel_usec: sl_time, min_usec: sl_time, max_usec: sl_time, type: type };\n' +
|
|
1304
|
+
'} else if (!__self.__performance_monitor[key]) {\n' +
|
|
1305
|
+
'__self.__performance_monitor[key] = { called:1, totel_usec: sl_time, min_usec: sl_time, max_usec: sl_time, type: type };\n' +
|
|
1306
|
+
'} else {\n' +
|
|
1307
|
+
'__self.__performance_monitor[key].called++;\n' +
|
|
1308
|
+
'__self.__performance_monitor[key].totel_usec+=sl_time;\n' +
|
|
1309
|
+
'if(__self.__performance_monitor[key].min_usec>sl_time){__self.__performance_monitor[key].min_usec=sl_time;}\n' +
|
|
1310
|
+
'if(__self.__performance_monitor[key].max_usec<sl_time){__self.__performance_monitor[key].max_usec=sl_time;}\n' +
|
|
1311
|
+
`}}})('${funcName}_sys', 'system')\n`;
|
|
1312
|
+
}
|
|
1313
|
+
return code;
|
|
1314
|
+
}
|
|
1315
|
+
convRenbun(node) {
|
|
1316
|
+
const right = this._convGen(node.right, true);
|
|
1317
|
+
const left = this._convGen(node.left, false);
|
|
1318
|
+
return `(function(){${left}; return ${right}}).call(this)`;
|
|
1319
|
+
}
|
|
1320
|
+
convOp(node) {
|
|
1321
|
+
const OP_TBL = {
|
|
1322
|
+
'&': '+""+',
|
|
1323
|
+
eq: '==',
|
|
1324
|
+
noteq: '!=',
|
|
1325
|
+
'===': '===',
|
|
1326
|
+
'!==': '!==',
|
|
1327
|
+
gt: '>',
|
|
1328
|
+
lt: '<',
|
|
1329
|
+
gteq: '>=',
|
|
1330
|
+
lteq: '<=',
|
|
1331
|
+
and: '&&',
|
|
1332
|
+
or: '||',
|
|
1333
|
+
shift_l: '<<',
|
|
1334
|
+
shift_r: '>>',
|
|
1335
|
+
shift_r0: '>>>',
|
|
1336
|
+
'÷': '/'
|
|
1337
|
+
};
|
|
1338
|
+
let op = node.operator || ''; // 演算子
|
|
1339
|
+
let right = this._convGen(node.right, true);
|
|
1340
|
+
let left = this._convGen(node.left, true);
|
|
1341
|
+
if (op === '+' && this.speedMode.implicitTypeCasting === 0) {
|
|
1342
|
+
if (node.left && node.left.type !== 'number') {
|
|
1343
|
+
left = `parseFloat(${left})`;
|
|
1344
|
+
}
|
|
1345
|
+
if (node.right && node.right.type !== 'number') {
|
|
1346
|
+
right = `parseFloat(${right})`;
|
|
1347
|
+
}
|
|
1348
|
+
}
|
|
1349
|
+
// 階乗
|
|
1350
|
+
if (op === '^') {
|
|
1351
|
+
return `(Math.pow(${left}, ${right}))`;
|
|
1352
|
+
}
|
|
1353
|
+
// 整数の割り算 #1152
|
|
1354
|
+
if (op === '÷÷') {
|
|
1355
|
+
return `(Math.floor(${left} / ${right}))`;
|
|
1356
|
+
}
|
|
1357
|
+
// 一般的なオペレータに変換
|
|
1358
|
+
if (OP_TBL[op]) {
|
|
1359
|
+
op = OP_TBL[op];
|
|
1360
|
+
}
|
|
1361
|
+
//
|
|
1362
|
+
return `(${left} ${op} ${right})`;
|
|
1363
|
+
}
|
|
1364
|
+
convInc(node) {
|
|
1365
|
+
// もし値が省略されていたら、変数「それ」に代入する
|
|
1366
|
+
let value = null;
|
|
1367
|
+
if (this.speedMode.invalidSore === 0) {
|
|
1368
|
+
value = this.varname('それ');
|
|
1369
|
+
}
|
|
1370
|
+
if (node.value) {
|
|
1371
|
+
value = this._convGen(node.value, true);
|
|
1372
|
+
}
|
|
1373
|
+
if (value == null) {
|
|
1374
|
+
throw NakoSyntaxError.fromNode('加算する先の変数名がありません。', node);
|
|
1375
|
+
}
|
|
1376
|
+
// 変数名
|
|
1377
|
+
const name = node.name.value;
|
|
1378
|
+
let res = this.findVar(name);
|
|
1379
|
+
let code = '';
|
|
1380
|
+
if (res === null) {
|
|
1381
|
+
this.varsSet.names.add(name);
|
|
1382
|
+
res = this.findVar(name);
|
|
1383
|
+
if (!res) {
|
|
1384
|
+
throw new Error('『増』または『減』で変数が見当たりません。');
|
|
1385
|
+
}
|
|
1386
|
+
}
|
|
1387
|
+
const jsName = res.js;
|
|
1388
|
+
// 自動初期化するか
|
|
1389
|
+
code += `if (typeof(${jsName}) === 'undefined') { ${jsName} = 0; }`;
|
|
1390
|
+
code += `${jsName} += ${value}`;
|
|
1391
|
+
return ';' + this.convLineno(node, false) + code + '\n';
|
|
1392
|
+
}
|
|
1393
|
+
convLet(node) {
|
|
1394
|
+
// もし値が省略されていたら、変数「それ」に代入する
|
|
1395
|
+
let value = null;
|
|
1396
|
+
if (this.speedMode.invalidSore === 0) {
|
|
1397
|
+
value = this.varname('それ');
|
|
1398
|
+
}
|
|
1399
|
+
if (node.value) {
|
|
1400
|
+
value = this._convGen(node.value, true);
|
|
1401
|
+
}
|
|
1402
|
+
if (value == null) {
|
|
1403
|
+
throw NakoSyntaxError.fromNode('代入する先の変数名がありません。', node);
|
|
1404
|
+
}
|
|
1405
|
+
// 変数名
|
|
1406
|
+
const name = node.name.value;
|
|
1407
|
+
const res = this.findVar(name);
|
|
1408
|
+
let code = '';
|
|
1409
|
+
if (res === null) {
|
|
1410
|
+
this.varsSet.names.add(name);
|
|
1411
|
+
code = `${this.varname(name)} = ${value};`;
|
|
1412
|
+
}
|
|
1413
|
+
else {
|
|
1414
|
+
// 定数ならエラーを出す
|
|
1415
|
+
if (this.varslistSet[res.i].readonly.has(name)) {
|
|
1416
|
+
throw NakoSyntaxError.fromNode(`定数『${name}』は既に定義済みなので、値を代入することはできません。`, node);
|
|
1417
|
+
}
|
|
1418
|
+
code = `${res.js} = ${value};`;
|
|
1419
|
+
}
|
|
1420
|
+
return ';' + this.convLineno(node, false) + code + '\n';
|
|
1421
|
+
}
|
|
1422
|
+
convDefLocalVar(node) {
|
|
1423
|
+
const value = (node.value === null) ? 'null' : this._convGen(node.value, true);
|
|
1424
|
+
const name = node.name.value;
|
|
1425
|
+
const vtype = node.vartype; // 変数 or 定数
|
|
1426
|
+
// 二重定義?
|
|
1427
|
+
if (this.varsSet.names.has(name)) {
|
|
1428
|
+
throw NakoSyntaxError.fromNode(`${vtype}『${name}』の二重定義はできません。`, node);
|
|
1429
|
+
}
|
|
1430
|
+
//
|
|
1431
|
+
this.varsSet.names.add(name);
|
|
1432
|
+
if (vtype === '定数') {
|
|
1433
|
+
this.varsSet.readonly.add(name);
|
|
1434
|
+
}
|
|
1435
|
+
const code = `${this.varname(name)}=${value};\n`;
|
|
1436
|
+
return this.convLineno(node, false) + code;
|
|
1437
|
+
}
|
|
1438
|
+
// #563 複数変数への代入
|
|
1439
|
+
convDefLocalVarlist(node) {
|
|
1440
|
+
let code = '';
|
|
1441
|
+
const vtype = node.vartype; // 変数 or 定数
|
|
1442
|
+
const value = (node.value === null) ? 'null' : this._convGen(node.value, true);
|
|
1443
|
+
this.loopId++;
|
|
1444
|
+
const varI = `$nako_i${this.loopId}`;
|
|
1445
|
+
code += `${varI}=${value}\n`;
|
|
1446
|
+
code += `if (!(${varI} instanceof Array)) { ${varI}=[${varI}] }\n`;
|
|
1447
|
+
const names = (node.names) ? node.names : [];
|
|
1448
|
+
for (let i = 0; i < names.length; i++) {
|
|
1449
|
+
const nameObj = names[i];
|
|
1450
|
+
const name = nameObj.value;
|
|
1451
|
+
// 二重定義?
|
|
1452
|
+
if (this.varsSet.names.has(name)) {
|
|
1453
|
+
// 複数変数文では、二重定義も許容する #1027
|
|
1454
|
+
// throw NakoSyntaxError.fromNode(`${vtype}『${name}』の二重定義はできません。`, node)
|
|
1455
|
+
}
|
|
1456
|
+
this.varsSet.names.add(name);
|
|
1457
|
+
if (vtype === '定数') {
|
|
1458
|
+
this.varsSet.readonly.add(name);
|
|
1459
|
+
}
|
|
1460
|
+
const vname = this.varname(name);
|
|
1461
|
+
code += `${vname}=${varI}[${i}];\n`;
|
|
1462
|
+
}
|
|
1463
|
+
return this.convLineno(node, false) + code;
|
|
1464
|
+
}
|
|
1465
|
+
convString(node) {
|
|
1466
|
+
let value = '' + node.value;
|
|
1467
|
+
const mode = node.mode;
|
|
1468
|
+
value = value.replace(/\\/g, '\\\\');
|
|
1469
|
+
value = value.replace(/"/g, '\\"');
|
|
1470
|
+
value = value.replace(/\r/g, '\\r');
|
|
1471
|
+
value = value.replace(/\n/g, '\\n');
|
|
1472
|
+
if (mode === 'ex') {
|
|
1473
|
+
const rf = (a, name) => {
|
|
1474
|
+
return '"+' + this.genVar(name, node) + '+"';
|
|
1475
|
+
};
|
|
1476
|
+
value = value.replace(/\{(.+?)\}/g, rf);
|
|
1477
|
+
value = value.replace(/{(.+?)}/g, rf);
|
|
1478
|
+
}
|
|
1479
|
+
return '"' + value + '"';
|
|
1480
|
+
}
|
|
1481
|
+
convTryExcept(node) {
|
|
1482
|
+
const block = this._convGen(node.block, false);
|
|
1483
|
+
const errBlock = this._convGen(node.errBlock, false);
|
|
1484
|
+
return this.convLineno(node, false) +
|
|
1485
|
+
`try {\n${block}\n} catch (e) {\n` +
|
|
1486
|
+
' __v0["エラーメッセージ"] = e.message;\n' +
|
|
1487
|
+
';\n' +
|
|
1488
|
+
`${errBlock}}\n`;
|
|
1489
|
+
}
|
|
1490
|
+
getUsedFuncSet() {
|
|
1491
|
+
return this.usedFuncSet;
|
|
1492
|
+
}
|
|
1493
|
+
getPluginInitCode() {
|
|
1494
|
+
// プラグインの初期化関数を実行する
|
|
1495
|
+
let code = '';
|
|
1496
|
+
let pluginCode = '';
|
|
1497
|
+
for (const name in this.__self.__module) {
|
|
1498
|
+
const initkey = `!${name}:初期化`;
|
|
1499
|
+
if (this.varslistSet[0].names.has(initkey)) {
|
|
1500
|
+
this.usedFuncSet.add(`!${name}:初期化`);
|
|
1501
|
+
pluginCode += `__v0["!${name}:初期化"](__self);\n`;
|
|
1502
|
+
}
|
|
1503
|
+
}
|
|
1504
|
+
if (pluginCode !== '') {
|
|
1505
|
+
code += '__v0.line=\'l0:プラグインの初期化\';\n' + pluginCode;
|
|
1506
|
+
}
|
|
1507
|
+
return code;
|
|
1508
|
+
}
|
|
1509
|
+
}
|
|
1510
|
+
/**
|
|
1511
|
+
* @param com
|
|
1512
|
+
* @param ast
|
|
1513
|
+
* @param opt
|
|
1514
|
+
*/
|
|
1515
|
+
export function generateJS(com, ast, opt) {
|
|
1516
|
+
const gen = new NakoGen(com);
|
|
1517
|
+
// ※ [関数定義に関するコード生成のヒント]
|
|
1518
|
+
// ※ 関数の名前だけを(1)で登録して、(2)で実際に関数のコードを生成する。
|
|
1519
|
+
// ※ ただし(2)では生成するだけなので、(3)でプログラム冒頭に関数定義のコードを記述する。
|
|
1520
|
+
// この順番を変えることはできない (グローバル変数が認識できなくなったり、関数定義のタイミングがずれる)
|
|
1521
|
+
// (1) ユーザー定義関数をシステムに登録する
|
|
1522
|
+
gen.registerFunction(ast);
|
|
1523
|
+
// (2) JSコードを生成する
|
|
1524
|
+
let js = gen.convGen(ast, opt);
|
|
1525
|
+
// (3) JSコードを実行するための事前ヘッダ部分の生成
|
|
1526
|
+
const jsInit = gen.getDefFuncCode(com, opt);
|
|
1527
|
+
// テストの実行
|
|
1528
|
+
if (js && opt.isTest) {
|
|
1529
|
+
js += '\n__self._runTests(__tests);\n';
|
|
1530
|
+
}
|
|
1531
|
+
// async method
|
|
1532
|
+
if (gen.numAsyncFn > 0) {
|
|
1533
|
+
const asyncMain = '__nako3async' + (new Date()).getTime() + '_' + ('' + Math.random()).replace('.', '_') + '__';
|
|
1534
|
+
js = `
|
|
1535
|
+
// --------------------------------------------------
|
|
1536
|
+
// <nadesiko3::gen::async times="${gen.numAsyncFn}">
|
|
1537
|
+
async function ${asyncMain}(self) {
|
|
1538
|
+
${jsInit}
|
|
1539
|
+
${js}
|
|
1540
|
+
} // end of ${asyncMain}
|
|
1541
|
+
${asyncMain}.call(self, self).catch(err => {
|
|
1542
|
+
self.numFailures++
|
|
1543
|
+
throw new NakoRuntimeError(err, self.__v0.line) // エラー位置を認識
|
|
1544
|
+
})
|
|
1545
|
+
// <nadesiko3::gen::async>
|
|
1546
|
+
// --------------------------------------------------
|
|
1547
|
+
`;
|
|
1548
|
+
}
|
|
1549
|
+
else {
|
|
1550
|
+
js = `
|
|
1551
|
+
// --------------------------------------------------
|
|
1552
|
+
try {
|
|
1553
|
+
${jsInit}
|
|
1554
|
+
${js}
|
|
1555
|
+
} catch (err) {
|
|
1556
|
+
self.numFailures++
|
|
1557
|
+
throw new NakoRuntimeError(err, self.__v0.line) // エラー位置を認識
|
|
1558
|
+
}
|
|
1559
|
+
// --------------------------------------------------
|
|
1560
|
+
`;
|
|
1561
|
+
}
|
|
1562
|
+
// デバッグメッセージ
|
|
1563
|
+
com.getLogger().trace('--- generate ---\n' + js);
|
|
1564
|
+
let codeImportFiles = '';
|
|
1565
|
+
const importNames = [];
|
|
1566
|
+
for (const f of opt.importFiles) {
|
|
1567
|
+
if (f === 'nako_errors.mjs') {
|
|
1568
|
+
continue;
|
|
1569
|
+
}
|
|
1570
|
+
const ff = 'nako3runtime_' + f.replace(/\.(js|mjs)$/, '').replace(/[^a-zA-Z0-9_]/g, '_');
|
|
1571
|
+
importNames.push(ff);
|
|
1572
|
+
codeImportFiles += `import ${ff} from './nako3runtime/${f}'\n`;
|
|
1573
|
+
}
|
|
1574
|
+
const standaloneJSCode = `\
|
|
1575
|
+
// <standaloneCode>
|
|
1576
|
+
// 将来的に ESModule に対応する #1217
|
|
1577
|
+
import path from 'path'
|
|
1578
|
+
import { NakoRuntimeError } from './nako3runtime/nako_errors.mjs'
|
|
1579
|
+
${codeImportFiles}
|
|
1580
|
+
const self = {}
|
|
1581
|
+
self.coreVersion = '${com.coreVersion}'
|
|
1582
|
+
self.version = '${com.version}'
|
|
1583
|
+
self.logger = {
|
|
1584
|
+
error: (message) => { console.error(message) },
|
|
1585
|
+
warn: (message) => { console.warn(message) },
|
|
1586
|
+
send: (level, message) => { console.log(message) },
|
|
1587
|
+
};
|
|
1588
|
+
self.__varslist = [{}, {}, {}]
|
|
1589
|
+
self.__v0 = self.__varslist[0]
|
|
1590
|
+
self.initFuncList = []
|
|
1591
|
+
self.clearFuncList = []
|
|
1592
|
+
// Copy module functions
|
|
1593
|
+
for (const mod of [${importNames.join(', ')}]) {
|
|
1594
|
+
for (const funcName in mod) {
|
|
1595
|
+
if (funcName === '初期化') {
|
|
1596
|
+
self.initFuncList.push(mod[funcName].fn)
|
|
1597
|
+
continue
|
|
1598
|
+
}
|
|
1599
|
+
if (funcName === '!クリア') {
|
|
1600
|
+
self.clearFuncList.push(mod[funcName].fn)
|
|
1601
|
+
continue
|
|
1602
|
+
}
|
|
1603
|
+
self.__varslist[0][funcName] = mod[funcName].fn
|
|
1604
|
+
}
|
|
1605
|
+
}
|
|
1606
|
+
self.__vars = self.__varslist[2];
|
|
1607
|
+
self.__module = {};
|
|
1608
|
+
self.__locals = {};
|
|
1609
|
+
self.__genMode = 'sync';
|
|
1610
|
+
|
|
1611
|
+
// プラグインの初期化コードを実行
|
|
1612
|
+
self.initFuncList.map(f => f(self))
|
|
1613
|
+
|
|
1614
|
+
try {
|
|
1615
|
+
${opt.codeStandalone}
|
|
1616
|
+
// <JS>
|
|
1617
|
+
${js}
|
|
1618
|
+
// </JS>
|
|
1619
|
+
// プラグインのクリアコードを実行
|
|
1620
|
+
self.clearFuncList.map(f => f(self))
|
|
1621
|
+
} catch (err) {
|
|
1622
|
+
self.logger.error(err);
|
|
1623
|
+
throw err;
|
|
1624
|
+
}
|
|
1625
|
+
// </standaloneCode>
|
|
1626
|
+
`;
|
|
1627
|
+
// ---
|
|
1628
|
+
const initCode = gen.getPluginInitCode();
|
|
1629
|
+
const runtimeEnvCode = `
|
|
1630
|
+
// runtimeEnv
|
|
1631
|
+
${NakoError.toString()}
|
|
1632
|
+
${NakoRuntimeError.toString()}
|
|
1633
|
+
const self = this
|
|
1634
|
+
${opt.codeEnv}
|
|
1635
|
+
${jsInit}
|
|
1636
|
+
${initCode}
|
|
1637
|
+
${js}
|
|
1638
|
+
// end of runtimeEnv
|
|
1639
|
+
`;
|
|
1640
|
+
return {
|
|
1641
|
+
// なでしこの実行環境ありの場合(thisが有効)
|
|
1642
|
+
runtimeEnv: runtimeEnvCode,
|
|
1643
|
+
// JavaScript単体で動かす場合
|
|
1644
|
+
standalone: standaloneJSCode,
|
|
1645
|
+
// コード生成に使ったNakoGenのインスタンス
|
|
1646
|
+
gen
|
|
1647
|
+
};
|
|
1648
|
+
}
|