@@ -5,6 +5,10 @@ import { fileProtocol, requestFromInput } from '../../../r-bridge/retriever';
5
5
import { cfgToMermaid , cfgToMermaidUrl } from '../../../util/mermaid/cfg' ;
6
6
import type { KnownParser } from '../../../r-bridge/parser' ;
7
7
import { ColorEffect , Colors , FontStyles } from '../../../util/text/ansi' ;
8
+ import type { ControlFlowInformation } from '../../../control-flow/control-flow-graph' ;
9
+ import type { NormalizedAst } from '../../../r-bridge/lang-4.x/ast/model/processing/decorate' ;
10
+ import type { CfgSimplificationPassName } from '../../../control-flow/cfg-simplification' ;
11
+ import { DefaultCfgSimplificationOrder } from '../../../control-flow/cfg-simplification' ;
8
12
9
13
async function controlflow ( parser : KnownParser , remainingLine : string ) {
10
14
return await createDataflowPipeline ( parser , {
@@ -20,40 +24,60 @@ function formatInfo(out: ReplOutput, type: string): string {
20
24
return out . formatter . format ( `Copied ${ type } to clipboard.` , { color : Colors . White , effect : ColorEffect . Foreground , style : FontStyles . Italic } ) ;
21
25
}
22
26
27
+
28
+ async function produceAndPrintCfg ( shell : KnownParser , remainingLine : string , output : ReplOutput , simplifications : readonly CfgSimplificationPassName [ ] , cfgConverter : ( cfg : ControlFlowInformation , ast : NormalizedAst ) => string ) {
29
+ const result = await controlflow ( shell , handleString ( remainingLine ) ) ;
30
+
31
+ const cfg = extractCFG ( result . normalize , result . dataflow . graph , [ ...DefaultCfgSimplificationOrder , ...simplifications ] ) ;
32
+ const mermaid = cfgConverter ( cfg , result . normalize ) ;
33
+ output . stdout ( mermaid ) ;
34
+ try {
35
+ const clipboard = await import ( 'clipboardy' ) ;
36
+ clipboard . default . writeSync ( mermaid ) ;
37
+ output . stdout ( formatInfo ( output , 'mermaid code' ) ) ;
38
+ } catch { /* do nothing this is a service thing */
39
+ }
40
+ }
41
+
23
42
export const controlflowCommand : ReplCommand = {
24
43
description : `Get mermaid code for the control-flow graph of R code, start with '${ fileProtocol } ' to indicate a file` ,
25
44
usageExample : ':controlflow' ,
26
45
aliases : [ 'cfg' , 'cf' ] ,
27
46
script : false ,
28
47
fn : async ( output , shell , remainingLine ) => {
29
- const result = await controlflow ( shell , handleString ( remainingLine ) ) ;
30
-
31
- const cfg = extractCFG ( result . normalize , result . dataflow . graph ) ;
32
- const mermaid = cfgToMermaid ( cfg , result . normalize ) ;
33
- output . stdout ( mermaid ) ;
34
- try {
35
- const clipboard = await import ( 'clipboardy' ) ;
36
- clipboard . default . writeSync ( mermaid ) ;
37
- output . stdout ( formatInfo ( output , 'mermaid code' ) ) ;
38
- } catch { /* do nothing this is a service thing */ }
48
+ await produceAndPrintCfg ( shell , remainingLine , output , [ ] , cfgToMermaid ) ;
39
49
}
40
50
} ;
41
51
52
+
42
53
export const controlflowStarCommand : ReplCommand = {
43
54
description : 'Returns the URL to mermaid.live' ,
44
55
usageExample : ':controlflow*' ,
45
56
aliases : [ 'cfg*' , 'cf*' ] ,
46
57
script : false ,
47
58
fn : async ( output , shell , remainingLine ) => {
48
- const result = await controlflow ( shell , handleString ( remainingLine ) ) ;
49
-
50
- const cfg = extractCFG ( result . normalize , result . dataflow . graph ) ;
51
- const mermaid = cfgToMermaidUrl ( cfg , result . normalize ) ;
52
- output . stdout ( mermaid ) ;
53
- try {
54
- const clipboard = await import ( 'clipboardy' ) ;
55
- clipboard . default . writeSync ( mermaid ) ;
56
- output . stdout ( formatInfo ( output , 'mermaid url' ) ) ;
57
- } catch { /* do nothing this is a service thing */ }
59
+ await produceAndPrintCfg ( shell , remainingLine , output , [ ] , cfgToMermaidUrl ) ;
60
+ }
61
+ } ;
62
+
63
+
64
+ export const controlflowBBCommand : ReplCommand = {
65
+ description : `Get mermaid code for the control-flow graph with basic blocks, start with '${ fileProtocol } ' to indicate a file` ,
66
+ usageExample : ':controlflowbb' ,
67
+ aliases : [ 'cfgb' , 'cfb' ] ,
68
+ script : false ,
69
+ fn : async ( output , shell , remainingLine ) => {
70
+ await produceAndPrintCfg ( shell , remainingLine , output , [ 'to-basic-blocks' ] , cfgToMermaid ) ;
71
+ }
72
+ } ;
73
+
74
+
75
+ export const controlflowBBStarCommand : ReplCommand = {
76
+ description : 'Returns the URL to mermaid.live' ,
77
+ usageExample : ':controlflowbb*' ,
78
+ aliases : [ 'cfgb*' , 'cfb*' ] ,
79
+ script : false ,
80
+ fn : async ( output , shell , remainingLine ) => {
81
+ await produceAndPrintCfg ( shell , remainingLine , output , [ 'to-basic-blocks' ] , cfgToMermaidUrl ) ;
58
82
}
59
83
} ;
0 commit comments