-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogram-code-writer.js
53 lines (49 loc) · 2.24 KB
/
program-code-writer.js
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
import ArithmeticCodeWriter from "./airthmetic-code-writer.js";
import { CommandType } from "./command.js";
import MemoryCodeWriter from "./memory-code-writer.js";
import CodeWriter from './code-writer.js';
import ControlFlowCodeWriter from "./control-flow-code-writer.js";
export default class ProgramCodeWriter extends CodeWriter {
set currentFileName(name) {
super.currentFileName = name;
this._arithmeticCodeWriter.currentFileName = name;
this._memoryCodeWriter.currentFileName = name;
this._controlFlowCodeWriter.currentFileName = name;
}
constructor(fileHandle) {
super(fileHandle);
this._inputFileName = '';
this._fileHandle = fileHandle;
this._labelIndex = 0;
this._arithmeticCodeWriter = new ArithmeticCodeWriter(fileHandle);
this._memoryCodeWriter = new MemoryCodeWriter(fileHandle);
this._controlFlowCodeWriter = new ControlFlowCodeWriter(fileHandle);
this._inFunction = true;
}
async writeBootstrap() {
await this.writeLine('@256 // Initialize SP to 256');
await this.writeLine('D=A');
await this.writeLine('@SP');
await this.writeLine('M=D');
await this._controlFlowCodeWriter.writeCallSysInit();
}
async write(command) {
if (command.type == CommandType.ARITHMETIC) {
await this._arithmeticCodeWriter.write(command);
} else if (command.type == CommandType.PUSH || command.type == CommandType.POP) {
await this._memoryCodeWriter.write(command);
} else if (command.type == CommandType.FUNCTION) {
await this._controlFlowCodeWriter.write(command);
} else if (command.type == CommandType.RETURN) {
await this._controlFlowCodeWriter.write(command);
} else if (command.type == CommandType.CALL) {
await this._controlFlowCodeWriter.write(command);
} else if (command.type == CommandType.LABEL) {
await this._controlFlowCodeWriter.write(command);
} else if (command.type == CommandType.GOTO) {
await this._controlFlowCodeWriter.write(command);
} else if (command.type == CommandType.IF) {
await this._controlFlowCodeWriter.write(command);
}
}
}