⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pcodevm.c

📁 简介:PL0语言是pascal语言的一个子集。编译VC工程之前
💻 C
字号:
#include <stdio.h>
#include "define.h"

#define gendo(a, b, c) if(-1 == gen(a, b, c)) return -1
#define ss 800

instruction code[CODEMAXCOUNT];    

int gen(int x, int y, int z) 
{
    if (cx > CODEMAXCOUNT) 
    {
        printf("program too long\n");
        return -1;
    }    
    code[cx].f = x;
    code[cx].l = y;
    code[cx].a = z;
	cx++;
    return 1;
}


void listcode() 
{
    int i;
    char name[][5]=
    {
        {"lit"}, 
        {"opr"}, 
        {"lod"}, 
        {"sto"}, 
        {"cal"}, 
        {"int"}, 
        {"jmp"}, 
        {"jpc"},
        {"lodx"}, 
        {"stox"},
		{"pop"}
    };
    char buf[100];
    for (i=0; i<cx; i++) 
    {
        sprintf(buf, "%d:\t%s %d,%d\n", i, name[code[i].f], code[i].l, code[i].a);
        printf(buf);
    }
}

int base(int l, int* s, int b) 
{
    int i;
    i = b;
    while (l > 0) 
    {
       i=s[i];
       l--;
    }
    return i;
}

void interpret() 
{
    int p, b, t;
    instruction i;
    int s[ss];
	int temp=0;
    printf("start pl0\n");
    t=1; b=1; p=0;	
    s[1]=0; s[2]=0; s[3]=0;
    do {
        i=code[p++];
        switch (i.f) 
        {
        case LIT: 
            t=t+1; s[t]=i.a; break;
		case LOD: 
            t=t+1; s[t]=s[base(i.l, s, b)+i.a]; break;
        case STO: 
            s[base(i.l, s, b)+i.a]=s[t]; t=t-1; break;
        case JMP: 
            p=i.a; break;
        case JPC: 
            if (s[t]==0) p=i.a; t=t-1; break;
        case LODX:	/*load a element from array*/
			s[t]=s[base(i.l, s, b)+i.a+s[t]]; break;																		
        case STOX:	/*store value to array element*/
            s[base(i.l, s, b)+s[t-1]]=s[t]; t=t-2;	break;
		case CAL:
            s[t+1]=base(i.l, s, b); s[t+2]=b; s[t+3]=p;temp=t+3; b=t+1; p=i.a; break;
        case INI: 
            t=t+i.a; break;
		case POP:
			t-=i.a; break;
        case OPR: 
            switch(i.a) 
            {
                case 0:
                    t=b-1; p=s[t+3]; b=s[t+2]; break;
                case 1: 
                    s[t]=-s[t]; break; 
                case 2: 
                    t=t-1; s[t]=s[t] + s[t+1]; break;
                case 3:
                    t=t-1; s[t]=s[t] - s[t+1]; break;
                case 4: 
                    t=t-1; s[t]=s[t] * s[t+1]; break;
                case 5: 
                    t=t-1; s[t]=s[t] / s[t+1]; break;
                case 6: 
                    s[t]=(s[t] % 2 == 1); break;
                case 8: 
                    t=t-1; s[t]=(s[t] == s[t+1]); break;
                case 9:
                    t=t-1; s[t]=(s[t] != s[t+1]); break;
                case 10:
                    t=t-1; s[t]=(s[t]<s[t+1]); break;
                case 11: 
                    t=t-1; s[t]=(s[t]>=s[t+1]); break;
                case 12: 
                    t=t-1; s[t]=(s[t]>s[t+1]); break;
                case 13: 
                    t=t-1; s[t]=(s[t]<=s[t+1]); break;
                case 14: 
                    printf(" %d", s[t]); t=t-1; break;
                case 15: 
                    printf("\n"); break;
                case 16: 
                    t=t+1; printf(" >> "); scanf("%d", &s[t]); break;
            }
            break;
        } 
    }while (p!=0);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -