-
Notifications
You must be signed in to change notification settings - Fork 0
📝 CodeRabbit Chat: Update Parser.cs implementation in Computer.V2.Lib #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,42 +44,57 @@ public static string Substitute(string expr, List<List<string>> funcs, List<List | |
| return (expr); | ||
| } | ||
|
|
||
| // Matches a single-argument function call, substitutes either variable or numeric args, | ||
| // computes the resulting expression, and returns at first successful match. | ||
| private static (string Variable, string Value) MatchFunction(string expr, List<List<string>> funcs, List<List<string>> vars) | ||
| { | ||
| var rgx = new Regex(@"([a-zA-Z]+\((([a-zA-Z]+)|((\-)?\d+([\.]\d+)?))\))"); | ||
| var match = rgx.Matches(expr); | ||
| var rgx = new Regex(@"([a-zA-Z]+)\((([a-zA-Z]+)|((\-)?\d+([\.]\d+)?))\)"); | ||
| var matches = rgx.Matches(expr); | ||
| var fVar = ""; | ||
| for (var i = 0; i < match.Count; i++) | ||
|
|
||
| foreach (Match match in matches) | ||
| { | ||
| if (!match[i].Success) continue; | ||
| var tmp = match[i].Value; | ||
| var func = Regex.Split(tmp, @"\(|\)"); | ||
| if (!match.Success) continue; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @coderabbitai pull requests must have a code block
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are on the CodeRabbit Free Plan. In order to use the Chat feature, please upgrade to CodeRabbit Pro. |
||
| var tmp = match.Value; | ||
| var parts = Regex.Split(tmp, @"\(|\)"); | ||
| var name = parts[0]; | ||
| var arg = parts[1]; | ||
|
|
||
| foreach (var f in funcs) | ||
| { | ||
| if (f[0] != func[0]) continue; | ||
| fVar = func[1]; | ||
| var rplc = f[2]; | ||
| if (Regex.IsMatch(func[1], @"^[a-zA-Z]+$")) | ||
| if (f[0] != name) continue; | ||
| fVar = arg; | ||
| var body = f[2]; | ||
| string rplc; | ||
|
|
||
| // Variable argument | ||
| if (Regex.IsMatch(arg, @"^[a-zA-Z]+$")) | ||
| { | ||
| foreach (var v in vars) | ||
| var varEntry = vars.FirstOrDefault(v => v[0] == arg); | ||
| if (varEntry != null) | ||
| { | ||
| if (v[0] != func[1]) continue; | ||
| rplc = rplc.Replace(f[1], $"1*{v[1]}"); | ||
| rplc = $"1*{Maths.Calculate(rplc)}"; | ||
| expr = expr.Replace(tmp, rplc); | ||
| break; | ||
| rplc = body.Replace(f[1], $"1*{varEntry[1]}"); | ||
| } | ||
| else | ||
| { | ||
| // Fallback: treat undefined variable as numeric literal | ||
| rplc = body.Replace(f[1], $"1*{arg}"); | ||
| } | ||
| expr = expr.Replace(tmp, f[2]); | ||
| } | ||
| else | ||
| { | ||
| rplc = rplc.Replace(f[1], $"1*{func[1]}"); | ||
| rplc = $"1*{Maths.Calculate(rplc)}"; | ||
| expr = expr.Replace(tmp, rplc); | ||
| // Numeric argument | ||
| rplc = body.Replace(f[1], $"1*{arg}"); | ||
| } | ||
|
|
||
| // Evaluate and substitute | ||
| rplc = $"1*{Maths.Calculate(rplc)}"; | ||
| expr = expr.Replace(tmp, rplc); | ||
| return (fVar, expr); | ||
| } | ||
| } | ||
| return (fVar, expr); | ||
|
|
||
| return (fVar, expr); | ||
| } | ||
|
|
||
| //find the value of the variable | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@coderabbitai use var insteaf of explicit variable types
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are on the CodeRabbit Free Plan. In order to use the Chat feature, please upgrade to CodeRabbit Pro.