-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterpreter.hpp
More file actions
71 lines (63 loc) · 1.76 KB
/
Interpreter.hpp
File metadata and controls
71 lines (63 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#pragma once
#include "Compiler.hpp"
#include <unordered_map>
#include <string>
#define stacksize 5000 // largest number of the elements in stack while running
#define STACK_MAX 999
//define memory region
#define STRING_CONSTANT_BASE 1000
#define STRING_RUNTIME_BASE 2000
#define STRING_HEAP_MAX 4999
enum Fct
{
LIT,
OPR,
LOD,
STO,
CAL,
INT,
JMP,
JPC,
WRT,
RED,
HLT,
LDA,
LDI,
STI
};
class Instruction
{
public:
Fct f; // Function code
int l; // Level
int a; // Address or value
Instruction(Fct function = LIT, int level = 0, int address = 0)
: f(function), l(level), a(address) {}
};
class Interpreter
{
private:
int base(int l, int *s, int b); // Base address calculation
/* data */
public:
// static const int STRING_BASE = 1000; // starting address for string storage
// static int sptr; // next free address in string storage
// static std::unordered_map<int, std::string> stringHeap; // string storage
int sptr_const;
int sptr_runtime;
int s[stacksize]; //虚拟机内存空间
Interpreter():cx(0), sptr_const(STRING_CONSTANT_BASE), sptr_runtime(STRING_RUNTIME_BASE) {}
void genString(const std::string &s); // generate string literal
int cx = 0; // Instruction pointer
Instruction code[cxmax]; // Code array
void gen(Fct x, int y, int z); // Generate code
void listCode(int cx0); // List code for debugging
void interpret(); // Start interpretation
void setCode(int addr, Fct f, int l, int a)
{ // Set code at specific address
if (addr >= 0 && addr < cxmax)
{
code[addr] = Instruction(f, l, a);
}
}
};