-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrossCompiler.js
More file actions
79 lines (75 loc) · 1.94 KB
/
crossCompiler.js
File metadata and controls
79 lines (75 loc) · 1.94 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
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
var re = require('re');
var sys = require('sys');
var file = open(sys.argv[1],'r').read().splitlines();
var outputText = '';
function inQuotes(str, subStr) {
var startQuote = "potato";
var startIndex = -1;
var endIndex = -1;
for (var i in range(0,str.length)) {
if (str[i] == startQuote) {
endIndex = i;
break;
}
if (str[i] == "'") {
startQuote = "'";
startIndex = i;
}
if (str[i] == '"') {
startQuote = '"';
startIndex = i;
}
}
if(str.substring(startIndex,endIndex +1).indexOf(subStr) > -1){
return true;
}
return false;
}
// console.log(file);
var inComment = false;
var indentLevel = 0;
for (var i in file) {
var text = i.replace('\t', '').replace(" ", '').replace(";","");
outputText += " " *indentLevel;
//single line comments
if (text.startswith("//")) {
outputText += text.replace("//", "#") + "\n";
continue;
}
//multi line comments
if(text.startswith("/*")) {
inComment = true;
}
if (inComment) {
outputText += "#" + text.replace("/*", "").replace("*/", "") + "\n";
if (text.endswith("*/")) {
inComment = false;
}
continue;
}
//requiring / import external libs
if (re.search('.*(?= = require)',text) && text.startswith('var')) {
//console.log(text.replace(/(= require)\(.*\)/,'').substring(4)+ " L" + (Number.parseInt(i)+1));
outputText += "import " + text[text.index('(') + 2:len(text) - 3] + " as " + re.sub('(= require)\(.*\);', '',text)[4:] + "\n";
continue;
}
//vars
if (text.startswith("var ")) {
outputText += text[4:].replace(";", "") + "\n";
continue;
}
if (text.endswith("{")) {
outputText += text.replace("{", ":") + "\n";
indentLevel+=1;
continue;
}
if (text.endswith("}")) {
outputText += text.replace("}", "\n");
indentLevel-=1;
continue;
}
//console.log(text + " L" + (Number.parseInt(i)+1));
//default output
outputText += text + "\n";
}
print(outputText);