Ejemplo n.º 1
0
        public void ExpressionSplittingTest1()
        {
            var expr = new StatementList(MetaData.Empty,
                                         IdDeclaration,
                                         new ExpressionStatement(MetaData.Empty,
                                                                 new FunctionCallExpression(MetaData.Empty,
                                                                                            new VariableExpression(MetaData.Empty, "id"),
                                                                                            new List <Expression>(new[]
            {
                new FunctionCallExpression(MetaData.Empty,
                                           new VariableExpression(MetaData.Empty, "id"),
                                           new List <Expression>(new[]
                {
                    new FunctionCallExpression(MetaData.Empty,
                                               new VariableExpression(MetaData.Empty, "id"),
                                               new List <Expression>(new[]
                    {
                        new IntLiteralExpression(MetaData.Empty, "123", true)
                    })
                                               )
                }))
            }))));

            expr.SurroundWith(Environment.SolarSystem);
            expr.PrintDumpInfo();
            Assert.IsNotNull(expr.ConvertedStatementList);
            expr.ConvertedStatementList.PrintDumpInfo();
        }
Ejemplo n.º 2
0
        public void FuncCallTest3()
        {
            var lambda = new LambdaExpression(MetaData.Empty,
                                              new StatementList(MetaData.Empty,
                                                                new ReturnStatement(MetaData.Empty,
                                                                                    new FunctionCallExpression(MetaData.Empty,
                                                                                                               new VariableExpression(MetaData.Empty, "recur"),
                                                                                                               new List <Expression>(new[]
            {
                new VariableExpression(MetaData.Empty, "a")
            })))),
                                              new List <VariableDeclaration>(new[]
            {
                new VariableDeclaration(MetaData.Empty, "a", type:
                                        new UnknownType(MetaData.Empty, "i8"))
            }));
            var example = new StatementList(MetaData.Empty,
                                            new VariableDeclaration(MetaData.Empty, "recurFunc",
                                                                    lambda),
                                            new VariableDeclaration(MetaData.Empty, "gg", isMutable: true, type:
                                                                    new UnknownType(MetaData.Empty, "i8")),
                                            new AssignmentStatement(MetaData.Empty,
                                                                    new VariableExpression(MetaData.Empty, "gg"),
                                                                    new FunctionCallExpression(MetaData.Empty,
                                                                                               new VariableExpression(MetaData.Empty, "recurFunc"),
                                                                                               new List <Expression>(new[]
            {
                new IntLiteralExpression(MetaData.Empty, "233", true, 8)
            }))));

            Assert.ThrowsException <CompilerException>(() => { example.SurroundWith(Environment.SolarSystem); });
        }
Ejemplo n.º 3
0
        public override void SurroundWith(Environment environment)
        {
            base.SurroundWith(environment);
            if (DeclaredType is UnknownType unknownType)
            {
                unknownType.SurroundWith(Env);
                DeclaredType = unknownType.Resolve();
            }
            foreach (var variableDeclaration in ParameterList)
            {
                variableDeclaration.SurroundWith(Env);
            }
            var bodyEnv = new Environment(Env);

            foreach (var variableDeclaration in ParameterList)
            {
                bodyEnv.Declarations.Add(variableDeclaration);
            }
            // FEATURE #37
            var recur = new VariableDeclaration(MetaData, ReservedWords.Recur, this);

            // https://github.com/Cm-lang/Cm-Document/issues/12
            if (null != DeclaredType)
            {
                Type = new LambdaType(MetaData, (
                                          from i in ParameterList
                                          select i.Type).ToList(), DeclaredType);
            }
            // FEATURE #39
            recur.SurroundWith(Env);
            bodyEnv.Declarations.Add(recur);
            Body.SurroundWith(bodyEnv);
            var retTypes = Body.FindReturnStatements().Select(i =>
            {
                i.WhereToJump = this;
                return(i.Expression.GetExpressionType());
            }).ToList();

            // FEATURE #24
            if (retTypes.Any(i => !Equals(i, DeclaredType ?? retTypes.First())))
            {
                Errors.Add(
                    $"{MetaData.GetErrorHeader()}ambiguous return types:\n" +
                    (DeclaredType != null ? $"<{DeclaredType}>" : "") +
                    $"[{string.Join(",", from i in retTypes select i.ToString())}]");
            }
            // FEATURE #12
            var retType = DeclaredType ?? (retTypes.Count != 0
                                              ? retTypes.First()
                                           // FEATURE #19
                                              : new PrimaryType(MetaData, PrimaryType.NullType));

            Type = new LambdaType(MetaData, (
                                      from i in ParameterList
                                      select i.Type).ToList(), retType);
        }
Ejemplo n.º 4
0
        public void TypeInferenceTest3()
        {
            const string varName = "otherVar";
            var          example = new StatementList(MetaData.Empty,
                                                     new VariableDeclaration(MetaData.Empty, varName,
                                                                             new NullExpression(MetaData.Empty),
                                                                             type: new UnknownType(MetaData.Empty, "i8")),
                                                     new ExpressionStatement(MetaData.Empty,
                                                                             new VariableExpression(MetaData.Empty, varName)));

            example.SurroundWith(Environment.SolarSystem);
            Console.WriteLine(string.Join("", example.Dump()));
            Assert.AreEqual("i8", ((ExpressionStatement)example.Statements.Last())
                            .Expression.GetExpressionType().ToString());
        }
Ejemplo n.º 5
0
        public void TypeInferenceTest2()
        {
            const string varName = "someOtherVar";
            var          example = new StatementList(MetaData.Empty,
                                                     new VariableDeclaration(MetaData.Empty, varName,
                                                                             new NullExpression(MetaData.Empty)),
                                                     new ExpressionStatement(MetaData.Empty,
                                                                             new VariableExpression(MetaData.Empty, varName)));

            example.SurroundWith(Environment.SolarSystem);
            example.PrintDumpInfo();
            Assert.AreEqual(PrimaryType.NullType,
                            ((ExpressionStatement)example.Statements.Last())
                            .Expression.GetExpressionType().ToString());
        }
Ejemplo n.º 6
0
        public void TypeInferenceTest1()
        {
            const string varName = "someVar";
            var          example = new StatementList(MetaData.Empty,
                                                     new VariableDeclaration(MetaData.Empty, varName,
                                                                             new IntLiteralExpression(MetaData.Empty, "123", false, 8)),
                                                     new ExpressionStatement(MetaData.Empty,
                                                                             new VariableExpression(MetaData.Empty, varName)));

            example.SurroundWith(Environment.SolarSystem);
            example.PrintDumpInfo();
            // ReSharper disable once PossibleNullReferenceException
            Assert.AreEqual("u8", ((ExpressionStatement)example.Statements.Last())
                            .Expression.GetExpressionType().ToString());
        }
Ejemplo n.º 7
0
        public void FuncCallTest2()
        {
            var example = new StatementList(MetaData.Empty,
                                            IdDeclaration,
                                            new VariableDeclaration(MetaData.Empty, "gg", isMutable: true, type:
                                                                    new UnknownType(MetaData.Empty, "i8")),
                                            new AssignmentStatement(MetaData.Empty,
                                                                    new VariableExpression(MetaData.Empty, "gg"),
                                                                    new FunctionCallExpression(MetaData.Empty,
                                                                                               new VariableExpression(MetaData.Empty, "id"),
                                                                                               new List <Expression>(new[] { new IntLiteralExpression(MetaData.Empty, "233", true, 8) }))));

            example.SurroundWith(Environment.SolarSystem);
            example.PrintDumpInfo();
            Console.WriteLine(string.Join("\n", Errors.ErrList));
            Assert.IsTrue(0 == Errors.ErrList.Count);
        }
Ejemplo n.º 8
0
        public void StatementTest4()
        {
            const string var1 = "variableOne";
            var          stmt = new StatementList(MetaData.Empty,
                                                  new VariableDeclaration(MetaData.Empty, var1,
                                                                          new BoolLiteralExpression(MetaData.Empty, true), true),
                                                  new WhileStatement(MetaData.Empty,
                                                                     new VariableExpression(MetaData.Empty, var1),
                                                                     new StatementList(MetaData.Empty,
                                                                                       new AssignmentStatement(MetaData.Empty,
                                                                                                               new VariableExpression(MetaData.Empty, var1),
                                                                                                               new BoolLiteralExpression(MetaData.Empty, false)))));

            stmt.SurroundWith(Environment.SolarSystem);
            stmt.PrintDumpInfo();
            Assert.IsTrue(0 == Errors.ErrList.Count);
        }
Ejemplo n.º 9
0
        public void StructTest1()
        {
            var @struct = new StatementList(MetaData.Empty,
                                            new StructDeclaration(MetaData.Empty, "Person", new List <VariableDeclaration>(new[]
            {
                new VariableDeclaration(MetaData.Empty, "name", new StringLiteralExpression(MetaData.Empty, "ice")),
                new VariableDeclaration(MetaData.Empty, "gender",
                                        new IntLiteralExpression(MetaData.Empty, "123", false))
            })),
                                            new VariableDeclaration(MetaData.Empty, "var", type:
                                                                    new UnknownType(MetaData.Empty, "Person")));

            @struct.SurroundWith(Environment.SolarSystem);
            @struct.PrintDumpInfo();
            var a = (VariableDeclaration)@struct.Statements.Last();

            Assert.AreEqual("Person", a.Type.ToString());
            Assert.IsTrue(a.Type is SecondaryType);
        }
Ejemplo n.º 10
0
        public void FuncCallTest5()
        {
            var lambda = new LambdaExpression(MetaData.Empty,
                                              new StatementList(MetaData.Empty,
                                                                new ReturnStatement(MetaData.Empty,
                                                                                    new FunctionCallExpression(MetaData.Empty,
                                                                                                               new LambdaExpression(MetaData.Empty,
                                                                                                                                    new StatementList(MetaData.Empty,
                                                                                                                                                      new ReturnStatement(MetaData.Empty,
                                                                                                                                                                          new FunctionCallExpression(MetaData.Empty,
                                                                                                                                                                                                     new VariableExpression(MetaData.Empty, "recur"),
                                                                                                                                                                                                     new List <Expression>(new[]
            {
                new VariableExpression(MetaData.Empty, "a")
            })))
                                                                                                                                                      ), returnType:
                                                                                                                                    new UnknownType(MetaData.Empty, "i8")),
                                                                                                               new List <Expression>()))),
                                              new List <VariableDeclaration>(new[]
            {
                new VariableDeclaration(MetaData.Empty, "a", type:
                                        new UnknownType(MetaData.Empty, "i8"))
            }), new UnknownType(MetaData.Empty, "i8"));
            var example = new StatementList(MetaData.Empty,
                                            new VariableDeclaration(MetaData.Empty, "recurFunc",
                                                                    lambda),
                                            new VariableDeclaration(MetaData.Empty, "gg", isMutable: true, type:
                                                                    new UnknownType(MetaData.Empty, "i8")),
                                            new AssignmentStatement(MetaData.Empty,
                                                                    new VariableExpression(MetaData.Empty, "gg"),
                                                                    new FunctionCallExpression(MetaData.Empty,
                                                                                               new VariableExpression(MetaData.Empty, "recurFunc"),
                                                                                               new List <Expression>(new[]
            {
                new IntLiteralExpression(MetaData.Empty, "233", true, 8)
            }))));

            example.SurroundWith(Environment.SolarSystem);
            example.PrintDumpInfo();
            Assert.IsTrue(0 == Errors.ErrList.Count);
        }
Ejemplo n.º 11
0
        public override void SurroundWith(Environment environment)
        {
            base.SurroundWith(environment);
            if (DeclaredType is UnknownType unknownType)
            {
                unknownType.SurroundWith(Env);
                DeclaredType = unknownType.Resolve();
            }
            foreach (var variableDeclaration in ParameterList)
            {
                variableDeclaration.SurroundWith(Env);
            }
            EndLabel.SurroundWith(Env);
            var bodyEnv = new Environment(Env);

            Env.Declarations.Add(EndLabel);
            foreach (var variableDeclaration in ParameterList)
            {
                bodyEnv.Declarations.Add(variableDeclaration);
            }
            // https://github.com/Cm-lang/Cm-Document/issues/12
            if (null != DeclaredType)
            {
                Type = new LambdaType(MetaData, (
                                          from i in ParameterList
                                          select i.Type).ToList(), DeclaredType);
            }
            if (Recur)
            {
                // FEATURE #37
                var recur = new VariableDeclaration(MetaData, ReservedWords.Recur, this);
                // FEATURE #39
                recur.SurroundWith(Env);
                bodyEnv.Declarations.Add(recur);
            }
            Body.SurroundWith(bodyEnv);
//			while (null != Body.OptimizedStatementList)
//				Body = Body.OptimizedStatementList;
            while (null != Body.ConvertedStatementList)
            {
                Body = Body.ConvertedStatementList;
            }
            Body.Statements.Add(EndLabel);
            var retTypes = EndLabel.StatementsUsingThis.Select(i =>
            {
                i.ReturnLabel = EndLabel;
                return(i.Expression.GetExpressionType());
            }).ToList();

//			Body.Flatten();
            if (retTypes.Any(i => !Equals(i, DeclaredType ?? retTypes.First())))
            {
                Errors.Add(
                    $"{MetaData.GetErrorHeader()}ambiguous return types:\n" +
                    (DeclaredType != null ? $"<{DeclaredType}>" : "") +
                    $"[{string.Join(",", from i in retTypes select i.ToString())}]");
            }
            // FEATURE #12
            var retType = DeclaredType ?? (retTypes.Count != 0
                                              ? retTypes.First()
                                           // FEATURE #19
                                              : new PrimaryType(MetaData, PrimaryType.NullType));

            // FEATURE #46
            // FEATURE #47
            if (retTypes.Count > 1)
            {
                var varName = $"retClctor{(ulong) GetHashCode()}";
                Body.Statements.Insert(0, new VariableDeclaration(MetaData, varName, type: retType));
                var returnValueCollector = new VariableExpression(MetaData, varName);
                foreach (var endLabelStatement in EndLabel.StatementsUsingThis)
                {
                    endLabelStatement.Unify(returnValueCollector);
                }
                Body.Statements.Add(new ReturnStatement(MetaData, returnValueCollector)
                {
                    ReturnLabel = EndLabel
                });
            }
            Type = new LambdaType(MetaData, (
                                      from i in ParameterList
                                      select i.Type).ToList(), retType);
        }