Example #1
0
        public void visit(AssignCommand that)
        {
            // determine the type of the right-hand-side (RHS) by visiting it
            that.Expression.visit(this);

            // check that the symbol exists - by trying to look it up
            Declaration declaration = _symbols.Lookup(that.Name);

            if (declaration == null)
            {
                throw new CheckerError(that.Position, "Variable '" + that.Name + "' not declared in assignment statement");
            }

            // check that the symbol is indeed a variable
            switch (declaration.Kind)
            {
            case SymbolKind.Constant:
                throw new CheckerError(that.Position, "Cannot assign to a constant");

            case SymbolKind.Function:
                throw new CheckerError(that.Position, "Cannot assign to a function");

            case SymbolKind.Parameter:
                throw new CheckerError(that.Position, "Cannot call parameter");

            case SymbolKind.Variable:
                break;

            default:
                throw new CheckerError(that.Position, "Unknown symbol kind: " + declaration.Kind.ToString());
            }

            // check that the types of the left-hand-side is equal to the type of the right-hand-side
            TypeKind firstType = declaration.Type.Kind;
            TypeKind otherType = that.Expression.Type.Kind;

            if (firstType != otherType)
            {
                throw new CheckerError(that.Position, "Type mismatch in assignment");
            }
        }