From 44a533d83d905933b2808f7ccbd673cb388c4063 Mon Sep 17 00:00:00 2001 From: Jackie Yang Date: Thu, 23 Nov 2023 01:02:10 -0800 Subject: [PATCH] added float and better handling of primitive types --- lib/dsl/dsl-interpreter.ts | 3 ++- lib/dsl/parser.pegjs | 13 +++++++++---- package-lock.json | 4 ++-- package.json | 2 +- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/lib/dsl/dsl-interpreter.ts b/lib/dsl/dsl-interpreter.ts index 8c787b0..f725517 100644 --- a/lib/dsl/dsl-interpreter.ts +++ b/lib/dsl/dsl-interpreter.ts @@ -377,7 +377,7 @@ export class DslInterpreter { return ast.value; case "object": if (ast.value !== undefined) { - if (ast.objectType == "string") { + if (ast.objectType == "string" || ast.objectType == "int" || ast.objectType == "float" || ast.objectType == "number") { return { type: "object", objectType: ast.objectType, @@ -452,6 +452,7 @@ export class DslInterpreter { // don't do anything for primitive types case "string": case "int": + case "float": case "boolean": case "accessor": case "object": diff --git a/lib/dsl/parser.pegjs b/lib/dsl/parser.pegjs index 68cc32a..3c87d89 100644 --- a/lib/dsl/parser.pegjs +++ b/lib/dsl/parser.pegjs @@ -81,17 +81,18 @@ parameter_list = pp:parameter_pair pl:(SP* ',' SP* parameter_pair)* { }, [pp])}; } - parameter_pair = sy:symbol SP* ':' SP* v:value { - return {"parameter": sy, "value": v}; + return {"parameter": sy, "value": v}; } value = 'true' { return { type: "boolean", value: true }; } / 'false' { return { type: "boolean", value: false }; +} / fl: float_literal { + return {"type": "float", "value": fl}; } / il: int_literal { - return {"type": "int", "value": il}; + return {"type": "int", "value": il}; } / s: all_symbol { return s; } / '.' s: symbol { @@ -102,6 +103,10 @@ value = 'true' { return { type: "array", value: s }; } +float_literal = s:("+" / "-")? i:[0-9]+ "." f:[0-9]+ { + return parseFloat((s === "-" ? "-" : "") + i.join("") + "." + f.join("")); +} + array_value = vi: value iv:(SP* ',' SP* value)* { return iv.reduce((acc, cur) => { acc.push(cur[3]); @@ -114,7 +119,7 @@ symbol = [a-zA-Z_][a-zA-Z0-9_]* { } int_literal = s:("+" / "-")? i:[0-9]+ { - return parseInt(i.join("")) * (s !== "-" ? 1 : -1); + return parseInt((s === "-" ? "-" : "") + i.join("")); } INTDIGIT = [1-9] diff --git a/package-lock.json b/package-lock.json index 35969ed..e6f532f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "reactgenie-dsl", - "version": "0.0.49", + "version": "0.0.50", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "reactgenie-dsl", - "version": "0.0.49", + "version": "0.0.50", "license": "Apache-2.0", "dependencies": { "@babel/cli": "^7.23.0", diff --git a/package.json b/package.json index 57b7d43..5756c42 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "reactgenie-dsl", - "version": "0.0.49", + "version": "0.0.50", "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",