Skip to content

Commit c517393

Browse files
authored
Merge pull request #48 from javecs/feature/constant
常数のPIとEを使えるようにしました。
2 parents a906ee9 + fbd0933 commit c517393

15 files changed

Lines changed: 201 additions & 98 deletions

File tree

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,15 @@
103103
z には、7 が代入されます。
104104
```
105105
106+
### 常数
107+
108+
常数名 | 説明 | 例 | 結果
109+
--------| --------|--------|--------
110+
[e](https://docs.oracle.com/javase/jp/8/docs/api/java/lang/Math.html#E) | 自然対数の底eにもっとも近いdouble値です。| e | 2.7182818284590452354
111+
[pi](https://docs.oracle.com/javase/jp/8/docs/api/java/lang/Math.html#PI) | 円周とその直径の比piにもっとも近いdouble値です。| pi | 3.14159265358979323846
112+
113+
- 常数名の文字は、大文字と小文字を区別しません。
114+
106115
### 関数
107116
108117
関数名 | 説明 | 例 | 結果

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ plugins {
1616
}
1717

1818
group 'xyz.javecs.tools'
19-
version '0.1.9'
19+
version '0.1.10'
2020

2121
apply plugin: 'kotlin'
2222
apply plugin: 'antlr'

src/main/antlr/Expr.g4

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,14 @@ expr: <assoc=right> expr '^' expr # Pow
1212
| expr '+' expr # Add
1313
| expr '-' expr # Sub
1414
| ID '(' expr ')' # Function
15+
| CONSTANT # Constant
1516
| NUMBER # Number
1617
| ID # Id
1718
| '(' expr ')' # Parens
1819
;
20+
CONSTANT: [Pp][Ii]
21+
| [Ee]
22+
;
1923
NUMBER: [0-9]+('.'[0-9]+)? ;
2024
ID: [a-zA-Z][a-zA-Z0-9]* ;
2125
WS: [ \t\r\n]+ -> skip ;

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ T__5=6
77
T__6=7
88
T__7=8
99
T__8=9
10-
NUMBER=10
11-
ID=11
12-
WS=12
10+
CONSTANT=10
11+
NUMBER=11
12+
ID=12
13+
WS=13
1314
'='=1
1415
'^'=2
1516
'+'=3

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ public class ExprBaseVisitor<T> extends AbstractParseTreeVisitor<T> implements E
8181
* {@link #visitChildren} on {@code ctx}.</p>
8282
*/
8383
@Override public T visitMul(ExprParser.MulContext ctx) { return visitChildren(ctx); }
84+
/**
85+
* {@inheritDoc}
86+
*
87+
* <p>The default implementation returns the result of calling
88+
* {@link #visitChildren} on {@code ctx}.</p>
89+
*/
90+
@Override public T visitConstant(ExprParser.ConstantContext ctx) { return visitChildren(ctx); }
8491
/**
8592
* {@inheritDoc}
8693
*

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

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class ExprLexer extends Lexer {
1818
new PredictionContextCache();
1919
public static final int
2020
T__0=1, T__1=2, T__2=3, T__3=4, T__4=5, T__5=6, T__6=7, T__7=8, T__8=9,
21-
NUMBER=10, ID=11, WS=12;
21+
CONSTANT=10, NUMBER=11, ID=12, WS=13;
2222
public static String[] channelNames = {
2323
"DEFAULT_TOKEN_CHANNEL", "HIDDEN"
2424
};
@@ -29,15 +29,15 @@ public class ExprLexer extends Lexer {
2929

3030
public static final String[] ruleNames = {
3131
"T__0", "T__1", "T__2", "T__3", "T__4", "T__5", "T__6", "T__7", "T__8",
32-
"NUMBER", "ID", "WS"
32+
"CONSTANT", "NUMBER", "ID", "WS"
3333
};
3434

3535
private static final String[] _LITERAL_NAMES = {
3636
null, "'='", "'^'", "'+'", "'-'", "'*'", "'/'", "'%'", "'('", "')'"
3737
};
3838
private static final String[] _SYMBOLIC_NAMES = {
39-
null, null, null, null, null, null, null, null, null, null, "NUMBER",
40-
"ID", "WS"
39+
null, null, null, null, null, null, null, null, null, null, "CONSTANT",
40+
"NUMBER", "ID", "WS"
4141
};
4242
public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES);
4343

@@ -97,26 +97,28 @@ public ExprLexer(CharStream input) {
9797
public ATN getATN() { return _ATN; }
9898

9999
public static final String _serializedATN =
100-
"\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\2\16H\b\1\4\2\t\2\4"+
100+
"\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\2\17O\b\1\4\2\t\2\4"+
101101
"\3\t\3\4\4\t\4\4\5\t\5\4\6\t\6\4\7\t\7\4\b\t\b\4\t\t\t\4\n\t\n\4\13\t"+
102-
"\13\4\f\t\f\4\r\t\r\3\2\3\2\3\3\3\3\3\4\3\4\3\5\3\5\3\6\3\6\3\7\3\7\3"+
103-
"\b\3\b\3\t\3\t\3\n\3\n\3\13\6\13/\n\13\r\13\16\13\60\3\13\3\13\6\13\65"+
104-
"\n\13\r\13\16\13\66\5\139\n\13\3\f\3\f\7\f=\n\f\f\f\16\f@\13\f\3\r\6\r"+
105-
"C\n\r\r\r\16\rD\3\r\3\r\2\2\16\3\3\5\4\7\5\t\6\13\7\r\b\17\t\21\n\23\13"+
106-
"\25\f\27\r\31\16\3\2\6\3\2\62;\4\2C\\c|\5\2\62;C\\c|\5\2\13\f\17\17\""+
107-
"\"\2L\2\3\3\2\2\2\2\5\3\2\2\2\2\7\3\2\2\2\2\t\3\2\2\2\2\13\3\2\2\2\2\r"+
108-
"\3\2\2\2\2\17\3\2\2\2\2\21\3\2\2\2\2\23\3\2\2\2\2\25\3\2\2\2\2\27\3\2"+
109-
"\2\2\2\31\3\2\2\2\3\33\3\2\2\2\5\35\3\2\2\2\7\37\3\2\2\2\t!\3\2\2\2\13"+
110-
"#\3\2\2\2\r%\3\2\2\2\17\'\3\2\2\2\21)\3\2\2\2\23+\3\2\2\2\25.\3\2\2\2"+
111-
"\27:\3\2\2\2\31B\3\2\2\2\33\34\7?\2\2\34\4\3\2\2\2\35\36\7`\2\2\36\6\3"+
112-
"\2\2\2\37 \7-\2\2 \b\3\2\2\2!\"\7/\2\2\"\n\3\2\2\2#$\7,\2\2$\f\3\2\2\2"+
113-
"%&\7\61\2\2&\16\3\2\2\2\'(\7\'\2\2(\20\3\2\2\2)*\7*\2\2*\22\3\2\2\2+,"+
114-
"\7+\2\2,\24\3\2\2\2-/\t\2\2\2.-\3\2\2\2/\60\3\2\2\2\60.\3\2\2\2\60\61"+
115-
"\3\2\2\2\618\3\2\2\2\62\64\7\60\2\2\63\65\t\2\2\2\64\63\3\2\2\2\65\66"+
116-
"\3\2\2\2\66\64\3\2\2\2\66\67\3\2\2\2\679\3\2\2\28\62\3\2\2\289\3\2\2\2"+
117-
"9\26\3\2\2\2:>\t\3\2\2;=\t\4\2\2<;\3\2\2\2=@\3\2\2\2><\3\2\2\2>?\3\2\2"+
118-
"\2?\30\3\2\2\2@>\3\2\2\2AC\t\5\2\2BA\3\2\2\2CD\3\2\2\2DB\3\2\2\2DE\3\2"+
119-
"\2\2EF\3\2\2\2FG\b\r\2\2G\32\3\2\2\2\b\2\60\668>D\3\b\2\2";
102+
"\13\4\f\t\f\4\r\t\r\4\16\t\16\3\2\3\2\3\3\3\3\3\4\3\4\3\5\3\5\3\6\3\6"+
103+
"\3\7\3\7\3\b\3\b\3\t\3\t\3\n\3\n\3\13\3\13\3\13\5\13\63\n\13\3\f\6\f\66"+
104+
"\n\f\r\f\16\f\67\3\f\3\f\6\f<\n\f\r\f\16\f=\5\f@\n\f\3\r\3\r\7\rD\n\r"+
105+
"\f\r\16\rG\13\r\3\16\6\16J\n\16\r\16\16\16K\3\16\3\16\2\2\17\3\3\5\4\7"+
106+
"\5\t\6\13\7\r\b\17\t\21\n\23\13\25\f\27\r\31\16\33\17\3\2\t\4\2RRrr\4"+
107+
"\2KKkk\4\2GGgg\3\2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\3\2\2\2\t\2\62\67=?EK\3\b\2\2";
120122
public static final ATN _ATN =
121123
new ATNDeserializer().deserialize(_serializedATN.toCharArray());
122124
static {

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ T__5=6
77
T__6=7
88
T__7=8
99
T__8=9
10-
NUMBER=10
11-
ID=11
12-
WS=12
10+
CONSTANT=10
11+
NUMBER=11
12+
ID=12
13+
WS=13
1314
'='=1
1415
'^'=2
1516
'+'=3

0 commit comments

Comments
 (0)