Powered by GeSHi

Source code for clcalc.cs

(view source code of clcalc.cs as plain text)

  1. using System;
  2. using System.CodeDom.Compiler;
  3. using System.Globalization;
  4. using System.Reflection;
  5. using System.Threading;
  6.  
  7.  
  8. namespace RobvanderWoude
  9. {
  10. 	class ClCalc
  11. 	{
  12. 		static int Main( string[] args )
  13. 		{
  14. 			if ( args.Length == 0 )
  15. 			{
  16. 				return WriteError( String.Empty );
  17. 			}
  18. 			string expression = string.Empty;
  19. 			foreach ( string arg in args )
  20. 			{
  21. 				if ( arg == "/?" )
  22. 				{
  23. 					return WriteError( String.Empty );
  24. 				}
  25. 				else
  26. 				{
  27. 					expression += " " + arg;
  28. 				}
  29. 			}
  30. 			expression = expression.Trim( );
  31. 			try
  32. 			{
  33. 				Thread.CurrentThread.CurrentCulture = new CultureInfo( "en-US" );
  34. 				Double result = JScriptEvaluator.EvalToDouble( expression );
  35. 				Console.Error.Write( "{0} = ", expression );
  36. 				Console.WriteLine( "{0}", result );
  37. 				try
  38. 				{
  39. 					return Convert.ToInt32( result );
  40. 				}
  41. 				catch ( Exception )
  42. 				{
  43. 					return 0;
  44. 				}
  45.  
  46. 			}
  47. 			catch ( Exception e )
  48. 			{
  49. 				return WriteError( e.Message );
  50. 			}
  51.  
  52. 		}
  53.  
  54. 		public static int WriteError( Exception e )
  55. 		{
  56. 			return WriteError( e == null ? null : e.Message );
  57. 		}
  58.  
  59. 		public static int WriteError( string errorMessage )
  60. 		{
  61. 			/*
  62. 			ClCalc,  Version 1.01
  63. 			Command Line Calculator
  64.  
  65. 			Usage:  CLCALC  expression
  66.  
  67. 			Notes:  Result is displayed on screen and returned as exit code ("errorlevel").
  68. 			        Exit code is integer value of result or 0 in case of error or overflow.
  69. 			        Result is displayed in Standard Output stream, expression in Standard
  70. 			        Error stream, to allow capturing or redirection of result value only.
  71. 			        "Culture" is set to "en-US", so use and expect decimal dots, no commas.
  72. 			        Based on Eval function (using JScript) by "markman":
  73. 			        www.codeproject.com/Articles/11939/Evaluate-C-Code-Eval-Function
  74.  
  75. 			Written by Rob van der Woude
  76. 			http://www.robvanderwoude.com
  77. 			*/
  78. 			if ( string.IsNullOrEmpty( errorMessage ) == false )
  79. 			{
  80. 				Console.Error.WriteLine( );
  81. 				Console.ForegroundColor = ConsoleColor.Red;
  82. 				Console.Error.Write( "ERROR: " );
  83. 				Console.ForegroundColor = ConsoleColor.White;
  84. 				Console.Error.WriteLine( errorMessage );
  85. 				Console.ResetColor( );
  86. 			}
  87.  
  88. 			Console.Error.WriteLine( );
  89. 			Console.Error.WriteLine( "ClCalc,  Version 1.01" );
  90. 			Console.Error.WriteLine( "Command Line Calculator" );
  91. 			Console.Error.WriteLine( );
  92. 			Console.Error.Write( "Usage:  " );
  93. 			Console.ForegroundColor = ConsoleColor.White;
  94. 			Console.Error.WriteLine( "CLCALC  expression" );
  95. 			Console.ResetColor( );
  96. 			Console.Error.WriteLine( );
  97. 			Console.Error.WriteLine( "Notes:  Result is displayed on screen and returned as exit code (\"errorlevel\")." );
  98. 			Console.Error.WriteLine( "        Exit code is integer value of result or 0 in case of error or overflow." );
  99. 			Console.Error.WriteLine( "        Result is displayed in Standard Output stream, expression in Standard" );
  100. 			Console.Error.WriteLine( "        Error stream, to allow capturing or redirection of result value only." );
  101. 			Console.Error.WriteLine( "        Culture is set to \"en-US\", so use and expect decimal dots, not commas." );
  102. 			Console.Error.WriteLine( "        Based on Eval function (using JScript) by \"markman\":" );
  103. 			Console.ForegroundColor = ConsoleColor.DarkGray;
  104. 			Console.Error.WriteLine( "        www.codeproject.com/Articles/11939/Evaluate-C-Code-Eval-Function" );
  105. 			Console.ResetColor( );
  106. 			Console.Error.WriteLine( );
  107. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  108. 			Console.Error.WriteLine( "http://www.robvanderwoude.com" );
  109. 			return 0;
  110. 		}
  111. 	}
  112.  
  113.  
  114. 	// Eval function using JScript, by "markman"
  115. 	// http://www.codeproject.com/Articles/11939/Evaluate-C-Code-Eval-Function
  116.  
  117. 	public class JScriptEvaluator
  118. 	{
  119. 		public static int EvalToInteger( string statement )
  120. 		{
  121. 			string s = EvalToString( statement );
  122. 			return int.Parse( s.ToString( ) );
  123. 		}
  124.  
  125. 		public static double EvalToDouble( string statement )
  126. 		{
  127. 			string s = EvalToString( statement );
  128. 			return double.Parse( s );
  129. 		}
  130.  
  131. 		public static string EvalToString( string statement )
  132. 		{
  133. 			object o = EvalToObject( statement );
  134. 			return o.ToString( );
  135. 		}
  136.  
  137. 		public static object EvalToObject( string statement )
  138. 		{
  139. 			return _evaluatorType.InvokeMember(
  140. 							  "Eval",
  141. 							  BindingFlags.InvokeMethod,
  142. 							  null,
  143. 							  _evaluator,
  144. 							  new object[] { statement }
  145. 						);
  146. 		}
  147.  
  148. 		static JScriptEvaluator( )
  149. 		{
  150. 			CodeDomProvider provider = new Microsoft.JScript.JScriptCodeProvider( );
  151.  
  152. 			CompilerParameters parameters;
  153. 			parameters = new CompilerParameters( );
  154. 			parameters.GenerateInMemory = true;
  155.  
  156. 			CompilerResults results;
  157. 			results = provider.CompileAssemblyFromSource( parameters, _jscriptSource );
  158.  
  159. 			Assembly assembly = results.CompiledAssembly;
  160. 			_evaluatorType = assembly.GetType( "Evaluator.Evaluator" );
  161.  
  162. 			_evaluator = Activator.CreateInstance( _evaluatorType );
  163. 		}
  164.  
  165. 		private static object _evaluator = null;
  166. 		private static Type _evaluatorType = null;
  167. 		private static readonly string _jscriptSource =
  168. 			  @"package Evaluator
  169.                   {
  170.                      class Evaluator
  171.                      {
  172.                            public function Eval(expr : String) : String
  173.                            {
  174.                               return eval(expr);
  175.                            }
  176.                      }
  177.                   }";
  178. 	}
  179. }
  180.