-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
54 lines (35 loc) · 1.23 KB
/
Copy pathmain.js
File metadata and controls
54 lines (35 loc) · 1.23 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
import fs from 'node:fs/promises';
import path from 'node:path';
import Parser from './parser.js';
import ProgramCodeWriter from './program-code-writer.js';
let fileOrFolder = process.argv[2];
let stat = await fs.stat(fileOrFolder);
let filePaths = [];
let outputFileName = '';
if (stat.isDirectory()) {
let dir = await fs.opendir(fileOrFolder);
for await (const dirent of dir) {
if (dirent.isFile()) {
filePaths.push(path.join(dirent.parentPath, dirent.name));
}
}
outputFileName = fileOrFolder + '.asm';
} else {
filePaths.push(fileOrFolder);
let parsedPath = path.parse(filePaths[0]);
outputFileName = parsedPath.name + '.asm';
}
let outputFileHandle = await fs.open(outputFileName, 'w');
let programCodeWriter = new ProgramCodeWriter(outputFileHandle);
await programCodeWriter.writeBootstrap();
for (let filePath of filePaths) {
let fileHandle = await fs.open(filePath, 'r');
let parser = new Parser(fileHandle);
await parser.parse();
let parsedPath = path.parse(filePath);
programCodeWriter.currentFileName = parsedPath.name;
for (let command of parser.commands) {
await programCodeWriter.write(command);
}
}
await outputFileHandle.close();