Skip to content

Commit f53f95e

Browse files
authored
Support for function invocations as If conditions (#28)
## Description Added support for function invocations in If conditions ## Example `if (function(x)){ _logic_ } ` ## Related Issue #22 ## Type of change - [ ] Bug fix - [x] New feature - [ ] Documentation update - [ ] Code refactoring ## How Has This Been Tested? Added tests that verify the correct functionality
1 parent bb89aec commit f53f95e

File tree

4 files changed

+84
-1
lines changed

4 files changed

+84
-1
lines changed

latte/src/main/java/typechecking/LatteTypeChecker.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,8 @@ public void visitCtIf(CtIf ifElement) {
522522
visitCtVariableRead((CtVariableRead<?>)condition);
523523
} else if (condition instanceof CtFieldRead){
524524
visitCtFieldRead((CtFieldRead<?>)condition);
525+
} else if (condition instanceof CtInvocation) {
526+
visitCtInvocation((CtInvocation<?>) condition);
525527
} else {
526528
logError("Cannot evaluate the condition of the if statement: " + condition.toString(), condition);
527529
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package latte;
2+
3+
import specification.Free;
4+
import specification.Unique;
5+
import specification.Shared;
6+
7+
/*
8+
* This file is part of the Latte test suite.
9+
*/
10+
class MyNode {
11+
12+
@Unique Object value;
13+
@Unique Node next;
14+
15+
/**
16+
* Constructor for the Node class using @Free value and next nodes
17+
* @param value
18+
* @param next
19+
*/
20+
public MyNode (@Free Object value, @Free Node next) {
21+
this.value = value;
22+
this.next = next;
23+
}
24+
25+
public @Unique Object test(@Free Object v1, @Free Object v2, @Free Node n1, boolean c1){
26+
@Shared MyNode n = new MyNode(v1, n1);
27+
Object o1 = new Object();
28+
Node n2 = new Node();
29+
30+
if (this.test(n, o1, n2)) {
31+
this.value = v2;
32+
} else {
33+
this.value = v2;
34+
}
35+
return this.value;
36+
}
37+
38+
public boolean test2(@Shared MyNode node, @Free Object o, @Free Node next){
39+
@Free MyNode mn = new MyNode(o, next);
40+
node = mn;
41+
42+
return node == this;
43+
}
44+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package latte;
2+
3+
import specification.Free;
4+
import specification.Unique;
5+
6+
/*
7+
* This file is part of the Latte test suite.
8+
*/
9+
class MyNode {
10+
11+
@Unique Object value;
12+
@Unique Node next;
13+
14+
/**
15+
* Constructor for the Node class using @Free value and next nodes
16+
* @param value
17+
* @param next
18+
*/
19+
public MyNode (@Free Object value, @Free Node next) {
20+
this.value = value;
21+
this.next = next;
22+
}
23+
24+
public void test(@Free Object v1, boolean c1){
25+
if (boolRead(c1)) {
26+
this.value = v1;
27+
} else {
28+
this.value = null;
29+
}
30+
}
31+
32+
private boolean boolRead(boolean b){
33+
return b;
34+
}
35+
}

latte/src/test/java/AppTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ private static Stream<Arguments> provideCorrectTestCases() {
4040
Arguments.of("src/test/examples/searching_state_space/TimerTaskCannotReschedule.java"),
4141
Arguments.of("src/test/examples/searching_state_space/ResultSetNoNext.java"),
4242
Arguments.of("src/test/examples/searching_state_space/ResultSetForwardOnly.java"),
43-
Arguments.of("src/test/examples/stack_overflow/MediaRecord.java")
43+
Arguments.of("src/test/examples/stack_overflow/MediaRecord.java"),
44+
Arguments.of("src/test/examples/MyNodeInvocationIf.java"),
45+
Arguments.of("src/test/examples/MyNodeIfInvocationPermission.java")
4446
);
4547
}
4648

0 commit comments

Comments
 (0)