Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7723341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:25:08+00:00 2026-06-01T04:25:08+00:00

I’m writting a PLC language interpreter using C#. That PLC language contains over 20

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T04:25:09+00:00Added an answer on June 1, 2026 at 4:25 am

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am doing a simple coin flipping experiment for class that involves flipping a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.