fix(gbdialog): Support for multiples hear blocks.

This commit is contained in:
Rodrigo Rodriguez (pragmatismo.io) 2018-12-01 23:01:42 -02:00
parent 6915d58db1
commit 3bb9d652fd
3 changed files with 136 additions and 126 deletions

View file

@ -64,12 +64,111 @@ export class GBVMService implements IGBCoreService {
// Example when handled through fs.watch() listener
fs.watchFile(source, async (curr, prev) => {
await this.run(source, path, localPath, min, deployer, filename);
await this.run(source, path, min, deployer, filename);
});
await this.run(source, path, localPath, min, deployer, filename);
await this.run(source, path, min, deployer, filename);
this.addHearDialog(min);
}
public async run(source: any, path: string, min: any, deployer: GBDeployer, filename: string) {
// Converts VBS into TS.
//vb2ts.convertFile(source);
// Convert TS into JS.
const tsfile = `bot.ts`;
const tsc = new TSCompiler();
//tsc.compile([UrlJoin(path, tsfile)]);
// Run JS into the GB context.
const jsfile = `bot.js`;
let localPath = UrlJoin(path, jsfile);
if (fs.existsSync(localPath)) {
let code: string = fs.readFileSync(localPath, 'utf8');
code = code.replace(/^.*exports.*$/gm, '');
let match1;
let match2;
let finalCode = '';
let pos = 0;
let nextCode = code;
let hearExp = /(\w+).*hear.*\(\)/;
while (match1 = hearExp.exec(nextCode)) {
// Write async body.
const variable = match1[1]; // variable = hear();
finalCode += code.substring(pos, pos + match1.index);
finalCode += `hear (async (${variable}) => {\n`;
// Skip old construction and point to the async block.
pos = pos + match1.index;
nextCode = code.substring(pos + match1[0].length + 1);
let start = pos;
// Find last }
let right = 0;
let left = 1;
while ((match2 = /\{|\}/.exec(nextCode))) {
const c = nextCode.substring(match2.index, match2.index + 1);
if (c === '}') {
right++;
} else if (c === '{') {
left++;
}
let match3
if (match3 = hearExp.exec(nextCode))
{
nextCode = nextCode.substring(match3.index + 1);
pos += match3.index;
break;
}
nextCode = nextCode.substring(match2.index + 1);
pos += match2.index + 1;
if (left === right) {
break;
}
}
finalCode += code.substring(start + match1[0].length + 1, pos + match1[0].length);
finalCode += '});\n';
nextCode = code.substring(pos + match1[0].length);
}
finalCode = finalCode.replace(/("[^"]*"|'[^']*')|\btalk\b/g, function($0, $1) {
return $1 == undefined ? 'this.talk' : $1;
});
finalCode = finalCode.replace(/("[^"]*"|'[^']*')|\bhear\b/g, function($0, $1) {
return $1 == undefined ? 'this.hear' : $1;
});
finalCode = finalCode.replace(/("[^"]*"|'[^']*')|\bsendEmail\b/g, function($0, $1) {
return $1 == undefined ? 'this.sendEmail' : $1;
});
finalCode = finalCode.replace(/this\./gm, 'await this.');
finalCode = finalCode.replace(/function/gm, 'async function');
console.log(finalCode);
const sandbox: DialogClass = new DialogClass(min);
const context = vm.createContext(sandbox);
vm.runInContext(finalCode, context);
min.sandbox = sandbox;
await deployer.deployScriptToStorage(min.instanceId, filename);
logger.info(`[GBVMService] Finished loading of ${filename}`);
}
}
private addHearDialog(min) {
min.dialogs.add(
new WaterfallDialog('/hear', [
@ -93,105 +192,4 @@ export class GBVMService implements IGBCoreService {
])
);
}
private async run(source: any, path: string, localPath: string, min: any, deployer: GBDeployer, filename: string) {
// Converts VBS into TS.
vb2ts.convertFile(source);
// Convert TS into JS.
const tsfile = `bot.ts`;
const tsc = new TSCompiler();
tsc.compile([UrlJoin(path, tsfile)]);
// Run JS into the GB context.
const jsfile = `bot.js`;
localPath = UrlJoin(path, jsfile);
if (fs.existsSync(localPath)) {
let code: string = fs.readFileSync(localPath, 'utf8');
code = code.replace(/^.*exports.*$/gm, '');
code = code.replace(/this\./gm, 'await this.');
code = code.replace(/function/gm, 'async function');
var match;
let finalCode: string;
let pos = 0;
while ((match = /hear.*\(\)/.exec(code)) != null) {
pos = match.index;
console.log(pos);
finalCode += code.substring(0, pos);
let nextCode = code.substring(pos);
// Find last }
while ((match = /\{|\}/g.exec(nextCode)) != null) {
console.log(match.index);
}
}
//code = code.replace(/this\.hear\(\){/gm, 'this.hear(async () => { ');
const sandbox: DialogClass = new DialogClass(min);
const context = vm.createContext(sandbox);
vm.runInContext(code, context);
min.sandbox = sandbox;
await deployer.deployScriptToStorage(min.instanceId, filename);
logger.info(`[GBVMService] Finished loading of ${filename}`);
}
}
public static async run2(source: any, path: string) {
// Converts VBS into TS.
//vb2ts.convertFile(source);
// Convert TS into JS.
const tsfile = `bot.ts`;
const tsc = new TSCompiler();
//tsc.compile([UrlJoin(path, tsfile)]);
// Run JS into the GB context.
const jsfile = `bot.js`;
let localPath = UrlJoin(path, jsfile);
if (fs.existsSync(localPath)) {
let code: string = fs.readFileSync(localPath, 'utf8');
code = code.replace(/^.*exports.*$/gm, '');
code = code.replace(/this\./gm, 'await this.');
code = code.replace(/function/gm, 'async function');
var match1;
var match2;
let finalCode: string;
let pos = 0;
while ((match1 = /hear.*\(\)/.exec(code))) {
pos = match1.index;
console.log(pos);
finalCode += code.substring(0, pos);
let nextCode = code.substring(pos);
// Find last }
let right = 0;
let left = 1;
while ((match2 = /\{|\}/.exec(nextCode))) {
let c = nextCode.substring(match2.index, match2.index+1);
if (c === '}') {
right++;
} else if (c === '{') {
left++;
}
nextCode = nextCode.substring(match2.index + 1);
if (left == right)
{
console.log('end '+match2.index);
}
console.log(match2.index);
}
}
console.log(finalCode);
//code = code.replace(/this\.hear\(\){/gm, 'this.hear(async () => { ');
}
}
}

View file

@ -31,23 +31,34 @@
'
'****************************************************************************
talk ("Please, what's your e-mail address?")
email = hear ()
talk("Thanks, sending e-mail to: " + email )
sendEmail(email, "Message from VBA Bot", "Yes, I can send e-mails.")
if email = "" then
talk ("Please, what's your e-mail address?")
talk ("Please, what's your e-mail address?")
talk ("Please, what's your e-mail address?")
talk ("Please, what's your e-mail address?")
talk ("Please, what's your e-mail address?")
talk ("Please, what's your e-mail address?")
talk ("Please, what's your e-mail address?")
talk ("Please, what's your e-mail address?")
talk ("Please, what's your e-mail address?")
talk ("Please, what's your e-mail address?")
talk ("Please, what's your e-mail address?")
talk ("Please, what's your e-mail address?")
talk ("Please, what's your e-mail address?")
talk ("Please, what's your e-mail address?")
talk ("Please, what's your e-mail address?")
talk ("Please, what's your e-mail address?")
talk ("Please, what's your e-mail address?")
talk ("Please, what's your e-mail address?")
talk ("Please, what's your e-mail address?")
talk ("Please, what's your e-mail address?")
talk ("Please, what's your e-mail address?")
talk ("Please, what's your e-mail address?")
email = hear ()
talk("Thanks, sending e-mail to: " + email )
sendEmail(email, "Message from VBA Bot", "Yes, I can send e-mails.")
end if
name = hear ()
talk("Hey " + name + "!")
select case email
case 1:
case 2:
end select
if i > 10 then
end if
%>
%>

View file

@ -150,9 +150,10 @@ export class GBServer {
}
}
// First line to run.
const path = 'packages/default.gbdialog';
const file = 'bot.vbs';
const source =(path + '/' + file);
GBVMService.run2(source, path)
// // First line to run.
const path = 'packages/default.gbdialog';
const file = 'bot.vbs';
const source =(path + '/' + file);
let s = new GBVMService();
s.run(source, path, null, null, null)
//GBServer.run();