📄 plxinterpret.cs
字号:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using PLX;
namespace PLX
{
class PLXInterpret
{
private int[] stack = new int[500];
private int p, b,t; //program register , base register, topstack register
private string strOutPut;
PLXAnalyzer.Instruction instruction; //instruction register
public string StrOutPut
{
get
{
return strOutPut;
}
}
PLXAnalyzer plxanalyzer = new PLXAnalyzer();
public PLXInterpret() { }
public int Base(int level)
{
int bl = b;
while (level > 0)
{
bl = stack[bl];
level--;
}
return bl;
}
public void interpret(PLXAnalyzer pa)
{
//Initial
t = 0;
p = 0;
b = 1;
//stack begin with 1!!!
stack[1] = 0; //SL
stack[2] = 0; //DL
stack[3] = 0; //RA
try
{
do
{
instruction = pa.code[p++];
switch (instruction.fct)
{
case PLXAnalyzer.fct.lit: //push const address into stacktop(now address is a const num)
stack[++t] = instruction.address;
break;
case PLXAnalyzer.fct.opr:
HandleOprInstruction(instruction);
break;
case PLXAnalyzer.fct.lod:
stack[++t] = stack[Base(instruction.level) + instruction.address]; //push value of var into stacktop
break;
case PLXAnalyzer.fct.loa: //load arrayitem
t++;
stack[t] = stack[Base(instruction.level) + instruction.address + stack[t - 1]];
stack[t - 1] = stack[t - 2]; //array to array assignment patch
break;
case PLXAnalyzer.fct.sto:
stack[Base(instruction.level) + instruction.address] = stack[t--];
break;
case PLXAnalyzer.fct.sta: //store arrayitem
stack[Base(instruction.level) + instruction.address + stack[t - 1]] = stack[t];
t--;
break;
case PLXAnalyzer.fct.cal:
stack[t + 1] = Base(instruction.level);
stack[t + 2] = b;
stack[t + 3] = p;
b = t + 1;
p = instruction.address;
break;
case PLXAnalyzer.fct.ini:
t = t + instruction.address; //now address is a num
break;
case PLXAnalyzer.fct.jmp:
p = instruction.address;
break;
case PLXAnalyzer.fct.jpc:
if (stack[t] == 0)
p = instruction.address;
t--;
break;
}
} while (p > 0);
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}
protected void HandleOprInstruction(PLXAnalyzer.Instruction i)
{
switch (i.address) //now address is a num which decides specified opr
{
//return allocate prog
case 0:
t = b - 1;
p = stack[t + 3];
b = stack[t + 2];
break;
// reverse
case 1:
stack[t] = -stack[t];
break;
//plus
case 2:
t--;
stack[t] = stack[t] + stack[t + 1];
break;
//minus
case 3:
t--;
stack[t] = stack[t] - stack[t + 1];
break;
//times
case 4:
t--;
stack[t] = stack[t] * stack[t + 1];
break;
//slash
case 5:
t--;
stack[t] = stack[t] / stack[t + 1];
break;
//mod
case 6:
t--;
stack[t] = stack[t] % stack[t + 1];
break;
//Is eql
case 7:
t--;
stack[t] = Convert.ToInt16(stack[t]==stack[t + 1]);
break;
//Is not eql
case 8:
t--;
stack[t] = Convert.ToInt16(stack[t] != stack[t + 1]);
break;
//Is gtr
case 9:
t--;
stack[t] = Convert.ToInt16(stack[t] > stack[t + 1]);
break;
//Is gtreql
case 10:
t--;
stack[t] = Convert.ToInt16(stack[t] >= stack[t + 1]);
break;
//Is less
case 11:
t--;
stack[t] = Convert.ToInt16(stack[t] < stack[t + 1]);
break;
//Is lsseql
case 12:
t--;
stack[t] = Convert.ToInt16(stack[t] <= stack[t + 1]);
break;
//not
case 13:
stack[t] = Convert.ToInt16(!Convert.ToBoolean(stack[t]));
break;
//output
case 14:
strOutPut += stack[t--].ToString() + "\n";
break;
default:
break;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -