Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 34x 31x 31x 34x 2x 2x 3x 3x 3x 3x 3x 1x 1x 1x 1x 34x 36x 36x 35x 35x 35x 35x 44x 2x 2x 2x 2x 44x 3x 42x 39x 39x 39x 44x 35x 35x 2x 35x 33x 1x 1x 1x 35x 36x 36x 36x 36x 36x 36x 36x 3x 3x 3x 3x 3x 36x 36x 35x 35x 35x 35x 35x 36x 36x 71x 71x 36x 36x 92x 92x 92x 89x 92x 3x 6x 3x 3x 3x 6x 3x 92x 92x 92x 92x 92x 36x 36x 74x 74x 6x 6x 6x 68x 68x 68x 74x 82x 82x 82x 53x 53x 53x 53x 1x 1x 1x 1x 53x 1x 52x 2x 2x 51x 3x 2x 3x 1x 1x 3x 49x 5x 5x 5x 82x 29x 29x 29x 82x 82x 58x 58x 58x 58x 36x 36x 29x 29x 29x 29x 29x 29x 29x 13x 13x 13x 13x 13x 13x 13x 13x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 13x 13x 13x 13x 2x 2x 2x 2x 2x 13x 11x 11x 29x 16x 16x 16x 16x 16x 16x 10x 10x 10x 10x 10x 10x 10x 16x 6x 6x 1x 1x 1x 1x 1x 1x 6x 5x 5x 5x 5x 3x 3x 3x 3x 3x 3x 3x 3x 2x 2x 2x 2x 2x 2x 2x 3x 5x 2x 2x 2x 2x 5x 5x 5x 1x 1x 5x 5x 5x 5x 5x 5x 5x 2x 1x 1x 2x 29x 29x 29x 36x 36x | import {StructureNode, StatementNode} from "../nodes"; import * as Structures from "../3_structures/structures"; import * as Statements from "../2_statements/statements"; import * as Expressions from "../2_statements/expressions"; import {FLOW_EDGE_TYPE, FlowGraph} from "./flow_graph"; import {AbstractToken} from "../1_lexer/tokens/abstract_token"; import {IObject} from "../../objects/_iobject"; import {FunctionGroup, Program} from "../../objects"; import {SELECTION_EVENTS} from "./selection_events"; // Levels: top, FORM, METHOD, FUNCTION-MODULE, (MODULE, AT, END-OF-*, GET, START-OF-SELECTION, TOP-OF-PAGE) // // Loop branching: LOOP, DO, WHILE, SELECT(loop), WITH, PROVIDE // // Branching: IF, CASE, CASE TYPE OF, TRY, ON, CATCH SYSTEM-EXCEPTIONS, AT // // Conditional exits: CHECK, ASSERT // // Exits: RETURN, EXIT, RAISE(not RESUMABLE), MESSAGE(type E and A?), CONTINUE, REJECT, RESUME, STOP // // Not handled? INCLUDE + malplaced macro calls ///////////////////////////////////// // TODO: handling static exceptions(only static), refactor some logic from UncaughtException to common file // TODO: RAISE interface IContext { procedureEnd: string; loopStart?: string; loopEnd?: string; } export class StatementFlow { private counter = 0; public build(stru: StructureNode, obj: IObject): FlowGraph[] { const ret: FlowGraph[] = []; let name = ""; const structures = stru.findAllStructuresMulti([ Structures.Form, Structures.ClassImplementation, Structures.FunctionModule, Structures.Module]); for (const s of structures) { if (s.get() instanceof Structures.Form) { name = "FORM " + s.findFirstExpression(Expressions.FormName)?.concatTokens(); ret.push(this.run(s, name)); } else if (s.get() instanceof Structures.ClassImplementation) { const className = s.findFirstExpression(Expressions.ClassName)?.concatTokens(); for (const method of s.findDirectStructures(Structures.Method)) { const methodName = method.findFirstExpression(Expressions.MethodName)?.concatTokens(); name = "METHOD " + methodName + ", CLASS " + className; ret.push(this.run(method, name)); } } else if (s.get() instanceof Structures.FunctionModule) { name = "FUNCTION " + s.findFirstExpression(Expressions.Field)?.concatTokens(); ret.push(this.run(s, name)); } else if (s.get() instanceof Structures.Module) { name = s.getFirstStatement()!.concatTokens().toUpperCase(); ret.push(this.run(s, name)); } else { throw new Error("StatementFlow, unknown structure"); } } if (obj instanceof Program || obj instanceof FunctionGroup) { // find the top level events let inEvent = false; let collected: (StatementNode | StructureNode)[] = []; for (const s of stru.getChildren() || []) { if (SELECTION_EVENTS.some(f => s.get() instanceof f)) { if (inEvent === true) { ret.push(this.runEvent(collected, name)); } collected = []; inEvent = true; name = s.concatTokens(); } else if (s.get() instanceof Structures.Normal) { collected.push(s); } else { if (inEvent === true) { ret.push(this.runEvent(collected, name)); inEvent = false; } collected = []; } } if (inEvent === true) { ret.push(this.runEvent(collected, name)); } else if (collected.length > 0 && !(obj instanceof FunctionGroup)) { // implicit START-OF-SELECTION ret.push(this.runEvent(collected, "START-OF-SELECTION.")); } } return ret.map(f => f.reduce()); } //////////////////// private runEvent(s: (StatementNode | StructureNode)[], name: string) { this.counter = 1; const graph = this.traverseBody(s, {procedureEnd: "end#1"}); graph.setLabel(name); return graph; } private run(s: StructureNode, name: string): FlowGraph { this.counter = 1; const graph = this.traverseBody(this.findBody(s), {procedureEnd: "end#1"}); graph.setLabel(name); return graph; } private findBody(f: StructureNode): readonly (StatementNode | StructureNode)[] { return f.findDirectStructure(Structures.Body)?.getChildren() || []; } private buildName(statement: StatementNode): string { let token: AbstractToken | undefined = undefined; const colon = statement.getColon(); if (colon === undefined) { token = statement.getFirstToken(); } else { for (const t of statement.getTokens()) { if (t.getStart().isAfter(colon.getEnd())) { token = t; break; } } } if (token === undefined) { return "tokenError"; } return statement.get().constructor.name + ":" + token.getRow() + "," + token.getCol(); } private traverseBody(children: readonly (StatementNode | StructureNode)[], context: IContext): FlowGraph { const graph = new FlowGraph(this.counter++); if (children.length === 0) { graph.addEdge(graph.getStart(), graph.getEnd(), FLOW_EDGE_TYPE.undefined); return graph; } let current = graph.getStart(); for (const c of children) { if (c.get() instanceof Structures.Normal) { const firstChild = c.getFirstChild(); // "Normal" only has one child if (firstChild instanceof StatementNode) { const name = this.buildName(firstChild); graph.addEdge(current, name, FLOW_EDGE_TYPE.undefined); current = name; if (firstChild.get() instanceof Statements.Check) { if (context.loopStart) { graph.addEdge(name, context.loopStart, FLOW_EDGE_TYPE.false); } else { graph.addEdge(name, context.procedureEnd, FLOW_EDGE_TYPE.false); } } else if (firstChild.get() instanceof Statements.Assert) { graph.addEdge(name, context.procedureEnd, FLOW_EDGE_TYPE.false); } else if (firstChild.get() instanceof Statements.Continue && context.loopStart) { graph.addEdge(name, context.loopStart, FLOW_EDGE_TYPE.undefined); return graph; } else if (firstChild.get() instanceof Statements.Exit) { if (context.loopEnd) { graph.addEdge(name, context.loopEnd, FLOW_EDGE_TYPE.undefined); } else { graph.addEdge(name, context.procedureEnd, FLOW_EDGE_TYPE.undefined); } return graph; } else if (firstChild.get() instanceof Statements.Return) { graph.addEdge(name, context.procedureEnd, FLOW_EDGE_TYPE.undefined); return graph; } } else if(firstChild instanceof StructureNode) { const sub = this.traverseStructure(firstChild, context); current = graph.addGraph(current, sub, FLOW_EDGE_TYPE.undefined); } } } graph.addEdge(current, graph.getEnd(), FLOW_EDGE_TYPE.undefined); return graph; } private traverseStructure(n: StructureNode | undefined, context: IContext): FlowGraph { const graph = new FlowGraph(this.counter++); if (n === undefined) { return graph; } let current = graph.getStart(); const type = n.get(); if (type instanceof Structures.If) { const ifName = this.buildName(n.findDirectStatement(Statements.If)!); const sub = this.traverseBody(this.findBody(n), context); graph.addEdge(current, ifName, FLOW_EDGE_TYPE.undefined); graph.addGraph(ifName, sub, FLOW_EDGE_TYPE.true); graph.addEdge(sub.getEnd(), graph.getEnd(), FLOW_EDGE_TYPE.undefined); current = ifName; for (const e of n.findDirectStructures(Structures.ElseIf)) { const elseifst = e.findDirectStatement(Statements.ElseIf); if (elseifst === undefined) { continue; } const elseIfName = this.buildName(elseifst); const sub = this.traverseBody(this.findBody(e), context); graph.addEdge(current, elseIfName, FLOW_EDGE_TYPE.false); graph.addGraph(elseIfName, sub, FLOW_EDGE_TYPE.true); graph.addEdge(sub.getEnd(), graph.getEnd(), FLOW_EDGE_TYPE.undefined); current = elseIfName; } const els = n.findDirectStructure(Structures.Else); const elsest = els?.findDirectStatement(Statements.Else); if (els && elsest) { const elseName = this.buildName(elsest); const sub = this.traverseBody(this.findBody(els), context); graph.addEdge(current, elseName, FLOW_EDGE_TYPE.false); graph.addGraph(elseName, sub, FLOW_EDGE_TYPE.undefined); graph.addEdge(sub.getEnd(), graph.getEnd(), FLOW_EDGE_TYPE.undefined); } else { graph.addEdge(ifName, graph.getEnd(), FLOW_EDGE_TYPE.false); } } else if (type instanceof Structures.Loop || type instanceof Structures.While || type instanceof Structures.With || type instanceof Structures.Provide || type instanceof Structures.Select || type instanceof Structures.LoopAtScreen || type instanceof Structures.Do) { const loopName = this.buildName(n.getFirstStatement()!); const sub = this.traverseBody(this.findBody(n), {...context, loopStart: loopName, loopEnd: graph.getEnd()}); graph.addEdge(current, loopName, FLOW_EDGE_TYPE.undefined); graph.addGraph(loopName, sub, FLOW_EDGE_TYPE.true); graph.addEdge(sub.getEnd(), loopName, FLOW_EDGE_TYPE.undefined); graph.addEdge(loopName, graph.getEnd(), FLOW_EDGE_TYPE.false); } else if (type instanceof Structures.Data || type instanceof Structures.Constants || type instanceof Structures.Types) { // these doesnt affect control flow, so just take the first statement const statement = n.getFirstStatement()!; const name = this.buildName(statement); graph.addEdge(current, name, FLOW_EDGE_TYPE.undefined); graph.addEdge(name, graph.getEnd(), FLOW_EDGE_TYPE.undefined); } else if (type instanceof Structures.AtFirst || type instanceof Structures.AtLast || type instanceof Structures.At || type instanceof Structures.OnChange) { const name = this.buildName(n.getFirstStatement()!); const body = this.traverseBody(this.findBody(n), context); graph.addEdge(current, name, FLOW_EDGE_TYPE.undefined); graph.addGraph(name, body, FLOW_EDGE_TYPE.undefined); graph.addEdge(body.getEnd(), graph.getEnd(), FLOW_EDGE_TYPE.undefined); graph.addEdge(current, graph.getEnd(), FLOW_EDGE_TYPE.undefined); } else if (type instanceof Structures.Try) { const tryName = this.buildName(n.getFirstStatement()!); const body = this.traverseBody(this.findBody(n), context); graph.addEdge(current, tryName, FLOW_EDGE_TYPE.undefined); graph.addGraph(tryName, body, FLOW_EDGE_TYPE.undefined); graph.addEdge(body.getEnd(), graph.getEnd(), FLOW_EDGE_TYPE.undefined); for (const c of n.findDirectStructures(Structures.Catch)) { const catchName = this.buildName(c.getFirstStatement()!); const catchBody = this.traverseBody(this.findBody(c), context); // TODO: this does not take exceptions into account graph.addEdge(body.getEnd(), catchName, FLOW_EDGE_TYPE.undefined); graph.addGraph(catchName, catchBody, FLOW_EDGE_TYPE.undefined); graph.addEdge(catchBody.getEnd(), graph.getEnd(), FLOW_EDGE_TYPE.undefined); } // TODO, handle CLEANUP } else if (type instanceof Structures.Case) { const caseName = this.buildName(n.getFirstStatement()!); graph.addEdge(current, caseName, FLOW_EDGE_TYPE.undefined); let othersFound = false; for (const w of n.findDirectStructures(Structures.When)) { const first = w.getFirstStatement(); if (first === undefined) { continue; } if (first.get() instanceof Statements.WhenOthers) { othersFound = true; } const firstName = this.buildName(first); const sub = this.traverseBody(this.findBody(w), context); graph.addEdge(caseName, firstName, FLOW_EDGE_TYPE.undefined); graph.addGraph(firstName, sub, FLOW_EDGE_TYPE.undefined); graph.addEdge(sub.getEnd(), graph.getEnd(), FLOW_EDGE_TYPE.undefined); } if (othersFound === false) { graph.addEdge(caseName, graph.getEnd(), FLOW_EDGE_TYPE.undefined); } } else if (type instanceof Structures.CaseType) { const caseName = this.buildName(n.getFirstStatement()!); graph.addEdge(current, caseName, FLOW_EDGE_TYPE.undefined); let othersFound = false; for (const w of n.findDirectStructures(Structures.WhenType)) { const first = w.getFirstStatement(); if (first === undefined) { continue; } if (first.get() instanceof Statements.WhenOthers) { othersFound = true; } const firstName = this.buildName(first); const sub = this.traverseBody(this.findBody(w), context); graph.addEdge(caseName, firstName, FLOW_EDGE_TYPE.undefined); graph.addGraph(firstName, sub, FLOW_EDGE_TYPE.undefined); graph.addEdge(sub.getEnd(), graph.getEnd(), FLOW_EDGE_TYPE.undefined); } if (othersFound === false) { graph.addEdge(caseName, graph.getEnd(), FLOW_EDGE_TYPE.undefined); } } else { console.dir("StatementFlow,todo, " + n.get().constructor.name); } return graph; } } |