-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrossCompilerBootstraper.js
More file actions
81 lines (77 loc) · 2.28 KB
/
crossCompilerBootstraper.js
File metadata and controls
81 lines (77 loc) · 2.28 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
80
81
var fs = require('fs');
var file = fs.readFileSync(process.argv[2], 'utf8');
function inQuotes(str, subStr) {
var startQuote = "potato";
var startIndex = -1;
var endIndex = -1;
for (var i = 0; i < str.length; i++) {
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;
}
var outputText = '#!/bin/python\n';
/*
what is
a muliline comment
*/
file = file.split("\n");
// console.log(file);
var inComment = false;
var indentLevel = 0;
for (var i in file) {
var text = file[i].replace('\t', '').replace(/ /g, '');
if (text.endsWith(";")) {
text = text.substring(0, text.length - 1);
}
outputText += " ".repeat(indentLevel);
//single line comments
if (text.startsWith("//") && !inQuotes(text,"//")) {
outputText += text.replace("//", "#") + "\n";
continue;
}
//multi line comments
inComment = text.startsWith("/*") ? true : inComment;
if (inComment) {
outputText += "#" + text.replace("/*", "").replace("*/", "") + "\n";
inComment = text.endsWith("*/") ? false : inComment;
continue;
}
//requiring / import external libs
if (text.search(/.*(?= = require)/) > -1 && text.startsWith('var')) {
//console.log(text.replace(/(= require)\(.*\)/,'').substring(4)+ " L" + (Number.parseInt(i)+1));
outputText += "import " + text.substring(text.indexOf('(') + 2, text.length - 2) + " as " + text.substring(4, text.indexOf("=")) + "\n";
continue;
}
//vars
if(text.startsWith("for")) {
text = text.replace(/(\(|\))/g,"");
}
text = text.replace(/var ?/, "").replace(/function/,"def").replace(/&&/g,"and").replace(/\|\|/g,"or").replace("false","False").replace("true","True");
text = text.replace(/.substring/,"");
if (text.endsWith("{")) {
outputText += text.substring(0,text.lastIndexOf("{")) + ":" + "\n";
indentLevel++;
continue;
}
if (text.endsWith("}")) {
outputText += text.replace("}", "\n");
indentLevel--;
continue;
}
//console.log(text + " L" + (Number.parseInt(i)+1));
//default output
outputText += text + "\n";
}
console.log(outputText);