Skip to content

Commit

Permalink
fix(gbdialog): VBA hear must be a wrapper call.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rodrigo Rodriguez (pragmatismo.io) committed Dec 1, 2018
1 parent 776fe03 commit 6915d58
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 9 deletions.
76 changes: 74 additions & 2 deletions packages/core.gbapp/services/GBVMService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export class GBVMService implements IGBCoreService {
// Convert TS into JS.
const tsfile = `bot.ts`;
const tsc = new TSCompiler();
// TODO: tsc.compile([UrlJoin(path, tsfile)]);
tsc.compile([UrlJoin(path, tsfile)]);
// Run JS into the GB context.
const jsfile = `bot.js`;
localPath = UrlJoin(path, jsfile);
Expand All @@ -112,8 +112,24 @@ export class GBVMService implements IGBCoreService {
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);
Expand All @@ -122,4 +138,60 @@ export class GBVMService implements IGBCoreService {
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 () => { ');
}
}
}
25 changes: 20 additions & 5 deletions packages/default.gbdialog/bot.vbs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,23 @@
'
'****************************************************************************

this.talk ("Please, what's your e-mail address?")
let email = this.hear()
this.talk("Thanks, sending e-mail to: " + email);
this.sendEmail(email, "Message from VBA Bot", "Yes, I can send e-mails.");
%>
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

end if

select case email
case 1:

case 2:

end select

if i > 10 then

end if

%>
8 changes: 6 additions & 2 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import { GBCoreService } from '../packages/core.gbapp/services/GBCoreService';
import { GBDeployer } from '../packages/core.gbapp/services/GBDeployer';
import { GBImporter } from '../packages/core.gbapp/services/GBImporterService';
import { GBMinService } from '../packages/core.gbapp/services/GBMinService';
import { GBVMService } from '../packages/core.gbapp/services/GBVMService';

const appPackages = new Array<IGBPackage>();

Expand Down Expand Up @@ -150,5 +151,8 @@ export class GBServer {
}

// First line to run.

GBServer.run();
const path = 'packages/default.gbdialog';
const file = 'bot.vbs';
const source =(path + '/' + file);
GBVMService.run2(source, path)
//GBServer.run();

0 comments on commit 6915d58

Please sign in to comment.