-
Notifications
You must be signed in to change notification settings - Fork 0
/
FunctionDefinition.java
121 lines (105 loc) · 3.8 KB
/
FunctionDefinition.java
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
import minipython.node.*;
import java.util.ArrayList;
import java.util.Objects;
public class FunctionDefinition {
String name;
int line;
int col;
// Minimum number of arguments ( excluding default args )
int sum;
// Maximum number of arguments ( including default args )
int total;
ArrayList<AIdentifierValue> identifierValues;
PStatement statement;
String retType;
// The functionDefinition object that is returned (if its returned)
FunctionDefinition retDef;
PFunctionCall retFuncCall;
ArrayList<String> expectedTypes;
public FunctionDefinition(String name, int line, int col, int sum, int total) {
this.name = name;
this.line = line;
this.col = col;
this.sum = sum;
this.total = total;
}
/**
* Returns true if the current function is a redefinition of @param other
* @param other the other function
*/
public boolean redefines(FunctionDefinition other){
// True iff if there is an overlap on the range of parameters that can be called with the function
if (sum <= other.sum){
return total >= other.sum;
}
else return other.total >= this.sum;
}
/**
* Returns trye if the function call can be called with this function definition
* @param call
* @return
*/
public boolean matches(FunctionCall call){
return call.numArgs >= sum && call.numArgs <= total;
}
public ArrayList<String> getExpectedTypes() {
return expectedTypes;
}
public void setExpectedTypes(ArrayList<String> expectedTypes) {
this.expectedTypes = expectedTypes;
}
public void setIdentifierValues(ArrayList<AIdentifierValue> identifierValues) {
this.identifierValues = identifierValues;
}
public void setStatement(PStatement statement) {
// If any argument is used in a arithmetic expression the expected type is number
// id the argument is not used anywhere the expected type is 'any'
this.statement = statement;
if (statement instanceof AReturnStatement){
AReturnStatement ret = (AReturnStatement) statement;
PExpression expr = ret.getExpression();
if (expr instanceof AArithmeticExpression){
retType = "number";
return;
}
if (expr instanceof AFuncCallExpression){
retType = "to_be_decided";
retFuncCall = ((AFuncCallExpression) expr).getFunctionCall();
return;
}
if (expr instanceof AValueExpression){
if(((AValueExpression) expr).getValue() instanceof AMethodValue){
retFuncCall = ((AMethodValue) ((AValueExpression) expr).getValue()).getFunctionCall();
retType = "to_be_decided";
return;
}
}
}
//if its if statement void can be returned
//while behaves like if statement
//for behaves like if statement
//func call returns void
retType = "void";
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
FunctionDefinition that = (FunctionDefinition) o;
return sum == that.sum &&
total == that.total &&
Objects.equals(name, that.name) &&
Objects.equals(line, that.line) &&
Objects.equals(col, that.col);
}
@Override
public int hashCode() {
return Objects.hash(name, line, col, sum, total);
}
/**
* Returns the function definition object of the function call if it is returned by the function
*/
public FunctionDefinition getReturnDef(){
return retDef;
}
}