1
1
using propcalc ;
2
2
3
- if ( args . Length == 0 ) {
4
- Console . WriteLine ( "Must specify file to open." ) ;
5
- Environment . Exit ( 1 ) ;
6
- }
7
-
8
3
4
+ bool [ ] mode = new bool [ 7 ] ;
9
5
const int no_table = 0 ;
10
6
const int props = 1 ;
11
7
const int compile = 2 ;
12
8
const int steps = 3 ;
13
9
const int output = 4 ;
10
+ const int shell = 5 ;
11
+ const int no_calc = 6 ;
12
+
13
+ bool fileIsCompiled = false ;
14
+ string rawCode = "" ;
15
+ string extension ;
16
+ string filenoext ;
17
+
18
+
19
+ // Program execution
20
+ getArguments ( ) ;
21
+ if ( mode [ shell ] && onShell ( ) )
22
+ exitProgram ( 0 ) ;
23
+ if ( mode [ output ] )
24
+ onOutput ( ) ;
25
+ if ( onFile ( ) )
26
+ exitProgram ( 0 ) ;
27
+ // On success end program
14
28
15
- bool [ ] mode = new bool [ 5 ] ;
16
- for ( int i = 1 ; i < args . Length ; ++ i ) {
17
- switch ( args [ i ] ) {
18
- case "no_table" : mode [ no_table ] = true ; break ;
19
- case "props" : mode [ props ] = true ; break ;
20
- case "compile" : mode [ compile ] = true ; break ;
21
- case "steps" : mode [ steps ] = true ; break ;
22
- case "out" : mode [ output ] = true ; break ;
29
+
30
+
31
+ void getArguments ( ) {
32
+ if ( args . Length == 0 )
33
+ args = new [ ] { "shell" } ; // Default execution path is shell
34
+
35
+ extension = Path . GetExtension ( args [ 0 ] ) ;
36
+ filenoext = Path . GetFileNameWithoutExtension ( args [ 0 ] ) ;
37
+
38
+ for ( int i = 0 ; i < args . Length ; ++ i ) {
39
+ switch ( args [ i ] ) {
40
+ case "no_table" : mode [ no_table ] = true ; break ;
41
+ case "props" : mode [ props ] = true ; break ;
42
+ case "compile" : mode [ compile ] = true ; break ;
43
+ case "steps" : mode [ steps ] = true ; break ;
44
+ case "out" : mode [ output ] = true ; break ;
45
+ case "shell" : mode [ shell ] = true ; break ;
46
+ case "no_calc" : mode [ no_calc ] = true ; break ;
47
+ }
23
48
}
24
49
}
25
50
26
- bool isCompiled ;
27
- string rawCode ;
28
- string extension = Path . GetExtension ( args [ 0 ] ) ;
29
- string filenoext = Path . GetFileNameWithoutExtension ( args [ 0 ] ) ;
30
51
31
- if ( mode [ output ] ) {
52
+ bool onShell ( ) {
53
+ Console . WriteLine ( "Enter a formula:" ) ;
54
+ rawCode = Console . ReadLine ( ) ?? string . Empty ;
55
+ Console . WriteLine ( ) ;
56
+
57
+ if ( mode [ output ] )
58
+ onOutput ( ) ;
59
+
60
+ parse ( ) ;
61
+ return true ;
62
+ }
63
+
64
+
65
+ void onOutput ( ) {
32
66
FileStream stream = new ( filenoext + "_out.txt" , FileMode . Create ) ;
33
67
StreamWriter writer = new ( stream ) ;
34
68
writer . AutoFlush = false ;
35
69
Console . SetOut ( writer ) ;
36
70
}
37
71
38
- switch ( extension ) {
39
- case ".pcl" :
40
- isCompiled = true ;
41
- interpret ( args [ 0 ] ) ;
42
- break ;
43
- default :
44
- isCompiled = false ;
45
- parse ( ) ;
46
- break ;
72
+
73
+ bool onFile ( ) {
74
+ switch ( extension ) {
75
+ case ".pcl" :
76
+ fileIsCompiled = true ;
77
+ interpret ( args [ 0 ] ) ;
78
+ break ;
79
+ default :
80
+ fileIsCompiled = false ;
81
+ parse ( ) ;
82
+ break ;
83
+ }
84
+ return true ;
47
85
}
48
86
87
+
49
88
void parse ( ) {
50
- var ( tokens , metadata ) = Tokenizer . tokenize ( args [ 0 ] , out rawCode ) ;
89
+ var ( tokens , metadata ) = Tokenizer . tokenize ( args [ 0 ] , ref rawCode , mode [ shell ] ) ;
90
+ if ( rawCode . Length == 0 ) // No code, exit program
91
+ exitProgram ( 3 ) ;
51
92
var split = Splitter . splitBrackets ( tokens ) ;
52
93
var ( atoms , atomBools ) = Atomizer . atomize ( split ) ;
53
94
compileCode ( atoms , atomBools , metadata ) ;
@@ -81,14 +122,19 @@ void interpret(object interpretableObject) {
81
122
interpreter = new Interpreter ( compilation ) ;
82
123
else throw new Exception ( "Object cannot be interpreted." ) ;
83
124
84
- interpreter . interpret ( ) ;
125
+ if ( ! mode [ no_calc ] )
126
+ interpreter . interpret ( ) ;
85
127
86
- if ( isCompiled && mode [ steps ] )
128
+ if ( fileIsCompiled && mode [ steps ] )
87
129
Steps . showSteps ( args [ 0 ] ) ;
88
- if ( mode [ props ] )
130
+ if ( mode [ props ] && ! mode [ no_calc ] )
89
131
interpreter . properties ( ) ;
90
- if ( ! mode [ no_table ] )
132
+ if ( ! mode [ no_table ] && ! mode [ no_calc ] )
91
133
interpreter . table ( ) ;
92
134
}
93
135
94
- Console . Out . Flush ( ) ;
136
+
137
+ void exitProgram ( int exitCode ) {
138
+ Console . Out . Flush ( ) ;
139
+ Environment . Exit ( exitCode ) ;
140
+ }
0 commit comments