-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
142 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package io.github.janlely.jparser; | ||
|
||
import io.github.janlely.jparser.parsers.NumberParsers; | ||
import io.github.janlely.jparser.parsers.TextParsers; | ||
import io.github.janlely.jparser.util.Buffer; | ||
import org.junit.Test; | ||
|
||
public class Calculator { | ||
|
||
@Test | ||
public void testCalc() { | ||
Result result = expr().parse(Buffer.builder().data("(1+2)*3-(4*2)".getBytes()).build()); | ||
assert result.<Double>get(0).compareTo(1.0) == 0; | ||
result = expr().parse(Buffer.builder().data("1+2*3-(4*2)".getBytes()).build()); | ||
assert result.<Double>get(0).compareTo(-1.0) == 0; | ||
} | ||
|
||
public Parser expr() { | ||
return Parser.choose( | ||
() -> term().chain(TextParsers.one('+').ignore()) | ||
.chain(() -> expr()).map(s -> (double)s.get(0) + (double)s.get(1)), | ||
() -> term().chain(TextParsers.one('-').ignore()) | ||
.chain(() -> expr()).map(s -> (double)s.get(0) - (double)s.get(1)), | ||
() -> term() | ||
); | ||
} | ||
|
||
public Parser term() { | ||
return Parser.choose( | ||
() -> factor().chain(TextParsers.one('*').trim(false).ignore()) | ||
.chain(() -> term()).map(s -> (double)s.get(0) * (double)s.get(1)), | ||
() -> factor().chain(TextParsers.one('/').trim(false).ignore()) | ||
.chain(() -> term()).map(s -> (double)s.get(0) / (double)s.get(1)), | ||
() -> factor() | ||
); | ||
} | ||
|
||
public Parser factor() { | ||
return Parser.choose( | ||
TextParsers.one('(').ignore() | ||
.chain(() -> expr()) | ||
.chain(TextParsers.one(')').ignore()), | ||
number() | ||
); | ||
} | ||
|
||
public Parser number() { | ||
return NumberParsers.anyDoubleStr(); | ||
} | ||
} | ||
|