Barely functional logic
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package mindustry.logic;
|
||||
|
||||
import arc.struct.*;
|
||||
import arc.util.ArcAnnotate.*;
|
||||
import mindustry.io.*;
|
||||
import mindustry.logic.LCanvas.*;
|
||||
import mindustry.logic.LExecutor.*;
|
||||
@@ -13,8 +12,6 @@ public class LAssembler{
|
||||
ObjectMap<String, BVar> vars = new ObjectMap<>();
|
||||
/** All instructions to be executed. */
|
||||
LInstruction[] instructions;
|
||||
/** Statement UI elements being processed. */
|
||||
@Nullable Seq<StatementElem> elements;
|
||||
|
||||
public LAssembler(){
|
||||
//add default constants
|
||||
@@ -26,35 +23,42 @@ public class LAssembler{
|
||||
public static LAssembler assemble(Seq<StatementElem> seq){
|
||||
LAssembler out = new LAssembler();
|
||||
|
||||
out.elements = seq;
|
||||
seq.each(s -> s.st.saveUI());
|
||||
out.instructions = seq.map(s -> s.st.build(out)).toArray(LInstruction.class);
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
public static String toJson(Seq<StatementElem> seq){
|
||||
seq.each(s -> s.st.saveUI());
|
||||
LStatement[] statements = seq.map(s -> s.st).toArray(LStatement.class);
|
||||
|
||||
return JsonIO.write(statements);
|
||||
}
|
||||
|
||||
//TODO this is awful and confusing
|
||||
public static LAssembler fromJson(String json){
|
||||
LAssembler asm = new LAssembler();
|
||||
|
||||
LStatement[] statements = JsonIO.read(LStatement[].class, json);
|
||||
for(LStatement s : statements){
|
||||
s.afterLoad(asm);
|
||||
s.setupUI();
|
||||
}
|
||||
asm.instructions = Seq.with(statements).map(l -> l.build(asm)).toArray(LInstruction.class);
|
||||
|
||||
return asm;
|
||||
}
|
||||
|
||||
public String toJson(){
|
||||
Seq<LStatement> states = elements.map(s -> s.st);
|
||||
states.each(s -> s.beforeSave(this));
|
||||
return JsonIO.write(states.toArray(LStatement.class));
|
||||
}
|
||||
|
||||
/** @return a variable ID by name.
|
||||
* This may be a constant variable referring to a number or object. */
|
||||
public int var(String symbol){
|
||||
symbol = symbol.trim();
|
||||
|
||||
//string case
|
||||
if(symbol.startsWith("\"") && symbol.endsWith("\"")){
|
||||
return putConst("___" + symbol, symbol.substring(1, symbol.length() - 1)).id;
|
||||
}
|
||||
|
||||
try{
|
||||
double value = Double.parseDouble(symbol);
|
||||
//this creates a hidden const variable with the specified value
|
||||
@@ -66,7 +70,7 @@ public class LAssembler{
|
||||
}
|
||||
|
||||
/** Adds a constant value by name. */
|
||||
private BVar putConst(String name, Object value){
|
||||
BVar putConst(String name, Object value){
|
||||
BVar var = putVar(name);
|
||||
var.constant = true;
|
||||
var.value = value;
|
||||
@@ -74,7 +78,7 @@ public class LAssembler{
|
||||
}
|
||||
|
||||
/** Registers a variable name mapping. */
|
||||
private BVar putVar(String name){
|
||||
BVar putVar(String name){
|
||||
if(vars.containsKey(name)){
|
||||
return vars.get(name);
|
||||
}else{
|
||||
|
||||
Reference in New Issue
Block a user