Allow constants names to start with digits#39
Allow constants names to start with digits#39jubianchi wants to merge 1 commit intohoaproject:masterfrom jubianchi:constant-start-digit
Conversation
jubianchi
commented
Mar 7, 2016
Arithmetic.pp
Outdated
| %token _bracket \) | ||
| %token comma , | ||
| %token number (0|[1-9]\d*)(\.\d+)?([eE][\+\-]?\d+)? | ||
| %token number (0|[1-9]\d*)(\.\d+)?([eE][\+\-]?\d+)?\b |
There was a problem hiding this comment.
You can move the number token after the constant token to avoid 7A to be consumed as a number 7 and a constant A instead of the constant 7A.
There was a problem hiding this comment.
i was wondering what the best solution was ;) I'll move the lines if you prefer :)
There was a problem hiding this comment.
It's better to move the line I guess.
|
@Hywan & @hoaproject/hoackers good for me, let me know if I have to change anything else. |
|
Good for me if you have thought about this: Constants starting with digits can looks like hexadecimal value, so With this grammar it's a constant since we don't recognize (yet?) hexa/binary values.
|
|
Here is what I can propose:
I already patched the grammar and the arithmetic visitor: diff --git c/Arithmetic.pp i/Arithmetic.pp
index 02cc0fe..b948e11 100644
--- c/Arithmetic.pp
+++ i/Arithmetic.pp
@@ -44,13 +44,16 @@
%token bracket_ \(
%token _bracket \)
%token comma ,
+%token hex \\x[0-9a-fA-F]+
+%token octal \\0[0-7]+
+%token binary \\b[01]+
%token constant [0-9_]*[A-Z_]+[A-Z0-9_]*
+%token id \w+
%token number (0|[1-9]\d*)(\.\d+)?([eE][\+\-]?\d+)?
%token plus \+
%token minus \-|−
%token times \*|×
%token div /|÷
-%token id \w+
expression:
primary() ( ::plus:: #addition expression() )?
@@ -73,7 +76,10 @@ term:
| function()
number:
- <number>
+ <number>
+ | <hex>
+ | <octal>
+ | <binary>
constant:
<constant>
diff --git c/Visitor/Arithmetic.php i/Visitor/Arithmetic.php
index a7da754..32d808f 100644
--- c/Visitor/Arithmetic.php
+++ i/Visitor/Arithmetic.php
@@ -255,6 +255,12 @@ class Arithmetic implements Visitor\Visit
}
} elseif ('id' === $element->getValueToken()) {
return $value;
+ } elseif ('binary' === $element->getValueToken()) {
+ return bindec($value);
+ } elseif ('hex' === $element->getValueToken()) {
+ return hexdec($value);
+ } elseif ('octal' === $element->getValueToken()) {
+ return octdec($value);
} else {
$out = (float) $value;
}I ran this through those tests: echo -n '2_SQRT_314 * 2' | vendor/bin/hoa compiler:pp Arithmetic.pp 0 -s
echo -n '2xfoo * 2' | vendor/bin/hoa compiler:pp Arithmetic.pp 0 -s
echo -n '0XA * 2' | vendor/bin/hoa compiler:pp Arithmetic.pp 0 -s
echo -n '\x1A * 2' | vendor/bin/hoa compiler:pp Arithmetic.pp 0 -s
echo -n '\0755 * 2' | vendor/bin/hoa compiler:pp Arithmetic.pp 0 -s
echo -n '\b01001 * 2' | vendor/bin/hoa compiler:pp Arithmetic.pp 0 -s
echo -n '\b01001' | vendor/bin/hoa compiler:pp Arithmetic.pp 0 -c Hoa.Math.Visitor.Arithmetic -s
echo -n '\x1A' | vendor/bin/hoa compiler:pp Arithmetic.pp 0 -c Hoa.Math.Visitor.Arithmetic -s
echo -n '\0755' | vendor/bin/hoa compiler:pp Arithmetic.pp 0 -c Hoa.Math.Visitor.Arithmetic -s
P.S: I could easily open a third PR for this feature if we want this ;) |
|
Could not resist opening the PR: #40 ;) |
|
Hehe, I am sorry but… this is a Maths library. Did you ever write an hexidecimal value in a Math equation :-p? Personally, no. Thoughts? |
|
@Hywan maths are not only decimal. One may need to do binary computations and get a decimal result. |
|
I agreee with @jubianchi : Math is just a tool. Mathematicians will use the most appropriate paradigm to solve their problems, and decimal is not always the best tool to solve a problem. As for syntax, PHP uses natively |
|
So a number must be start by |
|
let's close this one! (#41 (comment)) |