예제 #1
0
파일: CodeGen.cs 프로젝트: bencz/Beryl
 public void visit(AssignCommand that)
 {
     Console.Write("{0} = ", that.Name);
     // we must explicitly walk all children of all nodes...
     that.Expression.visit(this);
     Console.WriteLine(";");
 }
예제 #2
0
파일: Checker.cs 프로젝트: bencz/Beryl
        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");
        }