-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCompilerService.cs
More file actions
119 lines (104 loc) · 3.45 KB
/
Copy pathCompilerService.cs
File metadata and controls
119 lines (104 loc) · 3.45 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ScriptEngine.Compiler;
using ScriptEngine.Environment;
using ScriptEngine.Machine;
namespace ScriptEngine
{
public class CompilerService
{
SymbolScope _scope;
ModuleCompilerContext _currentContext;
List<int> _predefinedVariables = new List<int>();
internal CompilerService(CompilerContext outerContext)
{
_currentContext = new ModuleCompilerContext(outerContext);
}
public int DefineVariable(string name, SymbolType type)
{
RegisterScopeIfNeeded();
try
{
int varIdx;
if (type == SymbolType.Variable)
varIdx = _currentContext.DefineVariable(name).CodeIndex;
else
varIdx = _currentContext.DefineProperty(name).CodeIndex;
_predefinedVariables.Add(varIdx);
return varIdx;
}
catch
{
_currentContext.PopScope();
_scope = null;
throw;
}
}
public int DefineMethod(MethodInfo methodInfo)
{
RegisterScopeIfNeeded();
return _currentContext.DefineMethod(methodInfo).CodeIndex;
}
private void RegisterScopeIfNeeded()
{
if (_scope == null)
{
_scope = new SymbolScope();
_currentContext.PushScope(_scope);
}
}
public ScriptModuleHandle CreateModule(ICodeSource source)
{
try
{
return Compile(source);
}
finally
{
_currentContext.PopScope();
_scope = null;
}
}
private ScriptModuleHandle Compile(ICodeSource source)
{
RegisterScopeIfNeeded();
var parser = new Parser();
parser.Code = source.Code;
var compiler = new Compiler.Compiler();
ModuleImage compiledImage;
try
{
compiledImage = compiler.Compile(parser, _currentContext);
}
catch (ScriptException e)
{
e.ModuleName = source.SourceDescription;
throw;
}
foreach (var item in _predefinedVariables)
{
var varDef = _scope.GetVariable(item);
if (varDef.Type == SymbolType.ContextProperty)
{
compiledImage.ExportedProperties.Add(new ExportedSymbol()
{
SymbolicName = varDef.Identifier,
Index = varDef.Index
});
}
}
var mi = new ModuleInformation();
mi.CodeIndexer = parser.GetCodeIndexer();
// пока у модулей нет собственных имен, будет совпадать с источником модуля
mi.ModuleName = source.SourceDescription;
mi.Origin = source.SourceDescription;
compiledImage.ModuleInfo = mi;
return new ScriptModuleHandle()
{
Module = compiledImage
};
}
}
}