1- using System . Collections . Immutable ;
2- using System . Globalization ;
1+ using System . Globalization ;
32using System . Text . RegularExpressions ;
43using Antlr4 . Runtime ;
5- using Antlr4 . Runtime . Atn ;
64using clmath . Antlr ;
75
86namespace clmath ;
@@ -11,7 +9,10 @@ public static class Program
119{
1210 private static readonly string FuncExt = ".math" ;
1311 private static readonly string ConstExt = ".vars" ;
14- private static readonly string dir = Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . MyDocuments ) , "comroid" , "clmath" ) ;
12+
13+ private static readonly string dir = Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . MyDocuments ) ,
14+ "comroid" , "clmath" ) ;
15+
1516 private static readonly string constantsFile = Path . Combine ( dir , "constants" + ConstExt ) ;
1617
1718 private static bool _exiting ;
@@ -24,18 +25,19 @@ public static class Program
2425 { "e" , Math . E } ,
2526 { "tau" , Math . Tau }
2627 } ;
27- internal static Dictionary < string , double > constants { get ; private set ; } = null ! ;
2828
2929 static Program ( )
3030 {
3131 CultureInfo . CurrentCulture = CultureInfo . InvariantCulture ;
3232 if ( ! Directory . Exists ( dir ) )
3333 Directory . CreateDirectory ( dir ) ;
34- if ( ! File . Exists ( constantsFile ) )
35- SaveConstants ( new ( ) ) ;
34+ if ( ! File . Exists ( constantsFile ) )
35+ SaveConstants ( new Dictionary < string , double > ( ) ) ;
3636 LoadConstants ( ) ;
3737 }
3838
39+ internal static Dictionary < string , double > constants { get ; private set ; } = null ! ;
40+
3941 private static void SaveConstants ( Dictionary < string , double > ? values = null )
4042 {
4143 values ??= constants ;
@@ -46,7 +48,7 @@ private static void LoadConstants()
4648 {
4749 // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
4850 if ( constants == null )
49- constants = new ( ) ;
51+ constants = new Dictionary < string , double > ( ) ;
5052 else constants . Clear ( ) ;
5153 foreach ( var ( key , value ) in globalConstants )
5254 constants [ key ] = value ;
@@ -57,17 +59,17 @@ private static void LoadConstants()
5759 private static string ConvertValuesToString ( Dictionary < string , Component > values , Func < string , bool > ? skip = null )
5860 {
5961 skip ??= _ => false ;
60- string txt = string . Empty ;
62+ var txt = string . Empty ;
6163 foreach ( var ( key , value ) in values )
6264 if ( ! skip ( key ) )
6365 txt += $ "{ key } = { value } \n ";
6466 return txt ;
6567 }
66-
68+
6769 private static string ConvertValuesToString ( Dictionary < string , double > values , Func < string , bool > ? skip = null )
6870 {
6971 skip ??= _ => false ;
70- string txt = string . Empty ;
72+ var txt = string . Empty ;
7173 foreach ( var ( key , value ) in values )
7274 if ( ! skip ( key ) )
7375 txt += $ "{ key } = { value } \n ";
@@ -103,7 +105,9 @@ public static void Main(string[] args)
103105 else
104106 {
105107 if ( args [ 0 ] == "graph" )
108+ {
106109 StartGraph ( CreateArgsFuncs ( args ) ) ;
110+ }
107111 else
108112 {
109113 var arg = string . Join ( " " , args ) ;
@@ -116,8 +120,8 @@ public static void Main(string[] args)
116120
117121 private static string CleanupString ( string str )
118122 {
119- int leadingSpaces = 0 ;
120- for ( int i = 0 ; i < str . Length && str [ i ] == ' ' ; i ++ )
123+ var leadingSpaces = 0 ;
124+ for ( var i = 0 ; i < str . Length && str [ i ] == ' ' ; i ++ )
121125 leadingSpaces ++ ;
122126 return str . Substring ( leadingSpaces , str . Length - leadingSpaces ) ;
123127 }
@@ -152,16 +156,18 @@ private static void StdIoMode()
152156 break ;
153157 case "set" :
154158 var setConstN = ConvertValueFromString ( func . Substring ( "set " . Length , func . Length - "set " . Length ) ) ;
155- if ( setConstN is not { } setConst )
159+ if ( setConstN is not { } setConst )
156160 {
157161 Console . WriteLine ( "Error: Invalid declaration of constant variable; try 'x = 5'" ) ;
158162 break ;
159163 }
164+
160165 if ( globalConstants . ContainsKey ( setConst . key ) )
161166 {
162167 Console . WriteLine ( $ "Error: Cannot redefine { setConst . key } ") ;
163168 break ;
164169 }
170+
165171 constants [ setConst . key ] = setConst . value . Evaluate ( null ) ;
166172 SaveConstants ( ) ;
167173 break ;
@@ -173,11 +179,13 @@ private static void StdIoMode()
173179 Console . WriteLine ( $ "Error: Cannot unset { cmds [ 1 ] } ") ;
174180 break ;
175181 }
182+
176183 if ( ! constants . ContainsKey ( cmds [ 1 ] ) )
177184 {
178185 Console . WriteLine ( $ "Error: Unknown constant { cmds [ 1 ] } ") ;
179186 break ;
180187 }
188+
181189 constants . Remove ( cmds [ 1 ] ) ;
182190 SaveConstants ( ) ;
183191 break ;
@@ -187,53 +195,66 @@ private static void StdIoMode()
187195 Console . WriteLine ( "Error: Listing target unspecified; options are 'funcs' and 'constants'" ) ;
188196 break ;
189197 }
198+
190199 switch ( cmds [ 1 ] )
191200 {
192201 case "funcs" or "fx" :
193202 var funcs = Directory . EnumerateFiles ( dir , "*.math" ) . Select ( p => new FileInfo ( p ) ) . ToArray ( ) ;
194203 if ( funcs . Length == 0 )
204+ {
195205 Console . WriteLine ( "No saved functions" ) ;
206+ }
196207 else
197208 {
198209 Console . WriteLine ( "Available functions:" ) ;
199210 foreach ( var file in funcs )
200- Console . WriteLine ( $ "\t - { file . Name . Substring ( 0 , file . Name . Length - FuncExt . Length ) } ") ;
211+ Console . WriteLine (
212+ $ "\t - { file . Name . Substring ( 0 , file . Name . Length - FuncExt . Length ) } ") ;
201213 }
214+
202215 break ;
203216 case "constants" or "const" :
204- if ( constants . Count == 0 )
217+ if ( constants . Count == 0 )
218+ {
205219 Console . WriteLine ( "No available constants" ) ;
220+ }
206221 else
207222 {
208223 Console . WriteLine ( "Available constants:" ) ;
209224 foreach ( var ( key , value ) in constants )
210225 Console . WriteLine ( $ "\t { key } \t = { value } ") ;
211226 }
227+
212228 break ;
213229 case "stash" :
214230 if ( stash . Count == 0 )
231+ {
215232 Console . WriteLine ( "No functions in stash" ) ;
233+ }
216234 else
217235 {
218236 Console . WriteLine ( "Stashed Functions:" ) ;
219- int i = 0 ;
237+ var i = 0 ;
220238 foreach ( var ( fx , ctx ) in stash )
221239 {
222240 Console . WriteLine ( $ "\t stash[{ i ++ } ]\t = { fx } ") ;
223- ctx . DumpVariables ( "stash[#]" . Length / 8 + 1 , shouldError : false ) ;
241+ ctx . DumpVariables ( "stash[#]" . Length / 8 + 1 , false ) ;
224242 }
225243 }
244+
226245 break ;
227246 default :
228- Console . WriteLine ( $ "Error: Unknown listing target '{ cmds [ 1 ] } '; options are 'funcs', 'constants' and 'stash'") ;
247+ Console . WriteLine (
248+ $ "Error: Unknown listing target '{ cmds [ 1 ] } '; options are 'funcs', 'constants' and 'stash'") ;
229249 break ;
230250 }
251+
231252 break ;
232253 case "load" :
233254 if ( ! IsInvalidArgumentCount ( cmds , 2 ) )
234255 {
235256 var load = LoadFunc ( cmds [ 1 ] ) ;
236- if ( load is { } res )
257+ if ( load is { } res )
237258 EvalFunc ( res . func , ctx : res . ctx ) ;
238259 }
239260
@@ -256,13 +277,18 @@ private static void StdIoMode()
256277 File . Delete ( path0 ) ;
257278 Console . WriteLine ( $ "Function with name { cmds [ 1 ] } deleted") ;
258279 }
259- else Console . WriteLine ( $ "Function with name { cmds [ 1 ] } not found") ;
280+ else
281+ {
282+ Console . WriteLine ( $ "Function with name { cmds [ 1 ] } not found") ;
283+ }
260284
261285 break ;
262286 case "restore" :
263287 ( Component func , MathContext ctx ) entry ;
264288 if ( cmds . Length == 1 )
289+ {
265290 entry = stash . Pop ( ) ;
291+ }
266292 else
267293 {
268294 if ( Regex . Match ( cmds [ 1 ] , "\\ d+" ) is { Success : true } )
@@ -273,6 +299,7 @@ private static void StdIoMode()
273299 Console . WriteLine ( $ "Error: Backtrace index { index } too large") ;
274300 break ;
275301 }
302+
276303 entry = stash . ToArray ( ) [ index ] ;
277304 var bak = stash . ToList ( ) ;
278305 bak . Remove ( entry ) ;
@@ -286,6 +313,7 @@ private static void StdIoMode()
286313 break ;
287314 }
288315 }
316+
289317 EvalFunc ( entry . func , ctx : entry . ctx ) ;
290318 break ;
291319 case "graph" :
@@ -315,15 +343,20 @@ internal static (Component func, MathContext ctx)? LoadFunc(string name)
315343 Console . WriteLine ( $ "Function with name { name } not found") ;
316344 return null ;
317345 }
346+
318347 var data = File . ReadAllText ( path ) ;
319348 var lnb = data . IndexOf ( "\n " , StringComparison . Ordinal ) ;
320349 MathContext ctx ;
321350 if ( lnb != - 1 )
322351 {
323352 var vars = ConvertValuesFromString ( data . Substring ( lnb + 1 , data . Length - lnb - 2 ) ) ;
324- ctx = new ( vars ) ;
353+ ctx = new MathContext ( vars ) ;
354+ }
355+ else
356+ {
357+ ctx = new MathContext ( ) ;
325358 }
326- else ctx = new ( ) ;
359+
327360 return ( ParseFunc ( lnb == - 1 ? data : data . Substring ( 0 , lnb ) ) , ctx ) ;
328361 }
329362
@@ -379,7 +412,7 @@ private static void EvalFunc(Component func, string? f = null, MathContext? ctx
379412 else if ( constants . ContainsKey ( key ) )
380413 Console . WriteLine ( $ "Error: Cannot redefine { key } ") ;
381414 else ctx . var [ key ] = value ;
382-
415+
383416 if ( FindMissingVariables ( func , ctx ) . Count == 0 )
384417 PrintResult ( func , func . Evaluate ( ctx ) ) ;
385418 }
@@ -400,7 +433,8 @@ private static void EvalFunc(Component func, string? f = null, MathContext? ctx
400433 Console . WriteLine ( "\t drop\t \t Drops the current function" ) ;
401434 Console . WriteLine ( "\t clear [var]\t Clears all variables or just one from the cache" ) ;
402435 Console . WriteLine ( "\t dump\t \t Prints all variables in the cache" ) ;
403- Console . WriteLine ( "\t save <name>\t Saves the current function with the given name; append '-y' to store current variables as well" ) ;
436+ Console . WriteLine (
437+ "\t save <name>\t Saves the current function with the given name; append '-y' to store current variables as well" ) ;
404438 Console . WriteLine ( "\t stash\t \t Stores the function in stash" ) ;
405439 Console . WriteLine ( "\t graph\t \t Displays the function in a 2D graph" ) ;
406440 Console . WriteLine (
@@ -413,7 +447,7 @@ private static void EvalFunc(Component func, string? f = null, MathContext? ctx
413447 case "save" :
414448 if ( IsInvalidArgumentCount ( cmds , 2 ) )
415449 break ;
416- string data = f ?? func . ToString ( ) ;
450+ var data = f ?? func . ToString ( ) ;
417451 if ( cmds . Length > 2 && cmds [ 2 ] == "-y" )
418452 data += $ "\n { ConvertValuesToString ( ctx . var , globalConstants . ContainsKey ) } ";
419453 var path = Path . Combine ( dir , cmds [ 1 ] + FuncExt ) ;
@@ -432,7 +466,11 @@ private static void EvalFunc(Component func, string? f = null, MathContext? ctx
432466 ctx . var . Remove ( cmds [ 1 ] ) ;
433467 Console . WriteLine ( $ "Variable { cmds [ 1 ] } deleted") ;
434468 }
435- else ctx . var . Clear ( ) ;
469+ else
470+ {
471+ ctx . var . Clear ( ) ;
472+ }
473+
436474 break ;
437475 case "stash" :
438476 stash . Push ( ( func , ctx ) ) ;
@@ -450,7 +488,11 @@ private static void EvalFunc(Component func, string? f = null, MathContext? ctx
450488 Console . WriteLine (
451489 $ "Error: Missing variable{ ( missing . Count != 1 ? "s" : "" ) } { string . Join ( ", " , missing ) } ") ;
452490 }
453- else PrintResult ( func , func . Evaluate ( ctx ) , ctx ) ;
491+ else
492+ {
493+ PrintResult ( func , func . Evaluate ( ctx ) , ctx ) ;
494+ }
495+
454496 break ;
455497 default :
456498 Console . WriteLine ( "Error: Unknown command; type 'help' for a list of commands" ) ;
@@ -486,13 +528,16 @@ private static int DumpVariables(this MathContext ctx, int alignBase = 1, bool s
486528 Console . WriteLine ( "Error: No variables are set" ) ;
487529 return 1 ;
488530 }
489- int maxAlign = ctx . var . Keys . Max ( key => key . Length ) / 8 ;
531+
532+ var maxAlign = ctx . var . Keys . Max ( key => key . Length ) / 8 ;
490533 foreach ( var ( key , val ) in ctx . var )
491534 {
492- var align = Math . Max ( maxAlign > 0 ? maxAlign - alignBase : alignBase , maxAlign - ( key . Length / 8 + 1 ) + alignBase ) ;
535+ var align = Math . Max ( maxAlign > 0 ? maxAlign - alignBase : alignBase ,
536+ maxAlign - ( key . Length / 8 + 1 ) + alignBase ) ;
493537 var spacer = Enumerable . Range ( 0 , align ) . Aggregate ( string . Empty , ( str , _ ) => str + '\t ' ) ;
494538 Console . WriteLine ( $ "\t { key } { spacer } = { val } ") ;
495539 }
540+
496541 return maxAlign ;
497542 }
498543
0 commit comments