22
33import java .util .ArrayList ;
44
5+ import com .devkev .devscript .raw .ProcessUtils .ExitCodes ;
6+
57public class Block { //So stupid...
68
7- public static final byte DONE = 0 ;
8- public static final byte ERROR = 1 ;
9+ private Process host ;
10+ private ArrayList < Variable > variables = new ArrayList <>() ;
911
1012 public final StringBuilder blockCode ;
1113 public int executeIndex = 0 ; //The current char index, that is executed or compiled. 0 < ExecuteIndex < blockCode.length()
@@ -17,22 +19,33 @@ public class Block { //So stupid...
1719 public volatile boolean continued = false ;
1820
1921 public Block parent ;
20- public volatile boolean isFunction = false ; //This flag gets set to true, if a variable containing a block (= function) is made in the command = (function = {#some code#})
21- public Object functionReturn = null ;
2222 private byte stack = 0 ;
2323
24- public boolean loop = false ;
25- ArrayList <Command > cached = new ArrayList <>();
26- // ArrayList<CompiledCommand> compiled = new ArrayList<CompiledCommand>(0);
27- int exitCode = DONE ;
24+ ArrayList <Command > cached = new ArrayList <>(1 );
25+ int exitCode = ExitCodes .DONE ;
26+
27+ //Block flags
28+ //This flag gets set to true, if a variable containing a block (= function) is made in the command = (function = {#some code#}) (Flag: F)
29+ private volatile boolean isFunction = false ;
30+ public Object functionReturn = null ;
31+
32+ //If the block should isolate variables (A bit more complex than that) (Flag: O)
33+ private volatile boolean isConstrucor = false ;
34+
35+ //If true, errors dont cause the script to exit. (=> the block acts like a try/catch block.) (Flag: T)
36+ private volatile boolean isTryCatch = false ;
37+
38+ //True if the block belongs to a loop. This will affect return, continue and break commands. (Flag: L)
39+ private volatile boolean isLoop = false ;
2840
2941 class CompiledCommand {
3042 Command command ;
3143 ArrayList <Object > args ;
3244 }
3345
34- Block (StringBuilder blockCode , Block parent ) {
46+ public Block (StringBuilder blockCode , Block parent , Process host ) {
3547 this .blockCode = blockCode ;
48+ this .host = host ;
3649
3750 if (parent != null ) {
3851 this .parent = parent ;
@@ -41,8 +54,46 @@ class CompiledCommand {
4154 } else this .stack = 0 ;
4255 }
4356
57+ public boolean inheritTryCatch () {
58+ Block current = this ;
59+ while (current .parent != null ) {
60+ if (current .parent == null ) return current .isTryCatch ;
61+ else if (current .parent == host .main ) return current .isTryCatch ;
62+ else {
63+ current = current .parent ;
64+ if (current .isTryCatch ()) return true ;
65+ }
66+ }
67+ return current .isTryCatch ;
68+ }
69+
70+ public boolean isTryCatch () {
71+ return isTryCatch ;
72+ }
73+
74+ public void setAsTryCatch () {
75+ isTryCatch = true ;
76+ }
77+
78+ public boolean isObject () {
79+ return isConstrucor ;
80+ }
81+
82+ public void setAsObject () {
83+ isConstrucor = true ;
84+ }
85+
86+ public boolean isFunction () {
87+ return isFunction ;
88+ }
89+
90+ public void setAsFunction () {
91+ isFunction = true ;
92+ }
93+
4494 public synchronized void interrupt () {
4595 interrupted = true ;
96+ isLoop = false ;
4697 }
4798
4899 public synchronized boolean interrupted () {
@@ -54,28 +105,17 @@ public byte getStack() {
54105 }
55106
56107 public boolean isLoop () {
57- return loop ;
58- }
59-
60- public void beginLoop () {
61- loop = true ;
108+ return isLoop ;
62109 }
63110
64- public void endLoop ( boolean clearcache ) {
65- loop = false ;
111+ public void setAsLoop ( ) {
112+ isLoop = true ;
66113 }
67114
68115 public synchronized boolean isAlive () {
69116 return alive ;
70117 }
71118
72- // public void addCompiledCommand(Command command, ArrayList<Object> args) {
73- // CompiledCommand c = new CompiledCommand();
74- // c.args = args;
75- // c.command = command;
76- // compiled.add(c); //Index would be the "line"
77- // }
78-
79119 void addToCache (Command c ) {
80120 for (int i = 0 ; i < cached .size (); i ++) {
81121 if (c .equals (cached .get (i ))) return ;
@@ -87,7 +127,104 @@ void clearCache() {
87127 cached .clear ();
88128 }
89129
130+ private static void checkForBlockInheritage (Object obj , Block parent ) {
131+ if (obj instanceof Block )
132+ ((Block ) obj ).parent = parent ;
133+ }
134+
135+ /**@return true, if handled, that means a variable with the name was found or a variable was created*/
136+ public boolean setVariable (String name , Object value , boolean FINAL , boolean permanent , boolean allowCreation ) {
137+ if (name .equals ("true" )) return true ;
138+ else if (name .equals ("false" )) return true ;
139+ else if (name .equals ("null" )) return true ;
140+
141+ if (name .contains (" " ) || Alphabet .partOf (name )) {
142+ host .kill (this , "Variable name contains illegal/reserved characters" );
143+ return false ;
144+ }
145+
146+ for (Variable v : variables ) {
147+ if (v .name .equals (name )) {
148+ if (!v .FINAL ) {// || v.permanent) {
149+ if (!v .setValue (value , true )) {
150+ host .kill (this , "Failed to assign type " + value + " to existing variable " + v .value );
151+ checkForBlockInheritage (value , this );
152+ }
153+ }
154+ return true ;
155+ }
156+ }
157+ //If not found, check the parents!
158+ if (parent != null && !isConstrucor ) {
159+ if (parent .setVariable (name , value , FINAL , permanent , false )) {
160+ checkForBlockInheritage (value , this );
161+ return true ;
162+ }
163+ }
164+
165+ if (allowCreation ) {
166+ this .variables .add (new Variable (name , value , FINAL , permanent , this ));
167+ checkForBlockInheritage (value , this );
168+ return true ;
169+ }
170+ return false ;
171+ }
172+
173+ public void clearVariables () {
174+ variables .clear ();
175+ }
176+
177+ public void removeVariable (String name ) {
178+ for (Variable v : variables ) {
179+ if (v .name .equals (name ) && !v .FINAL ) {
180+ variables .remove (v );
181+ return ;
182+ }
183+ }
184+ //Otherwise look for the next stack if the block is not a constructor (aka "isolated")
185+ if (!isConstrucor && parent != null )
186+ parent .removeVariable (name );
187+ }
188+
189+ public ArrayList <Variable > getAccessibleVariables () {
190+ ArrayList <Variable > var = new ArrayList <>();
191+ var .addAll (variables );
192+
193+ if (parent != null )
194+ var .addAll (parent .getAccessibleVariables ());
195+
196+ return var ;
197+ }
198+
199+ public ArrayList <Variable > getLocalVariables () {
200+ return variables ;
201+ }
202+
203+ public Object getVariable (String name ) {
204+ //constants
205+ if (name .equals ("true" )) return Process .True ;
206+ else if (name .equals ("false" )) return Process .False ;
207+ else if (name .equals ("null" )) return Process .Null ;
208+ else if (name .equals ("n" )) return "\n " ;
209+ else if (name .equals ("t" )) return "\t " ;
210+
211+ for (Variable var : variables ) {
212+ if (var .name .equals (name ))
213+ return var .value ;
214+ }
215+ //Otherwise search the parent
216+ if (parent != null )
217+ return parent .getVariable (name );
218+ else return null ;
219+ }
220+
221+ public Process getHost () {
222+ return host ;
223+ }
224+
90225 public String toString () {
91- return "BLOCK:" + stack + "" + (thread != null ? " Thread: " + thread .getName () : " Thread: none" );
226+ return "BLOCK:" + (isFunction ? "F" : "" ) + (isConstrucor ? "O" : "" ) + (isTryCatch ? "T" : "" ) + (isLoop ? "L" : "" ) + " " + stack
227+ + (thread != null ? " Thread: " + thread .getName () : "" );
228+ //+ " CODE [" + blockCode + "]";
92229 }
93230}
0 commit comments