nadesiko3 3.3.71 → 3.3.72

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 (38) hide show
  1. package/batch/command.txt +242 -240
  2. package/batch/command_nakopad.txt +2 -0
  3. package/core/command/snako.mts +6 -6
  4. package/core/package.json +5 -5
  5. package/core/src/nako3.mjs +14 -6
  6. package/core/src/nako3.mts +43 -33
  7. package/core/src/nako_errors.mts +13 -13
  8. package/core/src/nako_from_dncl2.mjs +1 -0
  9. package/core/src/nako_from_dncl2.mts +1 -0
  10. package/core/src/nako_gen.mjs +24 -2
  11. package/core/src/nako_gen.mts +47 -24
  12. package/core/src/nako_global.mts +19 -19
  13. package/core/src/nako_lexer.mts +5 -5
  14. package/core/src/nako_logger.mts +1 -1
  15. package/core/src/nako_parser_base.mts +18 -18
  16. package/core/src/nako_prepare.mts +10 -9
  17. package/core/src/nako_source_mapping.mts +13 -13
  18. package/core/src/nako_types.mts +16 -0
  19. package/core/src/plugin_system.mjs +3 -0
  20. package/core/src/plugin_system.mts +3 -0
  21. package/core/test/dncl2_test.mjs +24 -0
  22. package/package.json +1 -2
  23. package/release/_hash.txt +24 -24
  24. package/release/_script-tags.txt +14 -14
  25. package/release/command.json +1 -1
  26. package/release/command.json.js +1 -1
  27. package/release/command_cnako3.json +1 -1
  28. package/release/command_list.json +1 -1
  29. package/release/editor.js +1 -1
  30. package/release/nako_gen_async.js +1 -1
  31. package/release/nako_gen_async.js.LICENSE.txt +0 -20
  32. package/release/stats.json +1 -1
  33. package/release/version.js +1 -1
  34. package/release/wnako3.js +1 -1
  35. package/release/wnako3webworker.js +1 -1
  36. package/release/wnako3webworker.js.LICENSE.txt +4 -0
  37. package/src/nako_version.mjs +2 -2
  38. package/src/nako_version.mts +2 -2
@@ -20,6 +20,8 @@
20
20
  |OK,"","true",0,,�ϐ�,OK
21
21
  |NG,"","false",0,,�ϐ�,NG
22
22
  |�L�����Z��,"","0",0,,�ϐ�,����񂹂�
23
+ |TRUE,"","true",0,,�ϐ�,TRUE
24
+ |FALSE,"","false",0,,�ϐ�,FALSE
23
25
  |PI,"","Math.PI",0,,�ϐ�,PI
24
26
  |��,"","''",0,,�ϐ�,����
25
27
  |NULL,"","null",0,,�ϐ�,NULL
@@ -12,12 +12,12 @@ const __dirname = url.fileURLToPath(new URL('.', import.meta.url))
12
12
 
13
13
  /** コマンドラインオプション */
14
14
  class CommandOptions {
15
- isDebug: boolean;
16
- filename: string;
17
- nodePath: string;
18
- scriptPath: string;
19
- evalStr: string;
20
- flagConvert: boolean;
15
+ isDebug: boolean
16
+ filename: string
17
+ nodePath: string
18
+ scriptPath: string
19
+ evalStr: string
20
+ flagConvert: boolean
21
21
  constructor () {
22
22
  this.nodePath = ''
23
23
  this.scriptPath = ''
package/core/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nadesiko3core",
3
- "version": "3.3.71",
3
+ "version": "3.3.72",
4
4
  "description": "Japanese Programming Language Nadesiko v3 core",
5
5
  "main": "index.mjs",
6
6
  "type": "module",
@@ -38,13 +38,13 @@
38
38
  "devDependencies": {
39
39
  "@types/mocha": "^9.1.1",
40
40
  "@types/node": "^17.0.35",
41
- "@typescript-eslint/eslint-plugin": "^4.33.0",
42
41
  "chai": "^4.3.6",
43
- "eslint": "^7.32.0",
44
- "eslint-config-standard-with-typescript": "^21.0.1",
42
+ "eslint": "^8.23.1",
45
43
  "eslint-plugin-import": "^2.26.0",
46
44
  "eslint-plugin-node": "^11.1.0",
47
- "eslint-plugin-promise": "^5.2.0",
45
+ "eslint-plugin-promise": "^6.0.0",
46
+ "@typescript-eslint/eslint-plugin": "^5.0.0",
47
+ "eslint-config-standard-with-typescript": "^23.0.0",
48
48
  "mocha": "^10.0.0",
49
49
  "typescript": "^4.6.4"
50
50
  },
@@ -61,6 +61,7 @@ export class NakoCompiler {
61
61
  this.nakoFuncList = {}; // __v1に配置するJavaScriptのコードで定義された関数
62
62
  this.eventList = []; // 実行前に環境を変更するためのイベント
63
63
  this.codeGenerateor = {}; // コードジェネレータ
64
+ this.debugOption = { useDebug: false, waitTime: 0 };
64
65
  this.logger = new NakoLogger();
65
66
  this.filename = 'main.nako3';
66
67
  /**
@@ -536,6 +537,7 @@ export class NakoCompiler {
536
537
  this.usedFuncs.add(block.name);
537
538
  }
538
539
  astQueue.push([block, block.block]);
540
+ // eslint-disable-next-line prefer-spread
539
541
  blockQueue.push.apply(blockQueue, [block.value].concat(block.args));
540
542
  }
541
543
  deleteUnNakoFuncs() {
@@ -626,6 +628,7 @@ export class NakoCompiler {
626
628
  * @param isReset
627
629
  * @param isTest テストかどうか。stringの場合は1つのテストのみ。
628
630
  * @param [preCode]
631
+ * @deprecated 代わりにrunAsyncメソッドを使ってください。(core #52)
629
632
  */
630
633
  async _run(code, fname, isReset, isTest, preCode = '') {
631
634
  const opts = newCompilerOptions({
@@ -669,11 +672,12 @@ export class NakoCompiler {
669
672
  this.eventList.filter(o => o.eventName === 'finish').map(e => e.callback(nakoGlobal));
670
673
  }
671
674
  /**
672
- * 同期的になでしこのプログラムcodeを実行する (ただし正しく動かない #52)
675
+ * 同期的になでしこのプログラムcodeを実行する
673
676
  * @param code なでしこのプログラム
674
677
  * @param filename ファイル名
675
678
  * @param options オプション
676
679
  * @returns 実行に利用したグローバルオブジェクト
680
+ * @deprecated 代わりにrunAsyncメソッドを使ってください。(core #52)
677
681
  */
678
682
  runSync(code, filename, options = undefined) {
679
683
  // コンパイル
@@ -683,8 +687,6 @@ export class NakoCompiler {
683
687
  const nakoGlobal = this.getNakoGlobal(options, out.gen);
684
688
  // 実行
685
689
  this.evalJS(out.runtimeEnv, nakoGlobal);
686
- // (現状動いていないことを通知する) https://github.com/kujirahand/nadesiko3core/issues/52
687
- this.getLogger().info('runSyncが呼ばれました');
688
690
  return nakoGlobal;
689
691
  }
690
692
  /**
@@ -747,6 +749,7 @@ export class NakoCompiler {
747
749
  * @param code
748
750
  * @param fname
749
751
  * @param [preCode]
752
+ * @deprecated 代わりに runAsync を使ってください。
750
753
  */
751
754
  run(code, fname = 'main.nako3', preCode = '') {
752
755
  const options = newCompilerOptions();
@@ -860,7 +863,9 @@ export class NakoCompiler {
860
863
  this.pluginFunclist[key] = cloneAsJSON(this.funclist[key]);
861
864
  this.__varslist[0][key] = fn;
862
865
  }
863
- // (非推奨) 互換性のため ... 関数を追加する
866
+ /** (非推奨) 互換性のため ... 関数を追加する
867
+ * @deprecated 代わりにaddFuncを使ってください
868
+ */
864
869
  setFunc(key, josi, fn, returnNone = true, asyncFn = false) {
865
870
  this.addFunc(key, josi, fn, returnNone, asyncFn);
866
871
  }
@@ -872,7 +877,9 @@ export class NakoCompiler {
872
877
  getFunc(key) {
873
878
  return this.funclist[key];
874
879
  }
875
- /** (非推奨) 同期的になでしこのプログラムcodeを実行する */
880
+ /** 同期的になでしこのプログラムcodeを実行する
881
+ * @deprecated 代わりにrunAsyncメソッドを使ってください。(core #52)
882
+ */
876
883
  _runEx(code, filename, opts, preCode = '', nakoGlobal = undefined) {
877
884
  // コンパイル
878
885
  opts.preCode = preCode;
@@ -881,11 +888,12 @@ export class NakoCompiler {
881
888
  }
882
889
  return this.runSync(code, filename, opts);
883
890
  }
884
- /** (非推奨) 同期的に実行
891
+ /** 同期的になでしこのプログラムcodeを実行する
885
892
  * @param code
886
893
  * @param fname
887
894
  * @param opts
888
895
  * @param [preCode]
896
+ * @deprecated 代わりにrunAsyncメソッドを使ってください。(core #52)
889
897
  */
890
898
  runEx(code, fname, opts, preCode = '') {
891
899
  return this._runEx(code, fname, opts, preCode);
@@ -2,7 +2,7 @@
2
2
  * nadesiko v3
3
3
  */
4
4
  // types
5
- import { Token, Ast, FuncList, FuncListItem, FuncArgs, NakoEvent, CompilerOptions, NakoComEventName } from './nako_types.mjs'
5
+ import { Token, Ast, FuncList, FuncListItem, FuncArgs, NakoEvent, CompilerOptions, NakoComEventName, NakoDebugOption } from './nako_types.mjs'
6
6
  // parser / lexer
7
7
  import { NakoParser } from './nako_parser3.mjs'
8
8
  import { NakoLexer } from './nako_lexer.mjs'
@@ -75,33 +75,35 @@ export function newCompilerOptions (initObj: any = {}): CompilerOptions {
75
75
 
76
76
  /** なでしこコンパイラ */
77
77
  export class NakoCompiler {
78
- private nakoFuncList: FuncList;
79
- private funclist: FuncList;
80
- private pluginFunclist: Record<string, FuncListItem>;
81
- private pluginfiles: Record<string, any>;
82
- private commandlist: Set<string>;
83
- private prepare: NakoPrepare;
84
- private parser: NakoParser;
85
- private lexer: NakoLexer;
86
- private dependencies: Dependencies;
87
- private usedFuncs: Set<string>;
88
- private codeGenerateor: {[key: string]: any};
89
- protected logger: NakoLogger;
90
- protected eventList: NakoEvent[];
78
+ private nakoFuncList: FuncList
79
+ private funclist: FuncList
80
+ private pluginFunclist: Record<string, FuncListItem>
81
+ private pluginfiles: Record<string, any>
82
+ private commandlist: Set<string>
83
+ private prepare: NakoPrepare
84
+ private parser: NakoParser
85
+ private lexer: NakoLexer
86
+ private dependencies: Dependencies
87
+ private usedFuncs: Set<string>
88
+ private codeGenerateor: {[key: string]: any}
89
+ protected logger: NakoLogger
90
+ protected eventList: NakoEvent[]
91
91
  // global objects
92
- __varslist: NakoVars[];
93
- __locals: NakoVars;
94
- __self: NakoCompiler;
95
- __vars: NakoVars;
96
- __v0: NakoVars;
97
- __v1: NakoVars;
98
- __globals: NakoGlobal[];
99
- __globalObj: NakoGlobal|null; // 現在のNakoGlobalオブジェクト
100
- __module: Record<string, Record<string, FuncListItem>>;
101
- numFailures: number; // エラーレポートの数を記録
102
- public version: string;
103
- public coreVersion: string;
104
- public filename: string;
92
+ __varslist: NakoVars[]
93
+ __locals: NakoVars
94
+ // eslint-disable-next-line no-use-before-define
95
+ __self: NakoCompiler
96
+ __vars: NakoVars
97
+ __v0: NakoVars
98
+ __v1: NakoVars
99
+ __globals: NakoGlobal[]
100
+ __globalObj: NakoGlobal|null // 現在のNakoGlobalオブジェクト
101
+ __module: Record<string, Record<string, FuncListItem>>
102
+ numFailures: number // エラーレポートの数を記録
103
+ public version: string
104
+ public coreVersion: string
105
+ public filename: string
106
+ public debugOption: NakoDebugOption
105
107
  /**
106
108
  * @param {undefined | {'useBasicPlugin':true|false}} options
107
109
  */
@@ -129,6 +131,7 @@ export class NakoCompiler {
129
131
  this.nakoFuncList = {} // __v1に配置するJavaScriptのコードで定義された関数
130
132
  this.eventList = [] // 実行前に環境を変更するためのイベント
131
133
  this.codeGenerateor = {} // コードジェネレータ
134
+ this.debugOption = { useDebug: false, waitTime: 0 }
132
135
 
133
136
  this.logger = new NakoLogger()
134
137
  this.filename = 'main.nako3'
@@ -644,6 +647,7 @@ export class NakoCompiler {
644
647
  this.usedFuncs.add(block.name as string)
645
648
  }
646
649
  astQueue.push([block, block.block as Ast])
650
+ // eslint-disable-next-line prefer-spread
647
651
  blockQueue.push.apply(blockQueue, [block.value].concat(block.args))
648
652
  }
649
653
 
@@ -731,6 +735,7 @@ export class NakoCompiler {
731
735
  * @param isReset
732
736
  * @param isTest テストかどうか。stringの場合は1つのテストのみ。
733
737
  * @param [preCode]
738
+ * @deprecated 代わりにrunAsyncメソッドを使ってください。(core #52)
734
739
  */
735
740
  async _run (code: string, fname: string, isReset: boolean, isTest: boolean, preCode = ''): Promise<NakoGlobal> {
736
741
  const opts: CompilerOptions = newCompilerOptions({
@@ -776,11 +781,12 @@ export class NakoCompiler {
776
781
  }
777
782
 
778
783
  /**
779
- * 同期的になでしこのプログラムcodeを実行する (ただし正しく動かない #52)
784
+ * 同期的になでしこのプログラムcodeを実行する
780
785
  * @param code なでしこのプログラム
781
786
  * @param filename ファイル名
782
787
  * @param options オプション
783
788
  * @returns 実行に利用したグローバルオブジェクト
789
+ * @deprecated 代わりにrunAsyncメソッドを使ってください。(core #52)
784
790
  */
785
791
  public runSync (code: string, filename: string, options: CompilerOptions|undefined = undefined): NakoGlobal {
786
792
  // コンパイル
@@ -790,8 +796,6 @@ export class NakoCompiler {
790
796
  const nakoGlobal = this.getNakoGlobal(options, out.gen)
791
797
  // 実行
792
798
  this.evalJS(out.runtimeEnv, nakoGlobal)
793
- // (現状動いていないことを通知する) https://github.com/kujirahand/nadesiko3core/issues/52
794
- this.getLogger().info('runSyncが呼ばれました')
795
799
  return nakoGlobal
796
800
  }
797
801
 
@@ -856,6 +860,7 @@ export class NakoCompiler {
856
860
  * @param code
857
861
  * @param fname
858
862
  * @param [preCode]
863
+ * @deprecated 代わりに runAsync を使ってください。
859
864
  */
860
865
  run (code: string, fname = 'main.nako3', preCode = ''): NakoGlobal {
861
866
  const options = newCompilerOptions()
@@ -970,7 +975,9 @@ export class NakoCompiler {
970
975
  this.__varslist[0][key] = fn
971
976
  }
972
977
 
973
- // (非推奨) 互換性のため ... 関数を追加する
978
+ /** (非推奨) 互換性のため ... 関数を追加する
979
+ * @deprecated 代わりにaddFuncを使ってください
980
+ */
974
981
  public setFunc (key: string, josi: FuncArgs, fn: any, returnNone = true, asyncFn = false): void {
975
982
  this.addFunc(key, josi, fn, returnNone, asyncFn)
976
983
  }
@@ -984,7 +991,9 @@ export class NakoCompiler {
984
991
  return this.funclist[key]
985
992
  }
986
993
 
987
- /** (非推奨) 同期的になでしこのプログラムcodeを実行する */
994
+ /** 同期的になでしこのプログラムcodeを実行する
995
+ * @deprecated 代わりにrunAsyncメソッドを使ってください。(core #52)
996
+ */
988
997
  private _runEx (code: string, filename: string, opts: CompilerOptions, preCode = '', nakoGlobal: NakoGlobal|undefined = undefined): NakoGlobal {
989
998
  // コンパイル
990
999
  opts.preCode = preCode
@@ -992,11 +1001,12 @@ export class NakoCompiler {
992
1001
  return this.runSync(code, filename, opts)
993
1002
  }
994
1003
 
995
- /** (非推奨) 同期的に実行
1004
+ /** 同期的になでしこのプログラムcodeを実行する
996
1005
  * @param code
997
1006
  * @param fname
998
1007
  * @param opts
999
1008
  * @param [preCode]
1009
+ * @deprecated 代わりにrunAsyncメソッドを使ってください。(core #52)
1000
1010
  */
1001
1011
  public runEx (code: string, fname: string, opts: CompilerOptions, preCode = '') {
1002
1012
  return this._runEx(code, fname, opts, preCode)
@@ -4,12 +4,12 @@ import { Ast, Token, SourceMap } from './nako_types.mjs'
4
4
  * なでしこ言語が投げる全てのエラーが継承するクラス
5
5
  */
6
6
  export class NakoError extends Error {
7
- public type: string;
8
- public tag: string;
9
- public msg: string;
10
- public file: string | undefined;
11
- public line: number | undefined;
12
- public positionJa: string;
7
+ public type: string
8
+ public tag: string
9
+ public msg: string
10
+ public file: string | undefined
11
+ public line: number | undefined
12
+ public positionJa: string
13
13
 
14
14
  constructor (tag: string, msg: string, file: string | undefined = undefined, line: number | undefined = undefined) {
15
15
  // エラー位置を分かりやすく日本語に変換
@@ -43,8 +43,8 @@ export class NakoIndentError extends NakoError {
43
43
 
44
44
  // コンパイラの内部でのみ使うエラー。投げられたらtryでキャッチしてLexerErrorへ変更する。
45
45
  export class InternalLexerError extends NakoError {
46
- public preprocessedCodeStartOffset: number;
47
- public preprocessedCodeEndOffset: number;
46
+ public preprocessedCodeStartOffset: number
47
+ public preprocessedCodeEndOffset: number
48
48
  /**
49
49
  * @param {string} msg
50
50
  * @param {number} preprocessedCodeStartOffset
@@ -63,8 +63,8 @@ export class InternalLexerError extends NakoError {
63
63
  }
64
64
 
65
65
  export class NakoLexerError extends NakoError {
66
- public startOffset: number;
67
- public endOffset: number;
66
+ public startOffset: number
67
+ public endOffset: number
68
68
  /**
69
69
  * @param {string} msg
70
70
  * @param {number | null} startOffset
@@ -89,8 +89,8 @@ export class NakoLexerError extends NakoError {
89
89
  }
90
90
 
91
91
  export class NakoSyntaxError extends NakoError {
92
- public startOffset: number | undefined;
93
- public endOffset: number | undefined;
92
+ public startOffset: number | undefined
93
+ public endOffset: number | undefined
94
94
  /**
95
95
  * @param {string} msg
96
96
  * @param {Ast} first
@@ -126,7 +126,7 @@ export class NakoSyntaxError extends NakoError {
126
126
  }
127
127
 
128
128
  export class NakoRuntimeError extends NakoError {
129
- public lineNo: string | undefined;
129
+ public lineNo: string | undefined
130
130
  /**
131
131
  * @param error エラー
132
132
  * @param lineNo 発生行
@@ -17,6 +17,7 @@ const DNCL_SIMPLES = {
17
17
  '}:}': [']', ']'],
18
18
  'word:and': ['and', 'かつ'],
19
19
  'word:or': ['or', 'または'],
20
+ 'word:not': ['not', '!'],
20
21
  'word:乱数': ['word', '乱数範囲'],
21
22
  'word:表示': ['word', '連続表示']
22
23
  };
@@ -20,6 +20,7 @@ const DNCL_SIMPLES: { [key: string]: string[] } = {
20
20
  '}:}': [']', ']'],
21
21
  'word:and': ['and', 'かつ'],
22
22
  'word:or': ['or', 'または'],
23
+ 'word:not': ['not', '!'],
23
24
  'word:乱数': ['word', '乱数範囲'],
24
25
  'word:表示': ['word', '連続表示']
25
26
  }
@@ -91,6 +91,7 @@ export class NakoGen {
91
91
  this.warnUndefinedCallingUserFunc = 1;
92
92
  this.warnUndefinedCallingSystemFunc = 1;
93
93
  this.warnUndefinedCalledUserFuncArgs = 1;
94
+ this.debugOption = com.debugOption;
94
95
  }
95
96
  static isValidIdentifier(name) {
96
97
  // TODO: いらなそうな部分は削る
@@ -123,8 +124,21 @@ export class NakoGen {
123
124
  }
124
125
  this.lastLineNo = lineNo;
125
126
  }
127
+ // 実行行のデータ
128
+ const lineDataJSON = JSON.stringify(lineNo);
129
+ // デバッグ実行か
130
+ let debugCode = '';
131
+ if (this.debugOption.useDebug) {
132
+ if (this.debugOption.messageAction) {
133
+ debugCode += `window.postMessage({action:'${this.debugOption.messageAction}',` +
134
+ `line: ${lineDataJSON}});`;
135
+ }
136
+ if ((node.line + incLine) >= 1) {
137
+ debugCode += `await __v0['秒待'](${this.debugOption.waitTime},__self);`;
138
+ }
139
+ }
126
140
  // 例: __v0.line='l1:main.nako3'
127
- return `__v0.line=${JSON.stringify(lineNo)};`;
141
+ return `__v0.line=${lineDataJSON};` + debugCode;
128
142
  }
129
143
  /**
130
144
  * ローカル変数のJavaScriptコードを生成する。
@@ -190,6 +204,7 @@ export class NakoGen {
190
204
  code += 'const __v1 = self.__v1 = self.__varslist[1];\n';
191
205
  code += 'const __vars = self.__vars = self.__varslist[2];\n';
192
206
  code += `const __modList = self.__modList = ${JSON.stringify(com.getModList())}\n`;
207
+ code += '__v0.line = 0;\n';
193
208
  // 定数を埋め込む
194
209
  code += 'self.constPools = ' + JSON.stringify(this.constPools) + ';\n';
195
210
  // なでしこの関数定義を行う
@@ -1541,6 +1556,7 @@ export class NakoGen {
1541
1556
  * @param opt
1542
1557
  */
1543
1558
  export function generateJS(com, ast, opt) {
1559
+ // NakoGenのインスタンスを作成
1544
1560
  const gen = new NakoGen(com);
1545
1561
  // ※ [関数定義に関するコード生成のヒント]
1546
1562
  // ※ 関数の名前だけを(1)で登録して、(2)で実際に関数のコードを生成する。
@@ -1557,7 +1573,7 @@ export function generateJS(com, ast, opt) {
1557
1573
  js += '\n__self._runTests(__tests);\n';
1558
1574
  }
1559
1575
  // async method
1560
- if (gen.numAsyncFn > 0) {
1576
+ if (gen.numAsyncFn > 0 || gen.debugOption.useDebug) {
1561
1577
  const asyncMain = '__nako3async' + (new Date()).getTime() + '_' + ('' + Math.random()).replace('.', '_') + '__';
1562
1578
  js = `
1563
1579
  // --------------------------------------------------
@@ -1575,8 +1591,11 @@ ${asyncMain}.call(self, self).catch(err => {
1575
1591
  `;
1576
1592
  }
1577
1593
  else {
1594
+ const syncMain = '__nako3sync' + (new Date()).getTime() + '_' + ('' + Math.random()).replace('.', '_') + '__';
1578
1595
  js = `
1579
1596
  // --------------------------------------------------
1597
+ // <nadesiko3::gen::syncMode>
1598
+ function ${syncMain}(self) {
1580
1599
  try {
1581
1600
  ${jsInit}
1582
1601
  ${js}
@@ -1584,6 +1603,9 @@ try {
1584
1603
  self.numFailures++
1585
1604
  throw self.logger.runtimeError(err, self.__v0.line)
1586
1605
  }
1606
+ } // end of ${syncMain}
1607
+ ${syncMain}(self)
1608
+ // </nadesiko3::gen::syncMode>
1587
1609
  // --------------------------------------------------
1588
1610
  `;
1589
1611
  }
@@ -4,8 +4,7 @@
4
4
  */
5
5
 
6
6
  import { NakoSyntaxError } from './nako_errors.mjs'
7
- import { NakoLexer } from './nako_lexer.mjs'
8
- import { Ast, FuncList, FuncArgs, Token } from './nako_types.mjs'
7
+ import { Ast, FuncList, FuncArgs, Token, NakoDebugOption } from './nako_types.mjs'
9
8
  import { NakoCompiler } from './nako3.mjs'
10
9
 
11
10
  // なでしこで定義した関数の開始コードと終了コード
@@ -57,29 +56,30 @@ export class NakoGenOptions {
57
56
  * 構文木からJSのコードを生成するクラス
58
57
  */
59
58
  export class NakoGen {
60
- private nakoFuncList: FuncList; // なでしこ自身で定義した関数の一覧
61
- private nakoTestFuncs: FuncList; // テストのための関数
62
- private usedFuncSet: Set<string>; // 利用があった関数をメモする
63
- private usedAsyncFn: boolean; // 非同期関数が使われているか判定
64
- private loopId: number; // ループを生成する際にダミーのループ変数を管理するため
65
- private flagLoop: boolean; // 変換中のソースがループの中かどうかを判定する
66
- private constPools: Array<any>; // 定数プール用
59
+ private nakoFuncList: FuncList // なでしこ自身で定義した関数の一覧
60
+ private nakoTestFuncs: FuncList // テストのための関数
61
+ private usedFuncSet: Set<string> // 利用があった関数をメモする
62
+ private usedAsyncFn: boolean // 非同期関数が使われているか判定
63
+ private loopId: number // ループを生成する際にダミーのループ変数を管理するため
64
+ private flagLoop: boolean // 変換中のソースがループの中かどうかを判定する
65
+ private constPools: Array<any> // 定数プール用
67
66
  // コード生成オプション
68
- private speedMode: SpeedMode;
69
- private performanceMonitor: PerformanceMonitor;
70
- private warnUndefinedVar: boolean;
71
- private warnUndefinedReturnUserFunc: number;
72
- private warnUndefinedCallingUserFunc: number;
73
- private warnUndefinedCallingSystemFunc: number;
74
- private warnUndefinedCalledUserFuncArgs: number;
67
+ private speedMode: SpeedMode
68
+ private performanceMonitor: PerformanceMonitor
69
+ private warnUndefinedVar: boolean
70
+ private warnUndefinedReturnUserFunc: number
71
+ private warnUndefinedCallingUserFunc: number
72
+ private warnUndefinedCallingSystemFunc: number
73
+ private warnUndefinedCalledUserFuncArgs: number
75
74
  // 変数管理
76
- private varslistSet: VarsSet[]; // [システム変数一覧, グローバル変数一覧, ローカル変数一覧]で変数セットを記録
77
- private varsSet: VarsSet; // ローカルな変数を記録
75
+ private varslistSet: VarsSet[] // [システム変数一覧, グローバル変数一覧, ローカル変数一覧]で変数セットを記録
76
+ private varsSet: VarsSet // ローカルな変数を記録
77
+ public debugOption: NakoDebugOption
78
78
  // public
79
- numAsyncFn: number;
80
- __self: NakoCompiler;
81
- genMode: string;
82
- lastLineNo: string | null; // `l123:main.nako3`形式
79
+ numAsyncFn: number
80
+ __self: NakoCompiler
81
+ genMode: string
82
+ lastLineNo: string | null // `l123:main.nako3`形式
83
83
 
84
84
  /** constructor
85
85
  * @param com コンパイラのインスタンス
@@ -164,6 +164,8 @@ export class NakoGen {
164
164
  this.warnUndefinedCallingUserFunc = 1
165
165
  this.warnUndefinedCallingSystemFunc = 1
166
166
  this.warnUndefinedCalledUserFuncArgs = 1
167
+
168
+ this.debugOption = com.debugOption
167
169
  }
168
170
 
169
171
  static isValidIdentifier (name: string) {
@@ -194,8 +196,21 @@ export class NakoGen {
194
196
  if (lineNo === this.lastLineNo) { return '' }
195
197
  this.lastLineNo = lineNo
196
198
  }
199
+ // 実行行のデータ
200
+ const lineDataJSON = JSON.stringify(lineNo)
201
+ // デバッグ実行か
202
+ let debugCode = ''
203
+ if (this.debugOption.useDebug) {
204
+ if (this.debugOption.messageAction) {
205
+ debugCode += `window.postMessage({action:'${this.debugOption.messageAction}',` +
206
+ `line: ${lineDataJSON}});`
207
+ }
208
+ if ((node.line + incLine) >= 1) {
209
+ debugCode += `await __v0['秒待'](${this.debugOption.waitTime},__self);`
210
+ }
211
+ }
197
212
  // 例: __v0.line='l1:main.nako3'
198
- return `__v0.line=${JSON.stringify(lineNo)};`
213
+ return `__v0.line=${lineDataJSON};` + debugCode
199
214
  }
200
215
 
201
216
  /**
@@ -262,6 +277,7 @@ export class NakoGen {
262
277
  code += 'const __v1 = self.__v1 = self.__varslist[1];\n'
263
278
  code += 'const __vars = self.__vars = self.__varslist[2];\n'
264
279
  code += `const __modList = self.__modList = ${JSON.stringify(com.getModList())}\n`
280
+ code += '__v0.line = 0;\n'
265
281
  // 定数を埋め込む
266
282
  code += 'self.constPools = ' + JSON.stringify(this.constPools) + ';\n'
267
283
  // なでしこの関数定義を行う
@@ -1611,6 +1627,7 @@ export interface NakoGenResult {
1611
1627
  * @param opt
1612
1628
  */
1613
1629
  export function generateJS (com: NakoCompiler, ast: Ast, opt: NakoGenOptions): NakoGenResult {
1630
+ // NakoGenのインスタンスを作成
1614
1631
  const gen = new NakoGen(com)
1615
1632
 
1616
1633
  // ※ [関数定義に関するコード生成のヒント]
@@ -1632,7 +1649,7 @@ export function generateJS (com: NakoCompiler, ast: Ast, opt: NakoGenOptions): N
1632
1649
  js += '\n__self._runTests(__tests);\n'
1633
1650
  }
1634
1651
  // async method
1635
- if (gen.numAsyncFn > 0) {
1652
+ if (gen.numAsyncFn > 0 || gen.debugOption.useDebug) {
1636
1653
  const asyncMain = '__nako3async' + (new Date()).getTime() + '_' + ('' + Math.random()).replace('.', '_') + '__'
1637
1654
  js = `
1638
1655
  // --------------------------------------------------
@@ -1649,8 +1666,11 @@ ${asyncMain}.call(self, self).catch(err => {
1649
1666
  // --------------------------------------------------
1650
1667
  `
1651
1668
  } else {
1669
+ const syncMain = '__nako3sync' + (new Date()).getTime() + '_' + ('' + Math.random()).replace('.', '_') + '__'
1652
1670
  js = `
1653
1671
  // --------------------------------------------------
1672
+ // <nadesiko3::gen::syncMode>
1673
+ function ${syncMain}(self) {
1654
1674
  try {
1655
1675
  ${jsInit}
1656
1676
  ${js}
@@ -1658,6 +1678,9 @@ try {
1658
1678
  self.numFailures++
1659
1679
  throw self.logger.runtimeError(err, self.__v0.line)
1660
1680
  }
1681
+ } // end of ${syncMain}
1682
+ ${syncMain}(self)
1683
+ // </nadesiko3::gen::syncMode>
1661
1684
  // --------------------------------------------------
1662
1685
  `
1663
1686
  }
@@ -8,25 +8,25 @@ import { CompilerOptions, FuncList } from './nako_types.mjs'
8
8
  * コンパイルされたなでしこのプログラムで、グローバル空間のthisが指すオブジェクト
9
9
  */
10
10
  export class NakoGlobal {
11
- guid: number;
12
- version: string;
13
- coreVersion: string;
14
- __locals: {[key: string]: any};
15
- __varslist: {[key: string]: any}[];
16
- __code: string[];
17
- __callstack: any[];
18
- __stack: any[];
19
- __labels: {[key: string]: any};
20
- __genMode: string;
21
- __module: {[key: string]: any};
22
- pluginfiles: {[key: string]: FuncList};
23
- index: number;
24
- nextIndex: number;
25
- numFailures: number;
26
- gen: NakoGen;
27
- logger: NakoLogger;
28
- compiler: NakoCompiler;
29
- lastJSCode: string;
11
+ guid: number
12
+ version: string
13
+ coreVersion: string
14
+ __locals: {[key: string]: any}
15
+ __varslist: {[key: string]: any}[]
16
+ __code: string[]
17
+ __callstack: any[]
18
+ __stack: any[]
19
+ __labels: {[key: string]: any}
20
+ __genMode: string
21
+ __module: {[key: string]: any}
22
+ pluginfiles: {[key: string]: FuncList}
23
+ index: number
24
+ nextIndex: number
25
+ numFailures: number
26
+ gen: NakoGen
27
+ logger: NakoLogger
28
+ compiler: NakoCompiler
29
+ lastJSCode: string
30
30
  /**
31
31
  * @param compiler
32
32
  * @param gen
@@ -19,11 +19,11 @@ import { NakoLexerError, InternalLexerError } from './nako_errors.mjs'
19
19
  import { Token, FuncList, FuncArgs } from './nako_types.mjs'
20
20
 
21
21
  export class NakoLexer {
22
- public logger: NakoLogger;
23
- public funclist: FuncList;
24
- public modList: string[];
25
- public result: Token[];
26
- public modName: string;
22
+ public logger: NakoLogger
23
+ public funclist: FuncList
24
+ public modList: string[]
25
+ public result: Token[]
26
+ public modName: string
27
27
  /**
28
28
  * @param logger
29
29
  */