コード例 #1
0
ファイル: CodeGenerator.cs プロジェクト: sw417f19/CBlunt
        public override int VisitFunction([NotNull] CBluntParser.FunctionContext context)
        {
#if DEBUG
            Console.WriteLine("VisitFunction");
#endif

            if (context.ID(0).GetText() == "Main")
            {
                this.filecontent += "static void Main()\n";
            }
            else
            {
                this.ConvertFunctionType(context.functiontype().GetText());
                this.AddText(context.ID(0).GetText() + "(");

                for (int counter = 0; counter < context.variabletype().Count(); ++counter)
                {
                    Visit(context.variabletype(counter));
                    this.AddText(context.ID(counter + 1).GetText());
                    if (counter < context.variabletype().Count() - 1)
                    {
                        this.AddText(",");
                    }
                }

                this.AddText(")");
            }
            Visit(context.block());

            return(0);
        }
コード例 #2
0
        public override int VisitFunction([NotNull] CBluntParser.FunctionContext context)
        {
#if DEBUG
            Console.WriteLine("VisitFunction");
#endif

            // Create a new scope to the linked list
            SymbolTable.MethodScopeLinkedList.AddLast(new Dictionary <string, VariableProperties>());

            // Get the method's type
            var methodType = context.functiontype().GetText();

            // Get the method's name
            var methodName = context.ID(0).GetText();

            // Create the actual properties for the method
            MethodProperties methodProperties = new MethodProperties
            {
                // Set the type of the method
                Type = methodType,

                // Create the parameter types list for parsing of the method's parameters
                ParameterTypes = new List <string>()
            };

            // Check whether the method contains parameters. If so, add the parameter types to the methodProperties and add the variables
            // to the main scope of the method
            for (int i = 0; i < context.variabletype().Count(); ++i)
            {
                // Get the parameter's type
                var parameterType = context.variabletype(i).GetText();

                // Get the parameter's name. This is +1 due to that the first ID is the method's name
                var parameterName = context.ID(i + 1).GetText();

                // Add the parameter type to the list of parameter types
                methodProperties.ParameterTypes.Add(parameterType);

                // All parameters are guranteed to be initialized, set the initialized flag
                var variableProperties = new VariableProperties(parameterType)
                {
                    Initialized = true
                };

                // Add the variable along with properties to the method scope
                SymbolTable.MethodScopeLinkedList.Last.Value.Add(parameterName, variableProperties);
            }

            SymbolTable.CurrentMethodType = methodType;

            // Visit the block of the method
            Visit(context.block());

            // After the function has finished, remove the scope
            SymbolTable.MethodScopeLinkedList.RemoveLast();

            return(0);
        }
コード例 #3
0
        public override int VisitFunction([NotNull] CBluntParser.FunctionContext context)
        {
#if DEBUG
            Console.WriteLine("VisitFunction");
#endif

            // The type of the method
            var methodType = context.functiontype().GetText();

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

            // The list of parameter types the method has
            var methodTypes = new List <string>();

            for (int i = 0; i < context.variabletype().Count(); ++i)
            {
                // Get the parameter type
                var methodParameter = context.variabletype(i).GetText();

                // Add it to the list of parameter types the method has
                methodTypes.Add(methodParameter);
            }

            // Create MethodProperties object
            var methodProperties = new MethodProperties
            {
                Type           = methodType,
                ParameterTypes = methodTypes
            };

            // Add the method along with its properties to the symbol table
            CreateMethod(methodName, methodProperties);

            return(base.VisitFunction(context));
        }