Exemple #1
0
        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));
        }
 public void visit(IdentifierExp identifier)
 {
     throw new NotImplementedException();
 }
 public override void visit(IdentifierExp n)
 {
     n.Scope = Scope;
 }
 public ASTType visit(IdentifierExp n)
 {
     return n.Scope.Lookup(n.ID.Name).accept(this);
 }