-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnodes.js
160 lines (160 loc) · 5.86 KB
/
nodes.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var _Operator = /** @class */ (function () {
function _Operator(type, left, right) {
this.type = type;
this.left = left;
this.right = right;
}
return _Operator;
}());
function nodeToString(N) {
console.log(N);
if (N.type === "error")
return errorToString(N);
else if (N.type === "program")
return programToString(N);
else if (N.type === "basic")
return basicToString(N);
else if (N.type === "basicline")
return basicLineToString(N);
else if (N.type === "string")
return stringToString(N);
else if (N.type === "integer")
return integerToString(N);
else if (N.type === "()")
return roundParensToString(N);
else if (N.type === "[]")
return squareParensToString(N);
else if (N.type === "id")
return idToString(N);
else if (N.type === "||")
return orToString(N);
else if (N.type === "&&")
return andToString(N);
else if (N.type === "|")
return bitwiseOrToString(N);
else if (N.type === "&")
return bitwiseAndToString(N);
else if (N.type === "^")
return xorToString(N);
else if (N.type === "<<")
return shiftleftToString(N);
else if (N.type === ">>")
return shiftrightToString(N);
else if (N.type === "+")
return plusToString(N);
else if (N.type === "-")
return minusToString(N);
else if (N.type === "!")
return logicalNotToString(N);
else if (N.type === "~")
return bitwiseNotToString(N);
else if (N.type === "*")
return multiplicationToString(N);
else if (N.type === "/")
return divisionToString(N);
else if (N.type === "%" || N.type === "MOD")
return modToString(N);
else if (N.type === "unaryplus")
return unaryplusToString(N);
else if (N.type === "unaryminus")
return unaryminusToString(N);
else if (N.type === "instruction")
return instructionToString(N);
else if (N.type === "immediate")
return immediateToString(N);
else if (N.type === "byte")
return byteToString(N);
else if (N.type === "dim")
return dimToString(N);
else if (N.type === "const")
return constToString(N);
/*
else if(N.type === "bitmap") return bitmapToString(N);
else if(N.type === "sprite") return spriteToString(N);
else if(N.type === "expr" ) return exprToString(N);
else if(N.type === "number") return numberToString(N);
*/
else
throw "node type not recognized: " + N.type;
}
exports.nodeToString = nodeToString;
function errorToString(N) {
return N.error.message;
}
function programToString(N) {
return N.items.map(function (e) { return nodeToString(e); }).join("\n");
}
function basicToString(N) {
var lines = N.lines.map(function (e) { return nodeToString(e); });
return lines.join("\n");
}
function basicLineToString(N) {
throw "not implemented";
}
// ========== expressions
function stringToString(N) {
return "\"" + N.str + "\"";
}
function integerToString(N) {
return String(N.num);
}
function roundParensToString(N) {
return "(" + nodeToString(N.expr) + ")"; // TODO use cross
}
function squareParensToString(N) {
return "[" + nodeToString(N.expr) + "]"; // TODO use cross
}
function idToString(N) {
console.log(N);
// simple identifier
if (N.args === undefined)
return N.id;
// function call
var args = N.args.map(function (e) { return nodeToString(e); }).join(",");
return N.id + "(" + args + ")";
}
function orToString(N) { return nodeToString(N.left) + " || " + nodeToString(N.right) + " "; }
function andToString(N) { return nodeToString(N.left) + " && " + nodeToString(N.right) + " "; }
function bitwiseOrToString(N) { return nodeToString(N.left) + " | " + nodeToString(N.right) + " "; }
function bitwiseAndToString(N) { return nodeToString(N.left) + " & " + nodeToString(N.right) + " "; }
function xorToString(N) { return nodeToString(N.left) + " ^ " + nodeToString(N.right) + " "; }
function shiftleftToString(N) { return nodeToString(N.left) + " << " + nodeToString(N.right) + " "; }
function shiftrightToString(N) { return nodeToString(N.left) + " >> " + nodeToString(N.right) + " "; }
function plusToString(N) { return nodeToString(N.left) + " + " + nodeToString(N.right) + " "; }
function minusToString(N) { return nodeToString(N.left) + " - " + nodeToString(N.right) + " "; }
function multiplicationToString(N) { return nodeToString(N.left) + " * " + nodeToString(N.right) + " "; }
function divisionToString(N) { return nodeToString(N.left) + " / " + nodeToString(N.right) + " "; }
function modToString(N) { return nodeToString(N.left) + " % " + nodeToString(N.right) + " "; }
function unaryplusToString(N) { return "+" + nodeToString(N.expr); }
function unaryminusToString(N) { return "-" + nodeToString(N.expr); }
function logicalNotToString(N) { return "!" + nodeToString(N.expr); }
function bitwiseNotToString(N) { return "~" + nodeToString(N.expr); }
// ======================
function instructionToString(N) {
var args = N.args.map(function (e) { return nodeToString(e); }).join(",");
return N.opcode.opcode + " " + args;
}
function immediateToString(N) {
return "#" + nodeToString(N.expr);
}
function byteToString(N) {
var list = N.list.map(function (e) { return nodeToString(e); });
return N.bytetype + " " + list;
}
function dimToString(N) {
if (N.spec.absolute !== undefined) {
return N.id + " = " + nodeToString(N.spec.absolute);
}
if (N.spec.zeropage) {
throw "not implemented";
}
var zero = { type: "integer", num: 0 };
var init = N.spec.init === undefined ? zero : N.spec.init;
return N.id + " " + N.size + " " + nodeToString(init);
}
function constToString(N) {
return N.id + " = " + nodeToString(N.value);
}
// Cond