More logic

This commit is contained in:
Anuken
2020-08-07 16:11:02 -04:00
parent 5040eacda7
commit a12211587c
25 changed files with 816 additions and 966 deletions

View File

@@ -0,0 +1,49 @@
package mindustry.logic;
import arc.struct.*;
/** "Compiles" a sequence of statements into instructions. */
public class LBuilder{
private int lastVar;
/** Maps names to variable IDs. */
private ObjectIntMap<String> vars = new ObjectIntMap<>();
/** Maps variable IDs to their constant value. */
private IntMap<Object> constants = new IntMap<>();
public LBuilder(){
//add default constant variables
putConst("false", 0);
putConst("true", 1);
}
/** @return a variable ID by name.
* This may be a constant variable referring to a number or object. */
public int var(String symbol){
try{
double value = Double.parseDouble(symbol);
//this creates a hidden const variable with the specified value
String key = "___" + value;
return putConst(key, value);
}catch(NumberFormatException e){
return putVar(symbol);
}
}
/** Adds a constant value by name. */
private int putConst(String name, double value){
int id = putVar(name);
constants.put(id, value);
return id;
}
/** Registers a variable name mapping. */
private int putVar(String name){
if(vars.containsKey(name)){
return vars.get(name);
}else{
int id = lastVar++;
vars.put(name, id);
return id;
}
}
}