forked from emkay/nesly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocess.js
46 lines (40 loc) · 1005 Bytes
/
preprocess.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
/**
* nesasm and nesly-assemboler
* don't handle global defines (yet)
*
* This function replaces all occurences of variables with the address.
* e.g.
* `MYVARIABLE = $2004
*
* lda #1
* sta MYVARIABLE
*`
* becomes:
* `
* lda #1
* sta $2004
* `
*/
function preprocess (file) {
tokens = {}
var fileLines = file.split('\n')
var onlyLinesWithDefineStatements = fileLines.filter(
function (line) { return line.match('=') }
)
// populate the tokens dict
onlyLinesWithDefineStatements.map(buildTokens)
function buildTokens(line) {
var splitLine = line.split('=');
tokens[splitLine[0].trim()] = splitLine[1].trim()
}
var intermediate = fileLines.filter(function (line) {
return onlyLinesWithDefineStatements.indexOf(line) < 0;
}).join('\n')
var output = intermediate
Object.keys(tokens).map(function (token) {
console.log(token)
output = output.replace(new RegExp(token, 'g'), tokens[token])
})
return output;
}
module.exports = preprocess