Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/dotnetcore.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: 3.1.101
dotnet-version: 9.0.300
- name: Install dependencies
run: dotnet restore
- name: Build
Expand Down
57 changes: 36 additions & 21 deletions src/Computer.V2.Lib/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Owner

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

Copy link
Contributor Author

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.

{
if (!match[i].Success) continue;
var tmp = match[i].Value;
var func = Regex.Split(tmp, @"\(|\)");
if (!match.Success) continue;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@coderabbitai pull requests must have a code block

Copy link
Contributor Author

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.

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
Expand Down