mirror of
https://github.com/Anuken/Mindustry.git
synced 2025-07-15 18:27:36 +07:00
Merged UnaryOp and BinaryOp
This commit is contained in:
@ -1,46 +0,0 @@
|
|||||||
package mindustry.logic;
|
|
||||||
|
|
||||||
import arc.math.*;
|
|
||||||
|
|
||||||
public enum BinaryOp{
|
|
||||||
add("+", (a, b) -> a + b),
|
|
||||||
sub("-", (a, b) -> a - b),
|
|
||||||
mul("*", (a, b) -> a * b),
|
|
||||||
div("/", (a, b) -> a / b),
|
|
||||||
mod("%", (a, b) -> a % b),
|
|
||||||
equal("==", (a, b) -> Math.abs(a - b) < 0.000001 ? 1 : 0),
|
|
||||||
notEqual("not", (a, b) -> Math.abs(a - b) < 0.000001 ? 0 : 1),
|
|
||||||
lessThan("<", (a, b) -> a < b ? 1 : 0),
|
|
||||||
lessThanEq("<=", (a, b) -> a <= b ? 1 : 0),
|
|
||||||
greaterThan(">", (a, b) -> a > b ? 1 : 0),
|
|
||||||
greaterThanEq(">=", (a, b) -> a >= b ? 1 : 0),
|
|
||||||
pow("^", Math::pow),
|
|
||||||
shl(">>", (a, b) -> (int)a >> (int)b),
|
|
||||||
shr("<<", (a, b) -> (int)a << (int)b),
|
|
||||||
or("or", (a, b) -> (int)a | (int)b),
|
|
||||||
and("and", (a, b) -> (int)a & (int)b),
|
|
||||||
xor("xor", (a, b) -> (int)a ^ (int)b),
|
|
||||||
max("max", Math::max),
|
|
||||||
min("min", Math::min),
|
|
||||||
atan2("atan2", (x, y) -> Mathf.atan2((float)x, (float)y) * Mathf.radDeg),
|
|
||||||
dst("dst", (x, y) -> Mathf.dst((float)x, (float)y));
|
|
||||||
|
|
||||||
public static final BinaryOp[] all = values();
|
|
||||||
|
|
||||||
public final OpLambda function;
|
|
||||||
public final String symbol;
|
|
||||||
|
|
||||||
BinaryOp(String symbol, OpLambda function){
|
|
||||||
this.symbol = symbol;
|
|
||||||
this.function = function;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString(){
|
|
||||||
return symbol;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface OpLambda{
|
|
||||||
double get(double a, double b);
|
|
||||||
}
|
|
||||||
}
|
|
@ -106,6 +106,26 @@ public class LAssembler{
|
|||||||
arr = new String[]{line};
|
arr = new String[]{line};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String type = arr[0];
|
||||||
|
|
||||||
|
//legacy stuff
|
||||||
|
if(type.equals("bop")){
|
||||||
|
arr[0] = "op";
|
||||||
|
|
||||||
|
//field order for bop used to be op a, b, result, but now it's op result a b
|
||||||
|
String res = arr[4];
|
||||||
|
arr[4] = arr[3];
|
||||||
|
arr[3] = arr[2];
|
||||||
|
arr[2] = res;
|
||||||
|
}else if(type.equals("uop")){
|
||||||
|
arr[0] = "op";
|
||||||
|
|
||||||
|
//field order for uop used to be op a, result, but now it's op result a
|
||||||
|
String res = arr[3];
|
||||||
|
arr[3] = arr[2];
|
||||||
|
arr[2] = res;
|
||||||
|
}
|
||||||
|
|
||||||
LStatement st = LogicIO.read(arr);
|
LStatement st = LogicIO.read(arr);
|
||||||
|
|
||||||
if(st != null){
|
if(st != null){
|
||||||
@ -121,6 +141,7 @@ public class LAssembler{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}catch(Exception parseFailed){
|
}catch(Exception parseFailed){
|
||||||
|
parseFailed.printStackTrace();
|
||||||
//when parsing fails, add a dummy invalid statement
|
//when parsing fails, add a dummy invalid statement
|
||||||
statements.add(new InvalidStatement());
|
statements.add(new InvalidStatement());
|
||||||
}
|
}
|
||||||
|
@ -377,40 +377,26 @@ public class LExecutor{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class BinaryOpI implements LInstruction{
|
public static class OpI implements LInstruction{
|
||||||
public BinaryOp op = BinaryOp.add;
|
public LogicOp op = LogicOp.add;
|
||||||
public int a, b, dest;
|
public int a, b, dest;
|
||||||
|
|
||||||
public BinaryOpI(BinaryOp op, int a, int b, int dest){
|
public OpI(LogicOp op, int a, int b, int dest){
|
||||||
this.op = op;
|
this.op = op;
|
||||||
this.a = a;
|
this.a = a;
|
||||||
this.b = b;
|
this.b = b;
|
||||||
this.dest = dest;
|
this.dest = dest;
|
||||||
}
|
}
|
||||||
|
|
||||||
BinaryOpI(){}
|
OpI(){}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run(LExecutor exec){
|
public void run(LExecutor exec){
|
||||||
exec.setnum(dest, op.function.get(exec.num(a), exec.num(b)));
|
if(op.unary){
|
||||||
}
|
exec.setnum(dest, op.function1.get(exec.num(a)));
|
||||||
}
|
}else{
|
||||||
|
exec.setnum(dest, op.function2.get(exec.num(a), exec.num(b)));
|
||||||
public static class UnaryOpI implements LInstruction{
|
}
|
||||||
public UnaryOp op = UnaryOp.negate;
|
|
||||||
public int value, dest;
|
|
||||||
|
|
||||||
public UnaryOpI(UnaryOp op, int value, int dest){
|
|
||||||
this.op = op;
|
|
||||||
this.value = value;
|
|
||||||
this.dest = dest;
|
|
||||||
}
|
|
||||||
|
|
||||||
UnaryOpI(){}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void run(LExecutor exec){
|
|
||||||
exec.setnum(dest, op.function.get(exec.num(value)));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -56,15 +56,23 @@ public class LStatements{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@RegisterStatement("getlink")
|
@RegisterStatement("read")
|
||||||
public static class GetLinkStatement extends LStatement{
|
public static class ReadStatement extends LStatement{
|
||||||
public String output = "result", address = "0";
|
public String output = "result", target = "cell1", address = "0";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void build(Table table){
|
public void build(Table table){
|
||||||
|
table.add(" read ");
|
||||||
|
|
||||||
field(table, output, str -> output = str);
|
field(table, output, str -> output = str);
|
||||||
|
|
||||||
table.add(" = link# ");
|
table.add(" = ");
|
||||||
|
|
||||||
|
fields(table, target, str -> target = str);
|
||||||
|
|
||||||
|
row(table);
|
||||||
|
|
||||||
|
table.add(" at ");
|
||||||
|
|
||||||
field(table, address, str -> address = str);
|
field(table, address, str -> address = str);
|
||||||
}
|
}
|
||||||
@ -76,7 +84,7 @@ public class LStatements{
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public LInstruction build(LAssembler builder){
|
public LInstruction build(LAssembler builder){
|
||||||
return new GetLinkI(builder.var(output), builder.var(address));
|
return new ReadI(builder.var(target), builder.var(address), builder.var(output));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -112,38 +120,6 @@ public class LStatements{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@RegisterStatement("read")
|
|
||||||
public static class ReadStatement extends LStatement{
|
|
||||||
public String output = "result", target = "cell1", address = "0";
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void build(Table table){
|
|
||||||
table.add(" read ");
|
|
||||||
|
|
||||||
field(table, output, str -> output = str);
|
|
||||||
|
|
||||||
table.add(" = ");
|
|
||||||
|
|
||||||
fields(table, target, str -> target = str);
|
|
||||||
|
|
||||||
row(table);
|
|
||||||
|
|
||||||
table.add(" at ");
|
|
||||||
|
|
||||||
field(table, address, str -> address = str);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public LCategory category(){
|
|
||||||
return LCategory.io;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public LInstruction build(LAssembler builder){
|
|
||||||
return new ReadI(builder.var(target), builder.var(address), builder.var(output));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@RegisterStatement("draw")
|
@RegisterStatement("draw")
|
||||||
public static class DrawStatement extends LStatement{
|
public static class DrawStatement extends LStatement{
|
||||||
public GraphicsType type = GraphicsType.clear;
|
public GraphicsType type = GraphicsType.clear;
|
||||||
@ -253,6 +229,26 @@ public class LStatements{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@RegisterStatement("print")
|
||||||
|
public static class PrintStatement extends LStatement{
|
||||||
|
public String value = "\"frog\"";
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void build(Table table){
|
||||||
|
field(table, value, str -> value = str).width(0f).growX().padRight(3);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public LInstruction build(LAssembler builder){
|
||||||
|
return new PrintI(builder.var(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public LCategory category(){
|
||||||
|
return LCategory.io;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@RegisterStatement("drawflush")
|
@RegisterStatement("drawflush")
|
||||||
public static class DrawFlushStatement extends LStatement{
|
public static class DrawFlushStatement extends LStatement{
|
||||||
public String target = "display1";
|
public String target = "display1";
|
||||||
@ -274,26 +270,6 @@ public class LStatements{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@RegisterStatement("print")
|
|
||||||
public static class PrintStatement extends LStatement{
|
|
||||||
public String value = "\"frog\"";
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void build(Table table){
|
|
||||||
field(table, value, str -> value = str).width(0f).growX().padRight(3);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public LInstruction build(LAssembler builder){
|
|
||||||
return new PrintI(builder.var(value));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public LCategory category(){
|
|
||||||
return LCategory.control;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@RegisterStatement("printflush")
|
@RegisterStatement("printflush")
|
||||||
public static class PrintFlushStatement extends LStatement{
|
public static class PrintFlushStatement extends LStatement{
|
||||||
public String target = "message1";
|
public String target = "message1";
|
||||||
@ -315,6 +291,30 @@ public class LStatements{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@RegisterStatement("getlink")
|
||||||
|
public static class GetLinkStatement extends LStatement{
|
||||||
|
public String output = "result", address = "0";
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void build(Table table){
|
||||||
|
field(table, output, str -> output = str);
|
||||||
|
|
||||||
|
table.add(" = link# ");
|
||||||
|
|
||||||
|
field(table, address, str -> address = str);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public LCategory category(){
|
||||||
|
return LCategory.blocks;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public LInstruction build(LAssembler builder){
|
||||||
|
return new GetLinkI(builder.var(output), builder.var(address));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@RegisterStatement("control")
|
@RegisterStatement("control")
|
||||||
public static class ControlStatement extends LStatement{
|
public static class ControlStatement extends LStatement{
|
||||||
public LAccess type = LAccess.enabled;
|
public LAccess type = LAccess.enabled;
|
||||||
@ -561,62 +561,51 @@ public class LStatements{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@RegisterStatement("bop")
|
@RegisterStatement("op")
|
||||||
public static class BinaryOpStatement extends LStatement{
|
public static class OperationStatement extends LStatement{
|
||||||
public BinaryOp op = BinaryOp.add;
|
public LogicOp op = LogicOp.add;
|
||||||
public String a = "a", b = "b", dest = "result";
|
public String dest = "result", a = "a", b = "b";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void build(Table table){
|
public void build(Table table){
|
||||||
|
rebuild(table);
|
||||||
|
}
|
||||||
|
|
||||||
|
void rebuild(Table table){
|
||||||
|
table.clearChildren();
|
||||||
|
|
||||||
field(table, dest, str -> dest = str);
|
field(table, dest, str -> dest = str);
|
||||||
|
|
||||||
table.add(" = ");
|
table.add(" = ");
|
||||||
|
|
||||||
row(table);
|
if(op.unary){
|
||||||
|
opButton(table);
|
||||||
|
|
||||||
field(table, a, str -> a = str);
|
field(table, a, str -> a = str);
|
||||||
|
}else{
|
||||||
|
row(table);
|
||||||
|
|
||||||
|
field(table, a, str -> a = str);
|
||||||
|
|
||||||
|
opButton(table);
|
||||||
|
|
||||||
|
field(table, b, str -> b = str);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void opButton(Table table){
|
||||||
table.button(b -> {
|
table.button(b -> {
|
||||||
b.label(() -> op.symbol);
|
b.label(() -> op.symbol);
|
||||||
b.clicked(() -> showSelect(b, BinaryOp.all, op, o -> op = o));
|
b.clicked(() -> showSelect(b, LogicOp.all, op, o -> {
|
||||||
|
op = o;
|
||||||
|
rebuild(table);
|
||||||
|
}));
|
||||||
}, Styles.logict, () -> {}).size(60f, 40f).pad(4f).color(table.color);
|
}, Styles.logict, () -> {}).size(60f, 40f).pad(4f).color(table.color);
|
||||||
|
|
||||||
field(table, b, str -> b = str);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public LInstruction build(LAssembler builder){
|
public LInstruction build(LAssembler builder){
|
||||||
return new BinaryOpI(op,builder.var(a), builder.var(b), builder.var(dest));
|
return new OpI(op,builder.var(a), builder.var(b), builder.var(dest));
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public LCategory category(){
|
|
||||||
return LCategory.operations;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@RegisterStatement("uop")
|
|
||||||
public static class UnaryOpStatement extends LStatement{
|
|
||||||
public UnaryOp op = UnaryOp.negate;
|
|
||||||
public String value = "b", dest = "result";
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void build(Table table){
|
|
||||||
field(table, dest, str -> dest = str);
|
|
||||||
|
|
||||||
table.add(" = ");
|
|
||||||
|
|
||||||
table.button(b -> {
|
|
||||||
b.label(() -> op.symbol);
|
|
||||||
b.clicked(() -> showSelect(b, UnaryOp.all, op, o -> op = o));
|
|
||||||
}, Styles.logict, () -> {}).size(50f, 40f).pad(3f).color(table.color);
|
|
||||||
|
|
||||||
field(table, value, str -> value = str);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public LInstruction build(LAssembler builder){
|
|
||||||
return new UnaryOpI(op, builder.var(value), builder.var(dest));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -61,7 +61,12 @@ public class LogicDialog extends BaseDialog{
|
|||||||
for(Prov<LStatement> prov : LogicIO.allStatements){
|
for(Prov<LStatement> prov : LogicIO.allStatements){
|
||||||
LStatement example = prov.get();
|
LStatement example = prov.get();
|
||||||
if(example instanceof InvalidStatement) continue;
|
if(example instanceof InvalidStatement) continue;
|
||||||
t.button(example.name(), Styles.cleart, () -> {
|
|
||||||
|
TextButtonStyle style = new TextButtonStyle(Styles.cleart);
|
||||||
|
style.fontColor = example.category().color;
|
||||||
|
style.font = Fonts.outline;
|
||||||
|
|
||||||
|
t.button(example.name(), style, () -> {
|
||||||
canvas.add(prov.get());
|
canvas.add(prov.get());
|
||||||
dialog.hide();
|
dialog.hide();
|
||||||
}).size(140f, 50f);
|
}).size(140f, 50f);
|
||||||
|
76
core/src/mindustry/logic/LogicOp.java
Normal file
76
core/src/mindustry/logic/LogicOp.java
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
package mindustry.logic;
|
||||||
|
|
||||||
|
import arc.math.*;
|
||||||
|
|
||||||
|
public enum LogicOp{
|
||||||
|
add("+", (a, b) -> a + b),
|
||||||
|
sub("-", (a, b) -> a - b),
|
||||||
|
mul("*", (a, b) -> a * b),
|
||||||
|
div("/", (a, b) -> a / b),
|
||||||
|
mod("%", (a, b) -> a % b),
|
||||||
|
equal("==", (a, b) -> Math.abs(a - b) < 0.000001 ? 1 : 0),
|
||||||
|
notEqual("not", (a, b) -> Math.abs(a - b) < 0.000001 ? 0 : 1),
|
||||||
|
lessThan("<", (a, b) -> a < b ? 1 : 0),
|
||||||
|
lessThanEq("<=", (a, b) -> a <= b ? 1 : 0),
|
||||||
|
greaterThan(">", (a, b) -> a > b ? 1 : 0),
|
||||||
|
greaterThanEq(">=", (a, b) -> a >= b ? 1 : 0),
|
||||||
|
pow("^", Math::pow),
|
||||||
|
shl(">>", (a, b) -> (int)a >> (int)b),
|
||||||
|
shr("<<", (a, b) -> (int)a << (int)b),
|
||||||
|
or("or", (a, b) -> (int)a | (int)b),
|
||||||
|
and("and", (a, b) -> (int)a & (int)b),
|
||||||
|
xor("xor", (a, b) -> (int)a ^ (int)b),
|
||||||
|
max("max", Math::max),
|
||||||
|
min("min", Math::min),
|
||||||
|
atan2("atan2", (x, y) -> Mathf.atan2((float)x, (float)y) * Mathf.radDeg),
|
||||||
|
dst("dst", (x, y) -> Mathf.dst((float)x, (float)y)),
|
||||||
|
|
||||||
|
negate("-", a -> -a),
|
||||||
|
not("not", a -> ~(int)(a)),
|
||||||
|
abs("abs", Math::abs),
|
||||||
|
log("log", Math::log),
|
||||||
|
log10("log10", Math::log10),
|
||||||
|
sin("sin", d -> Math.sin(d * 0.017453292519943295D)),
|
||||||
|
cos("cos", d -> Math.cos(d * 0.017453292519943295D)),
|
||||||
|
tan("tan", d -> Math.tan(d * 0.017453292519943295D)),
|
||||||
|
floor("floor", Math::floor),
|
||||||
|
ceil("ceil", Math::ceil),
|
||||||
|
sqrt("sqrt", Math::sqrt),
|
||||||
|
rand("rand", d -> Mathf.rand.nextDouble() * d),
|
||||||
|
|
||||||
|
;
|
||||||
|
|
||||||
|
public static final LogicOp[] all = values();
|
||||||
|
|
||||||
|
public final OpLambda2 function2;
|
||||||
|
public final OpLambda1 function1;
|
||||||
|
public final boolean unary;
|
||||||
|
public final String symbol;
|
||||||
|
|
||||||
|
LogicOp(String symbol, OpLambda2 function){
|
||||||
|
this.symbol = symbol;
|
||||||
|
this.function2 = function;
|
||||||
|
this.function1 = null;
|
||||||
|
this.unary = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
LogicOp(String symbol, OpLambda1 function){
|
||||||
|
this.symbol = symbol;
|
||||||
|
this.function1 = function;
|
||||||
|
this.function2 = null;
|
||||||
|
this.unary = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString(){
|
||||||
|
return symbol;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface OpLambda2{
|
||||||
|
double get(double a, double b);
|
||||||
|
}
|
||||||
|
|
||||||
|
interface OpLambda1{
|
||||||
|
double get(double a);
|
||||||
|
}
|
||||||
|
}
|
@ -1,38 +0,0 @@
|
|||||||
package mindustry.logic;
|
|
||||||
|
|
||||||
import arc.math.*;
|
|
||||||
|
|
||||||
public enum UnaryOp{
|
|
||||||
negate("-", a -> -a),
|
|
||||||
not("not", a -> ~(int)(a)),
|
|
||||||
abs("abs", Math::abs),
|
|
||||||
log("log", Math::log),
|
|
||||||
log10("log10", Math::log10),
|
|
||||||
sin("sin", d -> Math.sin(d * 0.017453292519943295D)),
|
|
||||||
cos("cos", d -> Math.cos(d * 0.017453292519943295D)),
|
|
||||||
tan("tan", d -> Math.tan(d * 0.017453292519943295D)),
|
|
||||||
floor("floor", Math::floor),
|
|
||||||
ceil("ceil", Math::ceil),
|
|
||||||
sqrt("sqrt", Math::sqrt),
|
|
||||||
rand("rand", d -> Mathf.rand.nextDouble() * d),
|
|
||||||
;
|
|
||||||
|
|
||||||
public static final UnaryOp[] all = values();
|
|
||||||
|
|
||||||
public final UnaryOpLambda function;
|
|
||||||
public final String symbol;
|
|
||||||
|
|
||||||
UnaryOp(String symbol, UnaryOpLambda function){
|
|
||||||
this.symbol = symbol;
|
|
||||||
this.function = function;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString(){
|
|
||||||
return symbol;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface UnaryOpLambda{
|
|
||||||
double get(double a);
|
|
||||||
}
|
|
||||||
}
|
|
Reference in New Issue
Block a user