Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added degree for sin(), cos() and tan() #60

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions lib/src/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ class Parser {
/// Creates a new parser.
Parser() : lex = Lexer();
Map<String, dynamic> functionHandlers = <String, dynamic>{};
final String pi = math.pi.toString();

/// Parses the given input string into an [Expression]. Throws a
/// [ArgumentError] if the given [inputString] is empty. Throws a
/// [StateError] if the token stream is invalid. Returns a valid
/// [Expression].
Expression parse(String inputString) {
Expression parse(String inputString, [bool isRadian = true]) {
if (inputString.trim().isEmpty) {
throw FormatException('The given input string was empty.');
}
Expand Down Expand Up @@ -91,13 +92,34 @@ class Parser {
currExpr = Root.fromExpr(left as Number, right);
break;
case TokenType.SIN:
currExpr = Sin(exprStack.removeLast());
if (isRadian) {
currExpr = Sin(exprStack.removeLast());
} else {
String sin =
exprStack.removeLast().toString() + '*($pi/180)';
Expression expr = parse(sin);
currExpr = Sin(expr);
}
break;
case TokenType.COS:
currExpr = Cos(exprStack.removeLast());
if (isRadian) {
currExpr = Cos(exprStack.removeLast());
} else {
String cos =
exprStack.removeLast().toString() + '*($pi/180)';
Expression expr = parse(cos);
currExpr = Cos(expr);
}
break;
case TokenType.TAN:
currExpr = Tan(exprStack.removeLast());
if (isRadian) {
currExpr = Tan(exprStack.removeLast());
} else {
String tan =
exprStack.removeLast().toString() + '*($pi/180)';
Expression expr = parse(tan);
currExpr = Tan(expr);
}
break;
case TokenType.ASIN:
currExpr = Asin(exprStack.removeLast());
Expand Down