Răsfoiți Sursa

[fix] 修复运算符优先级错误
[chore] 增加eslint屏蔽

Steven Yan 2 ani în urmă
părinte
comite
b59d65c3a6
2 a modificat fișierele cu 15 adăugiri și 12 ștergeri
  1. 7 0
      scripts/build.sh
  2. 8 12
      src/jison/calculator.jison

+ 7 - 0
scripts/build.sh

@@ -27,8 +27,15 @@ cat > output/calculator.mjs << EOF
  * @description 高级计算器解析库
  * @author Steven Yan
  * @build $build_time
+ *
+ * 不要直接修改此文件,修改calculator.jison文件,然后使用jison编译生成此文件。
+ * 代码库:https://git.steven.run/steven/jison-calculator
+ * 编译器代码库:https://github.com/zaach/jison
  */
 
+/* eslint-disable */
+/* eslint-disable no-new */
+
 EOF
 
 echo "$content" >> output/calculator.mjs

+ 8 - 12
src/jison/calculator.jison

@@ -135,7 +135,7 @@ var dComp = function (v) {
 %lex
 %%
 
-\s+                   /* skip whitespace */
+\s+                                       /* skip whitespace */
 [0-9]+("."[0-9]+)?("e""-"?"+"?[0-9]+)?\b  return 'NUMBER';
 "*"                                       return '*';
 "×"                                       return '*';
@@ -166,18 +166,14 @@ var dComp = function (v) {
 /lex
 
 /* operator associations and precedence */
-/* 优先级越小在上 */
-
 %left '+' '-'
-%left '*' '/'
+%left 'sin' 'cos' 'tan' 'asin' 'acos' 'atan' 'ln' 'log'
+%left 'SQRT' '*' '/'
 %right '%'
 %left '^'
-%left '!'
-%left 'sin' 'cos' 'tan' 'asin' 'acos' 'atan' 'ln' 'log' 'SQRT'
-%right '(' ')'
+%right '!'
 %left UMINUS
 %token INVALID
-
 %start expressions
 
 %% /* language grammar */
@@ -188,7 +184,7 @@ expressions
     ;
 
 e
-    : '-' e     %prec UMINUS
+    : '-' e   %prec UMINUS
         {$$ = -$2;}
     | NUMBER
         {$$ = Number(yytext);}
@@ -199,7 +195,7 @@ e
     | e '%'
         {$$ = $1 / 100;}
     | e '!'
-        {{$$ = fac($1);}}
+        {$$ = fac($1);}
     | e '^' e
         {$$ = Math.pow($1, $3);}
     | e '*' e
@@ -218,8 +214,6 @@ e
         {$$ = $2 ** 0.5}
     | e 'SQRT' e
         {$$ = ($3) ** (1 / $1);}
-    | 'isDeg' e
-        {$$ = setIsDeg($2);}
     | 'sin' e
         {$$ = sin($2);}
     | 'cos' e
@@ -234,5 +228,7 @@ e
         {$$ = atan($2);}
     | '(' e ')'
         {$$ = $2;}
+    | 'isDeg' e
+        {$$ = setIsDeg($2);}
     ;