Skip to content

Commit

Permalink
Support multiple statement in parsed command
Browse files Browse the repository at this point in the history
  • Loading branch information
valkjsaaa committed Nov 4, 2023
1 parent 70cab99 commit 378e7b5
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 15 deletions.
10 changes: 10 additions & 0 deletions lib/dsl/__test__/dsl-interpreter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,16 @@ test("Order array distribution function", async () => {
expect(funcCallResult["objectType"]).toEqual("void");
});

test("Multiple actions", async () => {
Restaurant.all();
const interpreter = new DslInterpreter(allDescriptors);
const funcCallResult = await interpreter.interpret(
'Order.all().placeOrder(); Order.all().dateTime[0].dayOfWeek'
);
expect(funcCallResult["objectType"]).toEqual("string");
});


test("[dry run] Order containing burger", async () => {
Restaurant.all();
const interpreter = new DslInterpreter(allDescriptors, true);
Expand Down
28 changes: 17 additions & 11 deletions lib/dsl/dsl-interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,20 +316,26 @@ export class DslInterpreter {
* interpret a DSL expression
**/
public async interpret(input: string): Promise<any> {
const ast = parse(input)[0];
// console.log(JSON.stringify(ast));
// ast = {"type":"access","parent":{"type":"access","parent":"Restaurant","access":{"type":"function_call","func_name":"current","parameters":null}},"access":{"type":"function_call","func_name":"book","parameters":[{"parameter":"dateTime","value":{"type":"function_call","func_name":"DateTime","parameters":[{"parameter":"year","value":{"type":"int","value":2020}},{"parameter":"month","value":{"type":"int","value":1}},{"parameter":"day","value":{"type":"int","value":1}},{"parameter":"hour","value":{"type":"int","value":12}},{"parameter":"minute","value":{"type":"int","value":0}}]}}]}}
return await this.resolve(ast);
const ast = parse(input) as object[];
var lastResult = null;
for (const statement of ast) {
lastResult = await this.resolve(statement);
}
return lastResult;
}

public async interpretSteps(input: string): Promise<any[]> {
const ast = parse(input)[0];
// console.log(JSON.stringify(ast));
this.resolveSteps = [];
this.resolveStepsEnabled = true;
await this.resolve(ast);
this.resolveStepsEnabled = false;
return this.resolveSteps;
const ast = parse(input) as object[];
var lastResult = null;
for (const statement of ast) {
// console.log(JSON.stringify(ast));
this.resolveSteps = [];
this.resolveStepsEnabled = true;
lastResult = await this.resolve(ast);
this.resolveStepsEnabled = false;
return this.resolveSteps;
}
return lastResult;
}

/**
Expand Down
12 changes: 11 additions & 1 deletion lib/dsl/parser.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,17 @@
}
}

top = (value / all_symbol) SP*
top = s: statement sl: (SP* ';' SP* statement)* {
return [s].concat(
sl.map((e) => {
return e[3];
})
);
}

statement = s: (value / all_symbol) SP* {
return s
}

all_symbol = s:(index_symbol) l:('.' all_symbol)? {
if (l !== null) {
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.31",
"version": "0.0.32",
"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 378e7b5

Please sign in to comment.