Skip to content

Commit

Permalink
variables can be used as params in functions
Browse files Browse the repository at this point in the history
  • Loading branch information
teplofizik committed Nov 10, 2022
1 parent c72f771 commit bdfcbd7
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 2 deletions.
3 changes: 2 additions & 1 deletion NyaFs/Processor/ImageProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ public void Process(Scripting.Script Script)
{
if (S.CheckCondition(this))
{
var Res = S.Exec(this);
var Step = S.GetPreprocessed(Scope);
var Res = Step.Exec(this);

WriteLogLine(S, Res);
}
Expand Down
8 changes: 8 additions & 0 deletions NyaFs/Processor/Scripting/ScriptArgsConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public ScriptArgsConfig(int Id, ScriptArgsParam[] Params)

protected bool CheckArgsLen(string[] Args) => (Params.Length == Args.Length);

private bool HasVariable(string Arg)
{
return Variables.VariableChecker.IsCorrectName(Arg);
}

public virtual bool IsMyConfig(string[] Args)
{
if (!CheckArgsLen(Args)) return false;
Expand All @@ -27,6 +32,9 @@ public virtual bool IsMyConfig(string[] Args)
var P = Params[i];
var A = Args[i];

if (HasVariable(A))
continue;

if (!P.CheckParam(A))
return false;
}
Expand Down
2 changes: 2 additions & 0 deletions NyaFs/Processor/Scripting/ScriptParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ private void ParseLine(Script Result, string Filename, string Name, int Line, st
{
var Step = Gen.Get(SArgs);
Step.SetScriptInfo(Filename, Name, Line);
Step.SetGenerator(Gen, Args);

if (Cond != null) Step.SetCondition(Conditions.ConditionParser.Parse(Cond));

Result.Steps.Add(Step);
Expand Down
43 changes: 43 additions & 0 deletions NyaFs/Processor/Scripting/ScriptStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,54 @@ public class ScriptStep

private Conditions.Condition Condition = null;

private ScriptStepGenerator Generator;
private string[] GeneratorArgs;

public ScriptStep(string Name)
{
this.Name = Name;
}

private string PreprocessArg(Variables.VariableScope Scope, string Arg)
{
if (Variables.VariableChecker.IsCorrectName(Arg) && Scope.IsDefined(Arg))
return Scope.GetValue(Arg);
else
return Arg;
}

private string[] PreprocessArgs(Variables.VariableScope Scope, string[] Args)
{
return Array.ConvertAll(Args, A => PreprocessArg(Scope, A));
}

public ScriptStep GetPreprocessed(Variables.VariableScope Scope)
{
var SArgs = Generator.GetArgs(PreprocessArgs(Scope, GeneratorArgs));
if (SArgs != null)
{
var Step = Generator.Get(SArgs);

Step.SetScriptInfo(ScriptFilename, ScriptName, ScriptLine);
Step.SetGenerator(Generator, GeneratorArgs);
if (Condition != null)
Step.SetCondition(Condition);

return Step;
}
else
{
Log.Error(0, $"Error at {ScriptName}:{ScriptLine}. Invalid arguments for command '{Name}'.");
return null;
}
}

public void SetGenerator(ScriptStepGenerator Gen, string[] Args)
{
Generator = Gen;
GeneratorArgs = Args;
}

public void SetScriptInfo(string Filename, string Name, int Line)
{
ScriptFilename = Filename;
Expand Down
2 changes: 1 addition & 1 deletion NyaFs/Processor/Scripting/Variables/VariableChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class VariableChecker
{
public static bool IsCorrectName(string Name)
{
return (Name != null) && (Name.Length > 2) && (Name.Count(C => (C == '%')) == 1) && (Name[0] == '%');
return (Name != null) && (Name.Length > 2) && (Name.Count(C => (C == '$')) == 1) && (Name[0] == '$');
}
}
}

0 comments on commit bdfcbd7

Please sign in to comment.