public override object VisitVariableDeclaration_Array([NotNull] CMinusParser.VariableDeclaration_ArrayContext context)
        {
            string variableName = context.ID().GetText();
            string variableType = SymbolTable.Symbol.RemoveExtras(context.typeSpecifier().GetText());

            if (variableType == "void")
            {
                this.EmitSemanticErrorMessage("Variable declared as void type", context);
            }

            SymbolTable.Symbol symbol = new SymbolTable.Symbol(
                id: variableName,
                type: variableType,
                construct: SymbolTable.Symbol.Construct.ARRAY,
                scope: 0,
                size: uint.Parse(context.NUM().GetText()),
                pointerCount: SymbolTable.Symbol.CountStringAsterisks(context.typeSpecifier().GetText())
                );

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

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

            return(null);
        }
Ejemplo n.º 2
0
        public override object VisitVariableDeclaration_Array([NotNull] CMinusParser.VariableDeclaration_ArrayContext context)
        {
            string arrayName = context.ID().GetText();
            int    arraySize = int.Parse(context.NUM().GetText());

            this.symbolTable.AddVariable(arrayName, arraySize + 1);

            int arrayPosition;

            if (this.inGlobalScope)
            {
                arrayPosition = 0;
            }
            else
            {
                arrayPosition = this.symbolTable.GetVariableIndex(arrayName);
            }

            this.writer.WriteVariableAddress($"{arrayName}[0]", arrayPosition + 1);
            this.writer.WriteContextRegisterRead();
            this.writer.WriteBinaryArithmeticExpression("+");

            this.writer.WriteVariableAddress(arrayName, arrayPosition);
            this.writer.WriteContextRegisterRead();
            this.writer.WriteBinaryArithmeticExpression("+");

            this.writer.WriteMemoryWrite();

            if (this.inGlobalScope)
            {
                this.writer.WriteContextRegisterRead();
                this.writer.WriteImmediate(arraySize + 1);
                this.writer.WriteBinaryArithmeticExpression("+");
                this.writer.WriteContextRegisterWrite();
            }

            return(null);
        }