Advertisement
Ardente

Untitled

May 7th, 2022
1,157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.72 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Dynamic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text.RegularExpressions;
  7. using CodingSeb.ExpressionEvaluator;
  8.  
  9. namespace CMSlib.ConsoleModule.Termios;
  10.  
  11. public class Constants : DynamicObject
  12. {
  13.     private readonly string[] _termiosPath;
  14.     private readonly List<ExpressionEvaluator> _evaluators;
  15.  
  16.     public Constants(params string[] termiosPath)
  17.     {
  18.         _termiosPath = termiosPath;
  19.         _evaluators = new List<ExpressionEvaluator>();
  20.  
  21.         ParseConstants();
  22.     }
  23.  
  24. #nullable enable
  25.     public override bool TryGetMember(GetMemberBinder binder, out object? result)
  26.     {
  27.         object? temp = null;
  28.  
  29.         if (_evaluators.Any(evaluator =>
  30.             {
  31.                 try
  32.                 {
  33.                     temp = evaluator.Evaluate(binder.Name);
  34.                     return true;
  35.                 }
  36.                 catch
  37.                 {
  38.                     return false;
  39.                 }
  40.             }))
  41.         {
  42.             result = temp;
  43.             return true;
  44.         }
  45.  
  46.         result = temp;
  47.         return false;
  48.     }
  49. #nullable disable
  50.  
  51.     private void ParseConstants()
  52.     {
  53.         var i = 0;
  54.         foreach (var termiosPath in _termiosPath)
  55.         {
  56.             _evaluators.Add(new ExpressionEvaluator());
  57.             var fileInfo = new FileInfo(termiosPath);
  58.  
  59.             using var file = fileInfo.OpenText();
  60.  
  61.             var fileString =
  62.                 Regex.Replace(file.ReadToEnd(),
  63.                         @"\/\*(?s).*?\*\/|#ifndef.*?(\n|\r\n|\r)|#include.*?(\n|\r\n|\r)|#if.*?(\n|\r\n|\r)|#endif.*?(\n|\r\n|\r)",
  64.                         string.Empty,
  65.                         RegexOptions.Multiline)
  66.                     .Replace("\t", " ");
  67.  
  68.             var stringReader = new StringReader(fileString);
  69.             string line;
  70.             while ((line = stringReader.ReadLine()) != null)
  71.             {
  72.                 var splitLine = (from word in line.Split(" ")
  73.                     where word != string.Empty
  74.                     select word).ToArray();
  75.  
  76.  
  77.                 if (splitLine.Length != 0 && splitLine.Length > 2)
  78.                 {
  79.                     switch (splitLine[0])
  80.                     {
  81.                         case "#if":
  82.                         case "#error":
  83.                             continue;
  84.                         case "#define" when !splitLine[1].Contains("("):
  85.                             _evaluators[i].Variables[splitLine[1]] =
  86.                                 new SubExpression(string.Join(' ', splitLine[2..]));
  87.                             break;
  88.                     }
  89.                 }
  90.             }
  91.  
  92.             i++;
  93.         }
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement