Skip to content

Commit

Permalink
fix(gbdialog): VBA is running.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rodrigo Rodriguez (pragmatismo.io) committed Dec 2, 2018
1 parent 3bb9d65 commit 2dd359a
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 74 deletions.
1 change: 0 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@
"arrowParens": "avoid",
"semi": true,
"singleQuote": true

}
72 changes: 36 additions & 36 deletions packages/core.gbapp/services/GBVMService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,96 +73,96 @@ export class GBVMService implements IGBCoreService {
public async run(source: any, path: string, min: any, deployer: GBDeployer, filename: string) {
// Converts VBS into TS.

//vb2ts.convertFile(source);
vb2ts.convertFile(source);

// Convert TS into JS.
const tsfile = `bot.ts`;
const tsc = new TSCompiler();
//tsc.compile([UrlJoin(path, tsfile)]);
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;

// Finds all hear calls.

let parsedCode = code;
let hearExp = /(\w+).*hear.*\(\)/;

while (match1 = hearExp.exec(nextCode)) {
let match1;

while ((match1 = hearExp.exec(code))) {

let pos = 0;

// Write async body.
// Writes async body.

const variable = match1[1]; // variable = hear();

finalCode += code.substring(pos, pos + match1.index);
finalCode += `hear (async (${variable}) => {\n`;
parsedCode = code.substring(pos, pos + match1.index);
parsedCode += `hear (async (${variable}) => {\n`;

// Skip old construction and point to the async block.
// Skips old construction and point to the async block.

pos = pos + match1.index;
nextCode = code.substring(pos + match1[0].length + 1);
let tempCode = code.substring(pos + match1[0].length + 1);
let start = pos;

// Find last }
// Balances code blocks and checks for exits.

let right = 0;
let left = 1;
while ((match2 = /\{|\}/.exec(nextCode))) {
const c = nextCode.substring(match2.index, match2.index + 1);
let match2;
while ((match2 = /\{|\}/.exec(tempCode))) {
const c = tempCode.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);
tempCode = tempCode.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';
parsedCode += code.substring(start + match1[0].length + 1, pos + match1[0].length);
parsedCode += '});\n';
parsedCode += code.substring(pos + match1[0].length);

// A interaction will be made for each hear.

nextCode = code.substring(pos + match1[0].length);
code = parsedCode;
}

finalCode = finalCode.replace(/("[^"]*"|'[^']*')|\btalk\b/g, function($0, $1) {
parsedCode = parsedCode.replace(/("[^"]*"|'[^']*')|\btalk\b/g, function($0, $1) {
return $1 == undefined ? 'this.talk' : $1;
});

finalCode = finalCode.replace(/("[^"]*"|'[^']*')|\bhear\b/g, function($0, $1) {
parsedCode = parsedCode.replace(/("[^"]*"|'[^']*')|\bhear\b/g, function($0, $1) {
return $1 == undefined ? 'this.hear' : $1;
});

finalCode = finalCode.replace(/("[^"]*"|'[^']*')|\bsendEmail\b/g, function($0, $1) {
parsedCode = parsedCode.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);
parsedCode = parsedCode.replace(/this\./gm, 'await this.');
parsedCode = parsedCode.replace(/function/gm, 'async function');

fs.writeFileSync(localPath, parsedCode);

const sandbox: DialogClass = new DialogClass(min);
const context = vm.createContext(sandbox);
vm.runInContext(finalCode, context);
vm.runInContext(parsedCode, context);
min.sandbox = sandbox;
await deployer.deployScriptToStorage(min.instanceId, filename);
logger.info(`[GBVMService] Finished loading of ${filename}`);
Expand Down
3 changes: 2 additions & 1 deletion packages/core.gbapp/services/TSCompiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ export class TSCompiler {
public compile(
fileNames: string[],
options: ts.CompilerOptions = {
noStrictGenericChecks: true,
noImplicitUseStrict: true,
noEmitOnError: false,
noImplicitAny: true,
target: ts.ScriptTarget.ES5,
Expand All @@ -51,7 +53,6 @@ export class TSCompiler {
noEmitHelpers: true,
maxNodeModuleJsDepth: 0,
esModuleInterop: false

}
) {
const program = ts.createProgram(fileNames, options);
Expand Down
141 changes: 112 additions & 29 deletions packages/default.gbdialog/bot.vbs
Original file line number Diff line number Diff line change
Expand Up @@ -31,34 +31,117 @@
'
'****************************************************************************

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.")

name = hear ()
talk("Hey " + name + "!")

talk ("How many installments do you want to pay your Credit?")
installments = hear ()

talk ("What is the amount requested?")
ammount = hear ()

talk ("What is the initial payment value?")
valorEntrada = hear ()


talk ("What is the best due date?")
dueDate = hear ()

juros =0
coeficiente1=0

if installments <12 then
juros = 1.60
coeficiente = 0.09748
end if

if installments > 12 and installments< 18 then
juros = 1.66
coeficiente = 0.06869
end if

if installments > 18 and installments< 36 then
juros = 1.64
coeficiente = 0.05397
end if

if installments > 36 and installments< 48 then
juros = 1.62
coeficiente = 0.03931
end if

if installments > 48 and installments< 60 then
juros = 1.70
coeficiente = 0.03270
end if

if installments =60 then
juros = 1.79
coeficiente = 0.02916
end if

if installments > 60 then
talk ("The maximum number of payments is 60")
end if


nInstallments = parseInt(installments)
vAmmount = parseFloat(ammount)
first = parseFloat(vAmmount) * 0.3 ' 30% of the value
tac = 800
coeficiente = 1.3

taxaJuros = parseFloat(juros)
valorFinanciado = ammount - valorEntrada + tac
valorParcela = valorFinanciado * coeficiente
valorTotalDoBem = valorParcela * nInstallments + valorEntrada

talk("Your credit analysis is done.")

talk("First payment" + valorEntrada)
talk("valorParcela" + valor)
talk("taxaJuros" + taxaJuros)
talk("valorFinanciado" + valorFinanciado)
talk("valorTotalDoBem" + valorTotalDoBem)


text = hear()

if email = "" then

text = hear()


if email = "" then
talk ()
text = hear()

i1 = 10
end if
else
text = hear()
if email = "" then
talk ()
text = hear()

i2 = 10

talk ()
text = hear()

talk ()
text = hear()
end if
end if

talk ()
text = hear()

i = 10

if i > 10 then
text = hear()
text = hear()
else
text = hear()
text = hear()
end if

%>
16 changes: 9 additions & 7 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,12 @@ export class GBServer {
}
}

// // 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();
// 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();

0 comments on commit 2dd359a

Please sign in to comment.