-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPurityAL.cs
172 lines (156 loc) · 6.9 KB
/
PurityAL.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
Handles everything related to the Purity Asyncronous Language.
*/
using System.Collections.Generic;
using System;
using System.Linq;
using System.Text;
namespace PurityEngine.AL {
[Serializable]
[Obsolete("Purity AL is currently not supported. We may pick it back up in the future, but at the moment, it is not supported.")]
public class PurityALException : System.Exception
{
public PurityALException() { }
public PurityALException(string message) : base(message) { }
public PurityALException(string message, System.Exception inner) : base(message, inner) { }
protected PurityALException(
System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext context) : base(info, context) { }
}
[Obsolete("Purity AL is currently not supported. We may pick it back up in the future, but at the moment, it is not supported.")]
public class Compiler {
public delegate void AsyncReciever(byte b);
public List<string> memLocs = new List<string>();
[Obsolete("Purity AL is currently not supported. We may pick it back up in the future, but at the moment, it is not supported.")]
public Operation Compile(string op) {
try {
string[] keywords = splitIntoKeywords(op);
if (keywords[0] != "if") {
throw new Exception();
}
Operation output = new Operation();
output.opcode = Opcode.If;
output.condition = Compile(keywords[1]);
output.ifTrue = Compile(keywords[2]);
return output;
} catch {}
try {
string[] keywords = splitIntoKeywords(op);
if (keywords[0] != "noop") {
throw new Exception();
}
Operation output = new Operation();
output.opcode = Opcode.Noop;
return output;
} catch {}
throw new PurityALException("Could not compile block: " + op);
}
[Obsolete("Purity AL is currently not supported. We may pick it back up in the future, but at the moment, it is not supported.")]
public string[] splitIntoKeywords(string s) {
string x = s;
while (x.EndsWith(' ')) {
x = x.Remove(x.Length-1);
}
while (x.StartsWith(' ')) {
x = x.Remove(0,1);
}
List<string> keywords = new List<string>();
int depth = 0;
string token = "";
bool inString = false;
for (int i = 0; i < x.Length; i++) {
if (x[i] == '"') {
inString=!inString;
} else if (inString) {
// Do nothing...
} else if (x[i] == '{' || x[i] == '(') {
depth++;
} else if (x[i] == '}' || x[i] == ')') {
depth--;
if (token != "") {
keywords.Add(token);
}
} else if (x[i] == ' ' && depth == 0) {
if (token != "") {
keywords.Add(token);
}
token = "";
} else {
token += x[i];
}
}
if (token != "") {
keywords.Add(token);
}
return keywords.ToArray();
}
[Obsolete("Purity AL is currently not supported. We may pick it back up in the future, but at the moment, it is not supported.")]
public int ReserveMemLoc(string varName) {
if (!memLocs.Contains(varName)) {
memLocs.Add(varName);
}
return memLocs.IndexOf(varName);
}
}
[Obsolete("Purity AL is currently not supported. We may pick it back up in the future, but at the moment, it is not supported.")]
public enum Opcode {
If,
Noop
}
[Obsolete("Purity AL is currently not supported. We may pick it back up in the future, but at the moment, it is not supported.")]
public class Operation {
public Opcode opcode;
public Operation ifTrue;
public Operation condition;
[Obsolete("Purity AL is currently not supported. We may pick it back up in the future, but at the moment, it is not supported.")]
public string ToString() {
switch (opcode) {
case Opcode.If:
return "if (" + condition.ToString() + ") {" + ifTrue.ToString() + "}";
case Opcode.Noop:
return "noop";
}
throw new NotImplementedException("Handling for " + opcode.ToString() + " in ToString() is not implemented yet.");
}
}
[Obsolete("Purity AL is currently not supported. We may pick it back up in the future, but at the moment, it is not supported.")]
public interface Runtime {
[Obsolete("Purity AL is currently not supported. We may pick it back up in the future, but at the moment, it is not supported.")]
public void Execute(Operation op, Compiler.AsyncReciever reciever);
}
[Obsolete("Purity AL is currently not supported. We may pick it back up in the future, but at the moment, it is not supported.")]
public class CPUVM : Runtime {
public Stack<long> stack = new Stack<long>();
public List<long> memory = new List<long>();
[Obsolete("Purity AL is currently not supported. We may pick it back up in the future, but at the moment, it is not supported.")]
public void Execute(Operation op, Compiler.AsyncReciever reciever) {
switch (op.opcode) {
case Opcode.If:
Execute(op.condition,reciever);
if (stack.Pop() == 1) {
Execute(op.ifTrue,reciever);
}
break;
case Opcode.Noop:
// It's a noop, whadd'ya expect
break;
default:
throw new NotImplementedException("Handling for opcode " + op.opcode.ToString() + " in the CPU VM has not been implemented yet.");
}
}
[Obsolete("Purity AL is currently not supported. We may pick it back up in the future, but at the moment, it is not supported.")]
public long Read(int index) {
while (memory.Count - 1 < index) {
memory.Add(0);
}
return memory[index];
}
[Obsolete("Purity AL is currently not supported. We may pick it back up in the future, but at the moment, it is not supported.")]
public void Write(int index, long value) {
while (memory.Count - 1 < index) {
memory.Add(0);
}
memory[index] = value;
}
}
}