Skip to content

Commit 7af6da2

Browse files
authored
Add files via upload
1 parent 1ca8059 commit 7af6da2

File tree

4 files changed

+103
-54
lines changed

4 files changed

+103
-54
lines changed

Deque.cs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,19 @@ public class Node {
77
public T data;
88
public Node prev, next;
99

10-
public Node() {}
10+
public Node() {
11+
data = default!; prev = default!; next = default!;
12+
}
1113
public Node(T d, Node p, Node n) {
1214
data = d; prev = p; next = n;
1315
}
1416
}
1517

16-
public DequeIterator iterator;
17-
private Node head, tail;
18+
public readonly DequeIterator iterator;
19+
private readonly Node head, tail;
1820
private int size;
19-
20-
private void Init() {
21+
22+
public Deque() {
2123
head = new Node();
2224
tail = new Node();
2325
head.next = tail;
@@ -26,13 +28,15 @@ private void Init() {
2628
tail.next = tail;
2729
iterator = new DequeIterator(this);
2830
}
29-
30-
public Deque() {
31-
Init();
32-
}
3331

3432
public Deque(IEnumerable<T> init) {
35-
Init();
33+
head = new Node();
34+
tail = new Node();
35+
head.next = tail;
36+
head.prev = head;
37+
tail.prev = head;
38+
tail.next = tail;
39+
iterator = new DequeIterator(this);
3640
foreach (var i in init)
3741
Append(i);
3842
}
@@ -153,7 +157,7 @@ public DequeIterator GetIterator() {
153157

154158
public class DequeIterator {
155159
private Node curr;
156-
private Deque<T> deque;
160+
private readonly Deque<T> deque;
157161
private int index = -1;
158162

159163
public DequeIterator(Deque<T> deq) {

EntryPoint.cs

Lines changed: 79 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,94 @@
11
using propcalc;
22

3-
if (args.Length == 0) {
4-
Console.WriteLine("Must specify file to open.");
5-
Environment.Exit(1);
6-
}
7-
83

4+
bool[] mode = new bool[7];
95
const int no_table = 0;
106
const int props = 1;
117
const int compile = 2;
128
const int steps = 3;
139
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
1428

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+
}
2348
}
2449
}
2550

26-
bool isCompiled;
27-
string rawCode;
28-
string extension = Path.GetExtension(args[0]);
29-
string filenoext = Path.GetFileNameWithoutExtension(args[0]);
3051

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() {
3266
FileStream stream = new(filenoext + "_out.txt", FileMode.Create);
3367
StreamWriter writer = new(stream);
3468
writer.AutoFlush = false;
3569
Console.SetOut(writer);
3670
}
3771

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;
4785
}
4886

87+
4988
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);
5192
var split = Splitter.splitBrackets(tokens);
5293
var (atoms, atomBools) = Atomizer.atomize(split);
5394
compileCode(atoms, atomBools, metadata);
@@ -81,14 +122,19 @@ void interpret(object interpretableObject) {
81122
interpreter = new Interpreter(compilation);
82123
else throw new Exception("Object cannot be interpreted.");
83124

84-
interpreter.interpret();
125+
if (!mode[no_calc])
126+
interpreter.interpret();
85127

86-
if (isCompiled && mode[steps])
128+
if (fileIsCompiled && mode[steps])
87129
Steps.showSteps(args[0]);
88-
if (mode[props])
130+
if (mode[props] && !mode[no_calc])
89131
interpreter.properties();
90-
if (!mode[no_table])
132+
if (!mode[no_table] && !mode[no_calc])
91133
interpreter.table();
92134
}
93135

94-
Console.Out.Flush();
136+
137+
void exitProgram(int exitCode) {
138+
Console.Out.Flush();
139+
Environment.Exit(exitCode);
140+
}

Interpreter.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System.Text;
21
using static propcalc.Compiler;
32
using static propcalc.Logic;
43

Tokenizer.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,18 @@ public class Tokenizer {
1111
private const string regexReduceSpaces = " {2,}";
1212

1313

14-
public static (Deque<string>, string[]) tokenize(string filename, out string raw) {
15-
string str = File.ReadAllText(filename);
14+
public static (Deque<string>, string[]) tokenize(string filename, ref string raw, bool isShell) {
15+
string str = isShell ? raw : File.ReadAllText(filename);
1616
string[] metadata = extractExplicitOrder(str, filename);
17-
18-
if (metadata.Length == 1)
17+
18+
if (metadata.Length <= 1)
1919
metadata = extractImplicitOrder(str, filename);
2020
else str = str.Replace(
21-
Regex.Matches(str, regexOrder)
22-
.Select(m => m.Value)
23-
.ToArray()[0]
24-
.ToString(),
25-
""); // Remove order statement from source code
21+
Regex.Matches(str, regexOrder)
22+
.Select(m => m.Value)
23+
.ToArray()[0]
24+
.ToString(),
25+
""); // Remove order statement from source code
2626

2727
str = Regex.Replace(str, regexSpecialSpaces, " ");
2828
str = Regex.Replace(str, regexReduceSpaces, " ");

0 commit comments

Comments
 (0)