Skip to content

Commit 0dd6a90

Browse files
1.15.2 (#19)
* interpreter fix
1 parent 5e46cc2 commit 0dd6a90

File tree

4 files changed

+127
-72
lines changed

4 files changed

+127
-72
lines changed

server/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tower",
3-
"version": "1.15.0",
3+
"version": "1.15.2",
44
"bin": "server/server.js",
55
"main": "server/server.js",
66
"engines": {

server/server/models/impl/template/interpreter.js

Lines changed: 123 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ class InterpreterCommon {
4343

4444
this.returnsJson = returnsJson;
4545

46+
this.globalVariables = [];
47+
4648
this.configurationVariables = configuration.variables;
4749
this.configuration = configuration;
4850

@@ -140,7 +142,7 @@ class InterpreterCommon {
140142
handler(line) {
141143
// replace know values
142144
if (line.split('%%').length >= 2) {
143-
Object.keys(this.configuration).forEach( (el, key) => {
145+
Object.keys(this.configuration).forEach((el, key) => {
144146
if (el !== 'variables') {
145147
if (line.includes(`%%${el}%%`) === true) {
146148
const value = this.returnsJson === true ? this.stringify(this.configuration[el])
@@ -149,7 +151,8 @@ class InterpreterCommon {
149151
}
150152
}
151153
});
152-
this.variables.forEach((el) => {
154+
const allVariables = [...this.globalVariables, ...this.variables, this.configuration.variables];
155+
allVariables.forEach((el) => {
153156
let regex = new RegExp(`%%${el.varName}[.](name|value|type)+%%`);
154157
if (regex.test(line) === true) {
155158
regex = new RegExp(`%%${el.varName}[.]name%%`);
@@ -174,12 +177,6 @@ class InterpreterCommon {
174177
});
175178
}
176179

177-
while (line.split('%%').length >= 2) {
178-
const tempLine = line.substring(line.indexOf('%%') + 2, line.lastIndexOf('%%'));
179-
const newLine = this.handler(tempLine);
180-
line = line.replace(`%%${tempLine}%%`, newLine);
181-
}
182-
183180
line = this.handleVariableReplace(line);
184181
return line;
185182
}
@@ -195,51 +192,89 @@ class InterpreterCommon {
195192
// function
196193
const tempText = line.trim();
197194

198-
if (tempText.startsWith('tower_')) {
199-
const methodName = tempText.substring(0, tempText.indexOf('('));
200-
const params = tempText.substring(tempText.indexOf('(') + 1, tempText.lastIndexOf(')'));
195+
const methodRegex = new RegExp(`%%\\s*tower_\\S+\\s*\\([^\)]*\\)%%`, 'g');
196+
const variablesRegex = new RegExp(`%%\\s*variables\\[["']*[^\\]]+["']*\\]\\s*%%`, 'g');
197+
const definedVariableRegex = new RegExp(`%%\\s*defined\\.(name|value|type)\\s*%%`, 'g');
198+
199+
if (methodRegex.test(tempText)) {
200+
const allMethods = line.match(methodRegex);
201+
202+
for (const method of allMethods) {
203+
const methodName = method.substring(2, method.indexOf('(')).trim();
204+
const params = method.substring(method.indexOf('(') + 1, method.lastIndexOf(')'));
201205

202-
return this.executeMethod(methodName, params);
203-
} else if (tempText.startsWith('variables') === true) {
204-
let variable = tempText.substring(tempText.indexOf('[')+1, tempText.length - 1);
205-
variable = variable.trim();
206-
if (variable.startsWith('"') || variable.startsWith(`'`) || variable.startsWith('`')) {
207-
variable = variable.substring(1, variable.length-1);
206+
const tempLine = this.executeMethod(methodName, params);
207+
line = line.replace(method, tempLine);
208208
}
209-
let find = this.variables.find((el) => {
210-
return el.varName === variable;
211-
});
209+
}
210+
if (variablesRegex.test(tempText)) {
211+
const allVariables = line.match(variablesRegex);
212212

213-
if (find) {
214-
return find.value;
215-
} else {
216-
find = this.configurationVariables.find((el) => {
217-
return el.name === variable;
213+
for (const variableTemp of allVariables) {
214+
let variable = variableTemp.trim();
215+
variable = variable.replace(new RegExp(`^%%\\s*variables\\[["']*`), '');
216+
variable = variable.replace(new RegExp(`["']*\]\\s*%%$`), '');
217+
218+
let find = this.variables.find((el) => {
219+
return el.varName === variable;
218220
});
219221

220222
if (find) {
221-
return find.value;
223+
line = line.replace(variableTemp, find.value);
224+
} else {
225+
find = this.configurationVariables.find((el) => {
226+
return el.name === variable;
227+
});
228+
229+
if (find) {
230+
line = line.replace(variableTemp, find.value);
231+
}
222232
}
223233
}
224-
} else if (tempText.split('.').length === 2) {
225-
const splitLine = tempText.split('.');
226-
let find = this.variables.find((el) => {
227-
return el.varName === splitLine[0];
228-
});
234+
}
229235

230-
if (find) {
231-
return find[splitLine[1]];
232-
} else {
233-
find = this.configurationVariables.find((el) => {
234-
return el.name === splitLine[0];
236+
if (definedVariableRegex.test(tempText)) {
237+
const allVariables = line.match(definedVariableRegex);
238+
239+
for (const variable of allVariables) {
240+
const splitLine = variable.split('.');
241+
let find = this.variables.find((el) => {
242+
return el.varName === splitLine[0];
235243
});
236244

237245
if (find) {
238-
return find[splitLine[1]];
246+
line = line.replace(variable, find[splitLine[1]]);
247+
} else {
248+
find = this.configurationVariables.find((el) => {
249+
return el.name === splitLine[0];
250+
});
251+
252+
if (find) {
253+
line = line.replace(variable, find[splitLine[1]]);
254+
}
239255
}
240256
}
241-
} else if (tempText !== 'variables' && this.configuration.variables[tempText]) {
242-
return this.configuration.variables[tempText];
257+
}
258+
259+
if (this.checkIfDef(tempText)) {
260+
const defRegex = new RegExp(`%%\\s*def\\s+[^=]+=[^%]+%%`, 'g');
261+
const allDefs = line.match(defRegex);
262+
263+
for (const def of allDefs) {
264+
const defName = def.replace(new RegExp(`^%%\\s*def\\s+`), '')
265+
.replace(new RegExp(`=.*`), '').trim();
266+
let defValue = def.replace(new RegExp(`^%%\\s*def\\s+`), '')
267+
.replace(defName, '').trim();
268+
defValue = defValue.substring(1, defValue.length - 2).trim();
269+
270+
this.globalVariables.push({
271+
varName: defName,
272+
value: defValue,
273+
type: 'string',
274+
});
275+
276+
line = line.replace(def, '');
277+
}
243278
}
244279

245280
return line;
@@ -268,7 +303,8 @@ class InterpreterCommon {
268303
'');
269304
compare = /[^'"`]+/.exec(compare)[0];
270305

271-
const find = this.variables.find( (el) => {
306+
const allVariables = [...this.globalVariables, ...this.variables, this.configuration.variables];
307+
const find = allVariables.find((el) => {
272308
return el.varName === variableName;
273309
});
274310

@@ -306,12 +342,12 @@ class InterpreterCommon {
306342

307343
// handle if statement
308344
i++;
309-
const definedVariables = [];
345+
const definedVariables = [...this.globalVariables];
310346
let whileLine = textLines[i];
311347
let textChange = '';
312348
let elseIfs = 0;
313349

314-
while (whileLine && this.checkIfIfEnd(whileLine) === false || elseIfs > 0 ) {
350+
while (whileLine && this.checkIfIfEnd(whileLine) === false || elseIfs > 0) {
315351
let inElse = false;
316352
if (this.checkIfElse(whileLine) === true) {
317353
ifPassed = !ifPassed;
@@ -330,12 +366,12 @@ class InterpreterCommon {
330366
} else if (this.checkIfDef(whileLine) === true) {
331367
const newVariable = this.handleDef(whileLine);
332368
definedVariables.push(newVariable);
333-
const findVariable = this.variables.find( (el) => {
369+
const findVariable = this.variables.find((el) => {
334370
return el.name === newVariable.name;
335371
});
336372

337373
if (findVariable) {
338-
this.variables = this.variables.map( (el) => {
374+
this.variables = this.variables.map((el) => {
339375
if (el.varName === newVariable.varName) {
340376
el.value = newVariable.value;
341377
el.type = newVariable.type;
@@ -361,8 +397,8 @@ class InterpreterCommon {
361397
whileLine = textLines[i];
362398
}
363399

364-
definedVariables.forEach( (variable) => {
365-
this.variables = this.variables.filter( (el) => {
400+
definedVariables.forEach((variable) => {
401+
this.variables = this.variables.filter((el) => {
366402
return el.varName !== variable.varName;
367403
});
368404
});
@@ -391,7 +427,7 @@ class InterpreterCommon {
391427
const currentLine = textLines[i];
392428
let varName = '';
393429
const variable = [];
394-
const definedVariables = [];
430+
const definedVariables = [...this.globalVariables];
395431

396432
// check forEach kind
397433
if (/^\s*%%forEach\s+\S+\s+in\s+list\s*\[/.test(currentLine)) {
@@ -411,7 +447,7 @@ class InterpreterCommon {
411447
});
412448

413449
if (!tempVariable) {
414-
tempVariable = this.variables.find( (el) => {
450+
tempVariable = this.variables.find((el) => {
415451
return el.varName === variableName;
416452
});
417453
}
@@ -447,7 +483,7 @@ class InterpreterCommon {
447483

448484
const reg = new RegExp(line);
449485

450-
this.configuration.variables.forEach( (el) => {
486+
this.configuration.variables.forEach((el) => {
451487
if (reg.test(el.name)) {
452488
variable.push({
453489
name: el.name,
@@ -468,7 +504,7 @@ class InterpreterCommon {
468504

469505
if (variable.length > 0) {
470506
for (let loop = 0; loop < variable.length; loop++) {
471-
let loopLine = textLines[i+1];
507+
let loopLine = textLines[i + 1];
472508
maxI = i + 1;
473509
let changedVariableLine = '';
474510

@@ -487,12 +523,12 @@ class InterpreterCommon {
487523
} else if (this.checkIfDef(loopLine) === true) {
488524
const newVariable = this.handleDef(loopLine);
489525
definedVariables.push(newVariable);
490-
const findVariable = this.variables.find( (el) => {
526+
const findVariable = this.variables.find((el) => {
491527
return el.name === newVariable.name;
492528
});
493529

494530
if (findVariable) {
495-
this.variables = this.variables.map( (el) => {
531+
this.variables = this.variables.map((el) => {
496532
if (el.varName === newVariable.varName) {
497533
el.value = newVariable.value;
498534
el.type = newVariable.type;
@@ -514,14 +550,14 @@ class InterpreterCommon {
514550
maxI++;
515551
loopLine = textLines[maxI];
516552

517-
this.variables = this.variables.filter( (el) => {
553+
this.variables = this.variables.filter((el) => {
518554
return el.varName !== varName;
519555
});
520556
}
521557

522558
if (this.returnsJson === true) {
523559
if (changedVariableLine.trim()) {
524-
changedText += wasAnythingAdded === true ? `,${changedVariableLine}`: changedVariableLine;
560+
changedText += wasAnythingAdded === true ? `,${changedVariableLine}` : changedVariableLine;
525561
}
526562
} else {
527563
changedText += changedVariableLine;
@@ -532,15 +568,15 @@ class InterpreterCommon {
532568
}
533569
}
534570
} else {
535-
let loopLine = textLines[i+1];
571+
let loopLine = textLines[i + 1];
536572
maxI = i + 1;
537573
while (loopLine && !this.checkIfForEnd(loopLine)) {
538574
maxI++;
539575
loopLine = textLines[maxI];
540576
}
541577
}
542578

543-
definedVariables.forEach( (el) => {
579+
definedVariables.forEach((el) => {
544580
this.variables = this.variables.filter((variable) => {
545581
return variable.varName !== el.varName;
546582
});
@@ -564,7 +600,7 @@ class InterpreterCommon {
564600

565601
if (/%%def\s+\S+=\S+\s*%%/.test(newText)) {
566602
newText = newText.replace(/\s*%%def\s*/, '');
567-
newText= newText.substring(0, newText.lastIndexOf('%%'));
603+
newText = newText.substring(0, newText.lastIndexOf('%%'));
568604

569605
const array = newText.split('=');
570606

@@ -658,22 +694,36 @@ class InterpreterCommon {
658694

659695
newText += text;
660696

661-
if (/%%\s*tower_[^%]+%%/.test(newText)) {
662-
const exec = /%%tower_[^%]+%%/.exec(text);
663-
const tempText = exec[0];
664-
const methodName = /%%tower_[^(]+/.exec(tempText)[0].substring(2);
665-
const params = tempText.substring(tempText.indexOf('(') + 1, tempText.lastIndexOf(')'));
697+
const methodRegex = new RegExp(`%%\\s*tower_\\S+\\s*\\([^\)]*\\)%%`, 'g');
698+
699+
if (methodRegex.test(newText)) {
700+
const allMethods = newText.match(methodRegex);
666701

667-
const methodOutput = this.executeMethod(methodName, params);
702+
for (const method of allMethods) {
703+
const methodName = method.substring(2, method.indexOf('(')).trim();
704+
const params = method.substring(method.indexOf('(') + 1, method.lastIndexOf(')'));
668705

669-
newText = newText.replace(tempText, methodOutput);
706+
const tempLine = this.executeMethod(methodName, params);
707+
newText = newText.replace(method, tempLine);
708+
}
670709
}
671710

711+
// if (/%%\s*tower_[^%]+%%/.test(newText)) {
712+
// const exec = /%%tower_[^%]+%%/.exec(text);
713+
// const tempText = exec[0];
714+
// const methodName = /%%tower_[^(]+/.exec(tempText)[0].substring(2);
715+
// const params = tempText.substring(tempText.indexOf('(') + 1, tempText.lastIndexOf(')')).trim();
716+
//
717+
// const methodOutput = this.executeMethod(methodName, params);
718+
//
719+
// newText = newText.replace(tempText, methodOutput);
720+
// }
721+
672722
return newText;
673723
}
674724

675725
/**
676-
* Executes givn method
726+
* Executes given method
677727
*
678728
* @param {string} method method name
679729
* @param {string} params method params
@@ -689,7 +739,7 @@ class InterpreterCommon {
689739
return buff.toString('base64');
690740
} else if (method === 'tower_random') {
691741
const array = params.split(',');
692-
if (array.length === 2 ) {
742+
if (array.length === 2) {
693743
const min = Math.ceil(array[0]);
694744
const max = Math.floor(array[1]);
695745
return `${Math.floor(Math.random() * (max - min)) + min}`;
@@ -764,8 +814,13 @@ module.exports = class Interpreter extends InterpreterCommon {
764814
i = value.i;
765815
this.textToChange += value.text;
766816
} else {
767-
this.textToChange += this.handler(line);
768-
this.textToChange += '\n';
817+
if (this.checkIfDef(line)) {
818+
// Add defined variable to globals
819+
this.handler(line);
820+
} else {
821+
this.textToChange += this.handler(line);
822+
this.textToChange += '\n';
823+
}
769824
}
770825
}
771826
}

ui/.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
VUE_APP_VERSION=1.15.0
1+
VUE_APP_VERSION=1.15.2

0 commit comments

Comments
 (0)