-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathCompilerFrontendBase.cs
More file actions
161 lines (131 loc) · 5.29 KB
/
Copy pathCompilerFrontendBase.cs
File metadata and controls
161 lines (131 loc) · 5.29 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
/*----------------------------------------------------------
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 OneScript.Compilation.Binding;
using OneScript.DependencyInjection;
using OneScript.Execution;
using OneScript.Language;
using OneScript.Language.LexicalAnalysis;
using OneScript.Language.SyntaxAnalysis;
using OneScript.Language.SyntaxAnalysis.AstNodes;
using OneScript.Sources;
namespace OneScript.Compilation
{
public abstract class CompilerFrontendBase : ICompilerFrontend
{
protected CompilerFrontendBase(
PreprocessorHandlers handlers,
IErrorSink errorSink,
IServiceContainer services)
{
PreprocessorHandlers = handlers;
ErrorSink = errorSink;
Services = services;
}
public IErrorSink ErrorSink { get; }
protected IServiceContainer Services { get; }
private PreprocessorHandlers PreprocessorHandlers { get; }
public bool GenerateDebugCode { get; set; }
public bool GenerateCodeStat { get; set; }
public IList<string> PreprocessorDefinitions { get; } = new List<string>();
public SymbolTable SharedSymbols { get; set; }
public SymbolScope FillSymbols(Type targetType)
{
var symbolsProvider = Services.Resolve<TypeSymbolsProviderFactory>();
var typeSymbols = symbolsProvider.Get(targetType);
ModuleSymbols = new SymbolScope();
typeSymbols.FillSymbols(ModuleSymbols);
return ModuleSymbols;
}
private SymbolScope ModuleSymbols { get; set; }
public IExecutableModule Compile(SourceCode source, IBslProcess process, Type classType = null)
{
var lexer = CreatePreprocessor(source);
var symbols = PrepareSymbols();
var parsedModule = ParseSyntaxConstruction(lexer, source, p => p.ParseStatefulModule());
return CompileInternal(symbols, parsedModule, classType, process);
}
public IExecutableModule CompileExpression(SourceCode source)
{
var lexer = new DefaultLexer
{
Iterator = source.CreateIterator()
};
var symbols = PrepareSymbols();
var parsedModule = ParseSyntaxConstruction(lexer, source, p => p.ParseExpression());
return CompileExpressionInternal(symbols, parsedModule);
}
public IExecutableModule CompileBatch(SourceCode source)
{
var lexer = CreatePreprocessor(source);
var symbols = PrepareSymbols();
var parsedModule = ParseSyntaxConstruction(lexer, source, p => p.ParseStatefulModule());
return CompileBatchInternal(symbols, parsedModule);
}
protected abstract IExecutableModule CompileInternal(SymbolTable symbols, ModuleNode parsedModule, Type classType, IBslProcess process);
protected abstract IExecutableModule CompileExpressionInternal(SymbolTable symbols, ModuleNode parsedModule);
protected abstract IExecutableModule CompileBatchInternal(SymbolTable symbols, ModuleNode parsedModule);
private SymbolTable PrepareSymbols()
{
var actualTable = new SymbolTable();
if (SharedSymbols != default)
{
for (int i = 0; i < SharedSymbols.ScopeCount; i++)
{
actualTable.PushScope(SharedSymbols.GetScope(i), SharedSymbols.GetBinding(i));
}
}
ModuleSymbols ??= new SymbolScope();
actualTable.PushScope(ModuleSymbols, null);
return actualTable;
}
private ModuleNode ParseSyntaxConstruction(
ILexer lexer,
SourceCode source,
Func<DefaultBslParser, BslSyntaxNode> action)
{
var parser = new DefaultBslParser(
lexer,
ErrorSink,
PreprocessorHandlers);
ModuleNode moduleNode;
try
{
moduleNode = (ModuleNode) action(parser);
}
catch (SyntaxErrorException e)
{
e.ModuleName ??= source.Name;
throw;
}
return moduleNode;
}
private ILexer CreatePreprocessor(
SourceCode source)
{
var baseLexer = new DefaultLexer
{
Iterator = source.CreateIterator()
};
var conditionals = PreprocessorHandlers?.Get<ConditionalDirectiveHandler>();
if (conditionals != default)
{
foreach (var constant in PreprocessorDefinitions)
{
conditionals.Define(constant);
}
}
var lexer = new PreprocessingLexer(baseLexer)
{
Handlers = PreprocessorHandlers,
ErrorSink = ErrorSink
};
return lexer;
}
}
}