expr-eval icon indicating copy to clipboard operation
expr-eval copied to clipboard

Getting an error when using parse().evaluate function

Open DragonOsman opened this issue 5 years ago • 3 comments

I'm getting this error message:

Error: unexpected TEOF: EOF
▶ 15 stack frames were collapsed.
handleEqualsClick
E:/programming/react/javascript-calculator/src/js/components/App.js:216
  213 |   setEqualsClicked(true);
  214 |   const stored = storedValue.concat(currentValue);
  215 |   setStoredValue(stored);
> 216 |   const calculatedValue = parser.parse(storedValue).evaluate();
      | ^  217 |   setCurrentValue(calculatedValue.toString());
  218 |   setStoredValue(`${stored} ${event.target.textContent}`);
  219 | };
View compiled
HTMLButtonElement.clickHandler
E:/programming/react/javascript-calculator/src/js/components/App.js:261
  258 |    } else if (event.target.name === "clear-entry") {
  259 |      handleClearEntryClick();
  260 |    } else if (event.target.name === "equals") {
> 261 |      handleEqualsClick(event);
      | ^  262 |    }
  263 |  };
  264 | 
View compiled

when I try to use the parse().evaluate() function. What am I doing wrong? Full source code is on GitHub here. Thanks in advance for replies and help.

DragonOsman avatar Sep 25 '20 15:09 DragonOsman

It looks like you're using the parse and evaluate functions correctly, but you're getting a syntax error in the evaluated string (storedValue). Take a look at its value right before you try to parse it, and feel free to add it as a comment. That should point to the problem.

silentmatt avatar Sep 25 '20 18:09 silentmatt

I'm only getting this stuff in the console:

Uncaught Error: unexpected TEOF: EOF
    at ParserState.push../node_modules/expr-eval/dist/index.mjs.ParserState.parseAtom (index.mjs:1070)
    at ParserState.push../node_modules/expr-eval/dist/index.mjs.ParserState.parseMemberExpression (index.mjs:1299)
    at ParserState.push../node_modules/expr-eval/dist/index.mjs.ParserState.parseFunctionCall (index.mjs:1271)
    at ParserState.push../node_modules/expr-eval/dist/index.mjs.ParserState.parsePostfixExpression (index.mjs:1254)
    at ParserState.push../node_modules/expr-eval/dist/index.mjs.ParserState.parseExponential (index.mjs:1246)
    at ParserState.push../node_modules/expr-eval/dist/index.mjs.ParserState.parseFactor (index.mjs:1241)
    at ParserState.push../node_modules/expr-eval/dist/index.mjs.ParserState.parseTerm (index.mjs:1209)
    at ParserState.push../node_modules/expr-eval/dist/index.mjs.ParserState.parseAddSub (index.mjs:1201)
    at ParserState.push../node_modules/expr-eval/dist/index.mjs.ParserState.parseComparison (index.mjs:1187)
    at ParserState.push../node_modules/expr-eval/dist/index.mjs.ParserState.parseAndExpression (index.mjs:1175)
    at ParserState.push../node_modules/expr-eval/dist/index.mjs.ParserState.parseOrExpression (index.mjs:1165)
    at ParserState.push../node_modules/expr-eval/dist/index.mjs.ParserState.parseConditionalExpression (index.mjs:1151)
    at ParserState.push../node_modules/expr-eval/dist/index.mjs.ParserState.parseVariableAssignmentExpression (index.mjs:1120)
    at ParserState.push../node_modules/expr-eval/dist/index.mjs.ParserState.parseExpression (index.mjs:1079)
    at Parser.push../node_modules/expr-eval/dist/index.mjs.Parser.parse (index.mjs:1765)
    at handleEqualsClick (App.js:216)
    at HTMLButtonElement.clickHandler (App.js:261)

Even when I click on the equals sign and then open up the console, I don't see anything that should've been logged from handleEqualsClick.

And the Uncaught Error: unexpected TEOF: EOF error is from like 1070 in index.mjs as well.

DragonOsman avatar Sep 25 '20 19:09 DragonOsman

@silentmatt I changed the equals click handler to this:

const parser = new exprEval.Parser();
  const handleEqualsClick = event => {
    setEqualsClicked(true);
    const stored = `${storedValue}${currentValue}`;
    setStoredValue(stored);
    console.log(storedValue);
    let calculatedValue;
    try {
      calculatedValue = parser.parse(storedValue).evaluate();
    } catch (err) {
      console.log(`Error occurred: ${err}`);
    }
    setCurrentValue(`${calculatedValue}`);
    setStoredValue(`${stored}${event.target.textContent}`);
  };

and made sure to remove spaces from the string constructed as the value for storedValue. But for some reason, the value logged to the console in handleEqualsClick is just ${number}${operator}. The number that should be right after that isn't logged with the rest of that. Is that what's going into the parse function? Or is it just not able to log the whole thing before the error is thrown?

And for some reason calculatedValue is undefined.

What am I doing wrong here?

DragonOsman avatar Sep 26 '20 15:09 DragonOsman