using sly.parser; using sly.parser.generator; namespace RhythmBase.Components.RDLang { /// /// RDLang class provides functionality to parse and evaluate expressions written in a custom language. /// public static class RDLang { private static readonly RDVariables variables = new(); private static readonly RDLangParser parserInstance; /// /// Gets the collection of variables used in the custom language. /// /// /// The collection of variables. /// public static RDVariables Variables { get => variables; } /// /// Lazy-initialized parser for the custom language expressions. /// internal static Lazy> Parser = new(() => { var builder = new ParserBuilder(); var build = builder.BuildParser(parserInstance, ParserType.LL_RECURSIVE_DESCENT, "expression"); var parser = build.Result; return parser; }); static RDLang() { parserInstance = new RDLangParser() { Variables = variables }; } /// /// Tries to parse the given code string into a float result. /// /// The code string to parse. /// The parsed float result if the parsing is successful. /// True if parsing is successful; otherwise, false. public static bool TryRun(string code, out float result) { var parseResult = Parser.Value.Parse(code); result = parseResult.Result; return parseResult.IsOk; } } }