Skip to content
This repository has been archived by the owner on Sep 20, 2021. It is now read-only.

Decimals without a leading integer cause errors #57

Open
BillBuilt opened this issue May 20, 2017 · 9 comments
Open

Decimals without a leading integer cause errors #57

BillBuilt opened this issue May 20, 2017 · 9 comments

Comments

@BillBuilt
Copy link

BillBuilt commented May 20, 2017

Example:
$expression = '.35 + .65';
$ast = $compiler->parse($expression);
echo $visitor->visit($ast);

Result:
Uncaught Hoa\Compiler\Exception\UnrecognizedToken, code: 0
Message: Unrecognized token "." at line 1 and column 1: .35 + .65 ↑
File: /web/htdocs/lmifirearms/vendor/hoa/compiler/Llk/Lexer.php
Line: 1

BUT

$expression = '0.35 + 0.65';
$ast = $compiler->parse($expression);
echo $visitor->visit($ast);

Works fine.

This is problematic if evaluating formulas entered by end users, which I am ;-).
Thanks


Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

@Hywan
Copy link
Member

Hywan commented May 22, 2017

Hello :-),

The syntax for real numbers must have an integer part, so something before .. This is expected.
Would you like to be able to default to 0 for the integer part is msising?

@BillBuilt
Copy link
Author

Hi.
The problem I'm having is that I am using your library (which I love, btw) to safely parse expressions entered by end users so I have no real control over how they enter these types of numbers. So yes, if you could default it that way, that would be perfect. So if .743 is sent to your parser, it could convert it to 0.743 internally to avoid the error? Otherwise, I'll need to do some tricky pre-checking before passing it to your parser. Thanks again!

@Hywan
Copy link
Member

Hywan commented May 22, 2017

Any @hoaproject/hoackers to try this? Maybe @Grummfy or @vonglasow?

@stephpy
Copy link
Member

stephpy commented May 22, 2017

👍 IMO.

Looks pragmatic to support .1234 as 0.1234, even if it's not defined in any arithmetic rules.
I don't see any possible conflict for this expression, so it would be a bonus.

@vonglasow
Copy link
Member

I don't think it will be complicated to implement also so 👍

@vonglasow vonglasow self-assigned this May 22, 2017
@Hywan
Copy link
Member

Hywan commented May 23, 2017

@vonglasow So, how would you tackle this problem? There is many ways to do so, can you expose yours :-)?

@BillBuilt
Copy link
Author

For now, I'm passing user entered expressions to the following function to "clean" them first. So far, so good, but I'm guessing you would have better ways of achieving this 😃 I just know at some point, someone is going to get something by this simple little regexp but until then, all is good. Thanks for a great lib. So far, yours is the only one I have tried that actually works and has built in support for floor().

function clean_math_expr($expression=null) {
	return str_replace(' ', '', preg_replace("/(\D|^)\.(?=\d)/", '$1 0.', $expression));
}

@Pierozi
Copy link
Member

Pierozi commented May 26, 2017

Like it seems all calculator (physically or virtual) allow to begin by dot for decimal it should be a good idea to follow the same common usage.

@Hywan
Copy link
Member

Hywan commented May 29, 2017

@vonglasow There is several strategies possible. I would like to hear yours before starting a PR :-).

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

No branches or pull requests

5 participants