-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathRoutineBlock.java
More file actions
112 lines (92 loc) Β· 3.45 KB
/
Copy pathRoutineBlock.java
File metadata and controls
112 lines (92 loc) Β· 3.45 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
package RoutineBlocks;
import Commands.Variable;
import Driver.Interpreter;
import References.GenesisKeyWords;
import UI.UIManager;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.HashMap;
public class RoutineBlock
{
private final String[] code;
private final int startLineNumber;
private final ArrayList<String> localVariables; // Stores the names of the local variables
private HashMap<String, Object> variablesSnapshotForRecursion;
private RoutineBlock blockSnapshotForRecursion;
public RoutineBlock(String[] code, int startLineNumber)
{
this.code = code;
this.startLineNumber = startLineNumber;
localVariables = new ArrayList<>();
checkForInvalidCode();
}
public void initiateCode()
{
boolean inRecursion = false;
if(Interpreter.runningRoutineBlock != null)
{
createVariablesSnapshot(Interpreter.runningRoutineBlock.localVariables); // Save last code block variables
cleanLocalVariables(Interpreter.runningRoutineBlock.localVariables); // Delete last code block variables from access
blockSnapshotForRecursion = Interpreter.runningRoutineBlock; // Save last code block pointer
inRecursion = true;
}
Interpreter.runningRoutineBlock = this;
for(int i = 0; i < this.code.length; i++)
{
Interpreter.initiateLine(code[i], startLineNumber + i);
}
cleanLocalVariables(this.localVariables); // Deleting the local variable from the "globalVariables" hashMap
localVariables.clear();
if(!inRecursion) Interpreter.runningRoutineBlock = null;
else
{
loadVariablesSnapshot(); // Reload the saved variables of the last code block
Interpreter.runningRoutineBlock = blockSnapshotForRecursion; // Reset the pointer to the code block
}
}
public void reportLocalVariable(String varName)
{
localVariables.add(varName);
}
private void cleanLocalVariables(@NotNull ArrayList<String> list)
{
for(String varName: list)
{
Variable.globalVariables.remove(varName);
}
}
private void checkForInvalidCode()
{
for(int i = 0; i < this.code.length; i++)
{
if(this.code[i].startsWith(GenesisKeyWords.FUNCTION.getGenesisCode() + " ")) UIManager.consoleInstance.printErrorMessage("π€π€π€
π€ π€π€π€π€π€π€ π€π€
π€π€π€π€π€ π€π€π€
π€ π€π€
π€π€π€π€π€ - π€π€
π€π€: " + startLineNumber + i, startLineNumber + i);
}
}
private void createVariablesSnapshot(@NotNull ArrayList<String> localVariablesList)
{
variablesSnapshotForRecursion = new HashMap<>(); // Also reset previous values
for(String varName: localVariablesList)
{
variablesSnapshotForRecursion.put(varName, Variable.globalVariables.get(varName));
}
}
private void loadVariablesSnapshot()
{
for(String varName: variablesSnapshotForRecursion.keySet())
{
Variable.globalVariables.put(varName, variablesSnapshotForRecursion.get(varName));
}
}
public String[] getCode()
{
return code;
}
public int getStartLineNumber()
{
return startLineNumber;
}
public RoutineBlock duplicate()
{
return new RoutineBlock(this.code, this.startLineNumber);
}
}