added input/output display

This commit is contained in:
overflowerror 2021-06-03 21:26:22 +02:00
parent 22396b5d69
commit 1ae631fcc8
3 changed files with 116 additions and 1 deletions

View file

@ -1,5 +1,5 @@
public class Number { public class Number {
private class NumberWrapper { private static class NumberWrapper {
Number n; Number n;
} }
@ -26,6 +26,11 @@ public class Number {
public void executeIfEquals(Number n, Action a) { public void executeIfEquals(Number n, Action a) {
n.executeIfZero(a); n.executeIfZero(a);
} }
@Override
public void executeIfNotEquals(Number n, Action a) {
n.executeIfNotZero(a);
}
} }
static class NegativeNumber extends Number { static class NegativeNumber extends Number {
@ -87,6 +92,17 @@ public class Number {
}); });
} }
@Override
public void executeIfNotEquals(Number n, Action a) {
n.executeIfNegative(() -> {
positive.executeIfNotEquals(((NegativeNumber) n).positive, a);
});
n.executeIfNotNegative(() -> {
a.execute();
});
}
@Override @Override
public void forEach(Action a) { public void forEach(Action a) {
positive.forEach(a); positive.forEach(a);
@ -145,6 +161,66 @@ public class Number {
return w.n; return w.n;
} }
public static Number max(Number n1, Number n2) {
NumberWrapper w = new NumberWrapper();
n1.executeIfNegative(() -> {
n2.executeIfNegative(() -> {
w.n = Number.max(((NegativeNumber) n2).positive, ((NegativeNumber) n1).positive);
});
n2.executeIfNotNegative(() -> {
w.n = n2;
});
});
n1.executeIfNotNegative(() -> {
n2.executeIfNegative(() -> {
w.n = n1;
});
n2.executeIfNotNegative(() -> {
n1.executeIfZero(() -> {
n2.executeIfZero(() -> {
w.n = zero();
});
n2.executeIfNotZero(() -> {
w.n = n2;
});
});
n1.executeIfNotZero(() -> {
n2.executeIfZero(() -> {
w.n = n1;
});
n2.executeIfNotZero(() -> {
Number tmp = Number.max(n1.pre, n2.pre);
tmp.executeIfEquals(n1.pre, () -> {
w.n = n1;
});
tmp.executeIfEquals(n2.pre, () -> {
w.n = n2;
});
});
});
});
});
return w.n;
}
public static Number min(Number n1, Number n2) {
Number max = max(n1, n2);
NumberWrapper w = new NumberWrapper();
n1.executeIfEquals(max, () -> {
w.n = n2;
});
n2.executeIfEquals(max, () -> {
w.n = n1;
});
return w.n;
}
public void executeIfZero(Action action) { public void executeIfZero(Action action) {
} }
@ -164,6 +240,14 @@ public class Number {
}); });
} }
public void executeIfNotEquals(Number n, Action a) {
n.executeIfNotNegative(() -> {
n.executeIfNotZero(() -> {
pre.executeIfNotEquals(n.pre, a);
});
});
}
public void executeIfNegative(Action a) { public void executeIfNegative(Action a) {
} }

View file

@ -4,15 +4,23 @@ public class Tape {
char c; char c;
} }
private class StringWrapper {
String s;
}
@FunctionalInterface @FunctionalInterface
private interface Cell { private interface Cell {
char value(Number i); char value(Number i);
} }
private Cell cells; private Cell cells;
private Number min;
private Number max;
public Tape() { public Tape() {
cells = (i) -> ' '; cells = (i) -> ' ';
min = Number.zero();
max = Number.zero();
} }
public char get(Number i) { public char get(Number i) {
@ -23,6 +31,9 @@ public class Tape {
final Cell _cells = cells; final Cell _cells = cells;
min = Number.min(min, i);
max = Number.max(max, i);
cells = (j) -> { cells = (j) -> {
CharWrapper w = new CharWrapper(); CharWrapper w = new CharWrapper();
@ -35,4 +46,21 @@ public class Tape {
return w.c; return w.c;
}; };
} }
private String cellString(Number min, Number max) {
StringWrapper w = new StringWrapper();
w.s = "";
w.s = String.valueOf(get(min));
min.executeIfNotEquals(max, () -> {
w.s += cellString(min.succ(), max);
});
return w.s;
}
public String toString() {
return min + " | " + cellString(min, max) + " | " + max;
}
} }

View file

@ -7,6 +7,7 @@ public class TuringMachine {
public TuringMachine() { public TuringMachine() {
tape = new Tape(); tape = new Tape();
sm = new StateMachine(() -> { sm = new StateMachine(() -> {
System.out.println(tape);
System.out.println("halted"); System.out.println("halted");
System.exit(0); System.exit(0);
}); });
@ -29,6 +30,8 @@ public class TuringMachine {
tape.set(n, '1'); tape.set(n, '1');
n = n.succ(); n = n.succ();
tape.set(n, '0'); tape.set(n, '0');
System.out.println(tape);
} }
public void step() { public void step() {