|
@@ -3,6 +3,7 @@ using System.Collections.Concurrent;
|
|
|
using System.Collections.Generic;
|
|
|
using System.IO;
|
|
|
using System.Linq;
|
|
|
+using System.Runtime;
|
|
|
using System.Text;
|
|
|
using System.Threading.Tasks;
|
|
|
using NTERA.Engine.Compiler;
|
|
@@ -16,6 +17,7 @@ namespace NTERA.Engine.Runtime
|
|
|
public ICollection<FunctionVariable> DefinedConstants { get; protected set; }
|
|
|
public CSVDefinition CSVDefinition { get; protected set; }
|
|
|
|
|
|
+ protected Dictionary<FunctionDefinition, string> ProcedureFiles { get; set; }
|
|
|
public Dictionary<FunctionDefinition, ExecutionNode[]> CompiledProcedures { get; protected set; } = new Dictionary<FunctionDefinition, ExecutionNode[]>();
|
|
|
|
|
|
public string InputDirectory { get; }
|
|
@@ -70,8 +72,11 @@ namespace NTERA.Engine.Runtime
|
|
|
});
|
|
|
#endif
|
|
|
|
|
|
+ DefinedConstants = preprocessedConstants.ToArray();
|
|
|
+
|
|
|
Console.WriteLine("Preprocessing functions...");
|
|
|
|
|
|
+ ProcedureFiles = new Dictionary<FunctionDefinition, string>();
|
|
|
ConcurrentDictionary<FunctionDefinition, string> preprocessedFunctions = new ConcurrentDictionary<FunctionDefinition, string>();
|
|
|
|
|
|
#if DEBUG
|
|
@@ -83,51 +88,44 @@ namespace NTERA.Engine.Runtime
|
|
|
}, file =>
|
|
|
#endif
|
|
|
{
|
|
|
- foreach (var kv in Preprocessor.PreprocessCodeFile(File.ReadAllText(file), Path.GetFileName(file), preprocessedConstants.ToArray()))
|
|
|
+ foreach (var kv in Preprocessor.PreprocessCodeFile(File.ReadAllText(file), Path.GetFileName(file), DefinedConstants))
|
|
|
+ {
|
|
|
preprocessedFunctions[kv.Key] = kv.Value;
|
|
|
+ ProcedureFiles[kv.Key] = file;
|
|
|
+ }
|
|
|
#if DEBUG
|
|
|
}
|
|
|
#else
|
|
|
});
|
|
|
#endif
|
|
|
DefinedProcedures = preprocessedFunctions.Select(kv => kv.Key).ToArray();
|
|
|
- var DeclaredProcedures = preprocessedFunctions.ToDictionary(kv => kv.Key, kv => kv.Value);
|
|
|
|
|
|
var declaredFunctions = BaseDefinitions.DefaultGlobalFunctions.ToList();
|
|
|
- declaredFunctions.AddRange(DeclaredProcedures.Keys.Where(x => x.IsReturnFunction));
|
|
|
+ declaredFunctions.AddRange(DefinedProcedures.Where(x => x.IsReturnFunction));
|
|
|
+ DefinedFunctions = declaredFunctions;
|
|
|
|
|
|
- var procedures = DeclaredProcedures.Keys.Where(x => !x.IsReturnFunction).ToList();
|
|
|
+ GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.CompactOnce;
|
|
|
+ GC.Collect();
|
|
|
+ }
|
|
|
|
|
|
+ public IEnumerable<ExecutionNode> GetExecutionNodes(FunctionDefinition function)
|
|
|
+ {
|
|
|
+ if (CompiledProcedures.ContainsKey(function))
|
|
|
+ return CompiledProcedures[function];
|
|
|
|
|
|
- Console.WriteLine("Compiling functions...");
|
|
|
+ string filename = ProcedureFiles[function];
|
|
|
+ var preprocessed = Preprocessor.PreprocessCodeFile(File.ReadAllText(filename), Path.GetFileName(filename), DefinedConstants);
|
|
|
|
|
|
-#if DEBUG
|
|
|
- foreach (var kv in DeclaredProcedures)
|
|
|
-#else
|
|
|
- Parallel.ForEach(DeclaredProcedures, new ParallelOptions
|
|
|
- {
|
|
|
- MaxDegreeOfParallelism = Threads
|
|
|
- }, kv =>
|
|
|
-#endif
|
|
|
- {
|
|
|
- Parser parser = new Parser(kv.Value, kv.Key, declaredFunctions, procedures, BaseDefinitions.DefaultGlobalVariables, kv.Key.Variables, BaseDefinitions.DefaultKeywords, csvDefinition, preprocessedConstants.ToArray());
|
|
|
+ Parser parser = new Parser(preprocessed.Single(x => x.Key.Name == function.Name).Value, function, DefinedFunctions, DefinedProcedures.Where(x => !x.IsReturnFunction).ToArray(), BaseDefinitions.DefaultGlobalVariables, function.Variables, BaseDefinitions.DefaultKeywords, CSVDefinition, DefinedConstants);
|
|
|
|
|
|
- var nodes = parser.Parse(out var localErrors, out var localWarnings);
|
|
|
+ var nodes = parser.Parse(out var localErrors, out var localWarnings)?.ToArray();
|
|
|
|
|
|
- if (localErrors.Count > 0)
|
|
|
- throw new ParserException($"Failed to compile '{kv.Key.Name}'");
|
|
|
+ if (localErrors.Count > 0)
|
|
|
+ throw new ParserException($"Failed to compile '{function.Name}'");
|
|
|
|
|
|
- CompiledProcedures.Add(kv.Key, nodes.ToArray());
|
|
|
-#if DEBUG
|
|
|
- }
|
|
|
-#else
|
|
|
- });
|
|
|
-#endif
|
|
|
- }
|
|
|
+ CompiledProcedures.Add(function, nodes);
|
|
|
|
|
|
- public IEnumerable<ExecutionNode> GetExecutionNodes(FunctionDefinition function)
|
|
|
- {
|
|
|
- return CompiledProcedures[function];
|
|
|
+ return nodes;
|
|
|
}
|
|
|
}
|
|
|
}
|