Esempio n. 1
0
        public IExpressionNode Visit(ArrowAnonymFunctionSyntaxNode arrowAnonymFunNode)
        {
            if (arrowAnonymFunNode.Definition == null)
            {
                throw ErrorFactory.AnonymousFunDefinitionIsMissing(arrowAnonymFunNode);
            }

            if (arrowAnonymFunNode.Body == null)
            {
                throw ErrorFactory.AnonymousFunBodyIsMissing(arrowAnonymFunNode);
            }

            //Anonym fun arguments list
            var argumentLexNodes = arrowAnonymFunNode.ArgumentsDefinition;

            //Prepare local variable scope
            //Capture all outerscope variables
            var localVariables = new VariableDictionary(_variables.GetAllSources());

            var arguments = new VariableSource[argumentLexNodes.Length];
            var argIndex  = 0;

            foreach (var arg in argumentLexNodes)
            {
                //Convert argument node
                var varNode = FunArgumentExpressionNode.CreateWith(arg);
                var source  = VariableSource.CreateWithStrictTypeLabel(varNode.Name, varNode.Type, arg.Interval, isOutput: false);
                //collect argument
                arguments[argIndex] = source;
                argIndex++;
                //add argument to local scope
                if (!localVariables.TryAdd(source))
                {   //Check for duplicated arg-names
                    //If outer-scope contains the conflict variable name
                    if (_variables.GetSourceOrNull(varNode.Name) != null)
                    {
                        throw ErrorFactory.AnonymousFunctionArgumentConflictsWithOuterScope(varNode.Name, arrowAnonymFunNode.Interval);
                    }
                    else //else it is duplicated arg name
                    {
                        throw ErrorFactory.AnonymousFunctionArgumentDuplicates(varNode, arrowAnonymFunNode.Definition);
                    }
                }
            }
            var body = arrowAnonymFunNode.Body;

            return(BuildAnonymousFunction(arrowAnonymFunNode.Interval, body, localVariables, arguments));
        }
Esempio n. 2
0
 public static Exception AnonymousFunctionArgumentDuplicates(FunArgumentExpressionNode argNode, ISyntaxNode funDefinition)
 => new FunParseException(557, $"'Argument name '{argNode.Name}' of anonymous fun duplicates ", argNode.Interval);