Example #1
0
        public void ExtendedConstructor_FileNotFoundException()
        {
            const string msg  = "Test MSG";
            const string path = "Test PATH";

            try
            {
                Raise <FileNotFoundException> .If(false, msg, path);
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }

            try
            {
                Raise <FileNotFoundException> .If(true, msg, path);
            }
            catch (FileNotFoundException fex)
            {
                Assert.That(fex.Message, Is.EqualTo(msg));
                Assert.That(fex.FileName, Is.EqualTo(path));
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
        }
        StructType GetStructType(string name)
        {
            name = name.ToLower();
            Raise <TypeCheckException> .If(!_structTypes.ContainsKey(name), "Type does not exist.");

            return(_structTypes[name]);
        }
Example #3
0
        public void ExtendedConstructor_BigCtorException()
        {
            var x = 3;
            var y = decimal.MinValue;
            var z = "ZZZ";
            var t = new KeyValuePair <string, string>(z, z + z);

            try
            {
                Raise <BigCtorException> .If(false, x, y, z, t);
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }

            try
            {
                Raise <BigCtorException> .If(true, x, y, z, t);
            }
            catch (BigCtorException fex)
            {
                Assert.That(fex.Message, Is.EqualTo(x.ToString()));
                Assert.That(fex.X, Is.EqualTo(x));
                Assert.That(fex.Y, Is.EqualTo(y));
                Assert.That(fex.Z, Is.EqualTo(z));
                Assert.That(fex.T, Is.EqualTo(t));
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
        }
        StructType AddStructType(StructDecl decl)
        {
            var name = decl.Name.ToLower();

            Raise <TypeCheckException> .If(_types.ContainsKey(name));

            const string         nmsp     = "DanglingLang.Runner";
            const TypeAttributes typeAttr =
                TypeAttributes.Sealed | TypeAttributes.Public | TypeAttributes.AnsiClass |
                TypeAttributes.BeforeFieldInit | TypeAttributes.SequentialLayout;
            var typeDef = new TypeDefinition(nmsp, name, typeAttr, Module.Import(typeof(object)));

            var type = new StructType(name, typeDef);

            foreach (var f in decl.Fields)
            {
                var fieldType = GetType(f.Item2);
                Raise <TypeCheckException> .IfAreEqual("void", fieldType.Name, "Field cannot be void");

                type.AddField(f.Item1, fieldType);
            }

            _types.Add(name, type);
            _structTypes.Add(name, type);
            return(type);
        }
Example #5
0
            /// <summary>
            ///   Deposits given amount into the bank.
            /// </summary>
            /// <param name="amount">A positive amount of money.</param>
            /// <exception cref="ArgumentOutOfRangeException">Amount is zero or negative.</exception>
            /// <exception cref="InvalidOperationException">Bank is closed.</exception>
            /// <exception cref="OverNineThousandException">Amount is over nine thousand!</exception>
            public void Deposit(decimal amount)
            {
                // Preconditions
                Raise.InvalidOperationException.IfNot(_isOpen, "Bank is still closed");
                Raise.ArgumentOutOfRangeException.IfIsLessOrEqual(amount, 0, nameof(amount), "Zero or negative amount");
                Raise <OverNineThousandException> .If(amount > 9000M, "You are too rich!");

                Amount += amount;
            }
        public FieldInfo AddField(string name, Type type)
        {
            Raise <TypeCheckException> .If(_fields.Any(f => f.Name == name), "Field with existing name");

            var fieldInfo = new FieldInfo(name, type);

            _fields.Add(fieldInfo);
            return(fieldInfo);
        }
Example #7
0
        public void Visit(Block block)
        {
            foreach (var s in block.Statements)
            {
                Raise <ReturnCheckException> .If(_foundReturn, "There must be no stmts after return");

                s.Accept(this);
            }
        }
Example #8
0
        public VarInfo AddVariable(string name, Type type)
        {
            Raise <ArgumentException> .If(_variables.Any(v => v.Name == name));

            var varInfo = new VarInfo(name, type);

            _variables.Add(varInfo);
            return(varInfo);
        }
Example #9
0
        public ParamInfo AddParam(string name, string typeName)
        {
            Raise <ArgumentException> .If(_params.Any(p => p.Name == name));

            var paramInfo = new ParamInfo(name, typeName);

            _params.Add(paramInfo);
            return(paramInfo);
        }
        Type AddType(string name, TypeReference reference)
        {
            name = name.ToLower();
            Raise <TypeCheckException> .If(_types.ContainsKey(name), "Type already existing.");

            var type = new Type(name, reference);

            _types.Add(name, type);
            return(type);
        }
Example #11
0
        // Math methods

        public static int Fact(int n)
        {
            Raise <ArgumentOutOfRangeException> .If(n < 0);

            var result = n;

            for (; n > 2; result *= --n)
            {
            }
            return(result > 1 ? result : 1);
        }
        void LoadType(TypeDefinition typeDef)
        {
            Raise <TypeCheckException> .If(_structTypes.ContainsKey(typeDef.Name), "There is a type with the same name!");

            var typeRef    = Module.Import(typeDef);
            var structType = new StructType(typeRef.Name, typeRef);

            structType.Ctor       = Module.Import(typeDef.Methods.First(m => m.Name == ".ctor"));
            structType.TypeEquals = Module.Import(typeDef.Methods.First(m => m.Name == "MyEquals"));
            _types.Add(typeRef.Name, structType);
            _structTypes.Add(typeRef.Name, structType);
        }
Example #13
0
        public void ExceptionPrivateConstructor3()
        {
            try
            {
                Raise <PrivateCtorException> .If(true, "msg");

                Assert.Fail();
            }
            catch (ThrowerException)
            {
                Should.Throw <ThrowerException>(() => Raise <PrivateCtorException> .If(true, "msg"));
            }
        }
Example #14
0
 public Exception RaiseGeneric()
 {
     try
     {
         var b = Identity(Rnd.Next() % 2 == 0);
         Raise <NotSupportedException> .If(b, nameof(RaiseVsThrow_NotSupportedException));
     }
     catch (Exception ex)
     {
         return(ex);
     }
     return(default(Exception));
 }
 public Exception RaiseGeneric()
 {
     try
     {
         var nullString = Identity <string>(null);
         Raise <ArgumentNullException> .If(nullString == null, nameof(nullString));
     }
     catch (Exception ex)
     {
         return(ex);
     }
     return(default(Exception));
 }
 public Exception RaiseGeneric()
 {
     try
     {
         var x = Identity(21);
         var y = Identity(3);
         Raise <ArgumentOutOfRangeException> .If(x >= y, nameof(x));
     }
     catch (Exception ex)
     {
         return(ex);
     }
     return(default(Exception));
 }
Example #17
0
        public void Visit(FunctionDecl funcDecl)
        {
            var oldRet = _foundReturn;

            _foundReturn = false;
            funcDecl.Body.Accept(this);
            Raise <ReturnCheckException> .If(!_foundReturn && funcDecl.ReturnTypeName != "void");

            if (!_foundReturn && funcDecl.ReturnTypeName == "void")
            {
                funcDecl.RequiresExplicitReturn = true;
            }
            _foundReturn = oldRet;
        }
Example #18
0
        public void ExceptionInternalConstructor3()
        {
            try
            {
                Raise <ThrowerException> .If(true, "A RANDOM MSG");

                Assert.Fail();
            }
            catch (ThrowerException ex)
            {
                Assert.AreNotEqual("A RANDOM MSG", ex.Message);
                Assert.Pass();
            }
        }
Example #19
0
        public void InvalidOperationException_WithMsg()
        {
            try
            {
                Raise <InvalidOperationException> .If(true, "Pino");

                Assert.Fail();
            }
            catch (InvalidOperationException ex)
            {
                Assert.AreEqual("Pino", ex.Message);
                Assert.Pass();
            }
        }
Example #20
0
        public void ArgumentNullException_WithMsg()
        {
            try
            {
                Raise <ArgumentNullException> .If(true, "Pino");

                Assert.Fail();
            }
            catch (ArgumentNullException ex)
            {
                Assert.AreEqual("Pino", ex.Message);
                Assert.Pass();
            }
        }
Example #21
0
 public void ExtendedConstructor_TooManyArgs()
 {
     try
     {
         Raise <ArgumentException> .If(true, 1, "snau", 3.14M);
     }
     catch (ArgumentException aex)
     {
         Assert.Pass(aex.Message);
     }
     catch (Exception ex)
     {
         Assert.Fail(ex.Message);
     }
 }
Example #22
0
 private static void WrongThreadTest()
 {
     for (var i = 0; i < 10000; ++i)
     {
         var msg = i.ToString(CultureInfo.InvariantCulture);
         try
         {
             Raise <NoCtorException> .If(true, msg);
         }
         catch (ThrowerException ex)
         {
             Assert.AreNotEqual(msg, ex.Message);
         }
     }
 }
Example #23
0
        public static CallResult InvokeUnmanagedFunction(CallData callData)
        {
            Type       dllHandle  = CreateTypeBuilder(callData);
            MethodInfo methodInfo = dllHandle.GetMethod(callData.ProcedureName);

            Raise <LegacyWrapperException> .If(methodInfo == null, $"Requested method {callData.ProcedureName} was not found in unmanaged DLL.");

            object result = methodInfo.Invoke(null, callData.Parameters);

            return(new CallResult()
            {
                Result = result,
                Parameters = callData.Parameters
            });
        }
        public void Visit(FunctionCall fc)
        {
            Raise <TypeCheckException> .If(!_funcDecls.ContainsKey(fc.FunctionName), "Function not existing");

            var fd = _funcDecls[fc.FunctionName];

            Raise <TypeCheckException> .IfAreNotEqual(fc.Arguments.Count, fd.Params.Count, "Wrong argument count!");

            for (var i = 0; i < fd.Params.Count; ++i)
            {
                fc.Arguments[i].Accept(this);
                Raise <TypeCheckException> .IfAreNotSame(fc.Arguments[i].Type, fd.Params[i].Type, "Wrong parameter type");
            }
            fc.Function = fd;
            fc.Type     = _result = fd.ReturnType;
        }
        void LoadFunc(MethodReference funcDef)
        {
            Raise <TypeCheckException> .If(_funcDecls.ContainsKey(funcDef.Name));

            var funcRef  = Module.Import(funcDef);
            var funcDecl = new FunctionDecl();

            funcDecl.Name           = funcRef.Name;
            funcDecl.ReturnType     = GetType(funcRef.ReturnType.Name);
            funcDecl.ReturnTypeName = funcDecl.ReturnType.Name;
            funcDecl.Reference      = funcRef;
            foreach (var p in funcDef.Parameters)
            {
                var paramType = GetType(p.ParameterType.Name);
                var paramInfo = funcDecl.AddParam(p.Name, paramType.Name);
                paramInfo.Type      = paramType;
                paramInfo.Reference = p;
            }
            _funcDecls.Add(funcDef.Name, funcDecl);
        }
Example #26
0
        static int Main(string[] args)
        {
            try {
                Raise <Exception> .If(args.Length == 0 || args.Length > 2, "Wrong argument count!");

                Raise <Exception> .If(args.Length == 2 && args[0] != "-e", "Wrong flag!");

                if (args.Length == 2)
                {
                    var output = Compile(args[1]);
                    Execute(output);
                }
                else
                {
                    Compile(args[0]);
                }
            } catch (Exception ex) {
                Console.WriteLine();
                Console.WriteLine("ERROR: {0}", ex.Message);
                return(1);
            }
            return(0);
        }
        public void Visit(FunctionDecl funcDecl)
        {
            Raise <TypeCheckException> .If(_funcDecls.ContainsKey(funcDecl.Name), "Function with existing name!");

            funcDecl.ReturnType = GetType(funcDecl.ReturnTypeName);
            var previousStaticEnv = _staticEnv;

            _staticEnv = new StaticEnv(new OutmostStaticEnv());
            foreach (var p in funcDecl.Params)
            {
                p.Type = GetType(p.TypeName);
                Raise <TypeCheckException> .IfAreSame(p.Type, _voidType, "Struct fields cannot be void");

                var vInfo = new StaticEnvBase.VarInfo(p.Name, p.Type, StaticEnvBase.Kind.Param, p);
                _staticEnv.SetVariable(p.Name, vInfo);
            }
            _funcDecls.Add(funcDecl.Name, funcDecl); // Must be put here to allow recursion...
            var prevFunc = _currFunc;

            _currFunc = funcDecl;
            funcDecl.Body.Accept(this);
            _currFunc  = prevFunc;
            _staticEnv = previousStaticEnv;
        }
Example #28
0
        public static int Pow(int b, int e)
        {
            Raise <ArgumentOutOfRangeException> .If(e < 0);

            return((int)Math.Pow(b, e));
        }
Example #29
0
        public void Visit(Return ret)
        {
            Raise <ReturnCheckException> .If(_foundReturn, "Two or more returns in same block");

            _foundReturn = true;
        }
 public void Visit(Print print)
 {
     print.Exp.Accept(this);
     Raise <TypeCheckException> .If(print.Exp.Type != _intType && print.Exp.Type != _boolType);
 }