-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCompiler.cs
202 lines (173 loc) · 5.82 KB
/
Compiler.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
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
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Reflection;
using Axion.Core.Hierarchy;
using Axion.Core.Processing.Errors;
using Axion.Core.Processing.Lexical;
using Axion.Core.Processing.Syntactic;
using Axion.Core.Processing.Syntactic.Expressions;
using Axion.Core.Processing.Translation;
using Axion.Core.Processing.Traversal;
using Axion.Core.Processing.Traversal.Reducers;
using Axion.Specification;
using NLog;
using Module = Axion.Core.Hierarchy.Module;
namespace Axion.Core;
public static class Compiler {
static readonly Assembly coreAsm = Assembly.GetExecutingAssembly();
public static readonly string Version = coreAsm.GetName().Version!.ToString();
/// <summary>
/// Path to directory where compiler executable is located.
/// </summary>
public static readonly string WorkDir = AppDomain.CurrentDomain.BaseDirectory;
/// <summary>
/// Path to directory where generated output is located.
/// </summary>
public static readonly string OutDir = Path.Join(WorkDir, "output");
public static readonly Dictionary<string, INodeTranslator> Translators = new();
static readonly Logger logger = LogManager.GetCurrentClassLogger();
public static readonly Dictionary<Mode, Func<Unit, ProcessingOptions, object>>
CompilationSteps = new() {
{ Mode.Lexing, Lex },
{ Mode.Parsing, Parse },
{ Mode.Reduction, Reduce },
{ Mode.Translation, Translate }
};
public static Collection<INodeReducer> Reducers { get; } = new() {
new DataMembersReducer(),
new FunctionPipeReducer(),
new IsNotReducer(),
new NoBreakBlockReducer(),
new TupleUnwrappingReducer(),
new UnionTypeReducer()
};
public static string GetTempSourceFilePath() {
return Path.Combine(
WorkDir,
"temp",
"tmp-" + DateTime.Now.ToFileName() + Spec.FileExtension
);
}
public static void AddTranslator(
string name,
INodeTranslator translator
) {
Translators[name.Trim().ToLowerInvariant()] = translator;
}
public static object? Process(
Project project,
ProcessingOptions options
) {
logger.Debug($"Processing project '{project.ConfigFile.Name}'");
var result = Process(project.MainModule, options);
return result;
}
public static object? Process(
Module module,
ProcessingOptions options
) {
logger.Debug(
module.Parent == null
? $"Processing module '{module.Name}'"
: $"Processing submodule '{module.Name}'"
);
object? result = null;
foreach (var (_, subModule) in module.Submodules) {
result = Process(subModule, options);
}
foreach (var (_, unit) in module.Units) {
result = Process(unit, options);
}
return result;
}
public static object? Process(Unit src, ProcessingOptions options) {
logger.Debug($"Processing '{src.SourceFile.Name}'");
if (src.TextStream.IsEmpty) {
logger.Error("Source is empty. Processing aborted.");
return null;
}
object? result = null;
foreach (var step in CompilationSteps) {
result = step.Value(src, options);
if (options.ProcessingMode == step.Key || src.HasErrors) {
break;
}
}
var errCount = 0;
foreach (var e in src.Blames) {
logger.Error(e.ToString());
if (e.Severity == BlameSeverity.Error) {
errCount++;
}
}
logger.Debug(
errCount > 0 ? "Processing aborted." : "Processing completed."
);
return result;
}
public static TokenStream Lex(Unit src, ProcessingOptions options) {
logger.Debug("Tokens list generation");
var lexer = new Lexer(src);
while (true) {
var token = lexer.Read();
if (token == null) {
continue;
}
src.TokenStream.Add(token);
if (lexer.ProcessTerminators.Contains(token.Type)) {
break;
}
}
foreach (var mismatch in lexer.MismatchingPairs) {
LanguageReport.MismatchedBracket(mismatch);
}
return src.TokenStream;
}
static Ast Parse(Unit src, ProcessingOptions options) {
logger.Debug("Abstract Syntax Tree generation");
src.Ast.Parse();
return src.Ast;
}
static Ast Reduce(Unit src, ProcessingOptions options) {
logger.Debug("Syntax tree reducing");
foreach (var reducer in Reducers) {
reducer.Accept(src.Ast);
}
return src.Ast;
}
static CodeWriter Translate(
Unit src,
ProcessingOptions options
) {
if (!Translators.TryGetValue(
options.TargetLanguage,
out var translator
)) {
logger.Error($"No frontend with name {options.TargetLanguage}");
return CodeWriter.Default;
}
var cw = new CodeWriter(translator);
try {
cw.Write(src.Ast);
var code = cw.ToString();
logger.Debug(code);
File.WriteAllText(
Path.Combine(
src.OutputDirectory.FullName,
Path.ChangeExtension(
src.SourceFile.Name,
cw.OutputFileExtension
)
),
code
);
}
catch (Exception ex) {
logger.Error("Translation failed:");
logger.Info(ex);
}
return cw;
}
}