示例#1
0
        public ITypeCheckType TypeCheck(ITypeEnvironment env)
        {
            Action <VarAccessEventArgs> onVarAccess = (args) => args.SetVarInstance(0);

            //TODO: Make it so no casting is needed
            ((Environment.TypeEnvironment)env).VarAccess += onVarAccess;

            if (!env.IsDeclared(Name))
            {
                env.ReportError(String.Format("Attempting to use 'sum' on a non-existing variable '{0}'." +
                                              "'sum' can only be used on variables used in a repeat.",
                                              Name), SourceStartPosition, SourceEndPosition);

                return(new UnknownType());
            }

            ITypeCheckType type = env.GetDeclared(Name);

            if (!type.CompatibleWith(ExpressionUpperBound))
            {
                env.ReportError(String.Format("Sum not possible. Incompatible type: '{0}'. Only numeric types are supported.",
                                              type), SourceStartPosition, SourceEndPosition);

                return(ExpressionUpperBound);
            }

            //TODO: Make it so no casting is needed
            ((Environment.TypeEnvironment)env).VarAccess -= onVarAccess;

            return(type);
        }
        public bool CompatibleWith(ITypeCheckType other)
        {
            if (Equals(other) || IsChildOf(other) || other.IsChildOf(this))
            {
                return true;
            }

            return false;
        }
示例#3
0
        public bool IsChildOf(ITypeCheckType other)
        {
            if (SuperType != null && (SuperType.Equals(other) || SuperType.IsChildOf(other)))
            {
                return(true);
            }

            return(false);
        }
        public bool IsChildOf(ITypeCheckType other)
        {
            if (SuperType != null && (SuperType.Equals(other) || SuperType.IsChildOf(other)))
            {
                return true;
            }

            return false;
        }
示例#5
0
        public bool CompatibleWith(ITypeCheckType other)
        {
            if (Equals(other) || IsChildOf(other) || other.IsChildOf(this))
            {
                return(true);
            }

            return(false);
        }
示例#6
0
        public override ITypeCheckType TypeCheck(ITypeEnvironment env)
        {
            ITypeCheckType a = Expr1.TypeCheck(env);
            ITypeCheckType b = Expr2.TypeCheck(env);

            if (!a.CompatibleWith(ExpressionUpperBound) || !a.CompatibleWith(b))
            {
                env.ReportError(String.Format("Comparison using '<=' not possible. Incompatible types: '{0}', '{1}'. Only numeric types are supported.",
                                              a, b), SourceStartPosition, SourceEndPosition);
            }

            return(new BoolType());
        }
示例#7
0
        public ITypeCheckType TypeCheck(ITypeEnvironment env)
        {
            ITypeCheckType a = Expr1.TypeCheck(env);

            if (!a.CompatibleWith(NegationUpperBound))
            {
                env.ReportError(String.Format("Negation not possible. Incompatible type: '{0}'. Only numerical types are supported.",
                                              a), SourceStartPosition, SourceEndPosition);

                return(NegationUpperBound);
            }

            return(a);
        }
        public ITypeCheckType GetLeastUpperBound(ITypeCheckType other)
        {
            if (Equals(other) || other.IsChildOf(this))
            {
                return this;
            }
            else if (IsChildOf(other))
            {
                return other;
            }

            Debug.Assert(SuperType != null, "SuperType is null!");

            return SuperType.GetLeastUpperBound(other);
        }
示例#9
0
        public ITypeCheckType GetLeastUpperBound(ITypeCheckType other)
        {
            if (Equals(other) || other.IsChildOf(this))
            {
                return(this);
            }
            else if (IsChildOf(other))
            {
                return(other);
            }

            Debug.Assert(SuperType != null, "SuperType is null!");

            return(SuperType.GetLeastUpperBound(other));
        }
示例#10
0
        public override ITypeCheckType TypeCheck(ITypeEnvironment env)
        {
            ITypeCheckType a = Expr1.TypeCheck(env);
            ITypeCheckType b = Expr2.TypeCheck(env);

            if (!a.CompatibleWith(ExpressionUpperBound) || !a.CompatibleWith(b))
            {
                env.ReportError(String.Format("'||' not possible. Incompatible types: '{0}', '{1}'. Only the bool type is supported.",
                                              a, b), SourceStartPosition, SourceEndPosition);

                return(ExpressionUpperBound);
            }

            return(a.GetLeastUpperBound(b));
        }
示例#11
0
        public void DuplicateLabelsWarning()
        {
            string         varName = "someVar";
            ITypeCheckType varType = tcFactory.IntType();

            ITypeCheckStmnt stmnt = tcFactory.Statements(
                tcFactory.Question("Test", tcFactory.VarDecl(varName, varType)),
                tcFactory.Question("Test", tcFactory.VarDecl(varName, varType))
                );

            ErrorManager    errMngr = new ErrorManager();
            TypeEnvironment te      = new TypeEnvironment(errMngr);

            stmnt.TypeCheck(te);

            Assert.True(te.IsDeclared(varName));
            Assert.True(errMngr.HasWarnings);
            Assert.False(errMngr.HasErrors);
        }
示例#12
0
        public void DetectsRedefinedQuestions()
        {
            string         varName = "someVar";
            ITypeCheckType type1   = tcFactory.RealType();
            ITypeCheckType type2   = tcFactory.BoolType();

            ITypeCheckStmnt stmnt = tcFactory.Statements(
                tcFactory.Question("Test", tcFactory.VarDecl(varName, type1)),
                tcFactory.Question("Test", tcFactory.VarDecl(varName, type2))
                );

            ErrorManager    errMngr = new ErrorManager();
            TypeEnvironment te      = new TypeEnvironment(errMngr);

            stmnt.TypeCheck(te);

            Assert.True(te.IsDeclared(varName));
            Assert.True(errMngr.HasErrors);
        }
示例#13
0
        public ITypeCheckType TypeCheck(ITypeEnvironment env)
        {
            ITypeCheckType a = Expr1.TypeCheck(env);
            ITypeCheckType b = Expr2.TypeCheck(env);
            ITypeCheckType c = Expr3.TypeCheck(env);

            if (!a.CompatibleWith(ExpressionUpperBound))
            {
                env.ReportError(String.Format("Inline 'if/else' evaluation failed. Incompatible type: '{0}'. Only the bool type is supported.",
                                              a), SourceStartPosition, SourceEndPosition);
            }

            if (!b.CompatibleWith(c))
            {
                env.ReportError(String.Format("Inline 'if/else' return value conflict. Incompatible types: '{0}', '{1}'. Only similar types can be used in the true/false bodies.",
                                              b, c), SourceStartPosition, SourceEndPosition);
            }

            return(b.GetLeastUpperBound(c));
        }
示例#14
0
        public void CyclicDependencyNotPossible()
        {
            string         varName1 = "var1";
            string         varName2 = "var2";
            ITypeCheckType type1    = tcFactory.RealType();
            ITypeCheckType type2    = tcFactory.BoolType();

            ITypeCheckStmnt stmnt = tcFactory.Statements(
                tcFactory.Label("Test", tcFactory.VarAssign(varName1, type1, tcFactory.Variable(varName2))),
                tcFactory.Label("Test", tcFactory.VarAssign(varName2, type2, tcFactory.Variable(varName1)))
                );

            ErrorManager    errMngr = new ErrorManager();
            TypeEnvironment te      = new TypeEnvironment(errMngr);

            stmnt.TypeCheck(te);

            Assert.True(te.IsDeclared(varName1));
            Assert.True(te.IsDeclared(varName2));
            Assert.True(errMngr.HasErrors);
        }
示例#15
0
 public bool CompatibleWith(ITypeCheckType other)
 {
     return(true);
 }
示例#16
0
 public bool IsChildOf(ITypeCheckType other)
 {
     return(true);
 }
示例#17
0
 public ITypeCheckType GetLeastUpperBound(ITypeCheckType other)
 {
     return(other);
 }
 public bool IsChildOf(ITypeCheckType other)
 {
     return true;
 }
 public ITypeCheckType GetLeastUpperBound(ITypeCheckType other)
 {
     return other;
 }
 public bool CompatibleWith(ITypeCheckType other)
 {
     return true;
 }