Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Rather than having your interpreter extract and operate on the values directly, perhaps it would be better to take a different approach and define operations as methods like this:</p> <pre><code>abstract class Var { ... public Var add(Var var); // corresponds to var + otherVar; in script public Var subtract(Var var); // corresponds to var - otherVar; in script public Var unarySubtract(); // corresponds to -var; in script ... } </code></pre> <p>This way you can evaluate expressions in the script by calling these methods, and your classes can override and overload like this for example:</p> <pre><code>public class IntVar extends Var { ... public IntVar add(IntVar var) { return new IntVar(value + var.value); // add another int } public FloatVar add(FloatVar var) { return new FloatVar(value + var.value); // add another float (and cast result to float? up to you whether you want to do this...) } public StringVar add(StringVar var) { return new StringVar("" + value + var.value); // add a string, and cast to string (for string concatenation) } public Var add(Var var) { throw new OperationNotSupportedException(); // no other types can be added to an IntValue so throw an exception } public Var subtract(Var var) {...} public IntVar unarySubtract() { return new IntVar(-value); } ... } </code></pre> <p>This way you can implement and evaluate operations on them however you want without ever dealing with the values directly in the interpreter.</p> <p>Another bonus is that using this method, if a user tries to run a bad script (tries to add a BoolVar to an IntVar for example) then an exception will be thrown in the interpreter.</p> <p>Perhaps you could even have the default implementation of operations in your abstract Var class just be this:</p> <pre><code>public Var add(Var var) { throw new OperationNotSupportedException(); // no other types can be added to an IntValue so throw an exception } </code></pre> <p>And only override/overload with safe uses of the operations.</p> <p>When you say 'read the content' here, presumably the content of an instance of a type will need to be dealt with in a uniform manner at some point at least, if this is just for output, then why not just have an abstract 'toString()' method? If for some other purpose than output, then what purpose is it that you could not just have some other toX() method and deal with each type instance in the same way?</p> <p>I realise this doesn't answer your question directly and rather suggests a different approach in the hopes of avoiding the issue entirely, which may be completely unhelpful since I may have made some silly assumptions about what you are trying to achieve w/o having seen more of the context of the problem, but I hope it's helpful nonetheless.</p> <p>If this is not helpful then please provide some more information and I will try to help a little more, because as it is I cannot recommend continuing with the current approach, but am unsure what else to suggest other than this.</p> <p>Edit: Wait, my bad, I didn't read the question properly. This is not an interpreter, but a generator. What sort of generator? A generator of scripts of what language? Well, those may not be important issues, but now that I have reread the question I realise I don't really understand much about what you need to achieve.</p> <p>I still think encapsulation is a better choice than the visitor pattern, in general though :). Unless of course there is some reason that behaviour will need to be added which can operate on any variable in such a way that the behaviour cannot be constructed from the behaviours already attached to each variable through the interface provided by Var... What behaviour might this be? For output, you will surely need each variable to be readable in a format common to all of them anyway, why not just toString(), toSomething(), toAnotherThing() etc.</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. This table or related slice is empty.
    1. This table or related slice is empty.
    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