Skip to content

Commit 66ba701

Browse files
authored
COBOL invariants and refactoring
1 parent ac588d0 commit 66ba701

File tree

10 files changed

+561
-25
lines changed

10 files changed

+561
-25
lines changed

ASTCompositeTerm.java

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40328,7 +40328,7 @@ public Vector cobolDataDefinitions(java.util.Map context, Vector invs)
4032840328
int levelNumber = 0;
4032940329
String fieldName = "";
4033040330

40331-
if ("77".equals(level))
40331+
if ("77".equals(level) || "66".equals(level))
4033240332
{ context.put("container", null);
4033340333
return res;
4033440334
}
@@ -40407,7 +40407,7 @@ public Vector cobolDataDefinitions(java.util.Map context, Vector invs)
4040740407
String progname =
4040840408
(String) context.get("programName");
4040940409

40410-
if (multiplicity >= 1 && contMult == 1)
40410+
if (multiplicity == 1 && contMult == 1)
4041140411
{ // fieldName = owner.subrange(startPos,endPos)
4041240412

4041340413
JOptionPane.showMessageDialog(null, progname + ":: " + fieldName +
@@ -40428,8 +40428,42 @@ public Vector cobolDataDefinitions(java.util.Map context, Vector invs)
4042840428
BasicExpression substr =
4042940429
BasicExpression.newFunctionBasicExpression(
4043040430
"subrange", owner, spars);
40431+
Expression convertedExpr =
40432+
Type.typeConversionFromString(
40433+
substr,typ);
4043140434
BinaryExpression inv =
40432-
new BinaryExpression("=", attr, substr);
40435+
new BinaryExpression("=", attr,
40436+
convertedExpr);
40437+
Constraint cons =
40438+
Constraint.getConstraint(inv);
40439+
cons.ownerName = progname;
40440+
invs.add(cons);
40441+
}
40442+
else if (multiplicity > 1 && contMult == 1)
40443+
{ // fieldName = owner.subrange(startPos,endPos)
40444+
int fwdth = wdth/multiplicity;
40445+
JOptionPane.showMessageDialog(null, progname + ":: " + fieldName +
40446+
"[i] = " + ownername +
40447+
".subrange(" + startPos + " + (i-1)*" + fwdth + ", i*" + fwdth + ")",
40448+
"",
40449+
JOptionPane.INFORMATION_MESSAGE);
40450+
BasicExpression owner =
40451+
BasicExpression.newAttributeBasicExpression(
40452+
ownername, stringType);
40453+
BasicExpression attr =
40454+
BasicExpression.newAttributeBasicExpression(
40455+
fieldName,
40456+
typ);
40457+
Vector spars = new Vector();
40458+
spars.add(new BasicExpression(startPos));
40459+
spars.add(new BasicExpression(endPos));
40460+
BasicExpression substr =
40461+
BasicExpression.newFunctionBasicExpression(
40462+
"subrange", owner, spars);
40463+
BinaryExpression inv =
40464+
new BinaryExpression("=",
40465+
new UnaryExpression("->sum", attr),
40466+
substr);
4043340467
Constraint cons =
4043440468
Constraint.getConstraint(inv);
4043540469
cons.ownerName = progname;
@@ -40469,8 +40503,26 @@ else if (multiplicity == 1 & contMult > 1)
4046940503
BasicExpression substr =
4047040504
BasicExpression.newFunctionBasicExpression(
4047140505
"subrange", owner, spars);
40506+
Expression convertedExpr =
40507+
Type.typeConversionFromString(
40508+
substr,typ);
4047240509
BinaryExpression inv =
40473-
new BinaryExpression("=", attr, substr);
40510+
new BinaryExpression("=", attr,
40511+
convertedExpr);
40512+
Vector ipars = new Vector();
40513+
ipars.add(new BasicExpression(1));
40514+
ipars.add(
40515+
new BasicExpression(contMult));
40516+
40517+
BasicExpression dmn =
40518+
BasicExpression.newFunctionBasicExpression(
40519+
"subrange", "Integer", ipars);
40520+
BinaryExpression indmn =
40521+
new BinaryExpression(":", indx, dmn);
40522+
40523+
inv =
40524+
new BinaryExpression("!", indmn, inv);
40525+
4047440526
Constraint cons =
4047540527
Constraint.getConstraint(inv);
4047640528
cons.ownerName = progname;

AgileUMLApp.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public void init()
8686
SimpleAttributeSet[] attrs = initAttributes(4);
8787

8888
try {
89-
doc.insertString(0, "class Person { String name; int age; }", attrs[1]);
89+
doc.insertString(0, "struct Person { char* name; int age; };\n\n struct Person* pp = malloc(sizeof(struct Person));\n", attrs[1]);
9090
}
9191
catch (BadLocationException ble) {
9292
System.err.println("!! Couldn't insert code text.");
@@ -185,6 +185,10 @@ protected JMenu createTranslationMenu()
185185
// checkAction.setMnemonic(KeyEvent.VK_K);
186186
menu.add(toCSAction);
187187

188+
javax.swing.Action toCPPAction = new Translate2CPPAction();
189+
// checkAction.setMnemonic(KeyEvent.VK_K);
190+
menu.add(toCPPAction);
191+
188192
return menu;
189193
}
190194

@@ -608,4 +612,27 @@ public void actionPerformed(ActionEvent e)
608612
messageArea.setText(res);
609613
}
610614
}
615+
616+
class Translate2CPPAction extends javax.swing.AbstractAction
617+
{ public Translate2CPPAction()
618+
{ super("Translate to C++"); }
619+
620+
public void actionPerformed(ActionEvent e)
621+
{ StringWriter sw = new StringWriter();
622+
PrintWriter out = new PrintWriter(sw);
623+
624+
StringWriter sw1 = new StringWriter();
625+
PrintWriter out1 = new PrintWriter(sw1);
626+
627+
for (int i = 0; i < entities.size(); i++)
628+
{ Entity ent = (Entity) entities.get(i);
629+
if (ent.isExternal() || ent.isComponent())
630+
{ continue; }
631+
ent.generateCPP(entities,types,out,out1);
632+
}
633+
String res = sw.toString();
634+
String res1 = sw1.toString();
635+
messageArea.setText(res + "\n\n" + res1);
636+
}
637+
}
611638
}

BasicExpression.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4326,7 +4326,7 @@ else if (t.getName().equals(objectRef + ""))
43264326
arrayType = type;
43274327
adjustTypeForArrayIndex(att);
43284328

4329-
System.out.println("**>> Adjusted type of " + this + " is " + type + "(" + elementType + ")");
4329+
// System.out.println("**>> Adjusted type of " + this + " is " + type + "(" + elementType + ")");
43304330

43314331
elementType = Type.correctElementType(type,elementType,types,entities);
43324332
att.setElementType(elementType);

BehaviouralFeature.java

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10008,6 +10008,153 @@ private static Type getElementType(BinaryExpression be)
1000810008
}
1000910009

1001010010
// Check completeness: if post updates v but not w when w data depends on v
10011+
10012+
public Statement selfCalls2Loops(Statement act)
10013+
{ // if there are simple calls to itself, replace by
10014+
// continue/break in a loop.
10015+
10016+
// ... ; self.nme() ; ***
10017+
// is while true do (...)
10018+
10019+
// ... ; if E then self.nme() else skip ; ***
10020+
// is while true do (... ; if E then continue else
10021+
// skip) ; ***
10022+
10023+
Statement oldact = act;
10024+
if (oldact == null)
10025+
{ oldact = activity; }
10026+
if (oldact == null)
10027+
{ return act; }
10028+
10029+
String nme = getName();
10030+
10031+
Vector contexts = new Vector();
10032+
Vector remainders = new Vector();
10033+
10034+
Vector opcalls = Statement.getOperationCallsContexts(
10035+
nme,oldact,contexts,remainders);
10036+
10037+
System.out.println(opcalls);
10038+
System.out.println(contexts);
10039+
System.out.println(remainders);
10040+
10041+
int selfcalls = 0;
10042+
Vector branches = new Vector();
10043+
Vector rems = new Vector();
10044+
10045+
for (int i = 0; i < opcalls.size(); i++)
10046+
{ InvocationStatement opcall = (InvocationStatement) opcalls.get(i);
10047+
Vector cntx = (Vector) contexts.get(i);
10048+
Vector rem = (Vector) remainders.get(i);
10049+
Expression expr = opcall.getCallExp();
10050+
10051+
if (("self." + nme + "()").equals(expr + ""))
10052+
{ selfcalls++;
10053+
branches.add(cntx);
10054+
rems.add(rem);
10055+
// System.out.println(cntx);
10056+
}
10057+
}
10058+
10059+
System.out.println(">>> There are " + selfcalls + " calls of self." + nme + "() in " + oldact);
10060+
System.out.println(">>> Branches to calls: " + branches);
10061+
System.out.println(">>> Remainders: " + rems);
10062+
10063+
if (selfcalls <= 0)
10064+
{ return act; }
10065+
10066+
if (selfcalls == 1)
10067+
{ // simple case. The call is the last item in
10068+
// branches[0]. Either a direct call or conditional.
10069+
10070+
SequenceStatement loopBody = new SequenceStatement();
10071+
10072+
Vector branch = (Vector) branches.get(0);
10073+
int blen = branch.size();
10074+
for (int i = 0; i < blen - 1; i++)
10075+
{ Statement sx = (Statement) branch.get(i);
10076+
loopBody.addStatement(sx);
10077+
}
10078+
10079+
Vector remainder = (Vector) rems.get(0);
10080+
Object selfcall = branch.get(blen-1);
10081+
10082+
if (selfcall instanceof Statement)
10083+
{
10084+
WhileStatement ws = new WhileStatement(
10085+
new BasicExpression(true),
10086+
loopBody);
10087+
10088+
System.out.println(">>> Restructured code: " + ws);
10089+
System.out.println();
10090+
10091+
return ws;
10092+
}
10093+
else if (selfcall instanceof Vector)
10094+
{ // conditional cases
10095+
Vector selfcallv = (Vector) selfcall;
10096+
if (selfcallv.size() == 4 &&
10097+
"if".equals(selfcallv.get(0) + ""))
10098+
{ Expression tst = (Expression) selfcallv.get(1);
10099+
Vector sts = (Vector) selfcallv.get(2);
10100+
Statement cde =
10101+
Statement.replaceSelfCallByContinue(nme,sts);
10102+
Statement elsePart = (Statement) selfcallv.get(3);
10103+
10104+
Statement newelse =
10105+
SequenceStatement.combineSequenceStatements(
10106+
elsePart,new BreakStatement());
10107+
ConditionalStatement cs =
10108+
new ConditionalStatement(tst,
10109+
cde,
10110+
newelse);
10111+
loopBody.addStatement(cs);
10112+
10113+
WhileStatement ws = new WhileStatement(
10114+
new BasicExpression(true),
10115+
loopBody);
10116+
SequenceStatement res = new SequenceStatement();
10117+
res.addStatement(ws);
10118+
res.addStatements(remainder);
10119+
10120+
System.out.println(">>> Restructured code: " + res);
10121+
System.out.println();
10122+
10123+
return res;
10124+
}
10125+
else if (selfcallv.size() == 4 &&
10126+
"else".equals(selfcallv.get(0) + ""))
10127+
{ Expression tst = (Expression) selfcallv.get(1);
10128+
Statement ifpart = (Statement) selfcallv.get(2);
10129+
10130+
Vector sts = (Vector) selfcallv.get(3);
10131+
Statement cde =
10132+
Statement.replaceSelfCallByContinue(nme,sts);
10133+
Statement newif =
10134+
SequenceStatement.combineSequenceStatements(
10135+
ifpart,new BreakStatement());
10136+
ConditionalStatement cs =
10137+
new ConditionalStatement(tst,
10138+
newif, cde);
10139+
loopBody.addStatement(cs);
10140+
10141+
WhileStatement ws = new WhileStatement(
10142+
new BasicExpression(true),
10143+
loopBody);
10144+
SequenceStatement res = new SequenceStatement();
10145+
res.addStatement(ws);
10146+
res.addStatements(remainder);
10147+
10148+
System.out.println(">>> Restructured code: " + res);
10149+
System.out.println();
10150+
10151+
return res;
10152+
}
10153+
}
10154+
}
10155+
10156+
return act;
10157+
}
1001110158
}
1001210159

1001310160

Compiler2.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5595,6 +5595,11 @@ public static String isKeywordOrPart(String st, String[] mess)
55955595
{ mess[0] = "Use case extension, eg., extendedBy errorCase;";
55965596
return "extendedBy";
55975597
}
5598+
5599+
if ("execute".startsWith(st))
5600+
{ mess[0] = "Execute expression as statement. Eg., execute (x->display())";
5601+
return "execute";
5602+
}
55985603
}
55995604

56005605

0 commit comments

Comments
 (0)