-
Notifications
You must be signed in to change notification settings - Fork 0
/
javanese-interpreter.js
executable file
·299 lines (252 loc) · 5.98 KB
/
javanese-interpreter.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#!/usr/bin/env node
const fs = require("fs");
const path = require("path");
let fileLocate = null
const parseArgs = () => {
const args = process.argv;
if(args.length < 3) {
console.log("Require file args, ex: \"node javanese-interpreter.js example/example1.javanese\" or \"javanese example/example1.javanese\"");
return false;
}
fileLocate = args[2];
if(!fs.existsSync(fileLocate)) {
console.log(`File "${args[2]}" not found, please verify file location`)
return false;
}
if(!(fs.statSync(fileLocate)).isFile()) {
console.log(`${path.basename(fileLocate)} is not a file`);
return false;
}
let ext = path.extname(fileLocate);
if(ext != ".javanese") {
console.log(`File extension is not supported, this script only supports \".javanese\" extension\n\nThe ${path.basename(fileLocate)} file extension is: ${ext}`);
return false;
}
return true;
}
if(!parseArgs()) {
process.exit(1)
}
const inputJavanese = fs.readFileSync(fileLocate, "utf-8")
function flexing(input) {
let cmds = []
const cmdLines = input.split("\n").filter(v => !!v)
cmds = getCmd(cmdLines)
return cmds;
}
function getCmd(cmdLines) {
let parser = [
breakFn,
varAssign,
varReassign,
consoleLog,
conditionIf,
conditionElIf,
conditionElse,
loopFor,
functionDeclarationBegin,
runFunction,
throwError,
tryFn,
catchFn,
finallyFn,
]
return cmdLines.map((line) => {
let cmd = null
for (const parse of parser) {
cmd = parse(line)
if(cmd) break;
}
return cmd
}).filter((v) => !!v)
}
let mapCompare = {
"padha karo": "==",
"ndhuwur": ">",
"ngisor": "<",
"luwih gedhe utawa padha karo": ">=",
"kurang saka utawa padha karo": "<="
}
const breakFn = (msg) => {
let format = /rampung/
let match = msg.match(format)
if(!match) return null
return {
exp: "}"
}
}
const varAssign = (msg) => {
let format = /nyetel ([a-zA-Z0-9]+) iku ([^\[\]\(\)\n]+)/
let match = msg.match(format)
if(!match) return null;
return {
exp: `let ${match[1]} = ${valueTransform(match[2])};`
}
}
const varReassign = (msg) => {
let format = /ganti ([a-zA-Z0-9]+) dadi ([^\[\]\(\)\n]+)/
let match = msg.match(format)
if(!match) return null;
return {
exp: `${match[1]} = ${valueTransform(match[2])};`
}
}
const valueTransform = (msg) => {
let transforms = [
booleanValue,
]
for (const transform of transforms) {
let res = transform(msg)
if(res) {
return res
}
}
// if not transformed
return msg;
}
const booleanValue = (msg) => {
if(msg.match(/nggih$/)) {
return "true"
}
if(msg.match(/ora$/)) {
return "false"
}
return null
}
const consoleLog = (msg) => {
let format = /nuduhake teks (.*)/
let match = msg.match(format)
if(!match) return null;
return {
exp: `console.log(${match[1]});`
}
}
const conditionIf = (msg) => {
let format = /yen\(([a-zA-Z0-9]+) ([a-zA-Z ]+) ([^\[\]\(\)\n]+)/
let match = msg.match(format)
if(!match) return null;
if(match[2]) {
match[2] = mapCompare[match[2]]
}
return {
exp: `if(${match[1]} ${match[2]} ${valueTransform(match[3])})`,
openGroup: true,
}
}
const conditionElIf = (msg) => {
let format = /utawa\(([a-zA-Z0-9]+) ([a-zA-Z ]+) ([^\[\]\(\)\n]+)/
let match = msg.match(format)
if(!match) return null;
if(match[2]) {
match[2] = mapCompare[match[2]]
}
return {
exp: `else if(${match[1]} ${match[2]} ${valueTransform(match[3])})`,
openGroup: true,
closeGroup: true
}
}
const conditionElse = (msg) => {
let format = /yen ora/
let match = msg.match(format)
if(!match) return null;
return {
exp: "else",
openGroup: true,
closeGroup: true
}
}
const loopFor = (msg) => {
let format = /baleni\(([a-zA-Z0-9]+) saka ([a-zA-Z0-9]+) nganti ([a-zA-Z0-9]+)\)/
let match = msg.match(format)
if(!match) return null;
return {
exp: `for(let ${match[1]} = ${match[2]}; ${match[1]} <= ${match[3]}; ${match[1]}++)`,
openGroup: true
}
}
/**
* @param msg {string}
* note: function name must be alphabet followed by optional alphanumeric [a-zA-Z0-9] or underscore (_)
*/
const functionDeclarationBegin = (msg) => {
let format = /fungsi (\w+)\(((\w((, )?)+)*)\)?/
let match = msg.match(format);
if(!match) return null;
const [,funcName, paramNames] = match
const params = paramNames?.trim().split(/\s+/) ?? []
return {
exp: `function ${funcName}(${paramNames})`,
openGroup: true
}
}
const runFunction = (msg) => {
let format = /mbukak (\w+)( args [A-z0-9 "'1\(\)\[\]\{\}\.\?\!\$\,]+)?/
let match = msg.match(format)
if(!match) return null
let args = msg.includes(" args ") ? match[2].replace(" args ", "").trim().split(/\s+/) : undefined
return {
exp: `${match[1]}(${args.join(", ") ?? ""})`
}
}
const throwError = (msg) => {
const format = /uncal (.*)/
const match = msg.match(format)
if(!match) return null;
return {
exp: `throw new Error(${match[1]});`
}
}
const tryFn = (msg) => {
let format = /coba/
let match = msg.match(format)
if(!match) return null;
return {
exp: "try",
openGroup: true,
}
}
const catchFn = (msg) => {
let format = /nyekel/
let match = msg.match(format)
if(!match) return null;
return {
exp: "catch",
openGroup: true,
closeGroup: true
}
}
const finallyFn = (msg) => {
let format = /pungkasanipun/
let match = msg.match(format)
if(!match) return null;
return {
exp: "finally",
openGroup: true,
closeGroup: true,
}
}
const execCmd = (cmds) => {
//console.log(cmds)
let resultCmds = "";
let isOpenGroup = false
for (const cmd of cmds) {
//console.log(cmd)
let tempRes = cmd.exp
if(cmd.closeGroup) {
tempRes = "} " + tempRes
isOpenGroup = false
}
if(cmd.openGroup) {
tempRes = tempRes + " {"
isOpenGroup = true
}
resultCmds += tempRes + "\n"
}
/*if(isOpenGroup) {
resultCmds += " }"
}*/
eval(resultCmds)
}
const result = flexing(inputJavanese)
execCmd(result)