nadesiko3 3.2.27

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.
Files changed (283) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +107 -0
  3. package/bin/cnako3 +10 -0
  4. package/bin/cnako3.bat +11 -0
  5. package/bin/nako3server.bat +6 -0
  6. package/demo/ace_editor.html +90 -0
  7. package/demo/ace_editor_tabs.html +162 -0
  8. package/demo/basic.html +71 -0
  9. package/demo/browsers.html +128 -0
  10. package/demo/css/basic.css +3 -0
  11. package/demo/css/common.css +157 -0
  12. package/demo/css/editor.css +8 -0
  13. package/demo/css/flow.css +3 -0
  14. package/demo/css/index.css +3 -0
  15. package/demo/extlib/ace@1.4.12/ace.js +17 -0
  16. package/demo/extlib/ace@1.4.12/ext-code_lens.min.js +1 -0
  17. package/demo/extlib/ace@1.4.12/ext-language_tools.min.js +1 -0
  18. package/demo/extlib/ace@1.4.12/ext-options.min.js +1 -0
  19. package/demo/extlib/ace@1.4.12/ext-settings_menu.js +8 -0
  20. package/demo/extlib/ace@1.4.12/keybinding-vscode.js +8 -0
  21. package/demo/extlib/ace@1.4.12/theme-monokai.js +8 -0
  22. package/demo/extlib/ace@1.4.12/theme-xcode.js +8 -0
  23. package/demo/extlib/chart.js@3.2.1/chart.min.js +13 -0
  24. package/demo/extlib/pure-min.css +11 -0
  25. package/demo/flow.html +97 -0
  26. package/demo/graph.html +53 -0
  27. package/demo/image/nako.png +0 -0
  28. package/demo/image/nakopad-icon256.png +0 -0
  29. package/demo/image/turtle.fla +0 -0
  30. package/demo/image/turtle.png +0 -0
  31. package/demo/index.html +134 -0
  32. package/demo/js/common.js +17 -0
  33. package/demo/js/turtle3d_test.js +44 -0
  34. package/demo/js/turtle_test.js +44 -0
  35. package/demo/runscript.html +47 -0
  36. package/demo/runscript2.html +33 -0
  37. package/demo/turtle.html +58 -0
  38. package/demo/turtle2.html +141 -0
  39. package/demo/turtle3.html +278 -0
  40. package/demo/turtle3d.html +58 -0
  41. package/demo/turtle3d2.html +107 -0
  42. package/demo/version.html +24 -0
  43. package/doc/SETUP.md +172 -0
  44. package/doc/about.md +34 -0
  45. package/doc/browsers.md +60 -0
  46. package/doc/docgen.md +21 -0
  47. package/doc/editor.md +44 -0
  48. package/doc/files.md +37 -0
  49. package/doc/plugins.md +195 -0
  50. package/doc/release.md +78 -0
  51. package/doc/win32.md +57 -0
  52. package/package.json +196 -0
  53. package/release/_hash.txt +65 -0
  54. package/release/_script-tags.txt +13 -0
  55. package/release/command.json +1 -0
  56. package/release/command.json.js +1 -0
  57. package/release/command_cnako3.json +1 -0
  58. package/release/command_list.json +1 -0
  59. package/release/editor.js +2 -0
  60. package/release/editor.js.LICENSE.txt +32 -0
  61. package/release/josi.json +48 -0
  62. package/release/nako_gen_async.js +1 -0
  63. package/release/nako_gen_async.js.LICENSE.txt +595 -0
  64. package/release/plugin_caniuse.js +1 -0
  65. package/release/plugin_caniuse.js.LICENSE.txt +411 -0
  66. package/release/plugin_csv.js +1 -0
  67. package/release/plugin_csv.js.LICENSE.txt +367 -0
  68. package/release/plugin_datetime.js +1 -0
  69. package/release/plugin_datetime.js.LICENSE.txt +471 -0
  70. package/release/plugin_kansuji.js +1 -0
  71. package/release/plugin_kansuji.js.LICENSE.txt +491 -0
  72. package/release/plugin_markup.js +1 -0
  73. package/release/plugin_markup.js.LICENSE.txt +363 -0
  74. package/release/plugin_turtle.js +1 -0
  75. package/release/plugin_turtle.js.LICENSE.txt +435 -0
  76. package/release/plugin_webworker.js +1 -0
  77. package/release/plugin_webworker.js.LICENSE.txt +491 -0
  78. package/release/version.js +2 -0
  79. package/release/version.js.LICENSE.txt +32 -0
  80. package/release/wnako3.js +2 -0
  81. package/release/wnako3.js.LICENSE.txt +1 -0
  82. package/release/wnako3webworker.js +1 -0
  83. package/release/wnako3webworker.js.LICENSE.txt +847 -0
  84. package/release/yoyakugo.json +30 -0
  85. package/src/browsers.md +60 -0
  86. package/src/cnako3.js +466 -0
  87. package/src/commander_ja.js +154 -0
  88. package/src/enako3.js +69 -0
  89. package/src/era.json +22 -0
  90. package/src/index.js +5 -0
  91. package/src/nako3.js +836 -0
  92. package/src/nako3_assert.js +37 -0
  93. package/src/nako3editorfix.sfd +106 -0
  94. package/src/nako3editorfix.woff +0 -0
  95. package/src/nako3server.js +51 -0
  96. package/src/nako_colors.js +86 -0
  97. package/src/nako_errors.js +176 -0
  98. package/src/nako_gen.js +1459 -0
  99. package/src/nako_gen_async.js +1622 -0
  100. package/src/nako_global.js +113 -0
  101. package/src/nako_indent.js +480 -0
  102. package/src/nako_josi_list.js +46 -0
  103. package/src/nako_lex_rules.js +259 -0
  104. package/src/nako_lexer.js +576 -0
  105. package/src/nako_logger.js +138 -0
  106. package/src/nako_parser3.js +1768 -0
  107. package/src/nako_parser_base.js +265 -0
  108. package/src/nako_parser_const.js +37 -0
  109. package/src/nako_prepare.js +293 -0
  110. package/src/nako_reserved_words.js +35 -0
  111. package/src/nako_source_mapping.js +251 -0
  112. package/src/nako_test.js +37 -0
  113. package/src/nako_version.js +8 -0
  114. package/src/plugin_browser.js +191 -0
  115. package/src/plugin_browser_ajax.js +352 -0
  116. package/src/plugin_browser_audio.js +109 -0
  117. package/src/plugin_browser_canvas.js +462 -0
  118. package/src/plugin_browser_chart.js +296 -0
  119. package/src/plugin_browser_color.js +49 -0
  120. package/src/plugin_browser_crypto.js +26 -0
  121. package/src/plugin_browser_dialog.js +53 -0
  122. package/src/plugin_browser_dom_basic.js +322 -0
  123. package/src/plugin_browser_dom_event.js +193 -0
  124. package/src/plugin_browser_dom_parts.js +163 -0
  125. package/src/plugin_browser_geolocation.js +51 -0
  126. package/src/plugin_browser_hotkey.js +25 -0
  127. package/src/plugin_browser_html.js +59 -0
  128. package/src/plugin_browser_in_worker.js +44 -0
  129. package/src/plugin_browser_location.js +21 -0
  130. package/src/plugin_browser_speech.js +111 -0
  131. package/src/plugin_browser_storage.js +121 -0
  132. package/src/plugin_browser_system.js +12 -0
  133. package/src/plugin_browser_websocket.js +73 -0
  134. package/src/plugin_caniuse.js +24 -0
  135. package/src/plugin_csv.js +57 -0
  136. package/src/plugin_datetime.js +414 -0
  137. package/src/plugin_express.js +212 -0
  138. package/src/plugin_kansuji.js +224 -0
  139. package/src/plugin_keigo.js +55 -0
  140. package/src/plugin_markup.js +32 -0
  141. package/src/plugin_math.js +319 -0
  142. package/src/plugin_node.js +1018 -0
  143. package/src/plugin_promise.js +94 -0
  144. package/src/plugin_system.js +2109 -0
  145. package/src/plugin_test.js +38 -0
  146. package/src/plugin_turtle.js +646 -0
  147. package/src/plugin_webworker.js +334 -0
  148. package/src/plugin_weykturtle3d.js +1216 -0
  149. package/src/plugin_worker.js +92 -0
  150. package/src/repl.nako3 +63 -0
  151. package/src/turtle-elephant.png +0 -0
  152. package/src/turtle-panda.png +0 -0
  153. package/src/turtle64.png +0 -0
  154. package/src/wnako3.js +162 -0
  155. package/src/wnako3_editor.css +215 -0
  156. package/src/wnako3_editor.js +1645 -0
  157. package/src/wnako3_editor_marker_red.png +0 -0
  158. package/src/wnako3_editor_marker_red.xcf +0 -0
  159. package/src/wnako3_editor_marker_yellow.png +0 -0
  160. package/src/wnako3_editor_marker_yellow.xcf +0 -0
  161. package/src/wnako3webworker.js +70 -0
  162. package/test/ace_editor/karma.config.js +94 -0
  163. package/test/ace_editor/test/.babelrc.json +3 -0
  164. package/test/ace_editor/test/ace_editor_test.js +178 -0
  165. package/test/ace_editor/test/html/custom_context.html +140 -0
  166. package/test/async/async_basic_test.js +124 -0
  167. package/test/browser/karma.config.js +212 -0
  168. package/test/browser/test/.babelrc.json +3 -0
  169. package/test/browser/test/compare_util.js +50 -0
  170. package/test/browser/test/html/canvas_basic.html +1 -0
  171. package/test/browser/test/html/div_basic.html +2 -0
  172. package/test/browser/test/html/event_dom_form.html +4 -0
  173. package/test/browser/test/html/event_dom_scrolldiv.html +5 -0
  174. package/test/browser/test/image/canvas_test1.png +0 -0
  175. package/test/browser/test/image/canvas_test2.png +0 -0
  176. package/test/browser/test/image/canvas_test3.png +0 -0
  177. package/test/browser/test/image/canvas_test4.png +0 -0
  178. package/test/browser/test/image/canvas_test7.png +0 -0
  179. package/test/browser/test/image/canvas_test8.png +0 -0
  180. package/test/browser/test/image/canvas_test_blank.png +0 -0
  181. package/test/browser/test/image/elephant_kana.png +0 -0
  182. package/test/browser/test/image/panda_kana.png +0 -0
  183. package/test/browser/test/image/turtle_kana.png +0 -0
  184. package/test/browser/test/import_plugin_checker.js +24 -0
  185. package/test/browser/test/plugin_browser_test.js +52 -0
  186. package/test/browser/test/plugin_browser_test_ajax.js +123 -0
  187. package/test/browser/test/plugin_browser_test_color.js +18 -0
  188. package/test/browser/test/plugin_browser_test_dialog.js +72 -0
  189. package/test/browser/test/plugin_browser_test_dom_event.js +598 -0
  190. package/test/browser/test/plugin_browser_test_dom_parts.js +125 -0
  191. package/test/browser/test/plugin_browser_test_system.js +9 -0
  192. package/test/browser/test/plugin_turtle_test.js +817 -0
  193. package/test/browser/test/plugin_webworker_test.js +87 -0
  194. package/test/browser/test/require_test.js +71 -0
  195. package/test/bundled/karma.config.base.js +117 -0
  196. package/test/bundled/karma.config.js +86 -0
  197. package/test/bundled/test/.babelrc.json +3 -0
  198. package/test/bundled/test/bundled_test.js +69 -0
  199. package/test/bundled/test/html/custom_context.html +65 -0
  200. package/test/bundled/test/html/custom_debug.html +66 -0
  201. package/test/bundled/test4b.cmd +52 -0
  202. package/test/bundled/test_base/.babelrc.json +3 -0
  203. package/test/bundled/test_base/_checktool_test.js +25 -0
  204. package/test/bundled/test_base/basic_ajax_test.js +56 -0
  205. package/test/bundled/test_base/basic_async_test.js +18 -0
  206. package/test/bundled/test_base/basic_test.js +153 -0
  207. package/test/bundled/test_base/calc_test.js +132 -0
  208. package/test/bundled/test_base/css/browsers_box.css +114 -0
  209. package/test/bundled/test_base/html/custom_context.html +69 -0
  210. package/test/bundled/test_base/html/custom_debug.html +71 -0
  211. package/test/bundled/test_base/js/browsers_box.js +72 -0
  212. package/test/bundled/test_base/plugin_csv_test.js +37 -0
  213. package/test/bundled/test_base/plugin_datetime_test.js +115 -0
  214. package/test/bundled/test_base/plugin_kansuji_test.js +49 -0
  215. package/test/bundled/test_base/plugin_system_test.js +410 -0
  216. package/test/bundled/test_base/plugin_webworker_test.js +53 -0
  217. package/test/bundled/test_base/resources/ok.txt +1 -0
  218. package/test/bundled/test_base/test_utils.js +191 -0
  219. package/test/common/array_test.js +40 -0
  220. package/test/common/basic_test.js +317 -0
  221. package/test/common/calc_test.js +139 -0
  222. package/test/common/debug_test.js +16 -0
  223. package/test/common/error_test.js +16 -0
  224. package/test/common/flow_test.js +360 -0
  225. package/test/common/func_call.js +136 -0
  226. package/test/common/func_test.js +149 -0
  227. package/test/common/indent_test.js +362 -0
  228. package/test/common/lex_test.js +146 -0
  229. package/test/common/literal_test.js +72 -0
  230. package/test/common/nako_logger_test.js +26 -0
  231. package/test/common/plugin_browser_test.js +24 -0
  232. package/test/common/plugin_browser_ut_audio_test.js +88 -0
  233. package/test/common/plugin_browser_ut_color_test.js +21 -0
  234. package/test/common/plugin_browser_ut_dialog_test.js +100 -0
  235. package/test/common/plugin_browser_ut_html_test.js +13 -0
  236. package/test/common/plugin_browser_ut_system_test.js +10 -0
  237. package/test/common/plugin_csv_test.js +39 -0
  238. package/test/common/plugin_datetime_test.js +120 -0
  239. package/test/common/plugin_kansuji_test.js +59 -0
  240. package/test/common/plugin_promise_test.js +18 -0
  241. package/test/common/plugin_system_test.js +451 -0
  242. package/test/common/prepare_test.js +93 -0
  243. package/test/common/re_test.js +20 -0
  244. package/test/common/variable_scope_test.js +105 -0
  245. package/test/jsconfig.json +19 -0
  246. package/test/karma.config.js +91 -0
  247. package/test/node/add_test.nako3 +1 -0
  248. package/test/node/async_test.js +80 -0
  249. package/test/node/commander_ja_test.js +82 -0
  250. package/test/node/error_message_test.js +244 -0
  251. package/test/node/kai_test.nako3 +6 -0
  252. package/test/node/node_test.js +43 -0
  253. package/test/node/plugin_broken.js.txt +3 -0
  254. package/test/node/plugin_browser_ut_ajax_test.js +355 -0
  255. package/test/node/plugin_browser_ut_location_test.js +32 -0
  256. package/test/node/plugin_markup_test.js +44 -0
  257. package/test/node/plugin_math_test.js +42 -0
  258. package/test/node/plugin_node_test.js +93 -0
  259. package/test/node/plugin_test.js +16 -0
  260. package/test/node/relative_import_test_1.nako3 +1 -0
  261. package/test/node/relative_import_test_2.nako3 +2 -0
  262. package/test/node/require_nako3_test.js +59 -0
  263. package/test/node/requiretest.nako3 +4 -0
  264. package/test/node/requiretest_indirect.nako3 +1 -0
  265. package/test/node/requiretest_name.nako3 +5 -0
  266. package/test/node/runtime_error.nako3 +2 -0
  267. package/test/node/side_effects_test.js +106 -0
  268. package/test/node/sjis.txt +5 -0
  269. package/test/node/syntax_error.nako3 +2 -0
  270. package/test/node/wnako3_editor_test.js +360 -0
  271. package/tools/README.md +7 -0
  272. package/tools/nako3edit/html/edit.html +83 -0
  273. package/tools/nako3edit/html/edit_plugin.js +6 -0
  274. package/tools/nako3edit/html/files.html +49 -0
  275. package/tools/nako3edit/html/nako3edit.css +66 -0
  276. package/tools/nako3edit/index.nako3 +145 -0
  277. package/tools/nako3edit/run.js +12 -0
  278. package/tools/nako3server/html/edit.html +104 -0
  279. package/tools/nako3server/html/edit_plugin.js +6 -0
  280. package/tools/nako3server/html/files.html +53 -0
  281. package/tools/nako3server/html/nako3edit.css +66 -0
  282. package/tools/nako3server/index.nako3 +129 -0
  283. package/tools/nako3server/run.js +12 -0
@@ -0,0 +1,30 @@
1
+ [
2
+ "回",
3
+ "回繰返",
4
+ "間",
5
+ "間繰返",
6
+ "繰返",
7
+ "反復",
8
+ "抜",
9
+ "続",
10
+ "戻",
11
+ "先",
12
+ "次",
13
+ "代入",
14
+ "実行速度優先",
15
+ "パフォーマンスモニタ適用",
16
+ "定",
17
+ "逐次実行",
18
+ "条件分岐",
19
+ "変数",
20
+ "定数",
21
+ "エラー監視",
22
+ "エラー",
23
+ "それ",
24
+ "そう",
25
+ "関数",
26
+ "インデント構文",
27
+ "非同期モード",
28
+ "モード設定",
29
+ "取込"
30
+ ]
@@ -0,0 +1,60 @@
1
+ # 対応ブラウザ
2
+
3
+ * ブラウザ
4
+ * モバイル
5
+ * Chrome for Android
6
+ * 80
7
+ * Firefox for Android
8
+ * 68
9
+ * QQ Browser
10
+ * 1.2
11
+ * UC Browser for Android
12
+ * 12.12
13
+ * Android Browser
14
+ * 80
15
+ * Baidu Browser
16
+ * 7.12
17
+ * iOS Safari
18
+ * 13.3
19
+ * 13.2
20
+ * 13.0-13.1
21
+ * 12.2-12.4
22
+ * 12.0-12.1
23
+ * 11.3-11.4
24
+ * 10.3
25
+ * 9.3
26
+ * KaiOS Browser
27
+ * 2.5
28
+ * Opera Mini
29
+ * all
30
+ * Opera Mobile
31
+ * 46
32
+ * Samsung Internet
33
+ * 11.1
34
+ * 10.1
35
+ * PC
36
+ * Chrome
37
+ * 80
38
+ * 79
39
+ * 49
40
+ * Edge
41
+ * 80
42
+ * 79
43
+ * 18
44
+ * Firefox
45
+ * 74
46
+ * 73
47
+ * 72
48
+ * 68
49
+ * Internet Explorer
50
+ * 11
51
+ * Opera
52
+ * 66
53
+ * 65
54
+ * Safari
55
+ * 13
56
+ * 12.1
57
+ * PC (Windows/macOS/Linux)
58
+ * Node
59
+ * 12.16.0
60
+ * 10.19.0
package/src/cnako3.js ADDED
@@ -0,0 +1,466 @@
1
+ #!/usr/bin/env node
2
+ // @ts-nocheck
3
+ /**
4
+ * コマンドライン版のなでしこ3
5
+ */
6
+ const fetch = require('node-fetch')
7
+ if (typeof globalThis !== 'undefined' && typeof globalThis.fetch === 'undefined') {
8
+ globalThis.fetch = fetch
9
+ } else
10
+ if (typeof global !== 'undefined' && typeof global.fetch === 'undefined') {
11
+ global.fetch = fetch
12
+ }
13
+
14
+ const fs = require('fs')
15
+ const exec = require('child_process').exec
16
+
17
+ const path = require('path')
18
+ const NakoCompiler = require('./nako3')
19
+ const PluginNode = require('./plugin_node')
20
+ const { NakoImportError } = require('./nako_errors')
21
+ const NakoGenASync = require('./nako_gen_async')
22
+
23
+ class CNako3 extends NakoCompiler {
24
+ /** @param {{ nostd?: boolean }} [opts] */
25
+ constructor (opts = {}) {
26
+ super({ useBasicPlugin: true })
27
+ this.silent = false
28
+ this.addCodeGenerator('非同期モード', NakoGenASync) // 「!非同期モード」をサポート
29
+ if (!opts.nostd) {
30
+ this.addPluginFile('PluginNode', path.join(__dirname, 'plugin_node.js'), PluginNode)
31
+ }
32
+ this.__varslist[0]['ナデシコ種類'] = 'cnako3'
33
+ }
34
+
35
+ // CNAKO3で使えるコマンドを登録する
36
+ registerCommands () {
37
+ // コマンド引数がないならば、ヘルプを表示(-hはcommandarにデフォルト用意されている)
38
+ if (process.argv.length <= 2) { process.argv.push('-h') }
39
+
40
+ // commanderを使って引数を解析する
41
+ const app = require('./commander_ja.js')
42
+ const nakoVersion = require('./nako_version.js')
43
+ app
44
+ .title('日本語プログラミング言語「なでしこ」v' + nakoVersion.version)
45
+ .version(nakoVersion.version, '-v, --version')
46
+ .usage('[オプション] 入力ファイル.nako3')
47
+ .option('-w, --warn', '警告を表示する')
48
+ .option('-d, --debug', 'デバッグモードの指定')
49
+ .option('-D, --trace', '詳細デバッグモードの指定')
50
+ .option('-c, --compile', 'コンパイルモードの指定')
51
+ .option('-t, --test', 'コンパイルモードの指定 (テスト用コードを出力)')
52
+ .option('-r, --run', 'コンパイルモードでも実行する')
53
+ .option('-e, --eval [src]', '直接プログラムを実行するワンライナーモード')
54
+ .option('-o, --output', '出力ファイル名の指定')
55
+ .option('-s, --silent', 'サイレントモードの指定')
56
+ .option('-l, --repl', '対話シェル(REPL)の実行')
57
+ .option('-b, --browsers', '対応機器/Webブラウザを表示する')
58
+ .option('-m, --man [command]', 'マニュアルを表示する')
59
+ .option('-p, --speed', 'スピード優先モードの指定')
60
+ .option('-A, --ast', 'パースした結果をASTで出力する')
61
+ // .option('-h, --help', '使い方を表示する')
62
+ // .option('-v, --version', 'バージョンを表示する')
63
+ .parse(process.argv)
64
+ return app
65
+ }
66
+
67
+ /**
68
+ * コマンドライン引数を解析
69
+ * @returns {{warn: boolean, debug: boolean, compile: any | boolean, test: any | boolean, one_liner: any | boolean, trace: any, run: any | boolean, repl: any | boolean, source: any | string}}
70
+ */
71
+ checkArguments () {
72
+ const app = this.registerCommands()
73
+
74
+ /** @type {import('./nako_logger').LogLevel} */
75
+ let logLevel = 'error'
76
+ if (app.trace) {
77
+ logLevel = 'trace'
78
+ } else if (app.debug) {
79
+ logLevel = 'debug'
80
+ } else if (app.warn) {
81
+ logLevel = 'warn'
82
+ }
83
+ this.logger.addListener(logLevel, ({ level, nodeConsole }) => {
84
+ if (this.silent && level === 'stdout') {
85
+ return
86
+ }
87
+ console.log(nodeConsole)
88
+ })
89
+
90
+ const args = {
91
+ compile: app.compile || false,
92
+ run: app.run || false,
93
+ source: app.eval || '',
94
+ man: app.man || '',
95
+ one_liner: app.eval || false,
96
+ debug: this.debug,
97
+ trace: app.trace,
98
+ warn: app.warn,
99
+ repl: app.repl || false,
100
+ test: app.test || false,
101
+ browsers: app.browsers || false,
102
+ speed: app.speed || false,
103
+ ast: app.ast || false
104
+ }
105
+ args.mainfile = app.args[0]
106
+ args.output = app.output
107
+ if (/\.(nako|nako3|txt|bak)$/.test(args.mainfile)) {
108
+ if (!args.output) {
109
+ if (args.test) {
110
+ args.output = args.mainfile.replace(/\.(nako|nako3)$/, '.spec.js')
111
+ } else {
112
+ args.output = args.mainfile.replace(/\.(nako|nako3)$/, '.js')
113
+ }
114
+ }
115
+ } else {
116
+ if (!args.output) {
117
+ if (args.test) {
118
+ args.output = args.mainfile + '.spec.js'
119
+ } else {
120
+ args.output = args.mainfile + '.js'
121
+ }
122
+ }
123
+ args.mainfile += '.nako3'
124
+ }
125
+ return args
126
+ }
127
+
128
+ // 実行する
129
+ execCommand () {
130
+ const opt = this.checkArguments()
131
+ if (opt.man) {
132
+ this.cnakoMan(opt.man)
133
+ return
134
+ }
135
+ if (opt.browsers) { // 対応ブラウザを表示する
136
+ this.cnakoBrowsers()
137
+ return
138
+ }
139
+ if (opt.mainfile) { this.filename = opt.mainfile }
140
+ if (opt.repl) { // REPLを実行する
141
+ this.cnakoRepl(opt)
142
+ return
143
+ }
144
+ if (opt.one_liner) { // ワンライナーで実行する
145
+ this.cnakoOneLiner(opt)
146
+ return
147
+ }
148
+
149
+ // メインプログラムを読み込む
150
+ const src = fs.readFileSync(opt.mainfile, 'utf-8')
151
+ if (opt.compile) {
152
+ this.nakoCompile(opt, src, false)
153
+ return
154
+ }
155
+ if (opt.ast) {
156
+ this.outputAST(opt, src)
157
+ return
158
+ }
159
+ try {
160
+ if (opt.test) {
161
+ this.loadDependencies(src, opt.mainfile, '')
162
+ this.test(src, opt.mainfile)
163
+ } else {
164
+ this.run(src, opt.mainfile)
165
+ }
166
+ if (opt.test && this.numFailures > 0) {
167
+ process.exit(1)
168
+ }
169
+ } catch (e) {
170
+ if (opt.debug || opt.trace) {
171
+ throw e
172
+ }
173
+ // エラーメッセージはloggerへ送られるため無視してよい
174
+ }
175
+ }
176
+
177
+ /**
178
+ * コンパイルモードの場合
179
+ * @param opt
180
+ * @param {string} src
181
+ * @param {boolean} isTest
182
+ */
183
+ nakoCompile (opt, src, isTest) {
184
+ // system
185
+ const jscode = this.compileStandalone(src, this.filename, isTest)
186
+ console.log(opt.output)
187
+ fs.writeFileSync(opt.output, jscode, 'utf-8')
188
+ if (opt.run) {
189
+ exec(`node ${opt.output}`, function (err, stdout, stderr) {
190
+ if (err) { console.log('[ERROR]', stderr) }
191
+ console.log(stdout)
192
+ })
193
+ }
194
+ }
195
+
196
+ // ワンライナーの場合
197
+ cnakoOneLiner (opt) {
198
+ const org = opt.source
199
+ try {
200
+ if (opt.source.indexOf('表示') < 0) {
201
+ opt.source = '' + opt.source + 'を表示。'
202
+ }
203
+ this.run(opt.source)
204
+ } catch (e) {
205
+ // エラーになったら元のワンライナーで再挑戦
206
+ try {
207
+ if (opt.source !== org) {
208
+ this.run(org)
209
+ } else {
210
+ throw e
211
+ }
212
+ } catch (e) {
213
+ if (this.debug) {
214
+ throw e
215
+ } else {
216
+ console.error(e.message)
217
+ }
218
+ }
219
+ }
220
+ }
221
+
222
+ /**
223
+ * ASTを出力
224
+ * @param opt
225
+ * @param {string} src
226
+ * @param {boolean} isTest
227
+ */
228
+ outputAST (opt, src) {
229
+ const ast = this.parse(src, opt.mainfile)
230
+ const makeIndent = (level) => {
231
+ let s = ''
232
+ for (let i = 0; i < level; i++) { s += ' ' }
233
+ return s
234
+ }
235
+ const trim = (s) => { return s.replace(/(^\s+|\s+$)/g, '') }
236
+ /**
237
+ * AST文字列に変換して返す
238
+ * @param {*} ast
239
+ * @param {number} level
240
+ * @return {string}
241
+ */
242
+ const outAST = (ast, level) => {
243
+ if (typeof (ast) === 'string') {
244
+ return makeIndent(level) + '"' + ast + '"'
245
+ }
246
+ if (typeof (ast) === 'number') {
247
+ return makeIndent(level) + ast
248
+ }
249
+ if (ast instanceof Array) {
250
+ const s = makeIndent(level) + '[\n'
251
+ const sa = []
252
+ ast.forEach((a) => {
253
+ sa.push(outAST(a, level + 1))
254
+ })
255
+ return s + sa.join(',\n') + '\n' + makeIndent(level) + ']'
256
+ }
257
+ if (ast instanceof Object) {
258
+ const s = makeIndent(level) + '{\n'
259
+ const sa = []
260
+ for (const key in ast) {
261
+ const sv = trim(outAST(ast[key], level + 1))
262
+ const so = makeIndent(level + 1) + '"' + key + '": ' + sv
263
+ sa.push(so)
264
+ }
265
+ return s + sa.join(',\n') + '\n' + makeIndent(level) + '}'
266
+ }
267
+ return makeIndent(level) + ast
268
+ }
269
+ console.log(outAST(ast, 0))
270
+ }
271
+
272
+ // REPL(対話実行環境)の場合
273
+ cnakoRepl (opt) {
274
+ const fname = path.join(__dirname, 'repl.nako3')
275
+ const src = fs.readFileSync(fname, 'utf-8')
276
+ this.run(src, true)
277
+ }
278
+
279
+ // マニュアルを表示する
280
+ cnakoMan (command) {
281
+ try {
282
+ const commands = require('../release/command_cnako3.json')
283
+ const data = commands[command]
284
+ for (const key in data) {
285
+ console.log(`${key}: ${data[key]}`)
286
+ }
287
+ } catch (e) {
288
+ if (e.code === 'MODULE_NOT_FOUND') {
289
+ console.log('コマンド一覧がないため、マニュアルを表示できません。以下のコマンドでコマンド一覧を生成してください。\n$ npm run build')
290
+ } else {
291
+ throw e
292
+ }
293
+ }
294
+ }
295
+
296
+ // 対応機器/Webブラウザを表示する
297
+ cnakoBrowsers () {
298
+ console.log(fs.readFileSync(path.join(__dirname, 'browsers.md'), 'utf-8'))
299
+ }
300
+
301
+ /**
302
+ * @param {string} code
303
+ * @param {string} filename
304
+ * @param {string} preCode
305
+ */
306
+ loadDependencies (code, filename, preCode) {
307
+ /** @type {string[]} */
308
+ const log = []
309
+ // 同期的に読み込む
310
+ const tasks = super._loadDependencies(code, filename, preCode, {
311
+ resolvePath: (name, token) => {
312
+ if (/\.js(\.txt)?$/.test(name) || /^[^.]*$/.test(name)) {
313
+ return { filePath: path.resolve(CNako3.findPluginFile(name, this.filename, __dirname, log)), type: 'js' }
314
+ }
315
+ if (/\.nako3?(\.txt)?$/.test(name)) {
316
+ if (path.isAbsolute(name)) {
317
+ return { filePath: path.resolve(name), type: 'nako3' }
318
+ } else {
319
+ // filename が undefined のとき token.file が undefined になる。
320
+ if (token.file === undefined) {
321
+ throw new Error('ファイル名を指定してください。')
322
+ }
323
+ return { filePath: path.resolve(path.join(path.dirname(token.file), name)), type: 'nako3' }
324
+ }
325
+ }
326
+ return { filePath: name, type: 'invalid' }
327
+ },
328
+ readNako3: (name, token) => {
329
+ if (!fs.existsSync(name)) {
330
+ throw new NakoImportError(`ファイル ${name} が存在しません。`, token.line, token.file)
331
+ }
332
+ return { sync: true, value: fs.readFileSync(name).toString() }
333
+ },
334
+ readJs: (name, token) => {
335
+ return {
336
+ sync: true,
337
+ value: () => {
338
+ try {
339
+ return require(name)
340
+ } catch (/** @type {unknown} */err) {
341
+ let msg = `プラグイン ${name} の取り込みに失敗: ${err instanceof Error ? err.message : err + ''}`
342
+ if (err instanceof Error && err.message.startsWith('Cannot find module')) {
343
+ msg += `\n次の場所を検索しました: ${log.join(', ')}`
344
+ }
345
+ throw new NakoImportError(msg, token.line, token.file)
346
+ }
347
+ }
348
+ }
349
+ }
350
+ })
351
+ if (tasks !== undefined) {
352
+ throw new Error('assertion error')
353
+ }
354
+ }
355
+
356
+ /**
357
+ * @param {string} code
358
+ * @param {string} fname
359
+ * @param {string} [preCode]
360
+ */
361
+ run (code, fname, preCode = '') {
362
+ const tasks = this.loadDependencies(code, fname, preCode)
363
+ if (tasks !== undefined) {
364
+ throw new Error('assertion error')
365
+ }
366
+ return this._runEx(code, fname, {}, preCode)
367
+ }
368
+
369
+ /**
370
+ * プラグインファイルの検索を行う
371
+ * @param {string} pname
372
+ * @param {string} filename
373
+ * @param {string} srcDir このファイルが存在するディレクトリ
374
+ * @param {string[]} [log]
375
+ * @return {string} フルパス
376
+ */
377
+ static findPluginFile (pname, filename, srcDir, log = []) {
378
+ log.length = 0
379
+ /** @type {string[]} */
380
+ // フルパス指定か?
381
+ const p1 = pname.substr(0, 1)
382
+ if (p1 === '/') {
383
+ // フルパス指定なので何もしない
384
+ return pname
385
+ }
386
+ // 各パスを調べる
387
+ const exists = (f, desc) => {
388
+ const result = fs.existsSync(f)
389
+ log.push(f)
390
+ // console.log(result, 'exists[', desc, '] =', f)
391
+ return result
392
+ }
393
+ const fCheck = (pathTest) => {
394
+ // 素直にチェック
395
+ let fpath = path.join(pathTest, pname)
396
+ if (exists(fpath, 'direct')) { return fpath }
397
+
398
+ // プラグイン名を分解してチェック
399
+ const m = pname.match(/^(plugin_|nadesiko3-)([a-zA-Z0-9_-]+)/)
400
+ if (!m) { return false }
401
+ const name = m[2]
402
+ // plugin_xxx.js
403
+ // eslint-disable-next-line camelcase
404
+ const plugin_xxx_js = 'plugin_' + name + '.js'
405
+ fpath = path.join(pathTest, plugin_xxx_js)
406
+ if (exists(fpath, 'plugin_xxx.js')) { return fpath }
407
+ fpath = path.join(pathTest, 'src', plugin_xxx_js)
408
+ if (exists(fpath, 'src/plugin_xxx.js')) { return fpath }
409
+ // nadesiko3-xxx
410
+ // eslint-disable-next-line camelcase
411
+ const nadesiko3_xxx = 'nadesiko3-' + name
412
+ fpath = path.join(pathTest, nadesiko3_xxx)
413
+ if (exists(fpath, 'nadesiko3-xxx')) { return fpath }
414
+ fpath = path.join(pathTest, 'node_modules', nadesiko3_xxx)
415
+ if (exists(fpath, 'node_modules/nadesiko3-xxx')) { return fpath }
416
+ return false
417
+ }
418
+ // 相対パスか?
419
+ if (p1 === '.') {
420
+ // 相対パス指定なので、なでしこのプログラムからの相対指定を調べる
421
+ const pathRelative = path.resolve(path.dirname(filename))
422
+ const fileRelative = fCheck(pathRelative)
423
+ if (fileRelative) { return fileRelative }
424
+ }
425
+ // nako3スクリプトパスか?
426
+ const pathScript = path.resolve(path.dirname(filename))
427
+ const fileScript = fCheck(pathScript)
428
+ if (fileScript) { return fileScript }
429
+
430
+ // ランタイムパス/src
431
+ const pathRuntimeSrc = path.resolve(srcDir)
432
+ const fileRuntimeSrc = fCheck(pathRuntimeSrc)
433
+ if (fileRuntimeSrc) { return fileRuntimeSrc }
434
+ // ランタイムパス
435
+ const pathRuntime = path.dirname(pathRuntimeSrc)
436
+ const fileRuntime = fCheck(pathRuntime)
437
+ if (fileRuntime) { return fileRuntime }
438
+
439
+ // 環境変数 NAKO_HOMEか?
440
+ if (process.env.NAKO_HOME) {
441
+ const NAKO_HOME = path.resolve(process.env.NAKO_HOME)
442
+ const fileHome = fCheck(NAKO_HOME)
443
+ if (fileHome) { return fileHome }
444
+ // NAKO_HOME/src ?
445
+ const pathNakoHomeSrc = path.join(NAKO_HOME, 'src')
446
+ const fileNakoHomeSrc = fCheck(pathNakoHomeSrc)
447
+ if (fileNakoHomeSrc) { return fileNakoHomeSrc }
448
+ }
449
+ // 環境変数 NODE_PATH (global) 以下にあるか?
450
+ if (process.env.NODE_PATH) {
451
+ const pathNode = path.resolve(process.env.NODE_PATH)
452
+ const fileNode = fCheck(pathNode)
453
+ if (fileNode) { return fileNode }
454
+ }
455
+ // Nodeのパス検索に任せる
456
+ return pname
457
+ }
458
+ }
459
+
460
+ // メイン
461
+ if (require.main === module) { // 直接実行する
462
+ const cnako3 = new CNako3()
463
+ cnako3.execCommand()
464
+ } else { // モジュールとして使う場合
465
+ module.exports = CNako3
466
+ }
@@ -0,0 +1,154 @@
1
+ // commander_ja.js
2
+ const app = {
3
+ args: /** @type {string[]} */([]),
4
+ _alias: /** @type {Record<string, string>} */({}),
5
+ _hasarg: /** @type {Record<string, boolean>} */({}),
6
+ _help: /** @type {[string, string][]} */([]),
7
+ _usage: '',
8
+ _version: '1.0.0',
9
+ _title: '',
10
+ /**
11
+ * set version info
12
+ * @param {*} ver version info
13
+ * @param {*} cmd -v, --version
14
+ */
15
+ version (ver, cmd) {
16
+ this.option(cmd, 'バージョン情報を表示')
17
+ this._version = ver
18
+ return this
19
+ },
20
+ /**
21
+ * @param {string} title
22
+ */
23
+ title (title) {
24
+ this._title = title
25
+ return this
26
+ },
27
+ /**
28
+ * @param {string} usage
29
+ */
30
+ usage (usage) {
31
+ this._usage = usage
32
+ return this
33
+ },
34
+ /**
35
+ * @param {string} cmd
36
+ * @param {string} desc
37
+ */
38
+ option (cmd, desc) {
39
+ app._help.push([cmd, desc])
40
+ let name1 = ''
41
+ const name2 = /** @type {string[]} */([])
42
+ const cmdList = cmd.split(',')
43
+ cmdList.forEach((t) => {
44
+ const c = t.replace(/\s+/, '')
45
+ const m = c.match(/^(-+)([a-zA-Z0-9_]+)/)
46
+ if (!m) {
47
+ throw Error('[Invalid option]: should be -(shortname) --(longname): ' + cmd)
48
+ }
49
+ if (m[1] === '--') {
50
+ name1 = m[2]
51
+ return
52
+ }
53
+ if (m[1] === '-') {
54
+ name2.push(m[2])
55
+ }
56
+ })
57
+ if (name1 === '') {
58
+ throw Error('Invalid option(no longname): ' + cmd)
59
+ }
60
+ if (name1 === 'version') { name1 = 'version_' }
61
+ this[name1] = false
62
+ name2.forEach((t) => {
63
+ this._alias[t] = name1
64
+ })
65
+ this._hasarg[name1] = (cmd.indexOf('[') >= 0)
66
+ return this
67
+ },
68
+ /**
69
+ * parse and return str
70
+ * @param {string[]} argv
71
+ * @return {string}
72
+ */
73
+ parseStr (argv) {
74
+ this.args = []
75
+ const paramStack = []
76
+ let lastOpt = ''
77
+ for (let i = 2; i < argv.length; i++) {
78
+ const arg = argv[i]
79
+ // Not Options
80
+ if (arg.charAt(0) !== '-') {
81
+ if (paramStack.length > 0) {
82
+ const argParam = paramStack.pop()
83
+ this[argParam] = arg
84
+ continue
85
+ }
86
+ this.args.push(arg)
87
+ continue
88
+ }
89
+ // Options
90
+ if (arg.substr(0, 2) === '--') {
91
+ lastOpt = arg.substr(2)
92
+ if (lastOpt === 'version') { lastOpt = 'version_' }
93
+ } else {
94
+ // Short Option
95
+ const shortName = arg.substr(1)
96
+ if (this._alias[shortName]) {
97
+ lastOpt = this._alias[shortName]
98
+ } else {
99
+ // Not exists
100
+ continue
101
+ }
102
+ }
103
+ // set option true
104
+ this[lastOpt] = true
105
+ if (this._hasarg[lastOpt]) { // has argument?
106
+ // init parameter
107
+ this[lastOpt] = ''
108
+ paramStack.push(lastOpt)
109
+ }
110
+ }
111
+ // show version?
112
+ if (this.version_) {
113
+ return this._version
114
+ }
115
+ // show help?
116
+ if (this.help) {
117
+ return this.getHelp()
118
+ }
119
+ return ''
120
+ },
121
+
122
+ /**
123
+ * parse
124
+ * @param {*} argv
125
+ */
126
+ parse (argv) {
127
+ const s = this.parseStr(argv)
128
+ if (s === '') {
129
+ return this
130
+ }
131
+ console.log(s)
132
+ process.exit(0)
133
+ },
134
+
135
+ getHelp () {
136
+ let ss = ''
137
+ if (this._title) {
138
+ ss += this._title + '\n'
139
+ }
140
+ ss += '使い方: ' + this._usage + '\n'
141
+ ss += 'オプション:\n'
142
+ const spc = ' '
143
+ this._help.forEach((c) => {
144
+ const opt = c[0] + spc
145
+ ss += ' ' + opt.substr(0, 20) + ' ' + c[1] + '\n'
146
+ })
147
+ return ss
148
+ }
149
+ }
150
+
151
+ app.option('-h, --help', 'コマンドの使い方を表示')
152
+
153
+ // register app
154
+ module.exports = app