Skip to content

Commit

Permalink
added float and better handling of primitive types
Browse files Browse the repository at this point in the history
  • Loading branch information
valkjsaaa committed Nov 23, 2023
1 parent 4283510 commit 44a533d
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
3 changes: 2 additions & 1 deletion lib/dsl/dsl-interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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":
Expand Down
13 changes: 9 additions & 4 deletions lib/dsl/parser.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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]);
Expand All @@ -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]
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.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",
Expand Down

0 comments on commit 44a533d

Please sign in to comment.