-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranspile.js
217 lines (210 loc) · 7.88 KB
/
transpile.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
const Tokens = require('./tokens');
const SyntaxTree = require('./syntax-tree');
// IMPORTS:
const fs = require('fs');
const path = require('path');
//console.log(require('./helpers/types'));
const Logger = require('./helpers/logger');
const logger = new Logger();
function Transpile(tree, functions = {}, vars = {}, classes = {}, exported = [], path = '') {
this.FUNCTIONS = functions;
this.VARIABLES = vars;
this.CLASSES = classes;
this.PATH = path;
this.EXPORTED = [];
this.js = "";
tree.forEach(element => this.js += this.toJS(element));
return this.js;
}
Transpile.prototype = {
toJS: function(element) {
switch(element.type) {
case "import": {
let code;
let p = element.path.replace(/ /g, '');
if (p == 'common' || p == 'browser') {
// Include the common library.
logger.info('Importing ' + p + ' library');
let lib = __dirname + '/lib/' + p + '/_index.saur';
let common = fs.readFileSync(lib, 'utf8');
code = new Transpile(new SyntaxTree(new Tokens(common)), this.FUNCTIONS, this.VARS, this.CLASSES, this.EXPORTED, path.dirname(lib));
} else {
if (!p.includes('.saur'))
p += '.saur';
p = path.join(this.PATH, p);
logger.info("Importing " + p);
let file = fs.readFileSync(p, 'utf8');
code = new Transpile(new SyntaxTree(new Tokens(file)), this.FUNCTIONS, this.VARS, this.CLASSES, this.EXPORTED, path.dirname(p));
}
return `${code.js}${code.EXPORTED.map(e => e.js).join(';')}`;
}
case "exported": {
let code = new Transpile([element.value], this.FUNCTIONS, this.VARS, this.CLASSES, this.EXPORTED);
this.EXPORTED.push(code);
return '';
}
case "function": {
// SaurScript is lazy, so don't add the function to the code, but save it for use.
let args = {};
element.args.forEach(arg => {
args[arg.name] = arg;
});
let fun = new Transpile(element.block, this.FUNCTIONS, Object.assign({}, args, this.VARIABLES), this.CLASSES);
this.FUNCTIONS[element.name] = {
args: element.args,
returnType: element.returnType,
code: fun.js
};
return "";
}
case "call": {
let fun = this.FUNCTIONS[element.function];
// FIX
if (fun != null) {
let args = [];
element.args.forEach((arg, i) => {
if (arg[0] == null)
arg = [arg]
if (fun.args[i].type != "Any" && arg[0].type != "reference" && arg[0].type != fun.args[i].type) {
this.error(`Argument #${i} of ${element.function} expects '${fun.args[i].type}' not ${arg[0].type}`);
} else {
args.push(`${fun.args[i].name} = ${(new Transpile(arg, this.FUNCTIONS, this.VARIABLES, this.CLASSES)).js}`);
}
});
return `(function(${args.join(',')}) {${fun.code}})()`;
} else if (element.function.includes('.')) {
let args = [];
element.args.forEach(arg => {
if (arg[0] != null)
arg = arg[0];
let code = new Transpile([arg], this.FUNCTIONS, this.VARIABLES, this.CLASSES);
args.push(code.js);
});
return `${element.function}(${args.join(',')})`
} else {
this.error(`The function '${element.function}' doesn't exist`);
}
}
case "class": {
let props = [];
element.properties.forEach(prop => {
let val = new Transpile(prop[0].value, this.FUNCTIONS, this.VARIABLES, this.CLASSES);
props.push(prop[0].name + " = " + (val.js || 'null'));
});
let initVals = "";
element.properties.forEach(prop => {
initVals += `this.${prop[0].name} = ${prop[0].name};`;
});
let code = new Transpile(element.block, {}, this.VARIABLES, this.CLASSES);
this.CLASSES[element.name] = element;
let funs = [];
for (let i in code.FUNCTIONS) {
let fun = code.FUNCTIONS[i];
funs.push(`${i}: function (${fun.args.map(arg => arg.name).join(',')}) { ${fun.code} }`)
}
return `const ${element.name} = function (${props.join(',')}) { ${initVals} ${code.js} }; ${element.name}.prototype = { ${funs.join(',')} };`;
}
case "classInstance": {
if (this.CLASSES[element.className] == null) {
this.error(`A class with the name '${element.className}' doesn't exist`);
}
let props = [];
element.properties.forEach((prop, i) => {
if (prop.type == null) {
prop = prop[0];
}
if (prop.type != this.CLASSES[element.className].properties[i][0].valueType && prop.type != "reference")
this.error(`Argument #${i} of ${element.className} expects '${this.CLASSES[element.className].properties[i][0].valueType}' not ${prop.type}`);
let val = new Transpile([prop], this.FUNCTIONS, this.VARIABLES, this.CLASSES);
props.push(val.js);
});
this.VARIABLES[element.name] = element;
this.warn("Classes are experimental. You may experience runtime errors.");
return `const ${element.name} = new ${element.className}(${props.join(',')});`;
}
case "array": {
let vals = [];
element.value.forEach(val => {
let js = new Transpile([val], this.FUNCTIONS, this.VARIABLES, this.CLASSES);
vals.push(js.js);
});
this.warn("Array literals are experimental. You may experience runtime errors.");
return `[${vals.join(',')}]`;
}
case "dictionary": {
let dict = [];
for (let pair in element.value) {
let key = new Transpile(element.value[pair].key, this.FUNCTIONS, this.VARIABLES, this.CLASSES);
let value = new Transpile(element.value[pair].value, this.FUNCTIONS, this.VARIABLES, this.CLASSES);
dict.push(`${key.js}: ${value.js}`);
};
this.warn("Dictionary literals are experimental. You may experience runtime errors.");
return `{${dict.join(',')}}`;
}
case "Int":
case "Float":
case "Bool": {
return element.value;
}
case "String": {
return `"${element.value}"`;
}
case "constant": {
return element.token;
}
case "identifier": {
if (this.VARIABLES[element.token] != null) {
return element.token;
} else {
this.error(`The variable ${element.token} doesn't exist`);
}
}
case "reference": {
if (this.VARIABLES[element.value] != null) {
return element.value;
} else {
this.error(`The variable ${element.value} doesn't exist`);
}
}
case "operator": {
// Make sure there's whitespace around the operators
return ` ${element.value} `;
}
case "variable": {
let val = new Transpile(element.value, this.FUNCTIONS, this.VARIABLES, this.CLASSES);
this.VARIABLES[element.name] = element;
return `const ${element.name} = ${val.js};`
}
case "return": {
let code = element.value[0];
console.log(element);
if (code == null)
code = element.value;
console.log(code);
let val = new Transpile([code], this.FUNCTIONS, this.VARIABLES, this.CLASSES);
return `return ${val.js};`;
}
case "js": {
return element.value;
}
default: {
if (element[0] != null) {
console.log("Nested");
let val = new Transpile(element);
return val.js;
}
this.warn(`Unhandled type '${element.type}':`);
console.log(element);
return "";
}
}
},
error: function(err) {
logger.error("Compiler Error: " + err);
process.exit();
},
warn: function(msg) {
logger.warning("Compiler Warning: " + msg);
}
}
module.exports = Transpile;