@@ -36,7 +36,7 @@ public static class Program
3636 { "rng_i" , double . NaN } ,
3737 { "rng_d" , double . NaN }
3838 } ;
39- internal static Dictionary < string , double > constants { get ; private set ; } = null ! ;
39+
4040 private static readonly Stack < ( Component func , MathContext ctx ) > stash = new ( ) ;
4141 internal static readonly ConcurrentDictionary < string , UnitPackage > unitPackages = new ( ) ;
4242 private static readonly List < string > enabledUnitPacks = new ( ) ;
@@ -55,15 +55,21 @@ static Program()
5555 {
5656 File . Delete ( configFile ) ;
5757 SaveConfig ( ) ;
58- }
58+ }
59+
5960 LoadConstants ( ) ;
6061 LoadUnits ( ) ;
6162 }
6263
64+ internal static Dictionary < string , double > constants { get ; private set ; } = null ! ;
65+
6366 public static CalcMode DRG { get ; set ; } = CalcMode . Deg ;
6467 internal static bool AutoEval { get ; set ; } = true ;
6568
66- public static void SetUp ( ) => CultureInfo . CurrentCulture = CultureInfo . InvariantCulture ;
69+ public static void SetUp ( )
70+ {
71+ CultureInfo . CurrentCulture = CultureInfo . InvariantCulture ;
72+ }
6773
6874 private static void SaveConstants ( Dictionary < string , double > ? values = null )
6975 {
@@ -94,7 +100,7 @@ private static void LoadUnits()
94100 unitPackages [ packageName ] = package ;
95101 }
96102 }
97-
103+
98104 private static void SaveConfig ( )
99105 {
100106 using var fs = File . OpenWrite ( configFile ) ;
@@ -123,6 +129,7 @@ private static bool LoadConfig()
123129 var len = BitConverter . ToInt32 ( Read ( fs , sizeof ( int ) ) ) ;
124130 enabledUnitPacks . Add ( Encoding . ASCII . GetString ( Read ( fs , len ) ) ) ;
125131 }
132+
126133 return true ;
127134 }
128135
@@ -299,7 +306,7 @@ private static (Component fx, MathContext ctx)[] CreateArgsFuncs(int start, para
299306 return args . ToList ( )
300307 . GetRange ( start , args . Length - start )
301308 . Select ( ParseFunc )
302- . Select ( fx => ( fx , new MathContext ( ) { enabledUnitPacks = enabledUnitPacks } ) )
309+ . Select ( fx => ( fx , new MathContext { enabledUnitPacks = enabledUnitPacks } ) )
303310 . ToArray ( ) ;
304311 }
305312
@@ -322,7 +329,7 @@ internal static (Component func, MathContext ctx)? LoadFunc(string name)
322329 }
323330 else
324331 {
325- ctx = new MathContext ( ) { enabledUnitPacks = enabledUnitPacks } ;
332+ ctx = new MathContext { enabledUnitPacks = enabledUnitPacks } ;
326333 }
327334
328335 return ( ParseFunc ( lnb == - 1 ? data : data . Substring ( 0 , lnb ) ) , ctx ) ;
@@ -359,13 +366,13 @@ private static void EvalFunc(Component func, string? f = null, MathContext? ctx
359366 {
360367 if ( func . EnumerateVars ( ) . Distinct ( ) . All ( constants . ContainsKey ) )
361368 {
362- var res = func . Evaluate ( new MathContext ( ) { enabledUnitPacks = enabledUnitPacks } ) ;
369+ var res = func . Evaluate ( new MathContext { enabledUnitPacks = enabledUnitPacks } ) ;
363370 PrintResult ( func , res ) ;
364371 }
365372 else
366373 {
367374 // enter editor mode
368- ctx ??= new MathContext ( ) { enabledUnitPacks = enabledUnitPacks } ;
375+ ctx ??= new MathContext { enabledUnitPacks = enabledUnitPacks } ;
369376 while ( true )
370377 {
371378 if ( _exiting || _dropAll )
@@ -494,23 +501,26 @@ private static void CmdSave(string[] cmds, string? f, Component func, MathContex
494501 if ( IsInvalidArgumentCount ( cmds , 2 ) )
495502 return ;
496503 if ( cmds [ 1 ] == "unit" )
497- { // save as unit
504+ {
505+ // save as unit
498506 if ( IsInvalidArgumentCount ( cmds , 4 ) )
499507 return ;
500- if ( func . type != Component . Type . Frac
508+ if ( func . type != Component . Type . Frac
501509 || ( func . type == Component . Type . Op &&
502510 func . op is not Component . Operator . Multiply or Component . Operator . Divide )
503511 || func . x ? . type != Component . Type . Var || func . y ? . type != Component . Type . Var )
504512 {
505513 Console . WriteLine ( $ "Error: Cannot convert operation { func } to a unit") ;
506514 return ;
507515 }
516+
508517 var pkg = unitPackages . GetOrAdd ( cmds [ 2 ] , id => new UnitPackage ( id ) ) ;
509518 var result = pkg . Get ( cmds [ 3 ] ) ;
510519 var unitA = func . x ? . unitX ? . ToUnit ( ctx ) ? . Unit ?? Unit . None ;
511520 var unitB = func . y ? . unitX ? . ToUnit ( ctx ) ? . Unit ?? Unit . None ;
512-
513- if ( func . type == Component . Type . Frac || ( func . type == Component . Type . Op && func . op == Component . Operator . Divide ) )
521+
522+ if ( func . type == Component . Type . Frac ||
523+ ( func . type == Component . Type . Op && func . op == Component . Operator . Divide ) )
514524 {
515525 unitA . AddQuotient ( unitB , result ) ;
516526 unitB . AddQuotient ( unitA , result ) ;
@@ -520,8 +530,12 @@ private static void CmdSave(string[] cmds, string? f, Component func, MathContex
520530 unitA . AddProduct ( unitB , result ) ;
521531 unitB . AddProduct ( unitA , result ) ;
522532 }
523- else throw new Exception ( "Assertion failure" ) ;
524- } else
533+ else
534+ {
535+ throw new Exception ( "Assertion failure" ) ;
536+ }
537+ }
538+ else
525539 {
526540 var data = f ?? func . ToString ( ) ;
527541 if ( cmds . Length > 2 && cmds [ 2 ] == "-y" )
@@ -623,6 +637,7 @@ private static void CmdList(string[] cmds)
623637 Console . WriteLine ( "No saved functions" ) ;
624638 break ;
625639 }
640+
626641 Console . WriteLine ( "Available functions:" ) ;
627642 foreach ( var file in funcs )
628643 Console . WriteLine (
@@ -635,6 +650,7 @@ private static void CmdList(string[] cmds)
635650 Console . WriteLine ( "No available constants" ) ;
636651 break ;
637652 }
653+
638654 Console . WriteLine ( "Available constants:" ) ;
639655 foreach ( var ( key , value ) in constants )
640656 Console . WriteLine ( $ "\t { key } \t = { value } ") ;
@@ -646,6 +662,7 @@ private static void CmdList(string[] cmds)
646662 Console . WriteLine ( "No functions in stash" ) ;
647663 break ;
648664 }
665+
649666 Console . WriteLine ( "Stashed Functions:" ) ;
650667 var i = 0 ;
651668 foreach ( var ( fx , ctx ) in stash )
@@ -661,8 +678,9 @@ private static void CmdList(string[] cmds)
661678 Console . WriteLine ( "No unit packs are enabled" ) ;
662679 break ;
663680 }
681+
664682 Console . WriteLine ( "Enabled unit packs:" ) ;
665- foreach ( var pack in enabledUnitPacks )
683+ foreach ( var pack in enabledUnitPacks )
666684 Console . WriteLine ( $ "\t - { pack } ") ;
667685 break ;
668686 case "packs" :
@@ -672,12 +690,14 @@ private static void CmdList(string[] cmds)
672690 Console . WriteLine ( "No unit packages defined" ) ;
673691 break ;
674692 }
693+
675694 Console . WriteLine ( "Available unit packages:" ) ;
676695 foreach ( var pack in directories )
677696 {
678697 var packName = new DirectoryInfo ( pack ) . Name . StripExtension ( UnitPackExt ) ;
679698 Console . WriteLine ( $ "\t - { packName } ") ;
680699 }
700+
681701 break ;
682702 case "units" :
683703 if ( IsInvalidArgumentCount ( cmds , 3 ) )
@@ -687,12 +707,14 @@ private static void CmdList(string[] cmds)
687707 Console . WriteLine ( $ "Error: Unit pack with name { cmds [ 2 ] } was not found") ;
688708 break ;
689709 }
710+
690711 var package = unitPackages [ cmds [ 2 ] ] ;
691712 if ( package . values . IsEmpty )
692713 {
693714 Console . WriteLine ( "No units loaded" ) ;
694715 break ;
695716 }
717+
696718 Console . WriteLine ( $ "Units in package '{ package . Name } ':") ;
697719 foreach ( var unit in package . values . Values )
698720 {
@@ -702,6 +724,7 @@ private static void CmdList(string[] cmds)
702724 foreach ( var ( dividend , result ) in unit . Quotients )
703725 Console . WriteLine ( $ "\t \t { unit } = { result } * { dividend } ") ;
704726 }
727+
705728 break ;
706729 default :
707730 Console . WriteLine (
@@ -720,11 +743,13 @@ private static void CmdToggleState(string[] cmds, bool newState)
720743 Console . WriteLine ( $ "Unit pack { desiredPack } does not exist") ;
721744 return ;
722745 }
746+
723747 if ( newState == enabledUnitPacks . Contains ( desiredPack ) )
724748 {
725749 Console . WriteLine ( $ "Unit pack { desiredPack } is already " + ( newState ? "enabled" : "disabled" ) ) ;
726750 return ;
727751 }
752+
728753 if ( newState )
729754 enabledUnitPacks . Add ( desiredPack ) ;
730755 else enabledUnitPacks . Remove ( desiredPack ) ;
@@ -927,7 +952,6 @@ public enum CalcMode : byte
927952public sealed class MathContext
928953{
929954 public readonly Dictionary < string , Component > var = new ( ) ;
930- public List < string > enabledUnitPacks { get ; init ; } = new ( ) ;
931955
932956 public MathContext ( ) : this ( null )
933957 {
@@ -947,8 +971,13 @@ public MathContext(Dictionary<string, Component>? copy, List<string>? enabledUni
947971 this . enabledUnitPacks . Add ( pack ) ;
948972 }
949973
950- public UnitPackage [ ] GetUnitPackages ( ) => Program . unitPackages
951- . Where ( pkg => enabledUnitPacks . Contains ( pkg . Key ) )
952- . Select ( pkg => pkg . Value )
953- . ToArray ( ) ;
974+ public List < string > enabledUnitPacks { get ; init ; } = new ( ) ;
975+
976+ public UnitPackage [ ] GetUnitPackages ( )
977+ {
978+ return Program . unitPackages
979+ . Where ( pkg => enabledUnitPacks . Contains ( pkg . Key ) )
980+ . Select ( pkg => pkg . Value )
981+ . ToArray ( ) ;
982+ }
954983}
0 commit comments