-
Notifications
You must be signed in to change notification settings - Fork 0
/
vm_writer.cpp
63 lines (48 loc) · 1.12 KB
/
vm_writer.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
#include "vm_writer.h"
using namespace std;
VMWriter::VMWriter(path p)
{
// initialize the VM Writer module
p.replace_extension( ".vm" );
m_out.open( p.c_str() );
}
void VMWriter::writePush(SEGMENT seg, int index)
{
m_out << "push " << segment_to_string(seg) << " " << index << endl;
}
void VMWriter::writePop(SEGMENT seg, int index)
{
m_out << "pop " << segment_to_string(seg) << " " << index << endl;
}
void VMWriter::writeArithmetic(COMMAND cmd)
{
m_out << command_to_string(cmd) << endl;
}
void VMWriter::writeLabel(string label, int counter)
{
m_out << "label " << label << counter << endl;
}
void VMWriter::writeGoto(string label, int counter)
{
m_out << "goto " << label << counter << endl;
}
void VMWriter::writeIf(string label, int counter)
{
m_out << "if-goto " << label << counter << endl;
}
void VMWriter::writeCall(string name, int nArgs)
{
m_out << "call " << name << " " << nArgs << endl;
}
void VMWriter::writeFunction(string name, int nArgs)
{
m_out << "function " << name << " " << nArgs << endl;
}
void VMWriter::writeReturn()
{
m_out << "return" << endl;
}
void VMWriter::close()
{
m_out.close();
}