Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Interesting problem. You are basically building a simple virtual processor of boolean values. So I would build the simulator as more of a processor. So essentially you'll have registers (input, output, or internal registers), then the logic would define your operands. Since you are only dealing with boolean logic it's pretty easy to come up with a set of operands that you need: AND, OR, NOT, XOR. For things like &amp;&amp;, || those operands will have two inputs and one output. But, for NOT you'll have only one input and one output. So I'd create a Class for each operand you want to support. And either an abstract Class or interface all operands extend/implement. That will provide the interface for the client to evaluate each operand in the same manner and execute the program.</p> <p>For example:</p> <pre><code>public class AndOperation implements Operand, Argument { private Argument argument1; private Argument argument2; private String output; public AndOperation( Argument arg1, Argument arg2 ) { this( arg1, arg2, null ); // this is for chaining where no output exists. } public AndOperation( Argument arg1, Argument arg2, String output ) { this.argument1 = arg1; this.argument2 = arg2; this.output = output; } public boolean evaluate() { return argument1.evaluate() &amp;&amp; argument2.evaluate(); } public String getOutputRegister() { return output; } } public interface Argument { public boolean evaluate(); } public class Register implements Argument { private String name; private boolean value; public boolean evaluate() { return value; } public void setValue( boolean value ) { this.value = value; } } public class Program implements Iterable&lt;Operand&gt; { public Map&lt;Register&gt; registers; public List&lt;Operand&gt; operands; public void parse( InputStream stream ) { // this will take in a stream, parse it, and create the // program. Create the registers, and operands used // to evaluate the program } public void evaluate() { for( Operand op : operands ) { evaluate( op ); } } public void evaluate( Operand operand ) { boolean output = op.evaluate(); String name = op.getOutputRegister(); Register register = registers.get( name ); register.setValue( output ); } public Iterator&lt;Operand&gt; iterator() { return new Debugger( this ); } } public class Debugger implements Iterator&lt;Operand&gt; { private Program program; private int line = 0; public boolean hasNext() { return line &lt; program.size(); } public Operand next() { Operand operand = program.getOperands().get( line ); program.evaluate( operand ); line++; return operand; } } </code></pre> <p>That's roughly it. However, one thing I wanted to point out was how chaining multiple operands together can be transparently done. The operand doesn't care if it's reading its input from a register or another operand. Since the Register and Operand implements Argument then it substituted for either. So for example:</p> <pre><code>Operand op = new AndOperand( register1, new OrOperand( register2, register3 ); boolean output = op.evaluate(); // this is register1 &amp;&amp; (register2 || register3 ) </code></pre> <p>Of course the tricky part is going to be parsing it, but that's a little difficult to show in this limited space. As far as graphically representing this you could build something that took this program and evaluated it operand by operand and rendered it to the screen in some way. It's possible to build a debugger off of this without much more effort. Just need a little more information that your parser could create (line number to operand map would be helpful).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload