我作编译原理课程设计时写的一个图形化的小型开发环境
源代码在线查看: vm.h
// VM.H: Header file for VM.CPP
#include "stack.h" // Stack template class
#include "mystring.h" // String class
// The opcodes
enum Opcode {
OP_NOP, // 空操作
OP_PUSH, // string [var]入栈
OP_GETTOP, // get string from top of stack (=assign) [var]
OP_DISCARD, // 丢弃栈顶的值
OP_PRINT, // 打印字符串
OP_INPUT, // 输入一个字符串 [var]
OP_JMP, // 无条件转移[dest]
OP_JMPF, // 为假则跳转 [dest]
OP_STR_EQUAL, // 测试两个字符串是否相同
OP_BOOL_EQUAL, // 测试两个不值是否相等
OP_CONCAT, // 连接两个字符串
OP_BOOL2STR, // convert bool to string
JUMPTARGET // not an opcode but a jump target;
// the target field points to the jump instruction
};
// Instruction class
class Instr {
public:
Instr () {opcode = OP_NOP; operand = 0;}
Instr (Opcode _opcode) {opcode = _opcode; operand = 0;}
Instr (Opcode _opcode, int _operand) {opcode = _opcode; operand = _operand;}
Opcode opcode; // the opcode
int operand; // string number or target instruction
};
// Some defines and constants
typedef Stack VM_Stack; // < 0 : boolean
// >= 0 : index to string table
const int ST_TRUE = -1, // stack codes for true/false
ST_FALSE = -2;
const int MAX_STR = 100; // 内存中最多能容纳的字符串数目(包括临时变量)
// The virtual machine class
class VMachine {
public:
VMachine ();
~VMachine ();
void Read ();
void Execute ();
void Reset ();
private:
// functions to make temp.copies of strings
int NewTempCopy ();
int NewTempCopy (int j);
int NewTempCopy (char *s);
void DelTempCopy (int i);
Instr *instr; // the instructions
int ninstr; // 指令的编号
String *str[MAX_STR]; // the string table (NULL=entry not used)
VM_Stack stack; // the stack
};