Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Hyoka (github.com/olamide203)
34 points by olamide203 on Aug 26, 2022 | hide | past | favorite | 19 comments


I created a lightweight and precise math expression parser and evaluator for JavaScript and TypeScript. it can handle complex arithmetic expressions


Do you have an online demo, where I can write an expression in a text box and get the result?

What are the security implications of this? Does the code run in the server or my computer? Does it only recognize math functions, or it can also run "alert"?


hyoka uses a full recursive descent parser to parse expressions into AST, then evaluates to get the final result. it is very secure for math expressions unlike the eval function which can run javascript code. you can try out a demo here https://codesandbox.io/s/naughty-chatterjee-kr3bsc


I would show examples in README on use of it along with string interpolation, as it's not too useful with only constants.

Eg.

  const a = 3;
  const b = 2;
  const expression = new Expression(`${a}*${b}(6/3(5--2))`);
  const result = expression.evaluate();
  document.getElementById("app").innerHTML = `a*b( 6 / 3( 5 - - 2 ) )= ${result}`;
Edit: formating


Nice tip, i'm planning to add an identifier node class so that you can pass a scope with the values to be used during evaluation. Eg.

   const a = 2;
   const b = 6;
   const c = 3
   const expression = new Expression('a \ b * c')
   const result = expression.scope({a, b, c}).evaluate();
   document.getElementById("app").innerHTML `${a} \ ${b} * 
   ${c}= ${result}`


[Add an empty line and then two spaces before each line to trigger the "code" mode.]


Very welcome solution. String interpolation makes it possible, but it's bad DX.


yeah having to type ${} everywhere makes the code a little bit messy.


Nice!

I was trying to change the expression, but I need to change it twice. At the top where `expression` is defined and at the bottom to show it in the html. Is it possible to "see" what is inside an Expression object?


you can log it to the console if you want to see the Abstract syntax tree of an Expression object


It can also be used in the browser as well as node.js or any other javascript runtime environment


Damn that's pretty sweet, I've had to pretty much just make anything that needs extreme decimal precision work out on server side. Cool to know someone came up with a way to do it on frontend too, thanks!


I'm glad you find it useful. thanks for the feedback


Does the name have any special meaning?


It means “evaluate”, but in the sense of evaluating a piece of artwork, so my guess is that the author justed ran the word through Google Translate.


According to this[1] okwave thread (in Japanese) it is used in mathematics literature but generally as a direct translation from English. There is an argument there, saying that the nuance of Hyouka in Japanese mathematics is more appropriate in cases where a specific value is harder to determine and as such is used more often for things like "inequality evaluation".

[1]: https://okwave.jp/qa/q5812331.html


you got me there :)


https://en.wikipedia.org/wiki/Hyouka came to mind, but it has a long vowel (ou) the post is missing.


I Scream




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: