c# - 谁能给我指出解释堆栈机的 C# 代码吗?

标签 c# compiler-construction interpreter

我正在寻找堆栈机的 C# 实现,最好是带有随附的单元测试或至少有几个示例。代码为http://en.wikipedia.org/wiki/P-code_machine似乎正是我正在寻找的东西。不幸的是,自从我用 Pascal 编程以来已经十多年了,并且在将其移植到 C# 时遇到了一堆问题。此外,没有使用该代码的示例。

无论如何,在这方面的任何帮助将不胜感激......

最佳答案

解释的堆栈机在概念上与Reverse Polish notation非常相似。 .

表达式

3 + (6 - 2)

用RPN表示为

3 6 2 - +

评估如下:

Input   Operation    Stack      Comment

3       Push value   3

6       Push value   6
                     3

2       Push value   2
                     6
                     3

-       Subtract     4          Pop two values (6, 2) and push result (4)
                     3

+       Add          7          Pop two values (3, 4) and push result (7)

From there, it should be easy to build a simple interpreted stack machine in C#. E.g.

var stack = new Stack<int>();

var program = new[] 
{ 
    OpCode.Ldc_3,
    OpCode.Ldc_6,
    OpCode.Ldc_2,
    OpCode.Sub,
    OpCode.Add,
};

for (int i = 0; i < program.Length; i++)
{
    int a, b;
    switch (program[i])
    {
    case OpCode.Add: b = stack.Pop(); a = stack.Pop(); stack.Push(a + b); break;
    case OpCode.Sub: b = stack.Pop(); a = stack.Pop(); stack.Push(a - b); break;
    case OpCode.Mul: b = stack.Pop(); a = stack.Pop(); stack.Push(a * b); break;
    case OpCode.Div: b = stack.Pop(); a = stack.Pop(); stack.Push(a / b); break;
    case OpCode.Ldc_0: stack.Push(0); break;
    case OpCode.Ldc_1: stack.Push(1); break;
    case OpCode.Ldc_2: stack.Push(2); break;
    case OpCode.Ldc_3: stack.Push(3); break;
    case OpCode.Ldc_4: stack.Push(4); break;
    case OpCode.Ldc_5: stack.Push(5); break;
    case OpCode.Ldc_6: stack.Push(6); break;
    case OpCode.Ldc_7: stack.Push(7); break;
    case OpCode.Ldc_8: stack.Push(8); break;
    }
}

var result = stack.Pop();

enum OpCode
{
    Nop,    // No operation is performed.
    Add,    // Adds two values and pushes the result onto the evaluation stack.
    Sub,    // Subtracts one value from another and pushes the result onto the
            // evaluation stack.
    Mul,    // Multiplies two values and pushes the result on the evaluation
            // stack.
    Div,    // Divides two values and pushes the result onto the evaluation
            // stack.
    Ldc_0,  // Pushes the integer value of 0 onto the evaluation stack.
    Ldc_1,  // Pushes the integer value of 1 onto the evaluation stack.
    Ldc_2,  // Pushes the integer value of 2 onto the evaluation stack.
    Ldc_3,  // Pushes the integer value of 3 onto the evaluation stack.
    Ldc_4,  // Pushes the integer value of 4 onto the evaluation stack.
    Ldc_5,  // Pushes the integer value of 5 onto the evaluation stack.
    Ldc_6,  // Pushes the integer value of 6 onto the evaluation stack.
    Ldc_7,  // Pushes the integer value of 7 onto the evaluation stack.
    Ldc_8,  // Pushes the integer value of 8 onto the evaluation stack.
}

有关现实世界的示例,请查看 fields of the OpCodes class在 .NET Framework 中。

关于c# - 谁能给我指出解释堆栈机的 C# 代码吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3866336/

相关文章:

c++ - 编译器内存屏障和互斥量

c# - 有人知道用 C# 编写的简单 C 解释器吗?

c# - foreach 中的多项选择到 LINQ

c# - 如何获取传递给属性构造函数的参数?

c - Objective-C 预处理器可用吗?

java - 为什么 Java 在连续整数上的开关似乎在增加的情况下运行得更快?

c# - 使用 System.Drawing 勾勒文本?

c# - 如何获取outlook联系人的头像?

compiler-construction - 适用于 Linux 的具有读取-评估-打印循环的快速标准 ML 编译器或字节码解释器?

haskell - 我可以在运行时从字符串编译haskell函数(使用插件)吗?