コード例 #1
0
ファイル: CodeGenerator.cs プロジェクト: sw417f19/CBlunt
        public override int VisitFunctioncall([NotNull] CBluntParser.FunctioncallContext context)
        {
#if DEBUG
            Console.WriteLine("VisitFunctioncall");
#endif
            if (context.GetChild(0).GetText() == "WriteLine")
            {
                this.AddText("Console.WriteLine (");
                Visit(context.expression(0));
                this.AddText(")");
            }
            else if (context.GetChild(0).GetText() == "ReadLine")
            {
                this.AddText("Console.ReadLine (");
                Visit(context.expression(0));
                this.AddText(")");
            }
            else
            {
                this.filecontent += context.ID().GetText() + " (";
                for (int count = 1; count < context.ChildCount; ++count)
                {
                    Visit(context.GetChild(count));
                    if (context.GetChild(count).GetText() == ",")
                    {
                        this.filecontent += ", ";
                    }
                }
                this.filecontent += ")";
            }
            return(0);
        }
コード例 #2
0
        public override int VisitFunctioncall([NotNull] CBluntParser.FunctioncallContext context)
        {
#if DEBUG
            Console.WriteLine("VisitFunctioncall");
#endif

            // The name of the method to call
            var methodName = context.ID().GetText();

            // Get the amout of expressions
            var expressionCount = context.expression().Count();

            // Get the method's properties
            var methodProperties = GetMethodProperties(methodName);

            // This rule denotes a method call with nothing to return. Simple check to see if the method even exists.
            if (methodProperties == null)
            {
                SyntaxError(context, "Attempt to call method " + methodName + " that does not exist");
                return(1);
            }

            // If there exists expressions, and the method does not actually take any parameters, stop and give syntax error
            if (expressionCount > 0 && methodProperties.ParameterTypes.Count == 0)
            {
                SyntaxError(context, "Method with name " + methodName + " does not take any parameters");
                return(1);
            }

            // Get the nice name for the method
            var methodNiceName = GetMethodNiceName(methodName, methodProperties.ParameterTypes);

            if (methodProperties.ParameterTypes.Count != expressionCount)
            {
                SyntaxError(context, "Method " + methodNiceName + " got " + expressionCount + " parameters, expected " + methodProperties.ParameterTypes.Count);
                return(1);
            }

            // Compare the method's parameters with the found parameters to see whether they match
            for (int i = 0; i < context.expression().Count(); ++i)
            {
                SymbolTable.ExpressionStoreLinkedList.AddLast(new ExpressionStore());

                Visit(context.expression(i));

                var parameterCount = i + 1;

                /// TODO: Potentially != here instead
                if (methodProperties.ParameterTypes.Count < parameterCount)
                {
                    SyntaxError(context, "Method " + methodNiceName + " does not take " + parameterCount + " parameters");
                    return(1);
                }

                // Get the expected parameter type from the method's properties
                var expectedParameterType = methodProperties.ParameterTypes[i];

                // Number has a ToString conversion, set appropriate type here if it is as such
                if (expectedParameterType == "text" && SymbolTable.ExpressionStoreLinkedList.Last.Value.Type == "number")
                {
                    SymbolTable.ExpressionStoreLinkedList.Last.Value.Type = "text";
                }

                // If it is not equal to the retrieved parameter type, be it variable, functioncall etc, an error is imminent
                if (expectedParameterType != SymbolTable.ExpressionStoreLinkedList.Last.Value.Type)
                {
                    SyntaxError(context, "Method " + methodNiceName + " got type " + SymbolTable.ExpressionStoreLinkedList.Last.Value.Type + " as parameter number " + parameterCount + ", expected " + expectedParameterType);
                    return(1);
                }

                SymbolTable.ExpressionStoreLinkedList.RemoveLast();
            }

            return(0);
        }