Pregunta

I'm writting a PLC language interpreter using C#. That PLC language contains over 20 data types and 25 instructions or so. As soon as I started to generate code I balance two differents ways to write instructions:

1) Every kind of instruction is represented in one class which contains a big switch in order to chose the data type. Example:

public class ADD : Instruction
{
    private string type;

    public ADD(string type)
    {
        this.type = type;
    }

    public bool Exec(Context c)
    {
        switch (type)
        {
            case "INT":
                short valor2 = c.PopINT();
                short valor = c.PopINT();
                short result = (short)(valor + valor2);
                c.PushINT(result);
                break;
            case "DINT":
                int valor4 = c.PopDINT();
                int valor3 = c.PopDINT();
                int result2 = (int)(valor4 + valor3);
                c.PushDINT(result2);
                break;
            case "BOOL":
                // Implement BOOL
                break;
            // Implement other types...
            default:
                break;
        }

        c.IP++;
        return false; ;
    }

}

2) Each class represent a single instruction with a single data type. This way avoid the big switch. Example:

public class ADDi : Instruction
{
    public bool Exec(Context c)
    {
        short valor = c.PopINT();
        short valor2 = c.PopINT();
        short result = (short)(valor + valor2);
        c.PushINT(result);
        c.IP++;
        return false;
    }
}

I'm using COMMAND desing pattern (Exec()) to write instructions. I think second choice is better because avoids the big switch, but that choice involves to write over 400 instructions.

Always keep in mind that in this case execution performance is more important than performance in translation.

So, my precise question is as follows: Is there any other way to factorize instructions and data types? I'm looking for writing the lesser amount of instructions without penalizing performance.

EDIT:

This picture shows my type hierarchy:

Type hierarchy

This is INT class implementation:

public class INT : ANY_INT
{

    public override string DefaultInitValue()
    {
        return "0";
    }

    public override int GetBytes()
    {
        return 2;
    }

    public override string GetLastType()
    {
        return this.ToString();
    }

    public override string ToString()
    {
        return "INT";
    }

}

Some classes are more complex (structs, arrays,...).

Operations Push and Pop are defined as follows:

public void PushINT(short value)
{
    //SP -> Stack Pointer
    resMem.WriteINT(SP, value);
    SP += 2;
}

public short PopINT()
{
    SP -= 2;
    short value = resMem.ReadINT(SP);
    return value;
}

And, finally, operations to read and write in memory.

public void WriteINT(int index, short entero)
{
    SetCapacity(index + 2); // Memory grows up dinamically
    memory[index] = (sbyte)((ushort)entero >> 8 & 0x00FF);
    memory[index + 1] = (sbyte)((ushort)entero >> 0 & 0x00FF);
}

public short ReadINT(int index)
{            
    return (short)(((short)(memory[index]) << 8 & 0xFF00) |
       ((short)(memory[index + 1]) & 0x00FF));
}

I hope this info helps. Thank you.

¿Fue útil?

Solución

If you can change the implementation of Context to support generic types (e.g., Pop<int> instead of PopINT()) you can use delegates to make the implementation simpler.

Addition:

var addInt = new MathInstruction<int>((a, b) => a + b));
var addDouble = new MathInstruction<double>((a, b) => a + b));
var addDecimal = new MathInstruction<decimal>((a, b) => a + b));

Subtraction:

var subtractInt = new MathInstruction<int>((a, b) => a - b));
var subtractDouble = new MathInstruction<double>((a, b) => a - b));
var subtractDecimal = new MathInstruction<decimal>((a, b) => a - b));

Division:

var divideIntAsDouble = new MathInstruction<int, double>((a, b) => a / b));
var divideDouble = new MathInstruction<double>((a, b) => a / b));
var divideDecimal = new MathInstruction<decimal>((a, b) => a / b));

And conversion between types:

var addIntAndDouble = new MathInstruction<int, double, double>((a, b) => a + b));

It would be implemented like this:

class MathInstruction<TA, TB, TResult> : Instruction
{
    private Func<TA, TB, TResult> callback;

    public MathInstruction(Func<TA, TB, TResult> callback) 
    {
        this.callback = callback;
    }

    public bool Exec(Context c)
    {
        var a = c.Pop<TA>();
        var b = c.Pop<TB>();
        var result = callback(a, b);
        c.Push<TResult>(result);
        return false;
    }
}

// Convenience
class MathInstruction<T, TResult> : MathInstruction<T, T, TResult>
class MathInstruction<T> : MathInstruction<T, T, T>

I'm imagining that your context simply has a Stack<object> and PopINT, PopBOOL etc. just pop the argument and cast. In that case you can probably just use:

public T Pop<T>()
{
    var o = stack.Pop();
    return Convert.ChangeType(o, typeof(T));
} 

public void Push<T>(T item)
{
    stack.Push(item);
} 

Note this could also handle your logical operators - for example:

var logicalAnd = new MathInstruction<bool>((a, b) => a && b);
var logicalOr = new MathInstruction<bool>((a, b) => a || b);

Otros consejos

Could you use inheritance ? I would see a clever combination of inheritance concerning the datatypes, and then a strategy pattern to delegate the execution to the appropriate objects.

But then we really would need to see a class diagramm to help you out.

Just remember to program to an interface, not a type, and also, composition is more powerful than inheritance. I hope this can help you out.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top