Skip to content

Commit

Permalink
safer promise handling
Browse files Browse the repository at this point in the history
  • Loading branch information
valkjsaaa committed Nov 22, 2023
1 parent 65958d6 commit 1380220
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 23 deletions.
52 changes: 32 additions & 20 deletions lib/dsl/dsl-interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,10 +362,13 @@ export class DslInterpreter {
public async describe(ast: any) {
switch (ast.type) {
case "array":
const elements = []
for (const e of ast.value) {
elements.push(await this.describe(e))
}
return {
type: "array",
elements: await promiseOneByOne(
ast.value.map(async (e) => await this.describe(e))),
elements: elements,
};
case "string":
case "int":
Expand All @@ -390,7 +393,11 @@ export class DslInterpreter {
}

public async describeSteps(list: any[]) {
return await promiseOneByOne(list.map((e) => this.describe(e.result)));
const steps = []
for (const e of list) {
steps.push(await this.describe(e))
}
return steps;
}

/**
Expand Down Expand Up @@ -480,12 +487,13 @@ export class DslInterpreter {
);
if (classDescriptor === undefined) {
if (parent.type == "array") {
const arrayValue = await promiseOneByOne(parent.value.map(
(v: any) => this.resolveAccess({
...ast,
parent: v
}, env)
));
const arrayValue = [];
for (const v of parent.value) {
arrayValue.push(await this.resolveAccess({
...ast,
parent: v
}, env));
}
let objType = null
if (arrayValue.length > 0) {
objType = arrayValue[0].objectType
Expand Down Expand Up @@ -585,7 +593,10 @@ export class DslInterpreter {
*/
private async resolveArray(ast: any, env: any) {
console.assert(env == null);
const values = await promiseOneByOne(ast.value.map((v) => this.resolve(v, null)));
const values = [];
for (const v of ast.value) {
values.push(await this.resolve(v, null));
}
// make sure all the elements are objects
console.assert(values.every((v) => v.type === "object"));
// make sure all the elements have the same type
Expand All @@ -603,15 +614,13 @@ export class DslInterpreter {
* @private
*/
private async resolveFunctionCall(ast: any, env: any) {
const parameters =
ast.parameters !== null
? new Map(await promiseOneByOne(
ast.parameters.map(async (p) => [
p.parameter,
await this.resolve(p.value, null),
])
))
: new Map([]);
const parameter_entries = [];
if (ast.parameters !== null) {
for (const p of ast.parameters) {
parameter_entries.push([p.parameter, await this.resolve(p.value, env)]);
}
}
const parameters = new Map(parameter_entries);
// find the function descriptor
let classDescriptor: ClassDescriptor<GenieObject>;
let funcDescriptor: FuncDescriptor;
Expand Down Expand Up @@ -658,7 +667,10 @@ export class DslInterpreter {
(f) => f.func_name === ast.func_name
);
if (funcDescriptor === undefined || env.value[0]["type"] === "array") {
const arrayValue = await promiseOneByOne(env.value.map((v) => this.resolveFunctionCall(ast, v)));
const arrayValue = [];
for (const v of env.value) {
arrayValue.push(await this.resolveFunctionCall(ast, v));
}

return {
type: "array",
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "reactgenie-dsl",
"version": "0.0.43",
"version": "0.0.44",
"description": "A natural language parser based on a large language model",
"scripts": {
"prepare": "peggy lib/dsl/parser.pegjs -o lib/dsl/parser.gen.js && tsc",
Expand Down

0 comments on commit 1380220

Please sign in to comment.