Skip to content

problem with equation: length * width #284

Open
@shanebdavis

Description

@shanebdavis

Parse Problem

Repro:

// fails with '*'
(new require('expr-eval').Parser).parse("length * width")

// fails with '/'
(new require('expr-eval').Parser).parse("length / width")

Throws error:

Uncaught Error: unexpected TOP: *
    at ParserState.parseAtom (/Users/shanebdavis/dev/kaiizen/thd/node_modules/expr-eval/dist/bundle.js:1076:13)
    at ParserState.parseMemberExpression (/Users/shanebdavis/dev/kaiizen/thd/node_modules/expr-eval/dist/bundle.js:1305:10)
    at ParserState.parseFunctionCall (/Users/shanebdavis/dev/kaiizen/thd/node_modules/expr-eval/dist/bundle.js:1277:12)
    at ParserState.parsePostfixExpression (/Users/shanebdavis/dev/kaiizen/thd/node_modules/expr-eval/dist/bundle.js:1260:10)
    at ParserState.parseExponential (/Users/shanebdavis/dev/kaiizen/thd/node_modules/expr-eval/dist/bundle.js:1252:10)
    at ParserState.parseFactor (/Users/shanebdavis/dev/kaiizen/thd/node_modules/expr-eval/dist/bundle.js:1247:12)
    at ParserState.parseFactor (/Users/shanebdavis/dev/kaiizen/thd/node_modules/expr-eval/dist/bundle.js:1244:12)
    at ParserState.parseTerm (/Users/shanebdavis/dev/kaiizen/thd/node_modules/expr-eval/dist/bundle.js:1215:10)
    at ParserState.parseAddSub (/Users/shanebdavis/dev/kaiizen/thd/node_modules/expr-eval/dist/bundle.js:1204:10)

But if you change the things just a little, it works:

// change the name to 'lengthh': works
(new require('expr-eval').Parser).parse("lengthh * width")

// change the order: works
(new require('expr-eval').Parser).parse("width * length")

// change the operator to '+': works
(new require('expr-eval').Parser).parse("length + width")

Eval Problem

On further testing, I also found that even if the equations parse, if the variable name "length" is used, it evaluates incorrectly:

> (new require('expr-eval').Parser).parse("width * length").evaluate({width: 5, length: 7})
NaN

> (new require('expr-eval').Parser).parse("length + width").evaluate({width: 5, length: 7})
1

But, if you don't use the variable name length it works:

> (new require('expr-eval').Parser).parse("len + width").evaluate({width: 5, len: 7})
12

Versions

  • expr-eval: 2.0.2
  • node: v22.12.0
  • MacOS: 15.3.2 (24D81)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions