-
Notifications
You must be signed in to change notification settings - Fork 0
/
Functions.java
340 lines (266 loc) · 13 KB
/
Functions.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package me.krypek.igb.cl2;
import static me.krypek.igb.cl1.datatypes.Instruction.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import me.krypek.igb.cl1.IGB_MA;
import me.krypek.igb.cl1.datatypes.Instruction;
import me.krypek.igb.cl2.IGB_CL2_Exception.Err;
import me.krypek.igb.cl2.datatypes.Field;
import me.krypek.igb.cl2.datatypes.function.CompilerFunction;
import me.krypek.igb.cl2.datatypes.function.Function;
import me.krypek.igb.cl2.datatypes.function.FunctionCall;
import me.krypek.igb.cl2.datatypes.function.FunctionCallField;
import me.krypek.igb.cl2.datatypes.function.FunctionCompilerField;
import me.krypek.igb.cl2.datatypes.function.FunctionCompilerField.FunctionCallCompilerField;
import me.krypek.igb.cl2.datatypes.function.FunctionNormalField.FunctionCallNormalField;
import me.krypek.igb.cl2.datatypes.function.FunctionStringField;
import me.krypek.igb.cl2.datatypes.function.FunctionStringField.FunctionCallStringField;
import me.krypek.utils.Pair;
import me.krypek.utils.Utils;
public class Functions {
public final LinkedHashMap<String, LinkedHashMap<Integer, Function>> functionMap;
public Functions() {
functionMap = new LinkedHashMap<>();
initCompilerFunctions();
}
public Function getFunction(String name, int argLength) {
name = name.strip();
HashMap<Integer, Function> map = functionMap.get(name);
if(map == null)
throw Err.normal("Function: \"" + name + "\" doesn't exist.");
Function func = map.get(argLength);
if(func == null)
throw Err.normal("Function: \"" + name + "\" doesn't exist with " + argLength + " arguments.");
return func;
}
public void addFunction(Function func) {
LinkedHashMap<Integer, Function> map = functionMap.get(func.name);
if(map == null) {
map = new LinkedHashMap<>();
functionMap.put(func.name, map);
}
final int argLength = func.fields.length;
if(map.containsKey(argLength))
throw Err.normal("Function: \"" + func.name + "\" with " + argLength + " arguments already exists.");
map.put(argLength, func);
}
private Field getFieldFromCall(FunctionCall call, int index) {
FunctionCallField f1 = call.fields[index];
if(f1 instanceof FunctionCallCompilerField)
return ((FunctionCallCompilerField) f1).field;
else if(f1 instanceof FunctionCallNormalField)
return ((FunctionCallNormalField) f1).field;
throw Err.notPossible();
}
private void initCompilerFunctions() {
addFunction(new CompilerFunction("sqrt", true, Function.fieldsOf(new FunctionCompilerField()), call -> {
Field f1 = getFieldFromCall(call, 0);
ArrayList<Instruction> list = new ArrayList<>();
var pair1 = call.eqsolver.getInstructionsFromField(f1);
list.addAll(pair1.getSecond());
list.add(Math_Sqrt(pair1.getFirst(), call.outputCell));
return list;
}));
addFunction(new CompilerFunction("screenUpdate", false, Function.fieldsOf(), call -> Utils.listOf(Device_ScreenUpdate())));
addFunction(new CompilerFunction("exit", false, Function.fieldsOf(), call -> Utils.listOf(Cell_Jump(-2))));
addFunction(new CompilerFunction("wait", false, Function.fieldsOf(new FunctionCompilerField()), call -> {
Field f1 = getFieldFromCall(call, 0);
if(!f1.isVal())
throw Err.normal("Wait function argument has to be an integer.");
double ticks = f1.value;
if(ticks % 1 != 0)
throw Err.normal("Wait function argument has to be an integer.");
return Utils.listOf(Device_Wait((int) ticks));
}));
addFunction(new CompilerFunction("dlog", false, Function.fieldsOf(new FunctionCompilerField()), call -> {
Field f1 = getFieldFromCall(call, 0);
ArrayList<Instruction> list = new ArrayList<>();
var numcell1 = call.eqsolver.getNumCell(f1, -1);
boolean isCell1 = numcell1.getFirst() != null;
if(isCell1)
list.addAll(numcell1.getFirst());
list.add(Device_Log(isCell1, numcell1.getSecond()));
return list;
}));
addFunction(new CompilerFunction("setpixel", false, Function.fieldsOf(new FunctionCompilerField(), new FunctionCompilerField()), call -> {
Field f1 = getFieldFromCall(call, 0), f2 = getFieldFromCall(call, 1);
ArrayList<Instruction> list = new ArrayList<>();
var numcell1 = call.eqsolver.getNumCell(f1, call.eqsolver.temp1);
boolean isCell1 = numcell1.getFirst() != null;
var numcell2 = call.eqsolver.getNumCell(f2, call.eqsolver.temp2);
boolean isCell2 = numcell2.getFirst() != null;
if(isCell1 && isCell2 && !f1.isVar() && !f2.isVar() && numcell1.getSecond().intValue() == numcell2.getSecond().intValue())
numcell1 = call.eqsolver.getNumCell(f1, IGB_MA.CHARLIB_TEMP_START + 9);
if(isCell1)
list.addAll(numcell1.getFirst());
if(isCell2)
list.addAll(numcell2.getFirst());
list.add(Pixel(isCell1, numcell1.getSecond().intValue(), isCell2, numcell2.getSecond().intValue()));
return list;
}));
addFunction(new CompilerFunction("pixelcache", false, Function.fieldsOf(new FunctionCompilerField()), call -> {
Field f1 = getFieldFromCall(call, 0);
ArrayList<Instruction> list = new ArrayList<>();
var numcell1 = call.eqsolver.getNumCell(f1, -1);
boolean isCell1 = numcell1.getFirst() != null;
if(isCell1)
list.addAll(numcell1.getFirst());
if(!isCell1)
return Utils.listOf(Pixel_Cache_Raw(numcell1.getSecond().intValue()));
list.add(Pixel_Cache(numcell1.getSecond().intValue()));
return list;
}));
addFunction(new CompilerFunction("pixelcache", false,
Function.fieldsOf(new FunctionCompilerField(), new FunctionCompilerField(), new FunctionCompilerField()), call -> {
ArrayList<Instruction> list = new ArrayList<>();
Field f1 = getFieldFromCall(call, 0), f2 = getFieldFromCall(call, 1), f3 = getFieldFromCall(call, 2);
var numcell1 = call.eqsolver.getNumCell(f1, call.eqsolver.temp1);
boolean isCell1 = numcell1.getFirst() != null;
if(isCell1)
list.addAll(numcell1.getFirst());
var numcell2 = call.eqsolver.getNumCell(f2, call.eqsolver.temp2);
boolean isCell2 = numcell2.getFirst() != null;
if(isCell2)
list.addAll(numcell2.getFirst());
var numcell3 = call.eqsolver.getNumCell(f3, call.eqsolver.temp3);
boolean isCell3 = numcell3.getFirst() != null;
if(isCell3)
list.addAll(numcell3.getFirst());
list.add(Pixel_Cache(isCell1, numcell1.getSecond().intValue(), isCell2, numcell2.getSecond().intValue(), isCell3,
numcell3.getSecond().intValue()));
return list;
}));
addFunction(new CompilerFunction("getpixel", true, Function.fieldsOf(new FunctionCompilerField(), new FunctionCompilerField()), call -> {
Field f1 = getFieldFromCall(call, 0), f2 = getFieldFromCall(call, 1);
ArrayList<Instruction> list = new ArrayList<>();
var numcell1 = call.eqsolver.getNumCell(f1, call.eqsolver.temp1);
boolean isCell1 = numcell1.getFirst() != null;
var numcell2 = call.eqsolver.getNumCell(f2, call.eqsolver.temp2);
boolean isCell2 = numcell2.getFirst() != null;
if(isCell1 && isCell2 && !f1.isVar() && !f2.isVar() && numcell1.getSecond().intValue() == numcell2.getSecond().intValue())
numcell1 = call.eqsolver.getNumCell(f1, IGB_MA.CHARLIB_TEMP_START + 11);
if(isCell1)
list.addAll(numcell1.getFirst());
if(isCell2)
list.addAll(numcell2.getFirst());
list.add(Pixel_Get(isCell1, numcell1.getSecond().intValue(), isCell2, numcell2.getSecond().intValue(),
call.outputCell == -1 ? IGB_MA.FUNC_RETURN : call.outputCell));
return list;
}));
addFunction(new CompilerFunction("getpixel", false, Function.fieldsOf(new FunctionCompilerField(), new FunctionCompilerField(),
new FunctionCompilerField(), new FunctionCompilerField(), new FunctionCompilerField()), call -> {
Field f1 = getFieldFromCall(call, 0), f2 = getFieldFromCall(call, 1), f3 = getFieldFromCall(call, 2), f4 = getFieldFromCall(call, 3),
f5 = getFieldFromCall(call, 4);
Pair<Integer, ArrayList<Instruction>> pair = call.eqsolver.getInstructionsFromField(f3);
ArrayList<Instruction> list = pair.getSecond();
var numcell1 = call.eqsolver.getNumCell(f1, call.eqsolver.temp1);
boolean isCell1 = numcell1.getFirst() != null;
var numcell2 = call.eqsolver.getNumCell(f2, call.eqsolver.temp2);
boolean isCell2 = numcell2.getFirst() != null;
if(isCell1 && isCell2 && !f1.isVar() && !f2.isVar() && numcell1.getSecond().intValue() == numcell2.getSecond().intValue())
numcell1 = call.eqsolver.getNumCell(f1, IGB_MA.CHARLIB_TEMP_START + 11);
if(isCell1)
list.addAll(numcell1.getFirst());
if(isCell2)
list.addAll(numcell2.getFirst());
int cell1 = switch (f3.fieldType) {
case Val -> (int) f3.value;
case Var -> f3.cell;
default -> throw Err.normal("Getpixel rgb arg 3: R output cell has to be an integer or a variable.");
};
int cell2 = switch (f4.fieldType) {
case Val -> (int) f4.value;
case Var -> f4.cell;
default -> throw Err.normal("Getpixel rgb arg 4: R output cell has to be an integer or a variable.");
};
int cell3 = switch (f5.fieldType) {
case Val -> (int) f5.value;
case Var -> f5.cell;
default -> throw Err.normal("Getpixel rgb arg 5: B output cell has to be an integer or a variable.");
};
int outCell = cell1 == cell2 - 1 && cell1 == cell3 - 2 ? cell1 : IGB_MA.CHARLIB_TEMP_START + 15;
list.add(Pixel_Get(isCell1, numcell1.getSecond().intValue(), isCell2, numcell2.getSecond().intValue(), outCell));
if(outCell == IGB_MA.CHARLIB_TEMP_START + 15) {
list.add(Copy(IGB_MA.CHARLIB_TEMP_START + 15, cell1));
list.add(Copy(IGB_MA.CHARLIB_TEMP_START + 16, cell2));
list.add(Copy(IGB_MA.CHARLIB_TEMP_START + 17, cell3));
}
return list;
}));
addFunction(new CompilerFunction("drawstring", true, Function.fieldsOf(new FunctionCompilerField(), new FunctionCompilerField(),
new FunctionStringField(), new FunctionStringField(), new FunctionCompilerField()), call -> {
Field f1 = getFieldFromCall(call, 0), f2 = getFieldFromCall(call, 1), f5 = getFieldFromCall(call, 4);
String str = ((FunctionCallStringField) call.fields[2]).str;
char[] charA = str.toCharArray();
if(charA.length == 0)
return new ArrayList<>();
String name = ((FunctionCallStringField) call.fields[3]).str;
ArrayList<Instruction> list = new ArrayList<>();
var pair1 = call.eqsolver.getInstructionsFromField(f1, IGB_MA.CHARLIB_X);
list.addAll(pair1.getSecond());
var pair2 = call.eqsolver.getInstructionsFromField(f2, IGB_MA.CHARLIB_Y);
list.addAll(pair2.getSecond());
var numcell4 = call.eqsolver.getNumCell(f5, IGB_MA.CHARLIB_SPACING);
boolean isCell4 = numcell4.getFirst() != null;
if(isCell4)
list.addAll(numcell4.getFirst());
list.add(Init(charA[0], IGB_MA.CHARLIB_CHAR));
list.add(Cell_Call(":f_" + name + "drawchar_3_start"));
if(charA.length == 1)
return list;
list.add(Add(IGB_MA.CHARLIB_X, true, IGB_MA.FUNC_RETURN, IGB_MA.CHARLIB_X));
list.add(Add(IGB_MA.CHARLIB_X, isCell4, (int) (double) numcell4.getSecond(), IGB_MA.CHARLIB_X));
for (int i = 1; i < charA.length; i++) {
char c = charA[i];
list.add(Init(c, IGB_MA.CHARLIB_CHAR));
list.add(Cell_Call(":f_" + name + "drawchar_3_start"));
if(i != charA.length - 1) {
list.add(Add(IGB_MA.CHARLIB_X, true, IGB_MA.FUNC_RETURN, IGB_MA.CHARLIB_X));
list.add(Add(IGB_MA.CHARLIB_X, isCell4, (int) (double) numcell4.getSecond(), IGB_MA.CHARLIB_X));
}
}
return list;
}));
addFunction(new CompilerFunction("random", true, Function.fieldsOf(new FunctionCompilerField(), new FunctionCompilerField()), call -> {
Field f1 = getFieldFromCall(call, 0), f2 = getFieldFromCall(call, 1);
if(!f1.isVal() || !f2.isVal())
throw Err.normal("random() arguments have to be integers.");
double min = f1.value;
double max = f2.value;
if(min % 1 != 0 || max % 1 != 0)
throw Err.normal("random() arguments have to be integers.");
final int outputcell = call.outputCell == -1 ? 0 : call.outputCell;
return Utils.listOf(Math_Random((int) min, (int) max, outputcell));
}));
/*
* addFunction(new Function("comment", obj -> { FunctionActionInputString faif =
* (FunctionActionInputString) obj; String comment = faif.str;
* if(comment.length() < 2 || !(comment.charAt(0) ==
* '\"' && comment.charAt(comment.length() - 1) == '\"')) throw new
* IGB_CL2_Exception("Comment has to start and end with \"."); return
* Utils.listOf(Instruction.Comment(faif.str.substring(1, comment.length() -
* 1))); }, -1, false, FunctionActionInput.FunctionActionInputString.class),
* false);
*/
}
@Override
public String toString() {
if(functionMap.size() == 0)
return IGB_CL2.getTabs() + "Functions: {}";
StringBuilder sb = new StringBuilder(IGB_CL2.getTabs() + "Functions: {\n");
IGB_CL2.toStringTabs++;
functionMap.forEach((k, v) -> {
v.forEach((k1, v1) -> {
if(!(v1 instanceof CompilerFunction)) {
sb.append(IGB_CL2.getTabs());
sb.append(v1.toString());
sb.append("\n");
}
});
});
IGB_CL2.toStringTabs--;
sb.append(IGB_CL2.getTabs() + "}\n");
return sb.toString();
}
}