1- // test: var a = PI * 2, b = sin(a) + e. var c = a^2 + b^2. sqrt(c) + deg
21
32#pragma once
43
2726#include < readline/history.h>
2827#endif
2928
29+ // namespace pythonic {
30+ // namespace print {
31+ // enum class Mode;
32+ // struct TextConfig;
33+ // enum class PrintType;
34+ // void print(PrintType, const std::string&, const TextConfig&);
35+ // }
36+ // }
37+
3038namespace pythonic
3139{
3240 namespace calculator
@@ -70,22 +78,22 @@ namespace pythonic
7078 ;
7179 }
7280
73- std::unordered_map<std::string, std::pair<Token, long double >> constant ;
81+ inline std::unordered_map<std::string, std::pair<Token, long double >> reserved_constant ;
7482
7583 static void set_constants ()
7684 {
7785 // Initialize defaults only once so user-updated values (e.g. ans) persist.
78- if (!constant .empty ())
86+ if (!reserved_constant .empty ())
7987 return ;
8088
81- constant [" PI" ] = std::make_pair (Token (TokenType::Identifier, " PI" ), 3 .14159265358979323846264338327950288419716939937510L );
82- constant [" e" ] = std::make_pair (Token (TokenType::Identifier, " e" ), 2 .71828182845904523536028747135266249775724709369995L );
83- constant [" tau" ] = std::make_pair (Token (TokenType::Identifier, " tau" ), 6 .28318530717958647692528676655900576839433879875021L ); // 2*PI
84- constant [" inf" ] = std::make_pair (Token (TokenType::Identifier, " inf" ), std::numeric_limits<long double >::infinity ());
85- constant [" infinity" ] = std::make_pair (Token (TokenType::Identifier, " infinity" ), std::numeric_limits<long double >::infinity ());
86- constant [" nan" ] = std::make_pair (Token (TokenType::Identifier, " nan" ), std::numeric_limits<long double >::quiet_NaN ());
87- constant [" ans" ] = std::make_pair (Token (TokenType::Identifier, " ans" ), 0 .0L ); // Placeholder, update after each calculation
88- constant [" deg" ] = std::make_pair (Token (TokenType::Identifier, " deg" ), 57 .295779513082320876798154814105170332405472466564L ); // 180/PI
89+ reserved_constant [" PI" ] = std::make_pair (Token (TokenType::Identifier, " PI" ), 3 .14159265358979323846264338327950288419716939937510L );
90+ reserved_constant [" e" ] = std::make_pair (Token (TokenType::Identifier, " e" ), 2 .71828182845904523536028747135266249775724709369995L );
91+ reserved_constant [" tau" ] = std::make_pair (Token (TokenType::Identifier, " tau" ), 6 .28318530717958647692528676655900576839433879875021L ); // 2*PI
92+ reserved_constant [" inf" ] = std::make_pair (Token (TokenType::Identifier, " inf" ), std::numeric_limits<long double >::infinity ());
93+ reserved_constant [" infinity" ] = std::make_pair (Token (TokenType::Identifier, " infinity" ), std::numeric_limits<long double >::infinity ());
94+ reserved_constant [" nan" ] = std::make_pair (Token (TokenType::Identifier, " nan" ), std::numeric_limits<long double >::quiet_NaN ());
95+ reserved_constant [" ans" ] = std::make_pair (Token (TokenType::Identifier, " ans" ), 0 .0L ); // Placeholder, update after each calculation
96+ reserved_constant [" deg" ] = std::make_pair (Token (TokenType::Identifier, " deg" ), 57 .295779513082320876798154814105170332405472466564L ); // 180/PI
8997 }
9098 // Precedence:
9199 // 1: + -
@@ -723,7 +731,7 @@ namespace pythonic
723731 result = evaluateExpression (exprTokens);
724732 result = auto_promote (result);
725733 std::cout << " Ans: " << std::setprecision (std::numeric_limits<long double >::digits10 + 1 ) << result << std::endl;
726- constant [" ans" ] = std::make_pair (Token (TokenType::Identifier, " ans" ), result);
734+ reserved_constant [" ans" ] = std::make_pair (Token (TokenType::Identifier, " ans" ), result);
727735 }
728736 }
729737 history[history_index].second = result;
@@ -845,7 +853,7 @@ namespace pythonic
845853 else if (is_constant (t.value ))
846854 {
847855 t.type = TokenType::Number;
848- t.value = longDoubleToString (constant [t.value ].second );
856+ t.value = longDoubleToString (reserved_constant [t.value ].second );
849857 }
850858 else
851859 {
@@ -866,7 +874,7 @@ namespace pythonic
866874 inline void wipe ()
867875 {
868876 variables.clear ();
869- constant .clear ();
877+ reserved_constant .clear ();
870878 set_constants ();
871879 history.clear ();
872880 history_index = 0 ;
@@ -990,56 +998,68 @@ namespace pythonic
990998 Calculator calc;
991999 std::string line;
9921000 print_title ();
993- while (true ) {
1001+ while (true )
1002+ {
9941003#ifdef HAVE_READLINE
995- char * input = readline (" >> " );
996- if (!input) break ; // Ctrl+D
997- line = input;
998- free (input);
999- if (!line.empty ()) add_history (line.c_str ());
1004+ char *input = readline (" >> " );
1005+ if (!input)
1006+ break ; // Ctrl+D
1007+ line = input;
1008+ free (input);
1009+ if (!line.empty ())
1010+ add_history (line.c_str ());
10001011#else
1001- std::cout << " >> " ;
1002- if (!std::getline (std::cin, line)) break ;
1012+ std::cout << " >> " ;
1013+ if (!std::getline (std::cin, line))
1014+ break ;
10031015#endif
10041016
1005- if (line == " exit" || line == " quit" ) break ;
1006- else if (line == " help" ) {
1007- help ();
1008- continue ;
1009- }
1010- else if (line == " clear" ) {
1017+ if (line == " exit" || line == " quit" )
1018+ break ;
1019+ else if (line == " help" )
1020+ {
1021+ help ();
1022+ continue ;
1023+ }
1024+ else if (line == " clear" )
1025+ {
10111026#ifdef WIN_32
1012- system (" cls" );
1027+ system (" cls" );
10131028#else
1014- system (" clear" );
1029+ system (" clear" );
10151030#endif
1016- print_title ();
1017- continue ;
1018- }
1019- else if (line == " wipe" ) {
1020- calc.wipe ();
1031+ print_title ();
1032+ continue ;
1033+ }
1034+ else if (line == " wipe" )
1035+ {
1036+ calc.wipe ();
10211037#ifdef WIN_32
1022- system (" cls" );
1038+ system (" cls" );
10231039#else
1024- system (" clear" );
1040+ system (" clear" );
10251041#endif
1026- print_title ();
1027- continue ;
1028- }
1029- else if (line == " history" ) {
1030- calc.show_history ();
1031- continue ;
1032- }
1042+ print_title ();
1043+ continue ;
1044+ }
1045+ else if (line == " history" )
1046+ {
1047+ calc.show_history ();
1048+ continue ;
1049+ }
10331050
1034- if (line.empty ()) continue ;
1051+ if (line.empty ())
1052+ continue ;
10351053
1036- try {
1037- calc.process (line);
1038- }
1039- catch (const std::exception& e) {
1040- std::cout << " Error: " << e.what () << std::endl;
1041- }
1042- }
1054+ try
1055+ {
1056+ calc.process (line);
1057+ }
1058+ catch (const std::exception &e)
1059+ {
1060+ std::cout << " Error: " << e.what () << std::endl;
1061+ }
1062+ }
10431063
10441064 return ;
10451065 }
0 commit comments