-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
137 lines (118 loc) · 3.17 KB
/
index.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
var fs = require('fs');
var falafel = require('falafel');
var std = require('./lib/std');
var header = require('./lib/header');
var bank = require('./lib/banking');
var palette = require('./lib/palettes');
var sprites = require('./lib/sprites');
var bg = require('./lib/bg.js');
var joy = require('./lib/joy');
var TOKENS = {
'reset': std.reset,
'vec': std.vec,
'vblankwait': std.vblankwait,
'clrmem': std.clrmem,
'header': header,
'bank': bank,
'palette': palette.palette,
'pData': palette.pData,
'addData': sprites.addData,
'loadSprites': sprites.loadSprites,
'sData': sprites.sData,
'setLowHighBytes': sprites.setLowHighBytes,
'moveRight': sprites.moveRight,
'moveLeft': sprites.moveLeft,
'moveUp': sprites.moveUp,
'moveDown': sprites.moveDown,
'enableBg': bg.enableBg,
'loadAttribute': bg.loadAttribute,
'addBg': bg.addBgRow,
'loadBg': bg.loadBg,
'write': bg.write,
'attributeTable': bg.attributeTable,
'loadNametable': bg.loadNametable,
'joyInit': joy.init,
'joyRead': joy.read,
'asm': function (exp) { return exp; }
};
function expressionStatement(node) {
var callee = node.expression.callee;
var args = [];
var name;
var fn;
var o;
var argumentFn = function (arg) {
var value;
if (arg.type === 'ObjectExpression') {
value = {};
arg.properties.forEach(function (p) {
var k = p.key.name;
var v;
if (p.value.type === 'Identifier') {
v = TOKENS[p.value.name];
} else {
v = p.value.value;
}
value[k] = v;
o = value;
});
} else {
if (arg.name) {
if (TOKENS[arg.name]) {
value = TOKENS[arg.name];
}
} else {
value = arg.value;
}
args.push(value);
}
};
if (callee && callee.name) {
name = callee.name;
}
if (name) {
fn = TOKENS[name];
callee.parent.arguments.forEach(argumentFn);
if (fn) {
if (o) {
node.update(fn(o));
delete o;
} else {
node.update(fn(args));
}
}
}
}
function variableDeclarator(node) {
var name = node && node.id && node.id.name;
var value = node && node.init && node.init.value;
var label = 'var_'+name+':\t';
var updatedValue;
switch (value) {
case true:
updatedValue = 1;
break;
case false:
updatedValue = 0;
break;
default:
updatedValue = value;
}
if (node.parent && node.parent.kind === 'var') {
node.parent.update('');
}
node.update(label + '.db\t' + updatedValue);
}
var TYPES = {
'ExpressionStatement': expressionStatement,
'VariableDeclarator': variableDeclarator
};
function compiler(node) {
if (TYPES[node.type]) {
TYPES[node.type](node);
}
}
module.exports.compile = function (file) {
var output = falafel(file, compiler);
return output;
};