Exemplo n.º 1
0
        public override object VisitFunctionDeclaration([NotNull] CMinusParser.FunctionDeclarationContext context)
        {
            this.inGlobalScope = false;
            this.writer.DisableGlobalBuffer();

            string functionLabel = this.labelGenerator.GenerateFunctionLabel(context.ID().GetText());

            this.writer.WriteLabel(functionLabel);

            this.writer.WriteNoOperation("PSP");

            this.EnterContext();
            this.Visit(context.parameters());

            this.Visit(context.compoundStatement());

            this.ExitContext();

            this.writer.WriteLabel(this.labelGenerator.FunctionReturnLabel());

            this.writer.WriteFunctionExit();

            this.writer.EnableGlobalBuffer();
            this.inGlobalScope = true;

            return(null);
        }
        public override object VisitFunctionDeclaration([NotNull] CMinusParser.FunctionDeclarationContext context)
        {
            string functionType = SymbolTable.Symbol.RemoveExtras(context.typeSpecifier().GetText());
            string functionName = context.ID().GetText();

            List <SymbolTable.Symbol> parameters = (List <SymbolTable.Symbol>) this.Visit(context.parameters()); // Visit parameters

            SymbolTable.Symbol functionSymbol = new SymbolTable.Symbol(
                id: functionName,
                type: functionType,
                construct: SymbolTable.Symbol.Construct.FUNCTION,
                size: (uint)parameters.Count,
                scope: this.internalScope,
                pointerCount: 0
                );

            functionSymbol.AddMembers(parameters);

            bool success = this.symbolTable.AddSymbol(functionSymbol);

            if (!success)
            {
                this.EmitSemanticErrorMessage($"Symbol {functionName} already in symbol table as a {this.symbolTable.GetSymbol(functionName).construct}", context);
            }

            return(null);
        }