-
Notifications
You must be signed in to change notification settings - Fork 0
/
vm.cpp
87 lines (63 loc) · 1.41 KB
/
vm.cpp
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include<bits/stdc++.h>
#include "intialize.h"
#include "parser.h"
#include "translator.h"
using namespace std;
// command type
enum c_type{
C_ARITHMETIC = 0,
C_PUSH = 1,
C_POP = 2,
C_LABLE = 3,
C_GOTO = 4,
c_IF = 5,
C_FUNCTION = 6,
C_RETURN = 7,
C_CALL = 8,
};
int main(int argc, char **argv) {
string in_fname = argv[1];
string out_fname = "";
ifstream fin; // input file stream
ofstream fout; // output file stream
fin.open(in_fname);
int ptr = 0;
// create output file name
while(in_fname[ptr] != '.'){
out_fname += in_fname[ptr++];
}
out_fname += ".asm";
fout.open(out_fname);
register_entry(fout); // intialize basic register entry in RAM
string line;
while(fin) {
getline(fin,line); // read line from input file
// remove comment and blank line
if(line[0] == '/' or (int)line[0] == 13){
continue;
}
else{
vector<string> cmds;
cmd_parser(line,cmds);
if(cmds[0] == "push" or cmds[0] == "pop") {
string arg1 = cmds[1];
string arg2 = cmds[2];
writePushPop(cmds[0],arg1,arg2,fout);
}
else if(cmds[0] == "add"
or cmds[0]=="sub"
or cmds[0]=="eq"
or cmds[0]=="or"
or cmds[0]=="not"
or cmds[0]=="gt"
or cmds[0]=="lt"
or cmds[0]=="and"
or cmds[0]=="neg"
) {
writeArithmetic(cmds[0],fout);
}
}
}
fin.close();
fout.close();
}