Skip to content

Commit

Permalink
Java CG: fix issue with seq binds in 'let be st'
Browse files Browse the repository at this point in the history
  • Loading branch information
peterwvj committed Oct 26, 2018
1 parent 7a6599f commit 6e40fe1
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Entry

functions

f : () -> nat
f () ==
let m in seq [42] be st true
in
m;

operations

public static Run : () ==> nat
Run () == f();

end Entry
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<testResult type="org.overture.codegen.tests.exec.ComplexExpJavaGenTest">
<result>
<output object="rO0ABXNyAC9vcmcub3ZlcnR1cmUuaW50ZXJwcmV0ZXIudmFsdWVzLk5hdHVyYWxPbmVWYWx1ZQAAAAAAAAABAgAAeHIALG9yZy5vdmVydHVyZS5pbnRlcnByZXRlci52YWx1ZXMuTmF0dXJhbFZhbHVlAAAAAAAAAAECAAB4cgAsb3JnLm92ZXJ0dXJlLmludGVycHJldGVyLnZhbHVlcy5JbnRlZ2VyVmFsdWUAAAAAAAAAAQIAAUoAB2xvbmdWYWx4cgAtb3JnLm92ZXJ0dXJlLmludGVycHJldGVyLnZhbHVlcy5SYXRpb25hbFZhbHVlAAAAAAAAAAECAAB4cgApb3JnLm92ZXJ0dXJlLmludGVycHJldGVyLnZhbHVlcy5SZWFsVmFsdWUAAAAAAAAAAQIAAHhyACxvcmcub3ZlcnR1cmUuaW50ZXJwcmV0ZXIudmFsdWVzLk51bWVyaWNWYWx1ZQAAAAAAAAABAgABRAAFdmFsdWV4cgAlb3JnLm92ZXJ0dXJlLmludGVycHJldGVyLnZhbHVlcy5WYWx1ZQAAAAAAAAABAgAAeHBARQAAAAAAAAAAAAAAAAAq" resource="LetBeStExpSeqBind.vdmpp" value="42"/>
</result>
</testResult>
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

import java.util.*;
import org.overture.codegen.runtime.*;

@SuppressWarnings("all")
public class Entry {
public static Number Run() {

return f();
}

public Entry() {}

private static Number f() {

Number letBeStExp_1 = null;
Number m = null;
Boolean success_1 = false;
VDMSeq set_1 = SeqUtil.seq(42L);
for (Iterator iterator_1 = set_1.iterator(); iterator_1.hasNext() && !(success_1); ) {
m = ((Number) iterator_1.next());
success_1 = true;
}
if (!(success_1)) {
throw new RuntimeException("Let Be St found no applicable bindings");
}

letBeStExp_1 = m;
return letBeStExp_1;
}

public String toString() {

return "Entry{}";
}
}

##########
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.LinkedList;
import java.util.List;

import org.overture.ast.patterns.ASetMultipleBind;
import org.overture.codegen.ir.*;
import org.overture.codegen.ir.analysis.AnalysisException;
import org.overture.codegen.ir.analysis.DepthFirstAnalysisAdaptor;
Expand Down Expand Up @@ -60,8 +61,8 @@ public class Exp2StmTrans extends DepthFirstAnalysisAdaptor
protected IterationVarPrefixes iteVarPrefixes;

public Exp2StmTrans(IterationVarPrefixes iteVarPrefixes,
TransAssistantIR transAssistant, CounterData counterData,
ILanguageIterator langIterator, Exp2StmVarPrefixes prefixes)
TransAssistantIR transAssistant, CounterData counterData,
ILanguageIterator langIterator, Exp2StmVarPrefixes prefixes)
{
this.transAssistant = transAssistant;
this.counterData = counterData;
Expand Down Expand Up @@ -193,21 +194,23 @@ public void caseALetBeStExpIR(ALetBeStExpIR node) throws AnalysisException
SStmIR enclosingStm = transAssistant.getEnclosingStm(node, "let be st expressions");

AHeaderLetBeStIR header = node.getHeader();
SMultipleBindIR binding = header.getBinding();
SExpIR suchThat = header.getSuchThat();

if (!(header.getBinding() instanceof ASetMultipleBindIR))
if(!(binding instanceof ASetMultipleBindIR || binding instanceof ASeqMultipleBindIR))
{
transAssistant.getInfo().addTransformationWarning(node.getHeader().getBinding(), "This transformation only works for 'let be st' "
+ "expressions with with multiple set binds and not multiple type binds in '"
+ this.getClass().getSimpleName() + "'");
transAssistant.getInfo().addTransformationWarning(node.getHeader().getBinding(),
"This transformation only works for 'let be st' " +
"expressions with with multiple set or sequence binds and not multiple type binds in '"
+ this.getClass().getSimpleName() + "'");
return;
}

ASetMultipleBindIR binding = (ASetMultipleBindIR) header.getBinding();
SExpIR suchThat = header.getSuchThat();
SSetTypeIR setType = transAssistant.getSetTypeCloned(binding.getSet());
STypeIR colType = getCol(binding).getType().clone();

ITempVarGen tempVarNameGen = transAssistant.getInfo().getTempVarNameGen();

LetBeStStrategy strategy = consLetBeStStrategy(suchThat, setType, tempVarNameGen);
LetBeStStrategy strategy = consLetBeStStrategy(suchThat, colType, tempVarNameGen);

ABlockStmIR outerBlock = new ABlockStmIR();

Expand Down Expand Up @@ -241,7 +244,7 @@ public void caseALetBeStExpIR(ALetBeStExpIR node) throws AnalysisException
transAssistant.replaceNodeWith(node, letBeStResult);

LinkedList<SPatternIR> patterns = binding.getPatterns();
ABlockStmIR block = transAssistant.consIterationBlock(patterns, binding.getSet(), tempVarNameGen, strategy, iteVarPrefixes);
ABlockStmIR block = transAssistant.consIterationBlock(patterns, getCol(binding), tempVarNameGen, strategy, iteVarPrefixes);
outerBlock.getStatements().addFirst(block);

// Replace the enclosing statement with the transformation
Expand All @@ -254,8 +257,22 @@ public void caseALetBeStExpIR(ALetBeStExpIR node) throws AnalysisException
outerBlock.setScoped(transAssistant.getInfo().getStmAssistant().isScoped(outerBlock));
}

public LetBeStStrategy consLetBeStStrategy(SExpIR suchThat, SSetTypeIR setType, ITempVarGen tempVarNameGen) {
return new LetBeStStrategy(transAssistant, suchThat, setType, langIterator, tempVarNameGen, iteVarPrefixes);
private SExpIR getCol(SMultipleBindIR binding)
{
if(binding instanceof ASetMultipleBindIR)
{
return ((ASetMultipleBindIR) binding).getSet();
}
else if(binding instanceof ASeqMultipleBindIR)
{
return ((ASeqMultipleBindIR) binding).getSeq();
}

return null;
}

public LetBeStStrategy consLetBeStStrategy(SExpIR suchThat, STypeIR colType, ITempVarGen tempVarNameGen) {
return new LetBeStStrategy(transAssistant, suchThat, colType, langIterator, tempVarNameGen, iteVarPrefixes);
}

@Override
Expand Down Expand Up @@ -590,7 +607,7 @@ public void caseALetDefExpIR(ALetDefExpIR node) throws AnalysisException
}

protected void replaceCompWithTransformation(SStmIR enclosingStm,
ABlockStmIR block, STypeIR type, String var, SExpIR comp)
ABlockStmIR block, STypeIR type, String var, SExpIR comp)
{
AIdentifierVarExpIR compResult = new AIdentifierVarExpIR();
compResult.setType(type.clone());
Expand All @@ -602,7 +619,7 @@ protected void replaceCompWithTransformation(SStmIR enclosingStm,
}

protected void transform(SStmIR enclosingStm, ABlockStmIR block,
SExpIR nodeResult, SExpIR node)
SExpIR nodeResult, SExpIR node)
{
// Replace the node with the node result
transAssistant.replaceNodeWith(node, nodeResult);
Expand Down Expand Up @@ -673,7 +690,7 @@ public void caseACasesExpIR(ACasesExpIR node) throws AnalysisException
}

protected AIfStmIR consAndExpCheck(AAndBoolBinaryExpIR node,
String andResultVarName)
String andResultVarName)
{
SExpIR left = node.getLeft().clone();
SExpIR right = node.getRight().clone();
Expand All @@ -696,7 +713,7 @@ protected AIfStmIR consAndExpCheck(AAndBoolBinaryExpIR node,
}

protected SStmIR consOrExpCheck(AOrBoolBinaryExpIR node,
String orResultVarName)
String orResultVarName)
{
SExpIR left = node.getLeft().clone();
SExpIR right = node.getRight().clone();
Expand All @@ -720,7 +737,7 @@ protected SStmIR consOrExpCheck(AOrBoolBinaryExpIR node,
}

protected boolean transformBoolBinaryExp(SBoolBinaryExpIR node,
SStmIR enclosingStm)
SStmIR enclosingStm)
{
// First condition: The enclosing statement can be 'null' if we only try to code generate an expression rather
// than
Expand All @@ -739,7 +756,7 @@ protected void visitBoolBinary(SBoolBinaryExpIR node)
}

protected void handleLogicExp(SBoolBinaryExpIR node, SStmIR enclosingStm,
SStmIR checkBlock, String resultName) throws AnalysisException
SStmIR checkBlock, String resultName) throws AnalysisException
{
AVarDeclIR andResultDecl = transAssistant.consBoolVarDecl(resultName, false);

Expand All @@ -759,7 +776,7 @@ protected void handleLogicExp(SBoolBinaryExpIR node, SStmIR enclosingStm,
}

protected List<SMultipleBindIR> filterBindList(INode node,
List<SMultipleBindIR> bindList)
List<SMultipleBindIR> bindList)
{
List<SMultipleBindIR> multipleBinds = new LinkedList<SMultipleBindIR>();

Expand Down

0 comments on commit 6e40fe1

Please sign in to comment.