From 70cfe8b98c4be0d4c0d5744d13c6dbed81e307ef Mon Sep 17 00:00:00 2001 From: avishek-sen-gupta Date: Wed, 20 Nov 2024 01:49:16 +0530 Subject: [PATCH] Passing section-paragraph map to intermediate AST build functions for use in RETCALL construction --- .../smojol/common/ast/SemanticCategory.java | 18 ++++++++++- .../transpiler/RetCallTranspilerNode.java | 18 +++++++++++ .../transpiler/BuildTranspilerASTTask.java | 2 +- .../task/transpiler/SectionParagraphMap.java | 30 +++++++++++++++++++ .../transpiler/EvaluateNodeTranslator.java | 18 ++++++----- .../transpiler/IfTranspilerNodeBuilder.java | 7 +++-- ...abelledTranspilerCodeBlockNodeBuilder.java | 27 ++++++++--------- .../PerformProcedureNodeBuilder.java | 11 +++---- .../transpiler/SearchWhenNodeTranslator.java | 11 +++---- .../TranspilerCodeBlockNodeBuilder.java | 25 +++++++++------- .../transpiler/TranspilerTreeBuilder.java | 23 +++++++------- 11 files changed, 132 insertions(+), 58 deletions(-) create mode 100644 smojol-core/src/main/java/org/smojol/common/transpiler/RetCallTranspilerNode.java create mode 100644 smojol-toolkit/src/main/java/org/smojol/toolkit/analysis/task/transpiler/SectionParagraphMap.java diff --git a/smojol-core/src/main/java/org/smojol/common/ast/SemanticCategory.java b/smojol-core/src/main/java/org/smojol/common/ast/SemanticCategory.java index 6f7af265..08fa2470 100644 --- a/smojol-core/src/main/java/org/smojol/common/ast/SemanticCategory.java +++ b/smojol-core/src/main/java/org/smojol/common/ast/SemanticCategory.java @@ -15,5 +15,21 @@ public enum SemanticCategory { DIALECT, PROGRAM, METADATA, - PLACEHOLDER, LOOP, SYMBOL, TRANSACTION_START, TRANSACTION, TRANSACTION_END, DECISION_BRANCH, COMPARISON, ADDRESS, RELATIONAL, FUNCTION, REFERENCE, DEREFERENCE, NULL, ITERATION, CODE_ROOT + PLACEHOLDER, + LOOP, + SYMBOL, + TRANSACTION_START, + TRANSACTION, + TRANSACTION_END, + DECISION_BRANCH, + COMPARISON, + ADDRESS, + RELATIONAL, + FUNCTION, + REFERENCE, + DEREFERENCE, + NULL, + ITERATION, + BLOCK_BOUNDARY, + CODE_ROOT } diff --git a/smojol-core/src/main/java/org/smojol/common/transpiler/RetCallTranspilerNode.java b/smojol-core/src/main/java/org/smojol/common/transpiler/RetCallTranspilerNode.java new file mode 100644 index 00000000..fc32ce9f --- /dev/null +++ b/smojol-core/src/main/java/org/smojol/common/transpiler/RetCallTranspilerNode.java @@ -0,0 +1,18 @@ +package org.smojol.common.transpiler; + +import com.google.common.collect.ImmutableList; +import org.smojol.common.ast.SemanticCategory; + +public class RetCallTranspilerNode extends TranspilerNode { + private final String fallthroughTarget; + + public RetCallTranspilerNode(String fallthroughTarget, TranspilerCodeBlockNode body) { + super(ImmutableList.of(), ImmutableList.of(SemanticCategory.BLOCK_BOUNDARY)); + this.fallthroughTarget = fallthroughTarget; + } + + @Override + public String description() { + return String.format("callret (\"%s\")", fallthroughTarget); + } +} diff --git a/smojol-toolkit/src/main/java/org/smojol/toolkit/analysis/task/transpiler/BuildTranspilerASTTask.java b/smojol-toolkit/src/main/java/org/smojol/toolkit/analysis/task/transpiler/BuildTranspilerASTTask.java index 2ecb683e..c77f29e9 100644 --- a/smojol-toolkit/src/main/java/org/smojol/toolkit/analysis/task/transpiler/BuildTranspilerASTTask.java +++ b/smojol-toolkit/src/main/java/org/smojol/toolkit/analysis/task/transpiler/BuildTranspilerASTTask.java @@ -21,6 +21,6 @@ public BuildTranspilerASTTask(ParseTree rawAST, CobolDataStructure dataStructure public TranspilerNode run() { FlowNode flowRoot = new IntermediateASTNodeBuilder(rawAST, dataStructures, symbolTable).build(); - return TranspilerTreeBuilder.flowToTranspiler(flowRoot, dataStructures); + return TranspilerTreeBuilder.flowToTranspiler(flowRoot, dataStructures, new SectionParagraphMap(flowRoot)); } } diff --git a/smojol-toolkit/src/main/java/org/smojol/toolkit/analysis/task/transpiler/SectionParagraphMap.java b/smojol-toolkit/src/main/java/org/smojol/toolkit/analysis/task/transpiler/SectionParagraphMap.java new file mode 100644 index 00000000..6783fbcc --- /dev/null +++ b/smojol-toolkit/src/main/java/org/smojol/toolkit/analysis/task/transpiler/SectionParagraphMap.java @@ -0,0 +1,30 @@ +package org.smojol.toolkit.analysis.task.transpiler; + +import lombok.Getter; +import org.apache.commons.lang3.tuple.ImmutablePair; +import org.apache.commons.lang3.tuple.Pair; +import org.smojol.common.ast.FlowNode; +import org.smojol.common.ast.FlowNodeType; +import org.smojol.common.navigation.FlowNodeNavigator; + +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +@Getter +public class SectionParagraphMap { + private final Map> sectionParagraphsPairs; + private final Map paragraphToSectionMap; + + public SectionParagraphMap(FlowNode flowRoot) { + List sectionFlowNodes = new FlowNodeNavigator(flowRoot).findAllByCondition(fn -> fn.type() == FlowNodeType.SECTION); + sectionParagraphsPairs = sectionFlowNodes.stream() + .map(sfn -> (Pair>) ImmutablePair.of(sfn, new FlowNodeNavigator(sfn).findAllByCondition(n -> n.type() == FlowNodeType.PARAGRAPH))) + .collect(Collectors.toMap(Pair::getLeft, Pair::getRight)); + paragraphToSectionMap = sectionFlowNodes.stream().flatMap(sfn -> + new FlowNodeNavigator(sfn).findAllByCondition(n -> n.type() == FlowNodeType.PARAGRAPH).stream() + .map(para -> ImmutablePair.of(para, sfn)) + ).collect(Collectors.toMap(Pair::getLeft, Pair::getRight)); + + } +} diff --git a/smojol-toolkit/src/main/java/org/smojol/toolkit/transpiler/EvaluateNodeTranslator.java b/smojol-toolkit/src/main/java/org/smojol/toolkit/transpiler/EvaluateNodeTranslator.java index 711c52a0..a04a9fa1 100644 --- a/smojol-toolkit/src/main/java/org/smojol/toolkit/transpiler/EvaluateNodeTranslator.java +++ b/smojol-toolkit/src/main/java/org/smojol/toolkit/transpiler/EvaluateNodeTranslator.java @@ -1,11 +1,15 @@ package org.smojol.toolkit.transpiler; import org.smojol.common.ast.FlowNode; -import org.smojol.common.transpiler.*; +import org.smojol.common.transpiler.DetachedTranspilerCodeBlockNode; +import org.smojol.common.transpiler.IfTranspilerNode; +import org.smojol.common.transpiler.TranspilerCodeBlockNode; +import org.smojol.common.transpiler.TranspilerNode; import org.smojol.common.vm.expression.CobolExpression; import org.smojol.common.vm.expression.ExpandedEvaluation; import org.smojol.common.vm.expression.TestActionPair; import org.smojol.common.vm.structure.CobolDataStructure; +import org.smojol.toolkit.analysis.task.transpiler.SectionParagraphMap; import org.smojol.toolkit.ast.EvaluateFlowNode; import java.util.List; @@ -15,19 +19,19 @@ import static org.smojol.common.list.CarCdr.tail; public class EvaluateNodeTranslator { - public static TranspilerNode build(EvaluateFlowNode n, CobolDataStructure dataStructures) { + public static TranspilerNode build(EvaluateFlowNode n, CobolDataStructure dataStructures, SectionParagraphMap sectionParagraphMap) { ExpandedEvaluation deconstructedRepresentation = n.getDeconstructedRepresentation(); List clauses = deconstructedRepresentation.testActionPairs(); - TranspilerCodeBlockNode elseBody = new DetachedTranspilerCodeBlockNode(deconstructedRepresentation.elseBody().stream().map(stmt -> TranspilerTreeBuilder.flowToTranspiler(stmt, dataStructures)).toList()); - return recursiveOr(head(clauses), tail(clauses), dataStructures, elseBody); + TranspilerCodeBlockNode elseBody = new DetachedTranspilerCodeBlockNode(deconstructedRepresentation.elseBody().stream().map(stmt -> TranspilerTreeBuilder.flowToTranspiler(stmt, dataStructures, sectionParagraphMap)).toList()); + return recursiveOr(head(clauses), tail(clauses), dataStructures, elseBody, sectionParagraphMap); } - private static TranspilerNode recursiveOr(Optional current, List remaining, CobolDataStructure dataStructures, TranspilerCodeBlockNode elseBody) { + private static TranspilerNode recursiveOr(Optional current, List remaining, CobolDataStructure dataStructures, TranspilerCodeBlockNode elseBody, SectionParagraphMap sectionParagraphMap) { TranspilerExpressionBuilder builder = new TranspilerExpressionBuilder(dataStructures); CobolExpression test = current.get().test(); List bodyStatements = current.get().actions(); - TranspilerCodeBlockNode transpilerBody = new DetachedTranspilerCodeBlockNode(bodyStatements.stream().map(stmt -> TranspilerTreeBuilder.flowToTranspiler(stmt, dataStructures)).toList()); + TranspilerCodeBlockNode transpilerBody = new DetachedTranspilerCodeBlockNode(bodyStatements.stream().map(stmt -> TranspilerTreeBuilder.flowToTranspiler(stmt, dataStructures, sectionParagraphMap)).toList()); if (remaining.isEmpty()) return new IfTranspilerNode(builder.build(test), transpilerBody, elseBody); - return new IfTranspilerNode(builder.build(test), transpilerBody, recursiveOr(head(remaining), tail(remaining), dataStructures, elseBody)); + return new IfTranspilerNode(builder.build(test), transpilerBody, recursiveOr(head(remaining), tail(remaining), dataStructures, elseBody, sectionParagraphMap)); } } diff --git a/smojol-toolkit/src/main/java/org/smojol/toolkit/transpiler/IfTranspilerNodeBuilder.java b/smojol-toolkit/src/main/java/org/smojol/toolkit/transpiler/IfTranspilerNodeBuilder.java index b6835e49..e162ab14 100644 --- a/smojol-toolkit/src/main/java/org/smojol/toolkit/transpiler/IfTranspilerNodeBuilder.java +++ b/smojol-toolkit/src/main/java/org/smojol/toolkit/transpiler/IfTranspilerNodeBuilder.java @@ -3,16 +3,17 @@ import org.smojol.common.transpiler.*; import org.smojol.common.vm.expression.CobolExpression; import org.smojol.common.vm.structure.CobolDataStructure; +import org.smojol.toolkit.analysis.task.transpiler.SectionParagraphMap; import org.smojol.toolkit.ast.ConditionalStatementFlowNode; import org.smojol.toolkit.ast.IfFlowNode; public class IfTranspilerNodeBuilder { - public static TranspilerNode build(IfFlowNode n, CobolDataStructure dataStructures) { + public static TranspilerNode build(IfFlowNode n, CobolDataStructure dataStructures, SectionParagraphMap sectionParagraphMap) { CobolExpression condition = n.getConditionExpression(); TranspilerExpressionBuilder nodeBuilder = new TranspilerExpressionBuilder(dataStructures); TranspilerNode transpilerCondition = nodeBuilder.build(condition); - TranspilerCodeBlockNode ifThenBlock = new DetachedTranspilerCodeBlockNode(n.getIfThenBlock().astChildren().stream().map(stmt -> TranspilerTreeBuilder.flowToTranspiler(stmt, dataStructures)).toList()); - TranspilerCodeBlockNode ifElseBlock = n.getIfElseBlock() != null ? new DetachedTranspilerCodeBlockNode(n.getIfElseBlock().astChildren().stream().filter(c -> c instanceof ConditionalStatementFlowNode).map(stmt -> TranspilerTreeBuilder.flowToTranspiler(stmt, dataStructures)).toList()) : new TranspilerCodeBlockNode(); + TranspilerCodeBlockNode ifThenBlock = new DetachedTranspilerCodeBlockNode(n.getIfThenBlock().astChildren().stream().map(stmt -> TranspilerTreeBuilder.flowToTranspiler(stmt, dataStructures, sectionParagraphMap)).toList()); + TranspilerCodeBlockNode ifElseBlock = n.getIfElseBlock() != null ? new DetachedTranspilerCodeBlockNode(n.getIfElseBlock().astChildren().stream().filter(c -> c instanceof ConditionalStatementFlowNode).map(stmt -> TranspilerTreeBuilder.flowToTranspiler(stmt, dataStructures, sectionParagraphMap)).toList()) : new TranspilerCodeBlockNode(); return new IfTranspilerNode(transpilerCondition, ifThenBlock, ifElseBlock); } } diff --git a/smojol-toolkit/src/main/java/org/smojol/toolkit/transpiler/LabelledTranspilerCodeBlockNodeBuilder.java b/smojol-toolkit/src/main/java/org/smojol/toolkit/transpiler/LabelledTranspilerCodeBlockNodeBuilder.java index 8707b51b..72823f10 100644 --- a/smojol-toolkit/src/main/java/org/smojol/toolkit/transpiler/LabelledTranspilerCodeBlockNodeBuilder.java +++ b/smojol-toolkit/src/main/java/org/smojol/toolkit/transpiler/LabelledTranspilerCodeBlockNodeBuilder.java @@ -4,11 +4,10 @@ import org.smojol.common.ast.FlowNode; import org.smojol.common.ast.FlowNodeType; import org.smojol.common.transpiler.LabelledTranspilerCodeBlockNode; -import org.smojol.common.transpiler.TranspilerCodeBlockNode; import org.smojol.common.transpiler.TranspilerNode; import org.smojol.common.vm.structure.CobolDataStructure; +import org.smojol.toolkit.analysis.task.transpiler.SectionParagraphMap; import org.smojol.toolkit.ast.ParagraphFlowNode; -import org.smojol.toolkit.ast.ParagraphsFlowNode; import org.smojol.toolkit.ast.ProcedureDivisionBodyFlowNode; import org.smojol.toolkit.ast.SectionFlowNode; @@ -16,25 +15,25 @@ import java.util.Map; public class LabelledTranspilerCodeBlockNodeBuilder { - public static TranspilerNode build(SectionFlowNode n, CobolDataStructure dataStructures) { - return labelledBlock(n, dataStructures, ImmutableMap.of("type", FlowNodeType.SECTION)); + public static TranspilerNode build(SectionFlowNode n, CobolDataStructure dataStructures, SectionParagraphMap sectionParagraphMap) { + return labelledBlock(n, dataStructures, ImmutableMap.of("type", FlowNodeType.SECTION), sectionParagraphMap); } - private static LabelledTranspilerCodeBlockNode labelledBlock(FlowNode n, CobolDataStructure dataStructures, Map properties) { - List childTranspilerNodes = n.astChildren().stream().map(child -> TranspilerTreeBuilder.flowToTranspiler(child, dataStructures)).toList(); + private static LabelledTranspilerCodeBlockNode labelledBlock(FlowNode n, CobolDataStructure dataStructures, Map properties, SectionParagraphMap sectionParagraphMap) { + List childTranspilerNodes = n.astChildren().stream().map(child -> TranspilerTreeBuilder.flowToTranspiler(child, dataStructures, sectionParagraphMap)).toList(); return new LabelledTranspilerCodeBlockNode(n.name(), childTranspilerNodes, properties); } - public static TranspilerNode build(ParagraphFlowNode n, CobolDataStructure dataStructures) { - return labelledBlock(n, dataStructures, ImmutableMap.of("type", FlowNodeType.PARAGRAPH)); + public static TranspilerNode build(ParagraphFlowNode n, CobolDataStructure dataStructures, SectionParagraphMap sectionParagraphMap) { + return labelledBlock(n, dataStructures, ImmutableMap.of("type", FlowNodeType.PARAGRAPH), sectionParagraphMap); } - public static TranspilerNode build(ProcedureDivisionBodyFlowNode n, CobolDataStructure dataStructures) { - return labelledBlock(n, dataStructures, ImmutableMap.of("type", FlowNodeType.PROCEDURE_DIVISION_BODY)); + public static TranspilerNode build(ProcedureDivisionBodyFlowNode n, CobolDataStructure dataStructures, SectionParagraphMap sectionParagraphMap) { + return labelledBlock(n, dataStructures, ImmutableMap.of("type", FlowNodeType.PROCEDURE_DIVISION_BODY), sectionParagraphMap); } - public static TranspilerNode build(ParagraphsFlowNode n, CobolDataStructure dataStructures) { - List childTranspilerNodes = n.astChildren().stream().map(child -> TranspilerTreeBuilder.flowToTranspiler(child, dataStructures)).toList(); - return new TranspilerCodeBlockNode(childTranspilerNodes); - } +// public static TranspilerNode build(ParagraphsFlowNode n, CobolDataStructure dataStructures) { +// List childTranspilerNodes = n.astChildren().stream().map(child -> TranspilerTreeBuilder.flowToTranspiler(child, dataStructures, sectionParagraphMap)).toList(); +// return new TranspilerCodeBlockNode(childTranspilerNodes); +// } } diff --git a/smojol-toolkit/src/main/java/org/smojol/toolkit/transpiler/PerformProcedureNodeBuilder.java b/smojol-toolkit/src/main/java/org/smojol/toolkit/transpiler/PerformProcedureNodeBuilder.java index 0b1e5397..5619f280 100644 --- a/smojol-toolkit/src/main/java/org/smojol/toolkit/transpiler/PerformProcedureNodeBuilder.java +++ b/smojol-toolkit/src/main/java/org/smojol/toolkit/transpiler/PerformProcedureNodeBuilder.java @@ -4,6 +4,7 @@ import org.smojol.common.transpiler.*; import org.smojol.common.vm.expression.FlowIteration; import org.smojol.common.vm.structure.CobolDataStructure; +import org.smojol.toolkit.analysis.task.transpiler.SectionParagraphMap; import org.smojol.toolkit.ast.ConditionalStatementFlowNode; import org.smojol.toolkit.ast.PerformInlineFlowNode; import org.smojol.toolkit.ast.PerformProcedureFlowNode; @@ -34,15 +35,15 @@ private static TranspilerNode toTranspilerLoop(FlowIteration loop, TranspilerNod ); } - public static TranspilerNode build(PerformInlineFlowNode n, CobolDataStructure dataStructures) { + public static TranspilerNode build(PerformInlineFlowNode n, CobolDataStructure dataStructures, SectionParagraphMap sectionParagraphMap) { List nestedLoops = n.getNestedLoops(); - if (nestedLoops.isEmpty()) return body(n, dataStructures); - return recurse(nestedLoops, body(n, dataStructures), dataStructures); + if (nestedLoops.isEmpty()) return body(n, dataStructures, sectionParagraphMap); + return recurse(nestedLoops, body(n, dataStructures, sectionParagraphMap), dataStructures); } - private static TranspilerNode body(FlowNode node, CobolDataStructure dataStructures) { + private static TranspilerNode body(FlowNode node, CobolDataStructure dataStructures, SectionParagraphMap sectionParagraphMap) { List inlineStatements = node.astChildren().stream().filter(n -> n instanceof ConditionalStatementFlowNode).toList(); - List inlineTranspilerNodes = inlineStatements.stream().map(istmt -> TranspilerTreeBuilder.flowToTranspiler(istmt, dataStructures)).toList(); + List inlineTranspilerNodes = inlineStatements.stream().map(istmt -> TranspilerTreeBuilder.flowToTranspiler(istmt, dataStructures, sectionParagraphMap)).toList(); return new TranspilerCodeBlockNode(inlineTranspilerNodes); } } diff --git a/smojol-toolkit/src/main/java/org/smojol/toolkit/transpiler/SearchWhenNodeTranslator.java b/smojol-toolkit/src/main/java/org/smojol/toolkit/transpiler/SearchWhenNodeTranslator.java index 08c300f8..d2486a0d 100644 --- a/smojol-toolkit/src/main/java/org/smojol/toolkit/transpiler/SearchWhenNodeTranslator.java +++ b/smojol-toolkit/src/main/java/org/smojol/toolkit/transpiler/SearchWhenNodeTranslator.java @@ -2,6 +2,7 @@ import org.smojol.common.transpiler.*; import org.smojol.common.vm.structure.CobolDataStructure; +import org.smojol.toolkit.analysis.task.transpiler.SectionParagraphMap; import org.smojol.toolkit.ast.SearchFlowNode; import org.smojol.toolkit.ast.SearchWhenFlowNode; @@ -11,19 +12,19 @@ import static org.smojol.common.list.CarCdr.tail; public class SearchWhenNodeTranslator { - public static TranspilerNode build(SearchFlowNode n, CobolDataStructure dataStructures) { + public static TranspilerNode build(SearchFlowNode n, CobolDataStructure dataStructures, SectionParagraphMap sectionParagraphMap) { TranspilerExpressionBuilder nodeBuilder = new TranspilerExpressionBuilder(dataStructures); List phrases = n.getWhenPhrases().stream().map(node -> (SearchWhenFlowNode) node).toList(); - return new ListIterationTranspilerNode(nodeBuilder.build(n.getSearchExpression()), build(head(phrases).get(), tail(phrases), dataStructures)); + return new ListIterationTranspilerNode(nodeBuilder.build(n.getSearchExpression()), build(head(phrases).get(), tail(phrases), dataStructures, sectionParagraphMap)); } - private static TranspilerNode build(SearchWhenFlowNode n, List remaining, CobolDataStructure dataStructures) { + private static TranspilerNode build(SearchWhenFlowNode n, List remaining, CobolDataStructure dataStructures, SectionParagraphMap sectionParagraphMap) { TranspilerExpressionBuilder nodeBuilder = new TranspilerExpressionBuilder(dataStructures); TranspilerNode transpilerCondition = nodeBuilder.build(n.getConditionExpression()); - TranspilerCodeBlockNode whenBlock = new DetachedTranspilerCodeBlockNode(n.getWhenFlowNodes().stream().map(node -> TranspilerTreeBuilder.flowToTranspiler(node, dataStructures)).toList()); + TranspilerCodeBlockNode whenBlock = new DetachedTranspilerCodeBlockNode(n.getWhenFlowNodes().stream().map(node -> TranspilerTreeBuilder.flowToTranspiler(node, dataStructures, sectionParagraphMap)).toList()); // whenBlock.add(new BreakTranspilerNode()); whenBlock.add(new JumpTranspilerNode(new ExitIterationScopeLocationNode())); if (head(remaining).isEmpty()) return new IfTranspilerNode(transpilerCondition, whenBlock); - return new IfTranspilerNode(transpilerCondition, whenBlock, new DetachedTranspilerCodeBlockNode(build(head(remaining).get(), tail(remaining), dataStructures))); + return new IfTranspilerNode(transpilerCondition, whenBlock, new DetachedTranspilerCodeBlockNode(build(head(remaining).get(), tail(remaining), dataStructures, sectionParagraphMap))); } } diff --git a/smojol-toolkit/src/main/java/org/smojol/toolkit/transpiler/TranspilerCodeBlockNodeBuilder.java b/smojol-toolkit/src/main/java/org/smojol/toolkit/transpiler/TranspilerCodeBlockNodeBuilder.java index 8f9b560a..6fa587d1 100644 --- a/smojol-toolkit/src/main/java/org/smojol/toolkit/transpiler/TranspilerCodeBlockNodeBuilder.java +++ b/smojol-toolkit/src/main/java/org/smojol/toolkit/transpiler/TranspilerCodeBlockNodeBuilder.java @@ -6,30 +6,33 @@ import org.smojol.common.transpiler.TranspilerCodeBlockNode; import org.smojol.common.transpiler.TranspilerNode; import org.smojol.common.vm.structure.CobolDataStructure; -import org.smojol.toolkit.ast.*; +import org.smojol.toolkit.analysis.task.transpiler.SectionParagraphMap; +import org.smojol.toolkit.ast.ParagraphsFlowNode; +import org.smojol.toolkit.ast.SectionFlowNode; +import org.smojol.toolkit.ast.SentenceFlowNode; import java.util.List; import java.util.Map; public class TranspilerCodeBlockNodeBuilder { - public static TranspilerNode build(SectionFlowNode n, CobolDataStructure dataStructures) { - return block(n, dataStructures); + public static TranspilerNode build(SectionFlowNode n, CobolDataStructure dataStructures, SectionParagraphMap sectionParagraphMap) { + return block(n, dataStructures, sectionParagraphMap); } - private static TranspilerCodeBlockNode block(FlowNode n, CobolDataStructure dataStructures, Map additionalAttributes) { - List childTranspilerNodes = n.astChildren().stream().map(child -> TranspilerTreeBuilder.flowToTranspiler(child, dataStructures)).toList(); + private static TranspilerCodeBlockNode block(FlowNode n, CobolDataStructure dataStructures, Map additionalAttributes, SectionParagraphMap sectionParagraphMap) { + List childTranspilerNodes = n.astChildren().stream().map(child -> TranspilerTreeBuilder.flowToTranspiler(child, dataStructures, sectionParagraphMap)).toList(); return new TranspilerCodeBlockNode(childTranspilerNodes, additionalAttributes); } - private static TranspilerCodeBlockNode block(FlowNode n, CobolDataStructure dataStructures) { - return block(n, dataStructures, ImmutableMap.of()); + private static TranspilerCodeBlockNode block(FlowNode n, CobolDataStructure dataStructures, SectionParagraphMap sectionParagraphMap) { + return block(n, dataStructures, ImmutableMap.of(), sectionParagraphMap); } - public static TranspilerNode build(ParagraphsFlowNode n, CobolDataStructure dataStructures) { - return block(n, dataStructures); + public static TranspilerNode build(ParagraphsFlowNode n, CobolDataStructure dataStructures, SectionParagraphMap sectionParagraphMap) { + return block(n, dataStructures, sectionParagraphMap); } - public static TranspilerNode build(SentenceFlowNode n, CobolDataStructure dataStructures) { - return block(n, dataStructures, ImmutableMap.of("type", FlowNodeType.SENTENCE)); + public static TranspilerNode build(SentenceFlowNode n, CobolDataStructure dataStructures, SectionParagraphMap sectionParagraphMap) { + return block(n, dataStructures, ImmutableMap.of("type", FlowNodeType.SENTENCE), sectionParagraphMap); } } diff --git a/smojol-toolkit/src/main/java/org/smojol/toolkit/transpiler/TranspilerTreeBuilder.java b/smojol-toolkit/src/main/java/org/smojol/toolkit/transpiler/TranspilerTreeBuilder.java index 47051384..7126ec53 100644 --- a/smojol-toolkit/src/main/java/org/smojol/toolkit/transpiler/TranspilerTreeBuilder.java +++ b/smojol-toolkit/src/main/java/org/smojol/toolkit/transpiler/TranspilerTreeBuilder.java @@ -3,31 +3,32 @@ import org.smojol.common.ast.FlowNode; import org.smojol.common.transpiler.*; import org.smojol.common.vm.structure.CobolDataStructure; +import org.smojol.toolkit.analysis.task.transpiler.SectionParagraphMap; import org.smojol.toolkit.ast.*; public class TranspilerTreeBuilder { - public static TranspilerNode flowToTranspiler(FlowNode node, CobolDataStructure dataStructures) { + public static TranspilerNode flowToTranspiler(FlowNode node, CobolDataStructure dataStructures, SectionParagraphMap sectionParagraphMap) { return switch (node) { case MoveFlowNode n -> SetTranspilerNodeBuilder.build(n, dataStructures); - case IfFlowNode n -> IfTranspilerNodeBuilder.build(n, dataStructures); + case IfFlowNode n -> IfTranspilerNodeBuilder.build(n, dataStructures, sectionParagraphMap); case GenericOnClauseFlowNode n -> new PlaceholderTranspilerNode(n.originalText()); - case SearchFlowNode n -> SearchWhenNodeTranslator.build(n, dataStructures); - case EvaluateFlowNode n -> EvaluateNodeTranslator.build(n, dataStructures); + case SearchFlowNode n -> SearchWhenNodeTranslator.build(n, dataStructures, sectionParagraphMap); + case EvaluateFlowNode n -> EvaluateNodeTranslator.build(n, dataStructures, sectionParagraphMap); case AddFlowNode n -> AddTranspilerNodeBuilder.build(n, dataStructures); case SubtractFlowNode n -> SubtractTranspilerNodeBuilder.build(n, dataStructures); case MultiplyFlowNode n -> MultiplyTranspilerNodeBuilder.build(n, dataStructures); case DivideFlowNode n -> DivideTranspilerNodeBuilder.build(n, dataStructures); case ComputeFlowNode n -> ComputeTranspilerNodeBuilder.build(n, dataStructures); case DisplayFlowNode n -> DisplayTranspilerNodeBuilder.build(n, dataStructures); - case ConditionalStatementFlowNode n -> flowToTranspiler(n.getActualStatement(), dataStructures); - case ProcedureDivisionBodyFlowNode n -> LabelledTranspilerCodeBlockNodeBuilder.build(n, dataStructures); - case SectionFlowNode n -> LabelledTranspilerCodeBlockNodeBuilder.build(n, dataStructures); - case ParagraphsFlowNode n -> TranspilerCodeBlockNodeBuilder.build(n, dataStructures); - case SentenceFlowNode n -> TranspilerCodeBlockNodeBuilder.build(n, dataStructures); - case ParagraphFlowNode n -> LabelledTranspilerCodeBlockNodeBuilder.build(n, dataStructures); + case ConditionalStatementFlowNode n -> flowToTranspiler(n.getActualStatement(), dataStructures, sectionParagraphMap); + case ProcedureDivisionBodyFlowNode n -> LabelledTranspilerCodeBlockNodeBuilder.build(n, dataStructures, sectionParagraphMap); + case SectionFlowNode n -> LabelledTranspilerCodeBlockNodeBuilder.build(n, dataStructures, sectionParagraphMap); + case ParagraphsFlowNode n -> TranspilerCodeBlockNodeBuilder.build(n, dataStructures, sectionParagraphMap); + case SentenceFlowNode n -> TranspilerCodeBlockNodeBuilder.build(n, dataStructures, sectionParagraphMap); + case ParagraphFlowNode n -> LabelledTranspilerCodeBlockNodeBuilder.build(n, dataStructures, sectionParagraphMap); case GoToFlowNode n -> JumpNodeBuilder.build(n, dataStructures); case PerformProcedureFlowNode n -> PerformProcedureNodeBuilder.build(n, dataStructures); - case PerformInlineFlowNode n -> PerformProcedureNodeBuilder.build(n, dataStructures); + case PerformInlineFlowNode n -> PerformProcedureNodeBuilder.build(n, dataStructures, sectionParagraphMap); case NextSentenceFlowNode n -> new JumpTranspilerNode(new NextLocationNode()); case ExitFlowNode n -> new ExitTranspilerNode(); case StopFlowNode n -> new JumpTranspilerNode(new ProgramTerminalLocationNode());