|
1 | 1 | import Parser from "./parse/parse.ts"; |
2 | 2 | import Code from "./code/code.ts"; |
3 | 3 | import SymbolTable from "./symbol_table/symbol_table.ts"; |
4 | | -import * as path from "jsr:@std/path"; |
5 | | - |
6 | | -const prog = Deno.args[0]; |
7 | | -if (!prog) Deno.exit(1); |
8 | | - |
9 | | -const file = await Deno.open( |
10 | | - `${import.meta.dirname}/${prog.replace("asm", "hack")}`, |
11 | | - { |
12 | | - write: true, |
13 | | - create: true, |
14 | | - }, |
15 | | -); |
16 | 4 |
|
17 | 5 | const symbolTable = new SymbolTable(); |
18 | 6 | const code = new Code(); |
19 | 7 |
|
20 | | -let parser = new Parser(path.join(import.meta.dirname!, prog)); |
21 | | -let lInstructions = 0; |
22 | | -for (let i = 0;; i++) { |
23 | | - await parser.advance(); |
24 | | - if (!parser.hasMoreLines) break; |
25 | | - if (!parser.currentInstruction) continue; |
| 8 | +export default class Assembler { |
| 9 | + #firstParser: InstanceType<typeof Parser>; |
| 10 | + #secondParser: InstanceType<typeof Parser>; |
| 11 | + #input: ReadableStream<string>; |
| 12 | + output: TransformStream; |
| 13 | + |
| 14 | + constructor(input: ReadableStream<string>) { |
| 15 | + this.#input = input; |
26 | 16 |
|
27 | | - if (parser.instructionType === "L_INSTRUCTION") { |
28 | | - const symbol = parser.symbol; |
| 17 | + const [firstReader, secondReader] = this.#input.tee(); |
29 | 18 |
|
30 | | - if (symbol) symbolTable.addEntry(symbol, i - lInstructions); |
31 | | - lInstructions++; |
| 19 | + this.#firstParser = new Parser(firstReader.getReader()); |
| 20 | + this.#secondParser = new Parser(secondReader.getReader()); |
| 21 | + |
| 22 | + this.output = new TransformStream(); |
32 | 23 | } |
33 | | -} |
34 | 24 |
|
35 | | -const writer = file.writable.getWriter(); |
36 | | -// Second Pass |
37 | | - |
38 | | -parser = new Parser(path.join(import.meta.dirname!, prog)); |
39 | | -while (true) { |
40 | | - await parser.advance(); |
41 | | - if (!parser.hasMoreLines) break; |
42 | | - if (!parser.currentInstruction) continue; |
43 | | - |
44 | | - let output = undefined; |
45 | | - |
46 | | - if (parser.instructionType === "A_INSTRUCTION") { |
47 | | - if (!parser.symbol) break; |
48 | | - |
49 | | - // Label |
50 | | - let symbolValue; |
51 | | - const parsed = parseInt(parser.symbol); |
52 | | - if (Number.isNaN(parsed)) { |
53 | | - if (symbolTable.contains(parser.symbol)) { |
54 | | - symbolValue = symbolTable.getAddress(parser.symbol); |
55 | | - } else { |
56 | | - symbolTable.addEntry(parser.symbol); |
57 | | - symbolValue = symbolTable.getAddress(parser.symbol); |
58 | | - } |
59 | | - } else { |
60 | | - symbolValue = parsed; |
61 | | - } |
| 25 | + async init() { |
| 26 | + await this.#firstPass(); |
62 | 27 |
|
63 | | - output = symbolValue.toString(2).padStart(16, "0"); |
64 | | - } else if (parser.instructionType === "C_INSTRUCTION") { |
65 | | - const comp = code.comp(parser.comp); |
66 | | - const dest = code.dest(parser.dest); |
67 | | - const jump = code.jump(parser.jump); |
68 | | - output = `111${comp}${dest}${jump}`; |
| 28 | + await this.#secondPass(this.output.writable.getWriter()); |
69 | 29 | } |
70 | 30 |
|
71 | | - if (output) writer.write(new TextEncoder().encode(output + "\n")); |
| 31 | + async #firstPass() { |
| 32 | + let lInstructions = 0; |
| 33 | + for (let i = 0;; i++) { |
| 34 | + await this.#firstParser.advance(); |
| 35 | + if (!this.#firstParser.hasMoreLines) break; |
| 36 | + if (this.#firstParser.instructionType !== "L_INSTRUCTION") continue; |
| 37 | + const symbol = this.#firstParser.symbol; |
| 38 | + |
| 39 | + if (symbol) symbolTable.addEntry(symbol, i - lInstructions); |
| 40 | + lInstructions++; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + async #secondPass(streamWriter: WritableStreamDefaultWriter) { |
| 45 | + while (true) { |
| 46 | + await this.#secondParser.advance(); |
| 47 | + if (!this.#secondParser.hasMoreLines) break; |
| 48 | + if (!this.#secondParser.currentInstruction) continue; |
| 49 | + |
| 50 | + let output = undefined; |
| 51 | + |
| 52 | + if (this.#secondParser.instructionType === "A_INSTRUCTION") { |
| 53 | + if (!this.#secondParser.symbol) break; |
| 54 | + |
| 55 | + // Label |
| 56 | + let symbolValue; |
| 57 | + const parsed = parseInt(this.#secondParser.symbol); |
| 58 | + if (Number.isNaN(parsed)) { |
| 59 | + if (symbolTable.contains(this.#secondParser.symbol)) { |
| 60 | + symbolValue = symbolTable.getAddress(this.#secondParser.symbol); |
| 61 | + } else { |
| 62 | + symbolTable.addEntry(this.#secondParser.symbol); |
| 63 | + symbolValue = symbolTable.getAddress(this.#secondParser.symbol); |
| 64 | + } |
| 65 | + } else { |
| 66 | + symbolValue = parsed; |
| 67 | + } |
| 68 | + |
| 69 | + output = symbolValue.toString(2).padStart(16, "0"); |
| 70 | + } else if (this.#secondParser.instructionType === "C_INSTRUCTION") { |
| 71 | + const comp = code.comp(this.#secondParser.comp); |
| 72 | + const dest = code.dest(this.#secondParser.dest); |
| 73 | + const jump = code.jump(this.#secondParser.jump); |
| 74 | + output = `111${comp}${dest}${jump}`; |
| 75 | + } |
| 76 | + |
| 77 | + if (output) console.log(output); |
| 78 | + if (output) streamWriter.write(new TextEncoder().encode(output + "\n")); |
| 79 | + } |
| 80 | + streamWriter.close(); |
| 81 | + } |
72 | 82 | } |
0 commit comments