nadesiko3 3.2.51 → 3.3.2

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 (169) hide show
  1. package/README.md +1 -1
  2. package/bin/cnako3 +1 -1
  3. package/bin/cnako3.bat +1 -1
  4. package/demo/browsers.html +9 -10
  5. package/demo/flow.html +1 -0
  6. package/demo/turtle3.html +18 -17
  7. package/demo/turtle3d.html +1 -1
  8. package/demo/turtle3d2.html +1 -1
  9. package/doc/browsers.md +9 -10
  10. package/package.json +58 -57
  11. package/release/_hash.txt +57 -52
  12. package/release/_script-tags.txt +14 -13
  13. package/release/command.json +1 -1
  14. package/release/command.json.js +1 -1
  15. package/release/command_cnako3.json +1 -1
  16. package/release/command_list.json +1 -1
  17. package/release/editor.js +1 -1
  18. package/release/editor.js.LICENSE.txt +9 -12
  19. package/release/nako_gen_async.js +1 -1
  20. package/release/nako_gen_async.js.LICENSE.txt +10 -666
  21. package/release/plugin_caniuse.js +1 -1
  22. package/release/plugin_caniuse.js.LICENSE.txt +9 -425
  23. package/release/plugin_csv.js +1 -1
  24. package/release/plugin_csv.js.LICENSE.txt +3 -419
  25. package/release/plugin_datetime.js +1 -1
  26. package/release/plugin_datetime.js.LICENSE.txt +6 -530
  27. package/release/plugin_kansuji.js +1 -1
  28. package/release/plugin_kansuji.js.LICENSE.txt +3 -559
  29. package/release/plugin_markup.js +1 -1
  30. package/release/plugin_markup.js.LICENSE.txt +4 -420
  31. package/release/plugin_turtle.js +1 -1
  32. package/release/plugin_turtle.js.LICENSE.txt +12 -500
  33. package/release/plugin_webworker.js +1 -1
  34. package/release/plugin_webworker.js.LICENSE.txt +3 -563
  35. package/release/plugin_weykturtle3d.js +1 -0
  36. package/release/stats.json +1 -1
  37. package/release/version.js +1 -1
  38. package/release/version.js.LICENSE.txt +9 -12
  39. package/release/wnako3.js +1 -1
  40. package/release/wnako3.js.LICENSE.txt +1 -1
  41. package/release/wnako3webworker.js +1 -1
  42. package/release/wnako3webworker.js.LICENSE.txt +1 -1117
  43. package/src/browsers.mjs +1 -0
  44. package/src/browsers.txt +15 -15
  45. package/src/browsers_agents.mjs +1 -0
  46. package/src/cnako3.mjs +13 -0
  47. package/src/{cnako3.js → cnako3mod.mjs} +46 -53
  48. package/src/{commander_ja.js → commander_ja.mjs} +16 -11
  49. package/src/{enako3.js → enako3.mjs} +0 -0
  50. package/src/{era.json → era.mjs} +1 -1
  51. package/src/image_turtle-elephant.mjs +5 -0
  52. package/src/image_turtle-panda.mjs +5 -0
  53. package/src/image_turtle64.mjs +5 -0
  54. package/src/index.mjs +9 -0
  55. package/src/{nako3.js → nako3.mjs} +56 -52
  56. package/src/{nako3_assert.js → nako3_assert.mjs} +7 -6
  57. package/src/{nako3server.js → nako3server.mjs} +12 -5
  58. package/src/{nako_colors.js → nako_colors.mjs} +1 -1
  59. package/src/{nako_errors.js → nako_errors.mjs} +8 -18
  60. package/src/{nako_from_dncl.js → nako_from_dncl.mjs} +8 -6
  61. package/src/{nako_gen.js → nako_gen.mjs} +107 -89
  62. package/src/{nako_gen_async.js → nako_gen_async.mjs} +5 -8
  63. package/src/{nako_global.js → nako_global.mjs} +4 -5
  64. package/src/{nako_indent.js → nako_indent.mjs} +4 -4
  65. package/src/{nako_josi_list.js → nako_josi_list.mjs} +16 -14
  66. package/src/nako_lex_rules.mjs +244 -0
  67. package/src/{nako_lexer.js → nako_lexer.mjs} +7 -10
  68. package/src/{nako_logger.js → nako_logger.mjs} +3 -4
  69. package/src/{nako_parser3.js → nako_parser3.mjs} +5 -5
  70. package/src/{nako_parser_base.js → nako_parser_base.mjs} +1 -9
  71. package/src/{nako_parser_const.js → nako_parser_const.mjs} +2 -6
  72. package/src/{nako_prepare.js → nako_prepare.mjs} +1 -2
  73. package/src/{nako_reserved_words.js → nako_reserved_words.mjs} +1 -1
  74. package/src/{nako_source_mapping.js → nako_source_mapping.mjs} +4 -11
  75. package/src/{nako_test.js → nako_test.mjs} +0 -0
  76. package/src/nako_version.mjs +7 -0
  77. package/src/{plugin_browser.js → plugin_browser.mjs} +30 -30
  78. package/src/{plugin_browser_ajax.js → plugin_browser_ajax.mjs} +7 -7
  79. package/src/{plugin_browser_audio.js → plugin_browser_audio.mjs} +1 -1
  80. package/src/{plugin_browser_canvas.js → plugin_browser_canvas.mjs} +1 -1
  81. package/src/{plugin_browser_chart.js → plugin_browser_chart.mjs} +1 -1
  82. package/src/{plugin_browser_color.js → plugin_browser_color.mjs} +1 -1
  83. package/src/{plugin_browser_crypto.js → plugin_browser_crypto.mjs} +1 -1
  84. package/src/{plugin_browser_dialog.js → plugin_browser_dialog.mjs} +1 -1
  85. package/src/{plugin_browser_dom_basic.js → plugin_browser_dom_basic.mjs} +1 -1
  86. package/src/{plugin_browser_dom_event.js → plugin_browser_dom_event.mjs} +1 -1
  87. package/src/{plugin_browser_dom_parts.js → plugin_browser_dom_parts.mjs} +1 -1
  88. package/src/{plugin_browser_geolocation.js → plugin_browser_geolocation.mjs} +1 -1
  89. package/src/{plugin_browser_hotkey.js → plugin_browser_hotkey.mjs} +2 -2
  90. package/src/{plugin_browser_html.js → plugin_browser_html.mjs} +1 -1
  91. package/src/{plugin_browser_in_worker.js → plugin_browser_in_worker.mjs} +4 -4
  92. package/src/{plugin_browser_location.js → plugin_browser_location.mjs} +1 -1
  93. package/src/{plugin_browser_speech.js → plugin_browser_speech.mjs} +1 -1
  94. package/src/{plugin_browser_storage.js → plugin_browser_storage.mjs} +1 -1
  95. package/src/{plugin_browser_system.js → plugin_browser_system.mjs} +1 -1
  96. package/src/{plugin_browser_websocket.js → plugin_browser_websocket.mjs} +1 -1
  97. package/src/{plugin_caniuse.js → plugin_caniuse.mjs} +8 -3
  98. package/src/{plugin_csv.js → plugin_csv.mjs} +4 -5
  99. package/src/{plugin_datetime.js → plugin_datetime.mjs} +19 -26
  100. package/src/{plugin_express.js → plugin_express.mjs} +4 -3
  101. package/src/{plugin_kansuji.js → plugin_kansuji.mjs} +1 -1
  102. package/src/{plugin_keigo.js → plugin_keigo.mjs} +0 -0
  103. package/src/{plugin_markup.js → plugin_markup.mjs} +6 -6
  104. package/src/{plugin_math.js → plugin_math.mjs} +1 -4
  105. package/src/{plugin_node.js → plugin_node.mjs} +25 -57
  106. package/src/{plugin_promise.js → plugin_promise.mjs} +1 -3
  107. package/src/{plugin_system.js → plugin_system.mjs} +32 -15
  108. package/src/{plugin_test.js → plugin_test.mjs} +1 -3
  109. package/src/{plugin_turtle.js → plugin_turtle.mjs} +5 -4
  110. package/src/{plugin_webworker.js → plugin_webworker.mjs} +3 -3
  111. package/src/{plugin_weykturtle3d.js → plugin_weykturtle3d.mjs} +1 -3
  112. package/src/{plugin_worker.js → plugin_worker.mjs} +4 -1
  113. package/src/{wnako3.js → wnako3.mjs} +5 -5
  114. package/src/{wnako3_editor.js → wnako3_editor.mjs} +24 -18
  115. package/src/{wnako3webworker.js → wnako3webworker.mjs} +0 -0
  116. package/test/async/{async_basic_test.js → async_basic_test.mjs} +3 -4
  117. package/test/browser/karma.config.js +9 -0
  118. package/test/browser/test/plugin_browser_test.js +2 -2
  119. package/test/browser/test/plugin_turtle_test.js +2 -2
  120. package/test/browser/test/plugin_webworker_test.js +3 -3
  121. package/test/browser/test/require_test.js +2 -2
  122. package/test/common/{array_test.js → array_test.mjs} +3 -3
  123. package/test/common/{basic_test.js → basic_test.mjs} +3 -13
  124. package/test/common/{calc_test.js → calc_test.mjs} +2 -2
  125. package/test/common/{debug_test.js → debug_test.mjs} +2 -2
  126. package/test/common/{dncl_test.js → dncl_test.mjs} +3 -3
  127. package/test/common/{error_test.js → error_test.mjs} +2 -2
  128. package/test/common/{flow_test.js → flow_test.mjs} +2 -2
  129. package/test/common/{func_call.js → func_call.mjs} +3 -3
  130. package/test/common/{func_test.js → func_test.mjs} +2 -2
  131. package/test/common/{indent_test.js → indent_test.mjs} +3 -2
  132. package/test/common/{lex_test.js → lex_test.mjs} +2 -2
  133. package/test/common/{literal_test.js → literal_test.mjs} +3 -2
  134. package/test/common/{nako_logger_test.js → nako_logger_test.mjs} +3 -3
  135. package/test/common/{plugin_browser_test.js → plugin_browser_test.mjs} +4 -4
  136. package/test/common/{plugin_browser_ut_audio_test.js → plugin_browser_ut_audio_test.mjs} +4 -4
  137. package/test/common/{plugin_browser_ut_color_test.js → plugin_browser_ut_color_test.mjs} +2 -2
  138. package/test/common/{plugin_browser_ut_dialog_test.js → plugin_browser_ut_dialog_test.mjs} +3 -3
  139. package/test/common/{plugin_browser_ut_html_test.js → plugin_browser_ut_html_test.mjs} +2 -2
  140. package/test/common/{plugin_browser_ut_system_test.js → plugin_browser_ut_system_test.mjs} +2 -2
  141. package/test/common/{plugin_csv_test.js → plugin_csv_test.mjs} +3 -3
  142. package/test/common/{plugin_datetime_test.js → plugin_datetime_test.mjs} +4 -4
  143. package/test/common/{plugin_kansuji_test.js → plugin_kansuji_test.mjs} +3 -3
  144. package/test/common/{plugin_markup_test.js → plugin_markup_test.mjs} +3 -3
  145. package/test/common/{plugin_promise_test.js → plugin_promise_test.mjs} +2 -2
  146. package/test/common/{plugin_system_test.js → plugin_system_test.mjs} +2 -2
  147. package/test/common/{prepare_test.js → prepare_test.mjs} +2 -2
  148. package/test/common/{re_test.js → re_test.mjs} +2 -2
  149. package/test/common/{variable_scope_test.js → variable_scope_test.mjs} +2 -2
  150. package/test/karma.config.js +3 -3
  151. package/test/node/{async_test.js → async_test.mjs} +4 -2
  152. package/test/node/{commander_ja_test.js → commander_ja_test.mjs} +17 -12
  153. package/test/node/{error_message_test.js → error_message_test.mjs} +13 -10
  154. package/test/node/node_test.mjs +57 -0
  155. package/test/node/{plugin_browser_ut_ajax_test.js → plugin_browser_ut_ajax_test.mjs} +7 -5
  156. package/test/node/{plugin_browser_ut_location_test.js → plugin_browser_ut_location_test.mjs} +3 -3
  157. package/test/node/{plugin_markup_test.js → plugin_markup_test.mjs} +6 -7
  158. package/test/node/{plugin_math_test.js → plugin_math_test.mjs} +5 -5
  159. package/test/node/{plugin_node_test.js → plugin_node_test.mjs} +11 -7
  160. package/test/node/{plugin_test.js → plugin_test.mjs} +11 -5
  161. package/test/node/{require_nako3_test.js → require_nako3_test.mjs} +19 -12
  162. package/test/node/{side_effects_test.js → side_effects_test.mjs} +24 -13
  163. package/test/node/{wnako3_editor_test.js → wnako3_editor_test.mjs} +16 -9
  164. package/tools/nako3edit/index.nako3 +1 -1
  165. package/tools/nako3edit/{run.js → run.mjs} +8 -2
  166. package/src/index.js +0 -5
  167. package/src/nako_lex_rules.js +0 -260
  168. package/src/nako_version.js +0 -8
  169. package/test/node/node_test.js +0 -43
@@ -1,19 +1,27 @@
1
1
  #!/usr/bin/env node
2
2
  /** なでしこ3簡易EXPRESSサーバー */
3
3
 
4
- const express = require('express')
4
+ import express from 'express'
5
+ import path from 'path'
6
+ import fs from 'fs'
7
+ import { execSync } from 'child_process'
8
+ import opener from 'opener'
9
+
10
+ // __dirname のために
11
+ import url from 'url'
12
+ const __filename = url.fileURLToPath(import.meta.url);
13
+ const __dirname = path.dirname(__filename);
14
+
15
+
5
16
  const app = express()
6
17
 
7
18
  // set static
8
- const path = require('path')
9
19
  const rootDir = path.resolve(path.join(__dirname, '../'))
10
20
  app.use(express.static(rootDir))
11
21
  console.log('documentRoot:', rootDir)
12
22
 
13
23
  // ライブラリがあるかチェック
14
- const fs = require('fs')
15
24
  if (!fs.existsSync(path.resolve(rootDir, 'extlib/pure.min.css'))) {
16
- const execSync = require('child_process').execSync;
17
25
  execSync('npm run extlib:install')
18
26
  }
19
27
 
@@ -26,7 +34,6 @@ app.get('/', function (req, res) {
26
34
  let port = 3000
27
35
  let server = null
28
36
  const callbackStart = () => {
29
- const opener = require('opener')
30
37
  const port = server.address().port
31
38
  console.log('+ サーバーを開始しました')
32
39
  console.log('+ [URL] http://localhost:%s', port)
@@ -83,4 +83,4 @@ const convertColorTextFormat = (text) => {
83
83
  }
84
84
  }
85
85
 
86
- module.exports = { convertColorTextFormat, colorNames, color }
86
+ export const NakoColors = { convertColorTextFormat, colorNames, color }
@@ -1,9 +1,9 @@
1
- const nakoVersion = require('./nako_version')
1
+ import nakoVersion from './nako_version.mjs'
2
2
 
3
3
  /**
4
4
  * なでしこ言語が投げる全てのエラーが継承するクラス
5
5
  */
6
- class NakoError extends Error {
6
+ export class NakoError extends Error {
7
7
  /**
8
8
  * @param {string} tag
9
9
  * @param {string} msg
@@ -19,7 +19,7 @@ class NakoError extends Error {
19
19
  }
20
20
  }
21
21
 
22
- class NakoIndentError extends NakoError {
22
+ export class NakoIndentError extends NakoError {
23
23
  /**
24
24
  * @param {string} msg
25
25
  * @param {number} line
@@ -33,7 +33,7 @@ class NakoIndentError extends NakoError {
33
33
  }
34
34
 
35
35
  // コンパイラの内部でのみ使うエラー。投げられたらtryでキャッチしてLexerErrorへ変更する。
36
- class InternalLexerError extends NakoError {
36
+ export class InternalLexerError extends NakoError {
37
37
  /**
38
38
  * @param {string} msg
39
39
  * @param {number} preprocessedCodeStartOffset
@@ -50,7 +50,7 @@ class InternalLexerError extends NakoError {
50
50
  }
51
51
  }
52
52
 
53
- class NakoLexerError extends NakoError {
53
+ export class NakoLexerError extends NakoError {
54
54
  /**
55
55
  * @param {string} msg
56
56
  * @param {number | null} startOffset
@@ -73,7 +73,7 @@ class NakoLexerError extends NakoError {
73
73
  }
74
74
  }
75
75
 
76
- class NakoSyntaxError extends NakoError {
76
+ export class NakoSyntaxError extends NakoError {
77
77
  /**
78
78
  * @param {string} msg
79
79
  * @param {import("./nako3").Ast | null | undefined} first
@@ -107,7 +107,7 @@ class NakoSyntaxError extends NakoError {
107
107
  }
108
108
  }
109
109
 
110
- class NakoRuntimeError extends NakoError {
110
+ export class NakoRuntimeError extends NakoError {
111
111
  /**
112
112
  * @param {Error | string} error エラー
113
113
  * @param {string | undefined} lineNo 発生行
@@ -152,7 +152,7 @@ class NakoRuntimeError extends NakoError {
152
152
  }
153
153
  }
154
154
 
155
- class NakoImportError extends NakoError {
155
+ export class NakoImportError extends NakoError {
156
156
  /**
157
157
  * @param {string} msg
158
158
  * @param {number} line
@@ -164,13 +164,3 @@ class NakoImportError extends NakoError {
164
164
  this.line = line
165
165
  }
166
166
  }
167
-
168
- module.exports = {
169
- NakoError,
170
- NakoIndentError,
171
- NakoLexerError,
172
- InternalLexerError,
173
- NakoSyntaxError,
174
- NakoRuntimeError,
175
- NakoImportError
176
- }
@@ -1,17 +1,18 @@
1
1
  /**
2
2
  * DNCLに対応する構文
3
3
  */
4
- const { NakoIndentError } = require('./nako_errors')
5
- const NakoPrepare = require('./nako_prepare')
4
+ // import { NakoIndentError } from './nako_errors.mjs'
5
+ import { NakoPrepare } from './nako_prepare.mjs'
6
6
 
7
7
  const DNCL_KEYWORDS = ['!DNCLモード']
8
+
8
9
  /**
9
10
  * DNCLのソースコードをなでしこに変換する
10
11
  * @param {String} src
11
12
  * @param {String} filename
12
13
  * @returns {String} converted soruce
13
14
  */
14
- function convert(src, filename) {
15
+ export function convertDNCL(src, filename) {
15
16
  // 改行を合わせる
16
17
  src = src.replace(/(\r\n|\r)/g, '\n')
17
18
  // 「!DNCLモード」を使うかチェック
@@ -258,7 +259,8 @@ function conv2half(src) {
258
259
  return result
259
260
  }
260
261
 
261
-
262
- module.exports = {
263
- convert,
262
+ /** @type {Object} */
263
+ export const NakoDncl = {
264
+ convert: convertDNCL
264
265
  }
266
+
@@ -4,10 +4,8 @@
4
4
  * なお速度優先で忠実にJavaScriptのコードを生成する。
5
5
  */
6
6
 
7
- 'use strict'
8
-
9
- const { NakoSyntaxError, NakoError, NakoRuntimeError } = require('./nako_errors')
10
- const nakoVersion = require('./nako_version')
7
+ import { NakoSyntaxError, NakoError, NakoRuntimeError } from './nako_errors.mjs'
8
+ import nakoVersion from './nako_version.mjs'
11
9
  const isIE11 = () => {
12
10
  if (typeof(window) == 'object' && window.navigator && window.navigator.userAgent) {
13
11
  return (window.navigator.userAgent.indexOf('MSIE') >= 0)
@@ -26,88 +24,7 @@ const topOfFunctionAsync = '(async function(){\n'
26
24
  /**
27
25
  * 構文木からJSのコードを生成するクラス
28
26
  */
29
- class NakoGen {
30
- /**
31
- * @param {import('./nako3')} com
32
- * @param {Ast} ast
33
- * @param {boolean | string} isTest 文字列なら1つのテストだけを実行する
34
- */
35
- static generate (com, ast, isTest) {
36
- const gen = new NakoGen(com)
37
-
38
- // ※ [関数定義に関するコード生成のヒント]
39
- // ※ 関数の名前だけを(1)で登録して、(2)で実際に関数のコードを生成する。
40
- // ※ ただし(2)では生成するだけなので、(3)でプログラム冒頭に関数定義のコードを記述する。
41
- // この順番を変えることはできない (グローバル変数が認識できなくなったり、関数定義のタイミングがずれる)
42
-
43
- // (1) ユーザー定義関数をシステムに登録する
44
- gen.registerFunction(ast)
45
-
46
- // (2) JSコードを生成する
47
- let js = gen.convGen(ast, !!isTest)
48
-
49
- // (3) JSコードを実行するための事前ヘッダ部分の生成
50
- js = gen.getDefFuncCode(isTest) + js
51
-
52
- // テストの実行
53
- if (js && isTest) {
54
- js += '\n__self._runTests(__tests);\n'
55
- }
56
- // async method
57
- if (gen.numAsyncFn > 0) {
58
- let canAsync = !isIE11()
59
- if (canAsync) {
60
- js = `
61
- // <nadesiko3::gen::async>
62
- (async () => { // async::main
63
- ${js}
64
- }).call(this).catch(err => {
65
- if (!(err instanceof this.NakoRuntimeError)) {
66
- err = new this.NakoRuntimeError(err, this.__varslist[0].line);
67
- }
68
- this.logger.error(err);
69
- throw err;
70
- }); // async::main
71
- // <nadesiko3::gen::async>\n`
72
- }
73
- }
74
-
75
- // デバッグメッセージ
76
- com.logger.trace('--- generate ---\n' + js)
77
-
78
- return {
79
- // なでしこの実行環境ありの場合
80
- runtimeEnv: js,
81
- // JavaScript単体で動かす場合
82
- standalone:
83
- `\
84
- const nakoVersion = ${JSON.stringify(nakoVersion)};
85
- ${NakoError.toString()}
86
- ${NakoRuntimeError.toString()}
87
- this.logger = {
88
- error(message) { console.error(message) },
89
- send(level, message) { console.log(message) },
90
- };
91
- this.__varslist = [{}, {}, {}];
92
- this.__vars = this.__varslist[2];
93
- this.__module = {};
94
- this.__locals = {};
95
- this.__genMode = 'sync';
96
- try {
97
- ${gen.getVarsCode()}
98
- ${js}
99
- } catch (err) {
100
- if (!(err instanceof NakoRuntimeError)) {
101
- err = new NakoRuntimeError(err, __varslist[0].line);
102
- }
103
- this.logger.error(err);
104
- throw err;
105
- }`,
106
- // コード生成に使ったNakoGenのインスタンス
107
- gen
108
- }
109
- }
110
-
27
+ export class NakoGen {
111
28
  /**
112
29
  * @param {import('./nako3')} com コンパイラのインスタンス
113
30
  */
@@ -199,6 +116,12 @@ try {
199
116
  systemFunctionBody: 0 // システム関数(呼び出しコードを除く)
200
117
  }
201
118
 
119
+ /**
120
+ * 未定義の変数の警告を行う
121
+ * @type { boolean }
122
+ */
123
+ this.warnUndefinedVar = true
124
+
202
125
  // 暫定変数
203
126
  /** @type { number } */
204
127
  this.warnUndefinedReturnUserFunc = 1
@@ -469,10 +392,14 @@ try {
469
392
  this.varslistSet = this.__self.__varslist.map((v) => ({ isFunction: false, names: new Set(Object.keys(v)), readonly: new Set() }))
470
393
  this.varslistSet[2] = this.varsSet
471
394
 
472
- // 非同期関数(asyncFn)があるかどうかテストする --- 後ほど改めて再度同じ関数を呼ぶ
395
+ // 非同期関数(asyncFn)があるかどうかテストする
396
+ // 後ほど改めて再度同じ関数を呼ぶため、警告などは抑止する
397
+ const tmpWarn = this.warnUndefinedVar
398
+ this.warnUndefinedVar = false // 未定義の変数の警告を抑止 #1192
473
399
  for (let ff of funcList) {
474
400
  this.convDefFuncCommon(ff.node, ff.name)
475
401
  }
402
+ this.warnUndefinedVar = tmpWarn
476
403
  }
477
404
 
478
405
  /**
@@ -673,7 +600,9 @@ try {
673
600
  if (name === '引数' || name === 'それ' || name === '対象' || name === '対象キー') {
674
601
  // デフォルト定義されている変数名
675
602
  } else {
676
- this.__self.logger.warn(`変数『${name}』は定義されていません。`, position)
603
+ if (this.warnUndefinedVar) {
604
+ this.__self.logger.warn(`変数『${name}』は定義されていません。`, position)
605
+ }
677
606
  }
678
607
  this.varsSet.names.add(name)
679
608
  return this.varname(name)
@@ -1611,4 +1540,93 @@ try {
1611
1540
  }
1612
1541
  }
1613
1542
 
1614
- module.exports = NakoGen
1543
+
1544
+ /**
1545
+ * @param {import('./nako3')} com
1546
+ * @param {Ast} ast
1547
+ * @param {boolean | string} isTest 文字列なら1つのテストだけを実行する
1548
+ */
1549
+ export function generateJS (com, ast, isTest) {
1550
+ const gen = new NakoGen(com)
1551
+
1552
+ // ※ [関数定義に関するコード生成のヒント]
1553
+ // ※ 関数の名前だけを(1)で登録して、(2)で実際に関数のコードを生成する。
1554
+ // ※ ただし(2)では生成するだけなので、(3)でプログラム冒頭に関数定義のコードを記述する。
1555
+ // この順番を変えることはできない (グローバル変数が認識できなくなったり、関数定義のタイミングがずれる)
1556
+
1557
+ // (1) ユーザー定義関数をシステムに登録する
1558
+ gen.registerFunction(ast)
1559
+
1560
+ // (2) JSコードを生成する
1561
+ let js = gen.convGen(ast, !!isTest)
1562
+
1563
+ // (3) JSコードを実行するための事前ヘッダ部分の生成
1564
+ js = gen.getDefFuncCode(isTest) + js
1565
+
1566
+ // テストの実行
1567
+ if (js && isTest) {
1568
+ js += '\n__self._runTests(__tests);\n'
1569
+ }
1570
+ // async method
1571
+ if (gen.numAsyncFn > 0) {
1572
+ let canAsync = !isIE11()
1573
+ if (canAsync) {
1574
+ js = `
1575
+ // <nadesiko3::gen::async>
1576
+ (async () => { // async::main
1577
+ ${js}
1578
+ }).call(this).catch(err => {
1579
+ if (!(err instanceof this.NakoRuntimeError)) {
1580
+ err = new this.NakoRuntimeError(err, this.__varslist[0].line);
1581
+ }
1582
+ this.logger.error(err);
1583
+ throw err;
1584
+ }); // async::main
1585
+ // <nadesiko3::gen::async>\n`
1586
+ }
1587
+ }
1588
+
1589
+ // デバッグメッセージ
1590
+ com.logger.trace('--- generate ---\n' + js)
1591
+ // todo: 将来的に mjs のコードを履くように修正する
1592
+ const standaloneJSCode = `\
1593
+ // <standaloneCode>
1594
+ const path = require('path')
1595
+ const nakoVersion = ${JSON.stringify(nakoVersion)};
1596
+ ${NakoError.toString()}
1597
+ ${NakoRuntimeError.toString()}
1598
+ function __nako3safunc() {
1599
+ this.logger = {
1600
+ error: (message) => { console.error(message) },
1601
+ send: (level, message) => { console.log(message) },
1602
+ };
1603
+ this.__varslist = [{}, {}, {}];
1604
+ this.__vars = this.__varslist[2];
1605
+ this.__module = {};
1606
+ this.__locals = {};
1607
+ this.__genMode = 'sync';
1608
+ try {
1609
+ ${gen.getVarsCode()}
1610
+ ${js}
1611
+ } catch (err) {
1612
+ if (!(err instanceof NakoRuntimeError)) {
1613
+ err = new NakoRuntimeError(err, __varslist[0].line);
1614
+ }
1615
+ this.logger.error(err);
1616
+ throw err;
1617
+ }
1618
+ //
1619
+ } // end of __nako3safunc function
1620
+ __nako3safunc();
1621
+ // </standaloneCode>
1622
+ `
1623
+
1624
+ return {
1625
+ // なでしこの実行環境ありの場合
1626
+ runtimeEnv: js,
1627
+ // JavaScript単体で動かす場合
1628
+ standalone: standaloneJSCode,
1629
+ // コード生成に使ったNakoGenのインスタンス
1630
+ gen
1631
+ }
1632
+ }
@@ -7,9 +7,9 @@
7
7
 
8
8
  'use strict'
9
9
 
10
- const { NakoSyntaxError, NakoError, NakoRuntimeError } = require('./nako_errors')
11
- const nakoVersion = require('./nako_version')
12
- const NakoGen = require('./nako_gen')
10
+ import { NakoSyntaxError, NakoError, NakoRuntimeError } from './nako_errors.mjs'
11
+ import nakoVersion from './nako_version.mjs'
12
+ import { NakoGen } from './nako_gen.mjs'
13
13
 
14
14
  /**
15
15
  * @typedef {import("./nako3").Ast} Ast
@@ -62,14 +62,14 @@ class NakoCode {
62
62
  /**
63
63
  * 構文木からJSのコードを生成するクラス
64
64
  */
65
- class NakoGenAsync {
65
+ export class NakoGenASync {
66
66
  /**
67
67
  * @param {import('./nako3')} com
68
68
  * @param {Ast} ast
69
69
  * @param {boolean | string} isTest 文字列なら1つのテストだけを実行する
70
70
  */
71
71
  static generate (com, ast, isTest) {
72
- const gen = new NakoGenAsync(com)
72
+ const gen = new NakoGenASync(com)
73
73
 
74
74
  // ユーザー定義関数をシステムに登録する
75
75
  gen.registerFunction(ast)
@@ -1704,7 +1704,4 @@ if (typeof (navigator) === 'object' && typeof (navigator.nako3) === 'object') {
1704
1704
  // Webブラウザの場合
1705
1705
  const nako3 = navigator.nako3
1706
1706
  if (nako3.addCodeGenerator) { nako3.addCodeGenerator('非同期モード', NakoGenAsync) }
1707
- } else {
1708
- // モジュールモード
1709
- module.exports = NakoGenAsync
1710
1707
  }
@@ -1,9 +1,10 @@
1
- const NakoColors = require('./nako_colors')
2
- const {NakoRuntimeError} = require('./nako_errors')
1
+ import { NakoColors } from './nako_colors.mjs'
2
+ import { NakoRuntimeError } from './nako_errors.mjs'
3
+
3
4
  /**
4
5
  * コンパイルされたなでしこのプログラムで、グローバル空間のthisが指すオブジェクト
5
6
  */
6
- class NakoGlobal {
7
+ export class NakoGlobal {
7
8
  /**
8
9
  * @param {import('./nako3')} compiler
9
10
  * @param {import('./nako_gen')} gen
@@ -110,5 +111,3 @@ class NakoGlobal {
110
111
  this.reset()
111
112
  }
112
113
  }
113
-
114
- module.exports = NakoGlobal
@@ -1,5 +1,5 @@
1
- const { NakoIndentError } = require('./nako_errors')
2
- const NakoPrepare = require('./nako_prepare')
1
+ import { NakoIndentError } from './nako_errors.mjs'
2
+ import { NakoPrepare } from './nako_prepare.mjs'
3
3
 
4
4
  /**
5
5
  * インデント構文指定があればコードを変換する
@@ -422,7 +422,7 @@ function replaceRetMark (src) {
422
422
  * インデント構文が有効化されていない場合にも使われる。
423
423
  * @param {string} code
424
424
  */
425
- function getBlockStructure (code) {
425
+ export function getBlockStructure (code) {
426
426
  /** @type {{ lines: number[], pairs: [number, number][], parents: (number | null)[], spaces: string[] }} */
427
427
  const result = {
428
428
  lines: [], // 各行のインデント量
@@ -472,7 +472,7 @@ function getBlockStructure (code) {
472
472
  return result
473
473
  }
474
474
 
475
- module.exports = {
475
+ export default {
476
476
  convert,
477
477
  getBlockStructure,
478
478
  getIndent,
@@ -2,7 +2,7 @@
2
2
  * 助詞の一覧
3
3
  */
4
4
 
5
- const josiList = [
5
+ export const josiList = [
6
6
  'について', 'くらい', 'なのか', 'までを', 'までの', 'による',
7
7
  'とは', 'から', 'まで', 'だけ', 'より', 'ほど', 'など',
8
8
  'いて', 'えて', 'きて', 'けて', 'して', 'って', 'にて', 'みて',
@@ -11,22 +11,30 @@ const josiList = [
11
11
  ]
12
12
 
13
13
  // 「もし」文で使う助詞
14
- const tararebaJosiList = [
14
+ export const tararebaJosiList = [
15
15
  'でなければ', 'なければ', 'ならば', 'なら', 'たら', 'れば'
16
16
  ]
17
17
 
18
18
  // 意味のない助詞(削除する) #936 #939 #974
19
- const removeJosiList = [
19
+ export const removeJosiList = [
20
20
  'こと', 'である', 'です', 'します', 'でした'
21
21
  ]
22
22
 
23
- // 一覧をプログラムで扱いやすいようマップに変換
24
- const tararebaMap = {}
23
+ /**
24
+ * 「もし」文で使う「たら」「れば」などの一覧をプログラムで扱いやすいようマップに変換したもの
25
+ * @type {Object<string, boolean>}
26
+ */
27
+ export const tararebaMap = {}
25
28
  tararebaJosiList.forEach(josi => {
26
29
  josiList.push(josi)
27
30
  tararebaMap[josi] = true
28
31
  })
29
- const removeJosiMap = {}
32
+
33
+ /**
34
+ * 意味のない助詞(削除する)をマップに変換したもの
35
+ * @type {Object<string, boolean>}
36
+ */
37
+ export const removeJosiMap = {}
30
38
  removeJosiList.forEach(josi => {
31
39
  josiList.push(josi)
32
40
  removeJosiMap[josi] = true
@@ -36,11 +44,5 @@ removeJosiList.forEach(josi => {
36
44
  josiList.sort((a, b) => b.length - a.length)
37
45
 
38
46
  // 正規表現で助詞をマッチできるようにする
39
- const josiRE = new RegExp('^[\\t ]*(' + josiList.join('|') + ')')
40
-
41
- module.exports = {
42
- josiRE,
43
- tararebaMap,
44
- removeJosiMap,
45
- josiList
46
- }
47
+ export const josiRE = new RegExp('^[\\t ]*(' + josiList.join('|') + ')')
48
+