private double CalculateIdentifierValue(IdentifierExp exp, Dictionary <string, DefineExp> context) { Dictionary <string, DefineExp> localContext = CopyContext(context); if (localContext.ContainsKey(exp.Name) == false) { throw new InvalidOperationException($"В списке переменных не найдена переменная с именем {exp.Name}."); } if (IsPrimitive(exp)) { return(CalculateExp(localContext[exp.Name].Children[1], localContext)); } //function defined var defineExp = localContext[exp.Name]; var functionSignature = (IdentifierExp)defineExp.Children[0]; for (int argumentIndex = 0; argumentIndex < functionSignature.Children.Count; argumentIndex++) { var functionArgument = (IdentifierExp)functionSignature.Children[argumentIndex]; var argumentSubstitution = new DefineExp(); argumentSubstitution.Children.Add(functionArgument); double argValue = CalculateExp(exp.Children[argumentIndex], context); argumentSubstitution.Children.Add(new DoubleExp(argValue)); localContext[functionArgument.Name] = argumentSubstitution; } var functionBody = defineExp.Children[1]; return(CalculateExp(functionBody, localContext)); }
private double ExecuteDefine(DefineExp exp) { _globalContext[exp.IdentifierName] = exp; return(exp.IsVariable ? CalculateExp(exp.Children[1], _globalContext) : 0); }