-
Notifications
You must be signed in to change notification settings - Fork 13
/
thengascript.js
171 lines (117 loc) · 4.74 KB
/
thengascript.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
const coreLangKeywords = {
"അടിസ്ഥാനം": "default",
"ആണെങ്കിൽ":"if",
"അല്ലെങ്കിൽ": "else",
"ആവട്ടെ": "let",
"അവസ്ഥ": "case",
"അസാധു": "null",
"ഇറക്കുമതി": "import",
"ഇത്": "this",
"ഇന്": "for",
"ഇല്": "in",
"ഉദാഹരണമാണ്": "instanceof",
"ഉയര്ന്നതരം": "super",
"എറിയുക": "throw",
"എണ്ണല്": "enum",
"നിർവചിച്ചിട്ടില്ല": "undefined",
"എത്തരം": "typeof",
"എന്നിരിക്കെ": "while",
"ഒടുവിൽ": "finally",
"ഒപ്പം": "with",
"കയറ്റുമതി": "export",
"കളയുക": "delete",
"കാത്തിരിക്കുക": "await",
"ഗണം": "class",
"ചെയ്യുക": "do",
"തിരിക്കുക": "switch",
"തിരുത്തൽ": "debugger",
"തുടരുക": "continue",
"തെറ്റ്": "false",
"നല്കുക": "yield",
"നിർവഹിക്കുന്നു": "implements",
"പരിവര്ത്തനം": "var",
"പരിവർത്തനം": "var",
"പിടിക്കുക": "catch",
"പുതിയ": "new",
"പ്രയോഗം": "function",
"പൊതു": "public",
"ഭാണ്ഡം": "package",
"മടക്കം": "return",
"മുടക്കുക": "break",
"മൂല്യനിർണ്ണയം": "eval",
"വാദം": "arguments",
"വ്യർത്ഥം": "void",
"വ്യാപിപ്പിക്കുന്നു": "extends",
"ശരി": "true",
"ശാശ്വതം": "const",
"ശ്രമിക്കുക": "try",
"സമ്പര്ക്കമുഖം": "interface",
"സുരക്ഷിതമാക്കപ്പെട്ട": "protected",
"സ്വകാര്യ": "private",
"സ്ഥായി": "static"
};
const browserObjects = {"കാണിക്കുക": "console.log", "മുന്നറിയിപ്പ്": "alert", "രേഖ":"document", "ജാലകം": "window"};
if(typeof HTMLDocument != "undefined") {
HTMLDocument.prototype.അന്വേഷണം = HTMLDocument.prototype.querySelector;
}
if(typeof HTMLElement != "undefined") {
Object.defineProperty(HTMLElement.prototype, "ഉള്ളടക്കം", {set(val) {this.textContent = val;}});
}
Array.prototype.സൂചിക = Array.prototype.indexOf;
Array.prototype.സന്ധി = Array.prototype.join;
Array.prototype.ഉന്ത് = Array.prototype.push;
Object.defineProperty(Array.prototype,"നീളം", {get () { return this.length }});
const മലയാളം_to_english = Object.assign({}, coreLangKeywords, browserObjects);
async function loadFile(src) {
let resp = null;
try {
resp = await fetch(src);
} catch(e) {
console.log("Error on loading file: ", e);
return false;
}
let body = null;
if(resp) body = await resp.text();
return body;
};
const translate = (x) => {
let keys = Object.keys(മലയാളം_to_english);
const captureGroup = x => `(?:${x})`;
const lookAhead = x => `(?=${x})`;
const or = (vals) => vals.join("|");
const many = (x) => `${x}*`;
const endsWith = (x) => `${x}$`
const singleQuotedLiterals = `'${many("[^']")}'`;
const doubleQuotedLiterals = `"${many('[^"]')}"`;
const nonQuotes = `[^'"]`;
const quotes = lookAhead(endsWith(many(captureGroup(or([nonQuotes, singleQuotedLiterals, doubleQuotedLiterals])))));
let replacer = new RegExp("(" + keys.join("|") + ")" + quotes,"gi");
return x.replace(replacer, matched => മലയാളം_to_english[matched]);
};
const run = (code) => {
try {
eval(code);
} catch(e) {
console.log("Error: ", e);
}
};
const compile = (x) => run(translate(x));
async function compileScripts() {
let scripts = document.querySelectorAll("script");
for(let script of scripts) {
if(script.type == "text/thengascript") {
if(script.src) {
let contents = await loadFile(script.src);
compile(contents);
} else {
compile(script.textContent);
}
}
}
};
/* ബ്രൗസേഴ്സിന് വേണ്ടി */
if(typeof window != "undefined")
window.addEventListener('DOMContentLoaded', compileScripts);
/* തർജ്ജമയും ഇവാലുവേഷനും നോഡിൽ ലഭ്യം */
if(typeof module != "undefined" && module.exports)
module.exports = {translate, run, compile};