Created
May 26, 2014 05:31
-
-
Save Announcement/8368c9073f5bb283fb6f to your computer and use it in GitHub Desktop.
JavaScript java compiler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var java = { | |
| io: { | |
| }, | |
| util: { | |
| Interface: { | |
| /* | |
| Collection | |
| Comparator | |
| Dequeue | |
| Enumeration | |
| EventListener | |
| Formattable | |
| Iterator | |
| List | |
| ListIterator | |
| Map | |
| "Map.Entry" | |
| NavigableMap | |
| NavigableSet | |
| Observer | |
| Queue | |
| RandomAccess | |
| Set | |
| SortedMap | |
| SortedSet | |
| */ | |
| }, | |
| Class: { | |
| /* | |
| AbstractCollection | |
| AbstractList | |
| AbstractMap | |
| "AbstractMap.SimpleEntry" | |
| "AbstractMap.SimpleImmutableEntry" | |
| AbstractQueue | |
| AbstractSequentialList | |
| AbstractSet | |
| ArrayDeque | |
| ArrayList | |
| Arrays | |
| BitSet | |
| Calendar | |
| Collections | |
| Currency | |
| Date | |
| Dictionary | |
| EnumMap | |
| EnumSet | |
| EventListenerProxy | |
| EventObject | |
| FormattableFlags | |
| Formatter | |
| GregorianCalender | |
| HashMap | |
| HashSet | |
| Hashtable | |
| IdentityHashMap | |
| LinkedHashMap | |
| LinkedHashSet | |
| LinkedList | |
| ListResourceBundle | |
| Locale | |
| Locale.Builder | |
| Objects | |
| Observable | |
| PriorityQueue | |
| Properties | |
| PropertyPermission | |
| PropertyResourceBundle | |
| Random | |
| ResourceBundle | |
| ResourceBundle.Control | |
| Scanner | |
| SimpleTimeZone | |
| Stack | |
| StringTokenizer | |
| Timer | |
| TimerTask | |
| TimeZone | |
| TreeMap | |
| TreeSet | |
| UUID | |
| Vector | |
| WeakHashMap | |
| */ | |
| }, | |
| Enum: { | |
| /* | |
| Formatter.BigDecimalLayoutForm | |
| Locale.Category | |
| */ | |
| }, | |
| Exception: { | |
| /* | |
| ConcurrentModificationException | |
| DuplicateFormatFlagsException | |
| EmptyStackException | |
| FormatFlagsConversionMismatchException | |
| FormatterClosedException | |
| IllegalFormatCodePointException | |
| IllegalFormatConversionException | |
| IllegalFormatException | |
| IllegalFormatFlagsException | |
| IllegalFormatPrecisionException | |
| IllegalFormatWidthException | |
| IllformedLocaleException | |
| InputMismatchException | |
| InvalidPropertiesFormatException | |
| MissingFormatArgumentException | |
| MissingFormatWidthException | |
| MissingResourceException | |
| NoSuchElementException | |
| TooManyListenersException | |
| UnknownFormatConversionException | |
| UnknownFormatFlagsException | |
| */ | |
| }, | |
| Error: { | |
| /* | |
| ServiceConfigurationError | |
| */ | |
| } | |
| }, | |
| text: { | |
| } | |
| }, | |
| element = document.getElementById("output"), | |
| System = { | |
| out: { | |
| print: function print(text) { | |
| 'use strict'; | |
| element.innerHTML += text; | |
| }, | |
| println: function println(text) { | |
| 'use strict'; | |
| element.innerHTML += text + "\n"; | |
| }, | |
| printf: function printf() { | |
| 'use strict'; | |
| }, | |
| clear: function clear() { | |
| 'use strict'; | |
| element.innerHTML = ""; | |
| } | |
| } | |
| }; | |
| var JavaCompiler = { | |
| compile: function compile(code) { | |
| 'use strict'; | |
| System.out.clear(); | |
| var lines = code.split(/[\n\r;]+/), | |
| internalBracket = 0, | |
| k, | |
| v, | |
| beg, | |
| i = [], | |
| file = "", | |
| open = [], | |
| dummy = {}, | |
| depend = {}, | |
| classes = {}, | |
| imports = [], | |
| toplevel = [], | |
| findFunction, | |
| dependOn, | |
| runFunction, | |
| brackets = []; | |
| depend.System = System; | |
| function fixArray(arr) { | |
| return arr.map(function (value, key) { | |
| var tmp = [""], master = {}; | |
| while (value.length > 0) { | |
| v = value.shift(); | |
| tmp[tmp.length - 1] += ";" + v; | |
| if (v.indexOf("}") !== -1) { | |
| tmp.push(""); | |
| } | |
| } | |
| tmp = tmp.filter(function (tester) { return tester; }); | |
| tmp.push(0); | |
| while (tmp[0] !== 0) { | |
| if (tmp[0] === 0) { | |
| break; | |
| } | |
| v = tmp.shift().substring(1); | |
| if (v.indexOf("class") !== -1) { | |
| dummy.childName = v.match(/\s+(\S+)\s*\{/)[1]; | |
| dummy.childClass = classes[dummy.childName]; | |
| delete classes[dummy.childName]; | |
| master[dummy.childName] = dummy.childClass.data; | |
| } else { | |
| tmp.push(v); | |
| } | |
| } | |
| tmp = tmp.filter(function (tester) { return tester; }); | |
| master.length = tmp.length; | |
| tmp.forEach(function (value2, index2) { master[index2] = value2; }); | |
| return master; | |
| }); | |
| } | |
| findFunction = function (classi, info) { | |
| var result = {}; | |
| for (i[0] = 0; i[0] < classi.length; i[0] += 1) { | |
| if (!info) { | |
| result[classi[i[0]].match(/([\S]+?)\(/)[1]] = classi[i[0]].match(/([\S]+?)\(/)[1]; | |
| } else if (classi[i[0]].match(/([\S]+?)\(/)[1] === info) { | |
| return i[0]; | |
| } | |
| } | |
| return result; | |
| }; | |
| function handleVar(name) { | |
| if (name.indexOf('"') === 0) { | |
| return name.substring(1, name.length - 1); | |
| } | |
| } | |
| dependOn = function (className, str) { | |
| var curObj = {}; | |
| if (depend.hasOwnProperty(str[0])) { | |
| curObj = depend[str[0]]; | |
| } else if (classes[className].hasOwnProperty(str[0])) { | |
| return false; | |
| //curObj = findFunction(classes[className][str[0]]); | |
| //console.log(curObj); | |
| } | |
| str.slice(1).forEach(function (value) { | |
| if (value.indexOf("(") === -1) { | |
| curObj = curObj[value]; | |
| } else if (value.indexOf("(") === 0) { | |
| if (typeof curObj[value] === "number") { | |
| console.log("dependency rewrite pending"); | |
| } else { | |
| curObj = curObj(handleVar(value.substring(1, value.length - 1))); | |
| } | |
| } | |
| }); | |
| return curObj; | |
| }; | |
| runFunction = function (className, info) { | |
| lines = classes[className][info].split(";").slice(1, -1); | |
| lines.forEach(function (line) { | |
| dummy.lh = line.split(/(\.|\([\S\s]*?\))/g).filter(function (q) { return q.replace(".", ""); }); | |
| dependOn(className, dummy.lh); | |
| /*if (line.indexOf(".") !== -1) { | |
| dependOn(line.split("(")[0]); | |
| } *///else { | |
| //runFunction(className, findFunction(className, line.substring(line.length - 2))); | |
| //} | |
| /*if (line.indexOf("System.out") === 0) { | |
| dummy.callName = line.substring(11).match(/([\S]+?)\(/)[1]; | |
| System.out[dummy.callName](line.substring(11 + dummy.callName.length + 2, line.length - 2)); | |
| }*/ | |
| }); | |
| }; | |
| for (k = 0; k < lines.length; k = k + 1) { | |
| if (!!lines[k]) { | |
| v = lines[k].replace(/\s+/, " ").trim(); | |
| if (internalBracket === 0) { | |
| toplevel.push(v); | |
| if (v.indexOf("import") === 0) { | |
| imports.push(v.substring(7, v.length)); | |
| } | |
| } | |
| if (v.indexOf("{") !== -1) { | |
| internalBracket += v.match(/\{/g).length; | |
| } | |
| if (v.indexOf("class") !== -1) { | |
| open.unshift(v.match(/\s+(\S+)\s*\{/)[1]); | |
| classes[open[0]] = {level: internalBracket}; | |
| if (!file) { | |
| file = open[0]; | |
| } | |
| } | |
| if (brackets.length <= internalBracket) { | |
| for (i[0] = 0; brackets.push([]) <= internalBracket; i[0] += 1) { i[0] -= 1; } | |
| } | |
| brackets[internalBracket].push(v); | |
| if (v.indexOf("}") !== -1) { | |
| internalBracket -= v.match(/\}/g).length; | |
| } | |
| if (open.length > 0 && classes[open[0]].level > internalBracket) { | |
| classes[open[0]].data = fixArray(brackets.splice(classes[open[0]].level + 1))[0]; | |
| open.shift(); | |
| } | |
| } | |
| } | |
| for (k in classes) { | |
| if (typeof classes[k] === "object") { | |
| classes[k] = classes[k].data; | |
| runFunction(k, findFunction(classes[k], "main")); | |
| } | |
| } | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment