@yanqirenshi/model.pad 0.1.0 → 0.2.0
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/README.md +71 -3
- package/dist/dhn/dhnDocumentToPadNodes.d.ts +27 -0
- package/dist/dhn/dhnDocumentToPadNodes.d.ts.map +1 -0
- package/dist/dhn/dhnDocumentToPadNodes.js +435 -0
- package/dist/ids.d.ts +3 -0
- package/dist/ids.d.ts.map +1 -0
- package/dist/ids.js +34 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -1
- package/dist/pad/padTreeToPadNodes.d.ts +8 -0
- package/dist/pad/padTreeToPadNodes.d.ts.map +1 -0
- package/dist/pad/padTreeToPadNodes.js +43 -0
- package/package.json +2 -1
- package/src/dhn/dhnDocumentToPadNodes.ts +491 -0
- package/src/ids.ts +36 -0
- package/src/index.ts +5 -1
- package/src/pad/padTreeToPadNodes.ts +74 -0
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
// PadNode の木(parse_js_code の結果を JSON.parse したもの)を、d3.pad の Pad に渡すノードの配列
|
|
2
|
+
// (_class ごとのオブジェクトの配列。d3.pad の汎用の要素)に変換する(#302)。
|
|
3
|
+
// d3 にも React にも依存しない。parse_js_code と PadNode は変えない。
|
|
4
|
+
//
|
|
5
|
+
// 最上位の sequence 子(関数の block)を、順に並べる。開始・終了の TERMINAL は出さない(関数の並びは、処理の順ではないため)
|
|
6
|
+
// block(関数) { _class: 'GROUP', label, rows }
|
|
7
|
+
// sequence(中身) 子を、そのまま並べる(入れ子の sequence は、平らにする)
|
|
8
|
+
// command { _class: 'PROCESS', cells: [label], dividers: [] }
|
|
9
|
+
// if { _class: 'SELECTION', whens }。whens は { condition, body }。else があれば、最後に { else: true, body }
|
|
10
|
+
// else の中身が、また if(else if)のときは、入れ子にせず、同じ SELECTION の whens に足す
|
|
11
|
+
// loop { _class: 'LOOP', condition, test: 'pre', rows }(いまのパーサが出すのは while と for だけ。どちらも前判定)
|
|
12
|
+
// error { _class: 'PROCESS', cells: ['error', message], dividers: ['dotted'] }
|
|
13
|
+
//
|
|
14
|
+
// 最上位のノードには、dhnDocumentToPadNodes と同じ形の id(`${_class}-${通し番号}`)を付け、入れ子の要素にも、
|
|
15
|
+
// 親の id を前にした id(GROUP-0.PROCESS-0 など)を付ける(src/ids.ts。#307)。
|
|
16
|
+
// 型は、動きを変えずに通すことを優先してゆるくしている(出すノードは any)。ノードの型とスキーマは、あとの段。
|
|
17
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
18
|
+
import type { PadNode } from '../types.js';
|
|
19
|
+
import { assignIds } from '../ids.js';
|
|
20
|
+
|
|
21
|
+
/** if(else if の連なりも)を、1 つの SELECTION にする */
|
|
22
|
+
function selectionFrom(node: Extract<PadNode, { type: 'if' }>): any {
|
|
23
|
+
const whens: any[] = [];
|
|
24
|
+
let current: PadNode | undefined = node;
|
|
25
|
+
|
|
26
|
+
while (current && current.type === 'if') {
|
|
27
|
+
whens.push({ condition: current.condition, body: convert(current.then_block) });
|
|
28
|
+
const next: PadNode | undefined = current.else_block;
|
|
29
|
+
|
|
30
|
+
if (next && next.type !== 'if') {
|
|
31
|
+
whens.push({ else: true, body: convert(next) });
|
|
32
|
+
break;
|
|
33
|
+
}
|
|
34
|
+
current = next; // else if なら続ける。else が無ければ undefined で終わる
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return { _class: 'SELECTION', whens };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** 1 つの PadNode を、ノードの配列にする(sequence は、子を平らに並べるので、0 個以上になる) */
|
|
41
|
+
function convert(node: PadNode): any[] {
|
|
42
|
+
switch (node.type) {
|
|
43
|
+
case 'sequence':
|
|
44
|
+
return node.children.flatMap(convert);
|
|
45
|
+
|
|
46
|
+
case 'block':
|
|
47
|
+
return [{ _class: 'GROUP', label: node.label, rows: node.children.flatMap(convert) }];
|
|
48
|
+
|
|
49
|
+
case 'command':
|
|
50
|
+
return [{ _class: 'PROCESS', cells: [node.label], dividers: [] }];
|
|
51
|
+
|
|
52
|
+
case 'if':
|
|
53
|
+
return [selectionFrom(node)];
|
|
54
|
+
|
|
55
|
+
case 'loop':
|
|
56
|
+
return [{ _class: 'LOOP', condition: node.condition, test: 'pre', rows: convert(node.body) }];
|
|
57
|
+
|
|
58
|
+
case 'error':
|
|
59
|
+
return [{ _class: 'PROCESS', cells: ['error', node.message], dividers: ['dotted'] }];
|
|
60
|
+
|
|
61
|
+
default:
|
|
62
|
+
return []; // 知らない type(PadNode に無い)は、読み飛ばす
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* PadNode の木を、Pad に渡すノードの配列にする。開始・終了の TERMINAL は出さない。
|
|
68
|
+
* Pad は、center を付けずに作ると、関数(GROUP)どうしを縦線でつながない(全体の縦線は center: true のときだけ。#307)。
|
|
69
|
+
*/
|
|
70
|
+
export function padTreeToPadNodes(tree: PadNode): any[] {
|
|
71
|
+
return assignIds(convert(tree));
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export default padTreeToPadNodes;
|