-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompiler.cs
More file actions
231 lines (194 loc) · 5.57 KB
/
Compiler.cs
File metadata and controls
231 lines (194 loc) · 5.57 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Kento.Utility;
namespace Kento
{
internal enum FallThroughType
{
Break = 0,
Continue,
Return,
NoFallthrough
}
struct DebugOptions
{
public bool OutputProfilerInfo { get; set; }
public bool OutputMemoryAllocation { get; set; }
public bool OutputDestruction { get; set; }
}
internal class Compiler
{
private static Stopwatch timer;
private static readonly LinkedList<Scope> scopeList = new LinkedList<Scope>();
private static readonly List<Value> memory = new List<Value>();
private static readonly Stack<int> availabilityStack = new Stack<int>();
private static readonly List<LinkedList<Reference>> referenceList = new List<LinkedList<Reference>>();
public static DebugOptions Options { get; set; }
static Compiler ()
{
Fallthrough = FallThroughType.NoFallthrough;
}
public static float RunningTime
{
get { return timer.ElapsedTicks / (float)Stopwatch.Frequency * 1000; }
}
public static bool Runtime { get; set; }
public static Scope GlobalScope { get; set; }
public static FallThroughType Fallthrough { get; set; }
public static bool PendingDot { get; set; }
public static bool PendingDotIsStatic { get; set; }
public static bool StoreValuesOutOfScope { get; set; }
public static Value GetValue ( int Index )
{
Value result = NoValue.Value;
if ( Index < memory.Count && Index >= 0 )
{
result = memory[ Index ];
}
return result;
}
public static void SetValue ( int Index, Value Value )
{
if ( Index < memory.Count && Index > 0 ) memory[ Index ] = Value;
}
public static Reference Identify ( string Name )
{
for ( LinkedListNode<Scope> node = scopeList.Last ; node != null ; node = node.Previous )
{
if ( node.Value.ContainsKey( Name ) ) return node.Value[ Name ];
if ( node.Value.Blocking ) break;
}
if ( GlobalScope.ContainsKey( Name ) ) return GlobalScope[ Name ];
return NullReference.Value;
}
public static void SetAlias ( string Name, Reference Reference )
{
Reference.Accessable = true;
scopeList.Last.Value[ Name ] = Reference;
}
public static void SetAlias ( string Name, int Index )
{
SetAlias( Name, new Reference( Index ) );
}
public static void SetAsCurrentScope ( Scope Scope )
{
Scope.Blocking = true;
scopeList.AddLast( Scope );
}
public static void EnterScope ()
{
scopeList.AddLast( new Scope() );
}
public static void ExitScope ( bool DoNotDestroy = false )
{
Scope scope = scopeList.Last.Value;
if ( !DoNotDestroy )
{
foreach ( var pair in scope )
{
pair.Value.Accessable = false;
}
}
foreach ( var reference in scope.NewReferences )
{
if ( !reference.Accessable )
{
if ( Options.OutputDestruction ) Console.WriteLine( "[DEBUG] Destroying reference identifiable by: " + reference.Identifier );
reference.Dereference();
}
}
scope.Blocking = false;
scopeList.RemoveLast();
}
public static Scope GetCurrentScope ()
{
return scopeList.Last.Value;
}
public static int StoreValue ( Value Value, bool ForceNew = false )
{
if ( Value is Reference ) return ( Value as Reference ).Index;
if ( Value is Literal ) ForceNew = true;
int foundIndex = memory.IndexOf( Value );
if ( !ForceNew && foundIndex != -1 ) return foundIndex;
int index;
if ( availabilityStack.Count > 0 )
{
index = availabilityStack.Pop();
memory[ index ] = Value;
} else
{
index = memory.Count;
memory.Add( Value );
referenceList.Add( new LinkedList<Reference>() );
}
if ( Options.OutputMemoryAllocation ) Console.WriteLine( "[DEBUG] New memory reservation at {0}", index );
return index;
}
public static void Destroy ( int Index )
{
memory[ Index ].Destroy();
FreeMemoy( Index );
}
public static void FreeMemoy ( int Index )
{
memory[ Index ] = null;
availabilityStack.Push( Index );
}
public static void RegisterReference ( Reference Reference, int Index )
{
if ( memory[ Index ] == null ) return;
referenceList[ Index ].AddLast( Reference );
if ( scopeList.Count > 0 )
{
if ( StoreValuesOutOfScope && scopeList.Last.Previous != null )
scopeList.Last.Previous.Value.NewReferences.AddLast( Reference );
else
scopeList.Last.Value.NewReferences.AddLast( Reference );
}
}
public static void Dereference ( Reference Reference, int Index )
{
referenceList[ Index ].Remove( Reference );
if ( memory[ Index ] == null ) return;
if ( referenceList[ Index ].Count == 0 ) Destroy( Index );
}
public static void Run ( List<Token> Code )
{
StandardLibrary.Load();
Scope defaultScope = StandardLibrary.Library;
GlobalScope = defaultScope;
scopeList.AddLast( defaultScope );
Runtime = true;
timer = new Stopwatch();
timer.Start();
new CodeBlock( Code ).Run();
timer.Stop();
}
public static void RunFromFile ( string Path, DebugOptions Options )
{
string source;
using ( var reader = new StreamReader( Path ) )
{
source = reader.ReadToEnd();
}
RunFromSource( source, Options );
}
public static void RunFromSource ( string Code, DebugOptions Options )
{
Compiler.Options = Options;
Run( Tokenizer.ParseInfixString( Code ).Tokenize() );
if ( Options.OutputProfilerInfo ) Profiler.OutputTime();
}
internal static Reference Reserve ( object Sender )
{
return new Reference( StoreValue( NoValue.Value, true ) );
}
public static int GetMemoryUsage ()
{
return memory.Count - availabilityStack.Count;
}
}
}