-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCustomFunction.java
More file actions
168 lines (147 loc) · 3.59 KB
/
CustomFunction.java
File metadata and controls
168 lines (147 loc) · 3.59 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import java.util.StringTokenizer;
import java.util.ArrayList;
public class CustomFunction implements FormulaBlock{
private String name ;
private int args;
private String[] coms;
private int[] argpos;
private Calculator calc;
public CustomFunction(String name, String definer, Calculator master)
throws Exception{
StringTokenizer arguments = new StringTokenizer(definer,"#");
int start = 0;
if(definer.substring(0,1).equals("#")){
coms = new String[arguments.countTokens()+1];
argpos = new int[coms.length];
start = 1;
coms[0] = "";
argpos[0] = Integer.parseInt(definer.substring(1,2))-1;
if(argpos[0] < 0){
throw new Exception();
}
if(argpos[0]+1 > args){
args = argpos[0]+1;
}
} else {
coms = new String[arguments.countTokens()];
argpos = new int[coms.length-1];
}
args = 0;
for(int i=start; i<coms.length; i++){
String bit = arguments.nextToken();
if(i == 0){
coms[i] = bit;
} else {
coms[i] = bit.substring(1,bit.length());
argpos[i-1] = Integer.parseInt(bit.substring(0,1))-1;
if(argpos[i-1] < 0){
throw new Exception();
}
if(argpos[i-1]+1 > args){
args = argpos[i-1]+1;
}
}
}
this.name = name;
this.calc = master;
//System.out.println(definer+" "+this.toString());
// test the validity, if expanding fails, it throws an exception
Variable[] dummies = new Variable[args];
for(int i=0; i<args; i++){
dummies[i] = new Variable("0",0);
}
expand(new VariableSet("x",dummies));
}
public String getName(){
return name;
}
public int getArgumentCount(){
return args;
}
public int[] getArgumentPositions(){
return argpos;
}
public String getExpression(String[] arguments){
String alias = "";
for(int i=0; i<this.coms.length-1; i++){
alias += this.coms[i];
if(arguments.length > argpos[i]){
alias += arguments[argpos[i]];
}
}
alias += this.coms[this.coms.length-1];
int i=args;
while(arguments.length > i){
alias += arguments[i]+" ";
i++;
}
return alias;
}
public String toString(){
String[] dummies = new String[args];
for(int i=0; i<args; i++){
dummies[i] = "#"+i;
}
return getExpression(dummies);
}
public boolean isOperator(){
return false;
}
public boolean isCustomFunction(){
return true;
}
public ArrayList<FormulaBlock> expand(FormulaBlock arg)
throws Exception{
Variable[] av = new Variable[args];
try{
if(args != 1){
throw new Exception();
}
av[0] = (Variable)arg;
} catch (Exception error) {
VariableSet list = (VariableSet)arg;
if(list.countElements() == args){
for(int i=0; i<args; i++){
av[i] = list.getElement(i);
}
} else {
System.out.println(this.name+" cannot operate on "+list);
throw new Exception();
}
}
String[] dummies = new String[args];
for(int i=0; i<args; i++){
dummies[i] = "00000000000"+i;
}
ArrayList<FormulaBlock> expression = calc.splitExpression(this.getExpression(dummies));
for(int i=0; i<expression.size(); i++){
try{
Variable tested = (Variable)(expression.get(i));
for(int j=0; j<args; j++){
if(tested.getName().equals("00000000000"+j)){
expression.set(i,av[j].copy());
}
}
} catch(Exception error){
}
}
return expression;
}
public String getSyntax(String[] arguments){
String info = "";
info += name+Operator.SINGLEOP;
if(args > 1){
info += "{";
for(int i=0; i<args; i++){
info += arguments[i];
if(i<args-1){
info += ",";
}
}
info += "}";
} else {
info += arguments[0];
}
return info;
}
}