Skip to content

Commit

Permalink
Run code style and clean up on project.
Browse files Browse the repository at this point in the history
  • Loading branch information
GGG-KILLER committed Jun 7, 2021
1 parent 3e32948 commit f926e7a
Show file tree
Hide file tree
Showing 30 changed files with 473 additions and 513 deletions.
120 changes: 59 additions & 61 deletions Calculator.Benchmark/EvaluateBenchmark.cs
Original file line number Diff line number Diff line change
@@ -1,91 +1,89 @@
using System;
using System.Collections.Generic;
using System.Text;
using Calculator.Definitions;

namespace Calculator.Benchmark
{
class EvaluateBenchmark
internal class EvaluateBenchmark
{
private static CalculatorLanguage GetCalculatorLanguage ( )
private static CalculatorLanguage GetCalculatorLanguage()
{
const Double PI_OVER_180 = Math.PI / 180;
const Double PI_UNDER_180 = 180 / Math.PI;
var lang = new CalculatorLanguageBuilder ( StringComparer.InvariantCultureIgnoreCase );
const double PI_OVER_180 = Math.PI / 180;
const double PI_UNDER_180 = 180 / Math.PI;
var lang = new CalculatorLanguageBuilder(StringComparer.InvariantCultureIgnoreCase);

// Constants
lang.AddConstant ( "E", Math.E )
.AddConstants ( new[] { "π", "pi" }, Math.PI );
lang.AddConstant("E", Math.E)
.AddConstants(new[] { "π", "pi" }, Math.PI);

// Binary operators - Logical Operators
lang.AddBinaryOperators ( Associativity.Left, new[] { "|", "∨" }, 1, ( left, right ) => ( ( Int64 ) left ) | ( ( Int64 ) right ) )
.AddBinaryOperators ( Associativity.Left, new[] { "xor", "⊻", "⊕" }, 2, ( left, right ) => ( ( Int64 ) left ) ^ ( ( Int64 ) right ) )
.AddBinaryOperators ( Associativity.Left, new[] { "&", "∧" }, 3, ( left, right ) => ( ( Int64 ) left ) & ( ( Int64 ) right ) )
.AddBinaryOperator ( Associativity.Left, ">>", 4, ( left, right ) => ( ( Int64 ) left ) >> ( ( Int32 ) right ) )
.AddBinaryOperator ( Associativity.Left, "<<", 4, ( left, right ) => ( ( Int64 ) left ) << ( ( Int32 ) right ) );
lang.AddBinaryOperators(Associativity.Left, new[] { "|", "∨" }, 1, (left, right) => ((long) left) | ((long) right))
.AddBinaryOperators(Associativity.Left, new[] { "xor", "⊻", "⊕" }, 2, (left, right) => ((long) left) ^ ((long) right))
.AddBinaryOperators(Associativity.Left, new[] { "&", "∧" }, 3, (left, right) => ((long) left) & ((long) right))
.AddBinaryOperator(Associativity.Left, ">>", 4, (left, right) => ((long) left) >> ((int) right))
.AddBinaryOperator(Associativity.Left, "<<", 4, (left, right) => ((long) left) << ((int) right));

// Binary operators - Math Operators
lang.AddBinaryOperators ( Associativity.Left, new[] { "+", "➕" }, 5, ( left, right ) => left + right )
.AddBinaryOperators ( Associativity.Left, new[] { "-" }, 5, ( left, right ) => left - right )
.AddBinaryOperators ( Associativity.Left, new[] { "*", "×", "✕", "❌", "✖", "·" }, 6, ( left, right ) => left * right )
.AddBinaryOperators ( Associativity.Left, new[] { "/", "÷" }, 6, ( left, right ) => left / right )
.AddBinaryOperator ( Associativity.Left, "%", 6, ( left, right ) => left % right )
.AddImplicitMultiplication ( 7, ( left, right ) => left * right );
lang.AddBinaryOperators(Associativity.Left, new[] { "+", "➕" }, 5, (left, right) => left + right)
.AddBinaryOperators(Associativity.Left, new[] { "-" }, 5, (left, right) => left - right)
.AddBinaryOperators(Associativity.Left, new[] { "*", "×", "✕", "❌", "✖", "·" }, 6, (left, right) => left * right)
.AddBinaryOperators(Associativity.Left, new[] { "/", "÷" }, 6, (left, right) => left / right)
.AddBinaryOperator(Associativity.Left, "%", 6, (left, right) => left % right)
.AddImplicitMultiplication(7, (left, right) => left * right);

// Unary operators
lang.AddUnaryOperator ( UnaryOperatorFix.Prefix, "-", 8, n => -n )
.AddUnaryOperator ( UnaryOperatorFix.Prefix, "~", 8, n => ~( Int64 ) n )
.AddUnaryOperator ( UnaryOperatorFix.Postfix, "!", 8, factorial );
lang.AddUnaryOperator(UnaryOperatorFix.Prefix, "-", 8, n => -n)
.AddUnaryOperator(UnaryOperatorFix.Prefix, "~", 8, n => ~(long) n)
.AddUnaryOperator(UnaryOperatorFix.Postfix, "!", 8, factorial);

// Exponentiation
lang.AddBinaryOperator ( Associativity.Right, "^", 9, Math.Pow )
.AddSuperscriptExponentiation ( 9, Math.Pow );
lang.AddBinaryOperator(Associativity.Right, "^", 9, Math.Pow)
.AddSuperscriptExponentiation(9, Math.Pow);

// Functions - Math.*
lang.AddFunction ( "abs", f => f.AddOverload ( Math.Abs ) )
.AddFunction ( "acos", f => f.AddOverload ( Math.Acos ) )
.AddFunction ( "asin", f => f.AddOverload ( Math.Asin ) )
lang.AddFunction("abs", f => f.AddOverload(Math.Abs))
.AddFunction("acos", f => f.AddOverload(Math.Acos))
.AddFunction("asin", f => f.AddOverload(Math.Asin))
//.AddFunction ( "asinh", f => f.AddOverload ( Math.Asinh ) )
.AddFunction ( "atan", f => f.AddOverload ( Math.Atan ) )
.AddFunction ( "atan2", f => f.AddOverload ( Math.Atan2 ) )
.AddFunction("atan", f => f.AddOverload(Math.Atan))
.AddFunction("atan2", f => f.AddOverload(Math.Atan2))
//.AddFunction ( "atanh", f => f.AddOverload ( Math.Atanh ) )
//.AddFunction ( "bitDecrement", f => f.AddOverload ( Math.BitDecrement ) )
//.AddFunction ( "bitIncrement", f => f.AddOverload ( Math.BitIncrement ) )
//.AddFunction ( "cbrt", f => f.AddOverload ( Math.Cbrt ) )
.AddFunction ( "ceil", f => f.AddOverload ( Math.Ceiling ) )
.AddFunction ( "clamp", f => f.AddOverload ( Math.Clamp ) )
.AddFunction("ceil", f => f.AddOverload(Math.Ceiling))
.AddFunction("clamp", f => f.AddOverload(Math.Clamp))
//.AddFunction ( "copySign", f => f.AddOverload ( Math.CopySign ) )
.AddFunction ( "cos", f => f.AddOverload ( Math.Cos ) )
.AddFunction ( "cosh", f => f.AddOverload ( Math.Cosh ) )
.AddFunction ( "exp", f => f.AddOverload ( Math.Exp ) )
.AddFunction ( "floor", f => f.AddOverload ( Math.Floor ) )
.AddFunction("cos", f => f.AddOverload(Math.Cos))
.AddFunction("cosh", f => f.AddOverload(Math.Cosh))
.AddFunction("exp", f => f.AddOverload(Math.Exp))
.AddFunction("floor", f => f.AddOverload(Math.Floor))
//.AddFunction ( "fusedMultiplyadd", f => f.AddOverload ( Math.FusedMultiplyAdd ) )
.AddFunction ( "IEEERemainder", f => f.AddOverload ( Math.IEEERemainder ) )
.AddFunction("IEEERemainder", f => f.AddOverload(Math.IEEERemainder))
//.AddFunction ( "ilogb", f => f.AddOverload ( x => Math.ILogB ( x ) ) )
.AddFunction ( "ln", f => f.AddOverload ( ( Func<Double, Double> ) Math.Log ) )
.AddFunction ( "log", f => f.AddOverload ( ( Func<Double, Double> ) Math.Log )
.AddOverload ( ( Func<Double, Double, Double> ) Math.Log ) )
.AddFunction ( "log10", f => f.AddOverload ( Math.Log10 ) )
.AddFunction("ln", f => f.AddOverload((Func<double, double>) Math.Log))
.AddFunction("log", f => f.AddOverload((Func<double, double>) Math.Log)
.AddOverload((Func<double, double, double>) Math.Log))
.AddFunction("log10", f => f.AddOverload(Math.Log10))
//.AddFunction ( "log2", f => f.AddOverload ( Math.Log2 ) )
.AddFunction ( "log2", f => f.AddOverload ( n => Math.Log ( n, 2 ) ) )
.AddFunction ( "max", f => f.AddOverload ( Math.Max ) )
.AddFunction("log2", f => f.AddOverload(n => Math.Log(n, 2)))
.AddFunction("max", f => f.AddOverload(Math.Max))
//.AddFunction ( "maxMagnitude", f => f.AddOverload ( Math.MaxMagnitude ) )
.AddFunction ( "min", f => f.AddOverload ( Math.Min ) )
.AddFunction("min", f => f.AddOverload(Math.Min))
//.AddFunction ( "minMagnitude", f => f.AddOverload ( Math.MinMagnitude ) )
.AddFunction ( "pow", f => f.AddOverload ( Math.Pow ) )
.AddFunction ( "round", f => f.AddOverload ( Math.Round ) )
.AddFunction("pow", f => f.AddOverload(Math.Pow))
.AddFunction("round", f => f.AddOverload(Math.Round))
//.AddFunction ( "scaleB", f => f.AddOverload ( ( x, n ) => Math.ScaleB ( x, ( Int32 ) n ) ) )
.AddFunction ( "sign", f => f.AddOverload ( n => Math.Sign ( n ) ) )
.AddFunction ( "sin", f => f.AddOverload ( Math.Sin ) )
.AddFunction ( "sinh", f => f.AddOverload ( Math.Sinh ) )
.AddFunction ( "sqrt", f => f.AddOverload ( Math.Sqrt ) )
.AddFunction ( "tan", f => f.AddOverload ( Math.Tan ) )
.AddFunction ( "tanh", f => f.AddOverload ( Math.Tanh ) )
.AddFunction ( "truncate", f => f.AddOverload ( Math.Truncate ) );
.AddFunction("sign", f => f.AddOverload(n => Math.Sign(n)))
.AddFunction("sin", f => f.AddOverload(Math.Sin))
.AddFunction("sinh", f => f.AddOverload(Math.Sinh))
.AddFunction("sqrt", f => f.AddOverload(Math.Sqrt))
.AddFunction("tan", f => f.AddOverload(Math.Tan))
.AddFunction("tanh", f => f.AddOverload(Math.Tanh))
.AddFunction("truncate", f => f.AddOverload(Math.Truncate));

// Functions - Custom
lang.AddFunction ( "rad", f => f.AddOverload ( n => n * PI_OVER_180 ) )
.AddFunction ( "deg", f => f.AddOverload ( n => n * PI_UNDER_180 ) )
lang.AddFunction("rad", f => f.AddOverload(n => n * PI_OVER_180))
.AddFunction("deg", f => f.AddOverload(n => n * PI_UNDER_180))
/*
* a - b
* c - d
Expand All @@ -94,23 +92,23 @@ private static CalculatorLanguage GetCalculatorLanguage ( )
*
* d = (c*b)/a
*/
.AddFunctions ( new[] { "rot", "ruleOfThree" }, f => f.AddOverload ( ( a, b, c ) => ( b * c ) / a ) );
.AddFunctions(new[] { "rot", "ruleOfThree" }, f => f.AddOverload((a, b, c) => (b * c) / a));

// Get immutable calculator language
return lang.ToCalculatorLanguage ( );
return lang.ToCalculatorLanguage();

Double factorial ( Double arg )
double factorial(double arg)
{
if ( Double.IsInfinity ( arg ) )
if (double.IsInfinity(arg))
return arg;

var res = 1d;
for ( var i = 2; i <= arg && !Double.IsInfinity ( res ) && !Double.IsInfinity ( i ); i++ )
for (var i = 2; i <= arg && !double.IsInfinity(res) && !double.IsInfinity(i); i++)
res *= i;
return res;
}
}

private readonly CalculatorLanguage language = GetCalculatorLanguage ( );
private readonly CalculatorLanguage language = GetCalculatorLanguage();
}
}
3 changes: 1 addition & 2 deletions Calculator.Benchmark/HighlightRangeBenchmark.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Environments;
Expand Down
9 changes: 4 additions & 5 deletions Calculator.Benchmark/Program.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using System;
using BenchmarkDotNet.Running;
using BenchmarkDotNet.Running;

namespace Calculator.Benchmark
{
class Program
internal class Program
{
static void Main ( String[] args ) =>
BenchmarkSwitcher.FromAssembly ( typeof ( Program ).Assembly ).Run ( args );
private static void Main(string[] args) =>
BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);
}
}
6 changes: 3 additions & 3 deletions Calculator.CLI/GlobalSuppressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// suppressions either have no target or are given a specific
// target and scoped to a namespace, type, member, etc.

[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage ( "Style", "CC0037:Remove commented code.", Justification = "<Pending>", Scope = "member", Target = "~M:Calculator.CLI.Program.BuildLanguage~Calculator.Lib.Definitions.CalculatorLang" )]
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage ( "Style", "CC0013:Use ternary operator", Justification = "<Pending>", Scope = "member", Target = "~M:Calculator.CLI.Program.HumanTime(System.Int64)~System.String" )]
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "CC0037:Remove commented code.", Justification = "<Pending>", Scope = "member", Target = "~M:Calculator.CLI.Program.BuildLanguage~Calculator.Lib.Definitions.CalculatorLang")]
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "CC0013:Use ternary operator", Justification = "<Pending>", Scope = "member", Target = "~M:Calculator.CLI.Program.HumanTime(System.Int64)~System.String")]

[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage ( "Style", "CC0037:Remove commented code.", Justification = "<Pending>", Scope = "member", Target = "~M:Calculator.CLI.Program.BuildLanguage~Calculator.CalculatorLanguage" )]
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "CC0037:Remove commented code.", Justification = "<Pending>", Scope = "member", Target = "~M:Calculator.CLI.Program.BuildLanguage~Calculator.CalculatorLanguage")]
2 changes: 1 addition & 1 deletion Calculator.Tests/GlobalSuppressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
// suppressions either have no target or are given a specific
// target and scoped to a namespace, type, member, etc.

[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage ( "Design", "CC0120:Your Switch maybe include default clause", Justification = "<Pending>", Scope = "member", Target = "~M:Calculator.Tests.ParserTests.GenerateRandomExpression(System.Int32)~Calculator.Parsing.AST.CalculatorASTNode" )]
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CC0120:Your Switch maybe include default clause", Justification = "<Pending>", Scope = "member", Target = "~M:Calculator.Tests.ParserTests.GenerateRandomExpression(System.Int32)~Calculator.Parsing.AST.CalculatorASTNode")]
8 changes: 1 addition & 7 deletions Calculator.UI/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows;

namespace Calculator.UI
{
Expand Down
19 changes: 8 additions & 11 deletions Calculator.UI/DiagnosticViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,34 @@ public class DiagnosticViewModel
private readonly Diagnostic _diagnostic;

/// <inheritdoc cref="Diagnostic.Id"/>
public String Id => this._diagnostic.Id;
public string Id => _diagnostic.Id;

/// <inheritdoc cref="Diagnostic.Severity"/>
public DiagnosticSeverity Severity => this._diagnostic.Severity;
public DiagnosticSeverity Severity => _diagnostic.Severity;

/// <inheritdoc cref="Diagnostic.Description"/>
public String Description => this._diagnostic.Description;
public string Description => _diagnostic.Description;

/// <summary>
/// The line where the code this diagnostic refers to starts at.
/// </summary>
public Int32 StartLine => this._diagnostic.Range.Start.Line;
public int StartLine => _diagnostic.Range.Start.Line;

/// <summary>
/// The column where the code this diagnostic refers to starts at.
/// </summary>
public Int32 StartColumn => this._diagnostic.Range.Start.Column;
public int StartColumn => _diagnostic.Range.Start.Column;

/// <summary>
/// The line where the code this diagnostic refers to ends at.
/// </summary>
public Int32 EndLine => this._diagnostic.Range.End.Line;
public int EndLine => _diagnostic.Range.End.Line;

/// <summary>
/// The column where the code this diagnostic refers to ends at.
/// </summary>
public Int32 EndColumn => this._diagnostic.Range.End.Column;
public int EndColumn => _diagnostic.Range.End.Column;

public DiagnosticViewModel ( Diagnostic diagnostic )
{
this._diagnostic = diagnostic ?? throw new ArgumentNullException ( nameof ( diagnostic ) );
}
public DiagnosticViewModel(Diagnostic diagnostic) => _diagnostic = diagnostic ?? throw new ArgumentNullException(nameof(diagnostic));
}
}
8 changes: 1 addition & 7 deletions Calculator/CalculatorCharUtils.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using GParse.Math;
using GParse.Utilities;

namespace Calculator
Expand Down
2 changes: 1 addition & 1 deletion Calculator/CalculatorLanguage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ public IEnumerable<Token<CalculatorTokenType>> Lex(string expression, out IEnume
var toks = new List<Token<CalculatorTokenType>>();
var diags = new DiagnosticList();
diagnostics = diags;
ILexer<CalculatorTokenType> lexer = GetLexer(expression, diags);
var lexer = GetLexer(expression, diags);
while (!lexer.EndOfFile)
toks.Add(lexer.Consume());
return toks;
Expand Down
Loading

0 comments on commit f926e7a

Please sign in to comment.