forked from EvilBeaver/OneScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModuleWriter.cs
More file actions
149 lines (137 loc) · 5.43 KB
/
ModuleWriter.cs
File metadata and controls
149 lines (137 loc) · 5.43 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
/*----------------------------------------------------------
This Source Code Form is subject to the terms of the
Mozilla Public License, v.2.0. If a copy of the MPL
was not distributed with this file, You can obtain one
at http://mozilla.org/MPL/2.0/.
----------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.IO;
using ScriptEngine.Environment;
using ScriptEngine.Machine;
namespace ScriptEngine.Compiler
{
public class ModuleWriter
{
readonly CompilerService _compiler;
public ModuleWriter(CompilerService compilerService)
{
_compiler = compilerService;
}
public void Write(TextWriter output, ICodeSource source)
{
var module = _compiler.Compile(source);
WriteImage(output, module);
}
public void Write(TextWriter output, ModuleImage module)
{
WriteImage(output, module);
}
private void WriteImage(TextWriter output, ModuleImage module)
{
output.WriteLine(".loadAt: {0}", module.LoadAddress);
output.WriteLine(".variableFrame:");
module.Variables.ForEach(x=>output.WriteLine(" " + x));
output.WriteLine(".constants");
for (int i = 0; i < module.Constants.Count; i++)
{
var item = module.Constants[i];
output.WriteLine(
String.Format("{0,-3}:type: {1}, val: {2}",
i,
Enum.GetName(typeof(DataType), item.Type),
item.Presentation));
}
output.WriteLine(".code");
for (int i = 0; i < module.Code.Count; i++)
{
var item = module.Code[i];
output.WriteLine(
String.Format("{0,-3}:({1,-10}{2,3})",
i,
Enum.GetName(typeof(OperationCode), item.Code),
item.Argument));
}
output.WriteLine(".procedures");
foreach (var item in module.Methods)
{
WriteMethodDefinition(output, item);
}
output.WriteLine(".varmap");
WriteSymbolMap(output, module.VariableRefs);
output.WriteLine(".procmap");
WriteSymbolMap(output, module.MethodRefs);
output.WriteLine(".moduleEntry");
output.WriteLine(module.EntryMethodIndex.ToString());
output.WriteLine(".exports");
WriteExports(output, module.ExportedProperties);
WriteExports(output, module.ExportedMethods);
}
private void WriteSymbolMap(TextWriter output, IList<SymbolBinding> map)
{
for (int i = 0; i < map.Count; i++)
{
var item = map[i];
output.Write(string.Format("{0,-3}:({1},{2})\n", i, item.ContextIndex, item.CodeIndex));
}
}
private void WriteAnnotationsList(TextWriter output, AnnotationDefinition[] annotations)
{
output.WriteLine(".annotations [");
foreach (var annotation in annotations)
{
output.Write(" {0}", annotation.Name);
if (annotation.ParamCount != 0)
{
var delimiter = ": ";
foreach (var parameter in annotation.Parameters)
{
output.Write(delimiter);
output.Write(parameter);
delimiter = ", ";
}
}
output.WriteLine("");
}
output.WriteLine("]");
}
private void WriteMethodDefinition(TextWriter output, MethodDescriptor item)
{
output.Write(item.Signature.IsFunction ? "Func " : "Proc ");
output.Write(string.Format("{0} entryPoint: {1}, frameSize:{2}\n",
item.Signature.Name,
item.EntryPoint,
item.Variables.Count));
if (item.Signature.AnnotationsCount != 0)
{
WriteAnnotationsList(output, item.Signature.Annotations);
}
output.Write(".args {0}\n", item.Signature.ArgCount);
if (item.Signature.Params != null)
{
for (int i = 0; i < item.Signature.Params.Length; i++)
{
output.Write("{0,-3}: ByVal={1}", i, item.Signature.Params[i].IsByValue);
if (item.Signature.Params[i].HasDefaultValue)
{
output.Write(" defValue: {0}\n", item.Signature.Params[i].DefaultValueIndex);
}
else
{
output.WriteLine();
}
}
}
output.WriteLine(".variables [");
item.Variables.ForEach(x=>output.WriteLine(" " + x));
output.WriteLine("]");
}
private void WriteExports(TextWriter output, IList<ExportedSymbol> exports)
{
for (int i = 0; i < exports.Count; i++)
{
output.WriteLine(String.Format("{0}:{1,-3}", exports[i].SymbolicName, exports[i].Index));
}
}
}
}