Beispiel #1
0
        public override object VisitVariable_ID([NotNull] CMinusParser.Variable_IDContext context)
        {
            string variableName  = context.ID().GetText();
            int    variableIndex = this.symbolTable.GetVariableIndex(variableName);

            this.writer.WriteVariableAddress(variableName, variableIndex);

            if (this.symbolTable.GetVariableScope(variableName) == 0)
            {
                this.writer.WriteProgramSize();
            }
            else
            {
                this.writer.WriteContextRegisterRead();
            }

            this.writer.WriteBinaryArithmeticExpression("+");

            return(null);
        }
        public override object VisitVariable_ID([NotNull] CMinusParser.Variable_IDContext context)
        {
            string variableName = context.ID().GetText();

            if (!this.symbolTable.HasSymbol(variableName))
            {
                this.EmitSemanticErrorMessage($"Variable {variableName} called but not declared", context);
                return("error");
            }

            SymbolTable.Symbol foundSymbol = this.symbolTable.GetSymbol(variableName);

            if (foundSymbol.construct == SymbolTable.Symbol.Construct.FUNCTION || foundSymbol.construct == SymbolTable.Symbol.Construct.ERROR)
            {
                this.EmitSemanticErrorMessage($"{variableName} used as a variable but declared as a {foundSymbol.construct}", context);
                return("error");
            }

            return(foundSymbol.type);
        }