1 /* 2 * Copyright 2006-2009 Haiku, Inc. All Rights Reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Stephan Aßmus <superstippi@gmx.de> 7 * John Scipione <jscipione@gmail.com> 8 * Ingo Weinhold <bonefish@cs.tu-berlin.de> 9 */ 10 #ifndef EXPRESSION_PARSER_H 11 #define EXPRESSION_PARSER_H 12 13 14 #include <String.h> 15 16 17 class Tokenizer; 18 19 class ParseException { 20 public: 21 ParseException(const char* message, int32 position) 22 : message(message), 23 position(position) 24 { 25 } 26 27 ParseException(const ParseException& other) 28 : message(other.message), 29 position(other.position) 30 { 31 } 32 33 BString message; 34 int32 position; 35 }; 36 37 struct Function; 38 struct Token; 39 class MAPM; 40 41 class ExpressionParser { 42 public: 43 ExpressionParser(); 44 ~ExpressionParser(); 45 46 bool DegreeMode(); 47 void SetDegreeMode(bool degrees); 48 49 void SetSupportHexInput(bool enabled); 50 51 BString Evaluate(const char* expressionString); 52 int64 EvaluateToInt64(const char* expressionString); 53 double EvaluateToDouble(const char* expressionString); 54 55 private: 56 MAPM _ParseBinary(); 57 MAPM _ParseSum(); 58 MAPM _ParseProduct(); 59 MAPM _ParsePower(); 60 MAPM _ParseUnary(); 61 void _InitArguments(MAPM values[], 62 int32 argumentCount); 63 MAPM _ParseFunction(const Token& token); 64 MAPM _ParseAtom(); 65 MAPM _ParseFactorial(MAPM value); 66 67 void _EatToken(int32 type); 68 69 Tokenizer* fTokenizer; 70 71 bool fDegreeMode; 72 }; 73 74 #endif // EXPRESSION_PARSER_H 75