Skip to content

Commit dcd0bd3

Browse files
authored
Merge pull request #42 from javecs/feature/operator
符号と累乗演算処理
2 parents 3cf8412 + 78722e1 commit dcd0bd3

11 files changed

Lines changed: 165 additions & 92 deletions

File tree

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,16 @@
6262
6363
## 計算可能な数式
6464
65-
### 演算
65+
### 演算子
6666
67-
演算 | 説明 | 例 | 結果
67+
演算子 | 説明 | 例 | 結果
6868
--------| --------|--------|--------
69+
 ^ | 累乗 | 2 ^ 3 ^ 2| 512 (`2^(3^2)`と処理されます)
70+
+(符号)| 正数 | +1 | 1
71+
-(符号)| 負数 | -1 | -1
6972
 * | 掛け算 | 3 * 4 | 12
7073
 / | 割り算 | 3 / 4 | 0.75
7174
 % | 余り | 7 % 4 | 3
72-
 ^ | 累乗 | 2 ^ 8 | 256
7375
 + | 足し算 | 3 + 4 | 7
7476
 - | 引き算 | 3 - 4 | -1
7577

src/main/antlr/Expr.g4

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
grammar Expr;
22

33
start: stat+ ;
4-
stat: expr # StatExpr
5-
| ID '=' expr # Assign
4+
stat: expr # StatExpr
5+
| ID '=' expr # Assign
66
;
7-
expr: expr '*' expr # Mul
8-
| expr '/' expr # Div
9-
| expr '%' expr # Mod
10-
| expr '^' expr # Pow
11-
| expr '+' expr # Add
12-
| expr '-' expr # Sub
13-
| NUMBER # Number
14-
| ID # Id
15-
| '(' expr ')' # Parens
16-
| func '(' expr ')' # Math
7+
expr: <assoc=right> expr '^' expr # Pow
8+
| ('+'|'-') expr # Sign
9+
| expr '*' expr # Mul
10+
| expr '/' expr # Div
11+
| expr '%' expr # Mod
12+
| expr '+' expr # Add
13+
| expr '-' expr # Sub
14+
| NUMBER # Number
15+
| ID # Id
16+
| '(' expr ')' # Parens
17+
| func '(' expr ')' # Math
1718
;
1819
func: 'sin'
1920
| 'cos'

src/main/java/xyz/javecs/tools/expr/parser/Expr.tokens

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ NUMBER=13
1414
ID=14
1515
WS=15
1616
'='=1
17-
'*'=2
18-
'/'=3
19-
'%'=4
20-
'^'=5
21-
'+'=6
22-
'-'=7
17+
'^'=2
18+
'+'=3
19+
'-'=4
20+
'*'=5
21+
'/'=6
22+
'%'=7
2323
'('=8
2424
')'=9
2525
'sin'=10

src/main/java/xyz/javecs/tools/expr/parser/ExprBaseVisitor.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,13 @@ public class ExprBaseVisitor<T> extends AbstractParseTreeVisitor<T> implements E
8888
* {@link #visitChildren} on {@code ctx}.</p>
8989
*/
9090
@Override public T visitPow(ExprParser.PowContext ctx) { return visitChildren(ctx); }
91+
/**
92+
* {@inheritDoc}
93+
*
94+
* <p>The default implementation returns the result of calling
95+
* {@link #visitChildren} on {@code ctx}.</p>
96+
*/
97+
@Override public T visitSign(ExprParser.SignContext ctx) { return visitChildren(ctx); }
9198
/**
9299
* {@inheritDoc}
93100
*

src/main/java/xyz/javecs/tools/expr/parser/ExprLexer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class ExprLexer extends Lexer {
3333
};
3434

3535
private static final String[] _LITERAL_NAMES = {
36-
null, "'='", "'*'", "'/'", "'%'", "'^'", "'+'", "'-'", "'('", "')'", "'sin'",
36+
null, "'='", "'^'", "'+'", "'-'", "'*'", "'/'", "'%'", "'('", "')'", "'sin'",
3737
"'cos'", "'tan'"
3838
};
3939
private static final String[] _SYMBOLIC_NAMES = {
@@ -112,8 +112,8 @@ public ExprLexer(CharStream input) {
112112
"\2\2\35\3\2\2\2\2\37\3\2\2\2\3!\3\2\2\2\5#\3\2\2\2\7%\3\2\2\2\t\'\3\2"+
113113
"\2\2\13)\3\2\2\2\r+\3\2\2\2\17-\3\2\2\2\21/\3\2\2\2\23\61\3\2\2\2\25\63"+
114114
"\3\2\2\2\27\67\3\2\2\2\31;\3\2\2\2\33@\3\2\2\2\35L\3\2\2\2\37T\3\2\2\2"+
115-
"!\"\7?\2\2\"\4\3\2\2\2#$\7,\2\2$\6\3\2\2\2%&\7\61\2\2&\b\3\2\2\2\'(\7"+
116-
"\'\2\2(\n\3\2\2\2)*\7`\2\2*\f\3\2\2\2+,\7-\2\2,\16\3\2\2\2-.\7/\2\2.\20"+
115+
"!\"\7?\2\2\"\4\3\2\2\2#$\7`\2\2$\6\3\2\2\2%&\7-\2\2&\b\3\2\2\2\'(\7/\2"+
116+
"\2(\n\3\2\2\2)*\7,\2\2*\f\3\2\2\2+,\7\61\2\2,\16\3\2\2\2-.\7\'\2\2.\20"+
117117
"\3\2\2\2/\60\7*\2\2\60\22\3\2\2\2\61\62\7+\2\2\62\24\3\2\2\2\63\64\7u"+
118118
"\2\2\64\65\7k\2\2\65\66\7p\2\2\66\26\3\2\2\2\678\7e\2\289\7q\2\29:\7u"+
119119
"\2\2:\30\3\2\2\2;<\7v\2\2<=\7c\2\2=>\7p\2\2>\32\3\2\2\2?A\t\2\2\2@?\3"+

src/main/java/xyz/javecs/tools/expr/parser/ExprLexer.tokens

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ NUMBER=13
1414
ID=14
1515
WS=15
1616
'='=1
17-
'*'=2
18-
'/'=3
19-
'%'=4
20-
'^'=5
21-
'+'=6
22-
'-'=7
17+
'^'=2
18+
'+'=3
19+
'-'=4
20+
'*'=5
21+
'/'=6
22+
'%'=7
2323
'('=8
2424
')'=9
2525
'sin'=10

0 commit comments

Comments
 (0)