Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Dynamic;
- using System.IO;
- using System.Linq;
- using System.Text.RegularExpressions;
- using CodingSeb.ExpressionEvaluator;
- namespace CMSlib.ConsoleModule.Termios;
- public class Constants : DynamicObject
- {
- private readonly string[] _termiosPath;
- private readonly List<ExpressionEvaluator> _evaluators;
- public Constants(params string[] termiosPath)
- {
- _termiosPath = termiosPath;
- _evaluators = new List<ExpressionEvaluator>();
- ParseConstants();
- }
- #nullable enable
- public override bool TryGetMember(GetMemberBinder binder, out object? result)
- {
- object? temp = null;
- if (_evaluators.Any(evaluator =>
- {
- try
- {
- temp = evaluator.Evaluate(binder.Name);
- return true;
- }
- catch
- {
- return false;
- }
- }))
- {
- result = temp;
- return true;
- }
- result = temp;
- return false;
- }
- #nullable disable
- private void ParseConstants()
- {
- var i = 0;
- foreach (var termiosPath in _termiosPath)
- {
- _evaluators.Add(new ExpressionEvaluator());
- var fileInfo = new FileInfo(termiosPath);
- using var file = fileInfo.OpenText();
- var fileString =
- Regex.Replace(file.ReadToEnd(),
- @"\/\*(?s).*?\*\/|#ifndef.*?(\n|\r\n|\r)|#include.*?(\n|\r\n|\r)|#if.*?(\n|\r\n|\r)|#endif.*?(\n|\r\n|\r)",
- string.Empty,
- RegexOptions.Multiline)
- .Replace("\t", " ");
- var stringReader = new StringReader(fileString);
- string line;
- while ((line = stringReader.ReadLine()) != null)
- {
- var splitLine = (from word in line.Split(" ")
- where word != string.Empty
- select word).ToArray();
- if (splitLine.Length != 0 && splitLine.Length > 2)
- {
- switch (splitLine[0])
- {
- case "#if":
- case "#error":
- continue;
- case "#define" when !splitLine[1].Contains("("):
- _evaluators[i].Variables[splitLine[1]] =
- new SubExpression(string.Join(' ', splitLine[2..]));
- break;
- }
- }
- }
- i++;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement