pvm.c

来自「压缩包里面的都是精致的基本C语言小程序」· C语言 代码 · 共 96 行

C
96
字号
#include <stdio.h>#include "../lib/error.h"#include "../lib/hash.h"#include "../lib/nat.h"#include "../lib/mystdlib.h"#include "store.h"#include "pvm.h"nat pvmOperand (store st, operand o){  switch (o->kind)  {    case PID :     {      nat n = storeLookup (st, (o->u).id);      return n;    }    case PNUM :     {      return (newNat ((o->u).i));    }    default: return (newNat (0));  }}void pvmInstruction (store st, instruction i){  switch (i->kind)  {    case MOVL:      {        nat n1 = pvmOperand (st, i->src);        storeUpdate (st, i->dst, n1);        break;      }    case ADDL:      {        nat n1 = pvmOperand (st, i->src);        nat n2 = storeLookup (st, i->dst);        nat n = natAdd (n1, n2);        storeUpdate (st, i->dst, n);        break;      }    case SUBL:      {        nat n1 = pvmOperand (st, i->src);        nat n2 = storeLookup (st, i->dst);        nat n = natSub (n2, n1);        storeUpdate (st, i->dst, n);        break;      }    case MULL:      {        nat n1 = pvmOperand (st, i->src);        nat n2 = storeLookup (st, i->dst);        nat n = natTimes (n1, n2);        storeUpdate (st, i->dst, n);        break;      }    case PPRINT:      {                nat n = pvmOperand (st, i->src);        natOutput (n);        printf ("\n");        break;      }    case COMMENT:      {                //printf ("DO NOTHING FOR COMMENT\n");        break;      }    default:      {        error ("no this case\n");        break;      }  }}void pvmProg (pentiumProg p){  printf ("\nPVM starts:\n");  linkedList l = linkedListGetFirst (p->instructions);  store st = newStore ();        while (l)  {    pvmInstruction (st, l->data);    l = l->next;  }    printf ("\nPVM halts\n");  return;}

⌨️ 快捷键说明

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