protected internal override void CheckSemantics(AstHelper astHelper)
        {
            TypeName.CheckSemantics(astHelper);

            string result = "#result";

            _inner = new LetInExpression
            {
                VariableDeclarations = new List <VariableDeclarationBase>
                {
                    new VariableDeclarationExpression
                    {
                        VariableName = new MemberIdentifierNode {
                            Name = result
                        },
                        Value = new NewExpression(TypeName.ReferencedType)
                    }
                },
                Body = new ListSemiExpression(from field in Fields
                                              select new AssignExpression
                {
                    Left = new FieldAccessExpression
                    {
                        FieldName = field.FieldName,
                        Target    = new VariableAccessExpression
                        {
                            VariableName = result
                        }
                    },
                    Right = field.Value,
                    Start = field.Start
                })
                {
                    new VariableAccessExpression
                    {
                        VariableName = result
                    }
                }
            };

            _inner.CheckSemantics(astHelper);
        }
        protected internal override void CheckSemantics(AstHelper astHelper)
        {
            TypeName.CheckSemantics(astHelper);

            string result = "#result";
            _inner = new LetInExpression
                {
                    VariableDeclarations = new List<VariableDeclarationBase>
                        {
                            new VariableDeclarationExpression
                                {
                                    VariableName = new MemberIdentifierNode {Name = result},
                                    Value = new NewExpression(TypeName.ReferencedType)
                                }
                        },
                    Body = new ListSemiExpression(from field in Fields
                                                  select new AssignExpression
                                                      {
                                                          Left = new FieldAccessExpression
                                                              {
                                                                  FieldName = field.FieldName,
                                                                  Target = new VariableAccessExpression
                                                                      {
                                                                          VariableName = result
                                                                      }
                                                              },
                                                          Right = field.Value,
                                                          Start = field.Start
                                                      })
                        {
                            new VariableAccessExpression
                                {
                                    VariableName = result
                                }
                        }
                };

            _inner.CheckSemantics(astHelper);
        }
Beispiel #3
0
        protected internal override void CheckSemantics(AstHelper astHelper)
        {
            string lenght     = "#length" + uniqueId++;
            string result     = "#result" + uniqueId++;
            string forVarName = "#i" + uniqueId++;

            var valueEnclosed = InitialValue.ProtectSemantics();

            _implementation = new LetInExpression
            {
                VariableDeclarations = new List <VariableDeclarationBase>
                {
                    new VariableDeclarationExpression
                    {
                        VariableName =
                            new MemberIdentifierNode
                        {
                            Name = lenght
                        },
                        Value = Lenght
                    },
                    new VariableDeclarationExpression
                    {
                        VariableName =
                            new MemberIdentifierNode
                        {
                            Name = result
                        },
                        Value = new ArrayInitializationExpression
                        {
                            ArrayTypeName =
                                ArrayTypeName,
                            Lenght =
                                new VariableAccessExpression
                            {
                                VariableName =
                                    lenght
                            }
                        }
                    }
                },
                Body = new ListSemiExpression
                {
                    new ForLoopExpression
                    {
                        VariableName = new IdentifierNode {
                            Name = forVarName
                        },
                        From = new IntLiteralExpression(0),
                        To   = new BinaryOperationExpression
                        {
                            Operator = TigerOperator.Subtract,
                            Left     = new VariableAccessExpression
                            {
                                VariableName = lenght
                            },
                            Right = new IntLiteralExpression(1)
                        },
                        Body = new AssignExpression
                        {
                            Left = new ArrayIndexExpression
                            {
                                Indexes = new[]
                                {
                                    new VariableAccessExpression
                                    {
                                        VariableName
                                            =
                                                forVarName
                                    }
                                },
                                Target =
                                    new VariableAccessExpression
                                {
                                    VariableName =
                                        result
                                }
                            },
                            Right = valueEnclosed
                        }
                    },
                    new VariableAccessExpression {
                        VariableName = result
                    }
                }
            };

            _implementation.CheckSemantics(astHelper);
        }
        protected internal override void CheckSemantics(AstHelper astHelper)
        {
            base.CheckSemantics(astHelper);
            var collectionWrapper =  Collection.ProtectSemantics();
            collectionWrapper.CheckSemantics(astHelper);

            if (!astHelper.Errors.Check(new NotEnumerableError(collectionWrapper.Type, Collection.Start)))
            {
                Type collectionType = TypeUtils.GetCollectionType(collectionWrapper.Type);

                string enumerator = "#enumerator" + _uniqueId++;
                _implementation = new LetInExpression
                    {
                        VariableDeclarations = new List<VariableDeclarationBase>
                            {
                                new VariableDeclarationExpression
                                    {
                                        VariableName = new MemberIdentifierNode
                                            {
                                                Name = enumerator
                                            },
                                        Value = new MethodCallExpression
                                            {
                                                MethodName = new IdentifierNode
                                                    {
                                                        Name = "GetEnumerator"
                                                    },
                                                Target = collectionWrapper
                                            }
                                    },
                                new VariableDeclarationBase
                                    {
                                        VariableName = new MemberIdentifierNode
                                            {
                                                Name = Identifier.Name
                                            },
                                        VariableTypeName = new TypeReferenceNode(collectionType)
                                    }
                            },
                        Body = new ListSemiExpression
                            {
                                new WhileExpression
                                    {
                                        Test = new MethodCallExpression
                                            {
                                                Target = new VariableAccessExpression
                                                    {
                                                        VariableName = enumerator
                                                    },
                                                MethodName = new IdentifierNode
                                                    {
                                                        Name = "MoveNext"
                                                    }
                                            },
                                        Body = new ListSemiExpression
                                            {
                                                new AssignExpression
                                                    {
                                                        Left = new VariableAccessExpression
                                                            {
                                                                VariableName = Identifier.Name
                                                            },
                                                        Right = new TypeCastExpression
                                                            {
                                                                Expression = new FieldAccessExpression
                                                                    {
                                                                        Target = new VariableAccessExpression
                                                                            {
                                                                                VariableName = enumerator
                                                                            },
                                                                        FieldName = new IdentifierNode
                                                                            {
                                                                                Name = "Current"
                                                                            }
                                                                    },
                                                                TargetType = collectionType
                                                            }
                                                    },
                                                Body
                                            }
                                    }
                            }
                    };
                _implementation.CheckSemantics(astHelper);
            }
        }
        public void variable_declaration_without_type_equals_null()
        {
            // let var x:=nil in x and
            var target = new LetInExpression
                {
                    VariableDeclarations = new List<VariableDeclarationBase>
                        {
                            new VariableDeclarationExpression
                                {
                                    VariableName =
                                        new MemberIdentifierNode {Name = "x"},
                                    VariableTypeName = new TypeReferenceNode(),
                                    Value = new NullLiteralExpression()
                                }
                        },
                    Body = new ListSemiExpression
                        {
                            new VariableAccessExpression {VariableName = "x"}
                        }
                };
            AstHelper helper = Mother.CreateRuntime();

            try
            {
                target.Compile(helper);
            }
            catch (SemanticException e)
            {
                var error = e.Errors.First() as CanNotInferTypeError;
                Assert.IsNotNull(error);
                Assert.Pass();
            }
            Assert.Fail();
        }
        protected internal override void CheckSemantics(AstHelper astHelper)
        {
            base.CheckSemantics(astHelper);
            var collectionWrapper = Collection.ProtectSemantics();

            collectionWrapper.CheckSemantics(astHelper);

            if (!astHelper.Errors.Check(new NotEnumerableError(collectionWrapper.Type, Collection.Start)))
            {
                Type collectionType = TypeUtils.GetCollectionType(collectionWrapper.Type);

                string enumerator = "#enumerator" + _uniqueId++;
                _implementation = new LetInExpression
                {
                    VariableDeclarations = new List <VariableDeclarationBase>
                    {
                        new VariableDeclarationExpression
                        {
                            VariableName = new MemberIdentifierNode
                            {
                                Name = enumerator
                            },
                            Value = new MethodCallExpression
                            {
                                MethodName = new IdentifierNode
                                {
                                    Name = "GetEnumerator"
                                },
                                Target = collectionWrapper
                            }
                        },
                        new VariableDeclarationBase
                        {
                            VariableName = new MemberIdentifierNode
                            {
                                Name = Identifier.Name
                            },
                            VariableTypeName = new TypeReferenceNode(collectionType)
                        }
                    },
                    Body = new ListSemiExpression
                    {
                        new WhileExpression
                        {
                            Test = new MethodCallExpression
                            {
                                Target = new VariableAccessExpression
                                {
                                    VariableName = enumerator
                                },
                                MethodName = new IdentifierNode
                                {
                                    Name = "MoveNext"
                                }
                            },
                            Body = new ListSemiExpression
                            {
                                new AssignExpression
                                {
                                    Left = new VariableAccessExpression
                                    {
                                        VariableName = Identifier.Name
                                    },
                                    Right = new TypeCastExpression
                                    {
                                        Expression = new FieldAccessExpression
                                        {
                                            Target = new VariableAccessExpression
                                            {
                                                VariableName = enumerator
                                            },
                                            FieldName = new IdentifierNode
                                            {
                                                Name = "Current"
                                            }
                                        },
                                        TargetType = collectionType
                                    }
                                },
                                Body
                            }
                        }
                    }
                };
                _implementation.CheckSemantics(astHelper);
            }
        }
        protected internal override void CheckSemantics(AstHelper astHelper)
        {
            base.CheckSemantics(astHelper);

            var from = From.ProtectSemantics();
            var to =To.ProtectSemantics();

            from.CheckSemantics(astHelper);
            to.CheckSemantics(astHelper);

            bool error = false;
            error |= astHelper.Errors.Check(new NotAssignableError(typeof (int), from.Type, VariableName.Start));
            error |= astHelper.Errors.Check(new NotAssignableError(typeof (int), to.Type, VariableName.Start));

            if (error) return;

            _implementation = new LetInExpression
                {
                    VariableDeclarations = new List<VariableDeclarationBase>
                        {
                            new VariableDeclarationExpression
                                {
                                    VariableName =
                                        new MemberIdentifierNode
                                            {Name = VariableName.Name},
                                    Value = from
                                }
                        },
                    Body = new ListSemiExpression
                        {
                            new WhileExpression
                                {
                                    Test = new LogicalBinaryOperationExpression
                                        {
                                            Left = new VariableAccessExpression
                                                {
                                                    VariableName =
                                                        VariableName.Name
                                                },
                                            Right = to,
                                            Operator = TigerOperator.LessThanOrEqual
                                        },
                                    Body = new ListSemiExpression
                                        {
                                            Body,
                                            new UnaryOperationExpression
                                                {
                                                    Expression =
                                                        new VariableAccessExpression
                                                            {
                                                                VariableName =
                                                                    VariableName.Name
                                                            },
                                                    Operator =
                                                        TigerOperator.PreIncrementAssign
                                                },
                                            new EmptyExpression()
                                        }
                                }
                        }
                };

            _implementation.CheckSemantics(astHelper);
        }
Beispiel #8
0
        protected internal override void CheckSemantics(AstHelper astHelper)
        {
            base.CheckSemantics(astHelper);

            var from = From.ProtectSemantics();
            var to   = To.ProtectSemantics();

            from.CheckSemantics(astHelper);
            to.CheckSemantics(astHelper);

            bool error = false;

            error |= astHelper.Errors.Check(new NotAssignableError(typeof(int), from.Type, VariableName.Start));
            error |= astHelper.Errors.Check(new NotAssignableError(typeof(int), to.Type, VariableName.Start));

            if (error)
            {
                return;
            }

            _implementation = new LetInExpression
            {
                VariableDeclarations = new List <VariableDeclarationBase>
                {
                    new VariableDeclarationExpression
                    {
                        VariableName =
                            new MemberIdentifierNode
                        {
                            Name = VariableName.Name
                        },
                        Value = from
                    }
                },
                Body = new ListSemiExpression
                {
                    new WhileExpression
                    {
                        Test = new LogicalBinaryOperationExpression
                        {
                            Left = new VariableAccessExpression
                            {
                                VariableName =
                                    VariableName.Name
                            },
                            Right    = to,
                            Operator = TigerOperator.LessThanOrEqual
                        },
                        Body = new ListSemiExpression
                        {
                            Body,
                            new UnaryOperationExpression
                            {
                                Expression =
                                    new VariableAccessExpression
                                {
                                    VariableName =
                                        VariableName.Name
                                },
                                Operator =
                                    TigerOperator.PreIncrementAssign
                            },
                            new EmptyExpression()
                        }
                    }
                }
            };

            _implementation.CheckSemantics(astHelper);
        }
        protected internal override void CheckSemantics(AstHelper astHelper)
        {
            string lenght = "#length" + uniqueId++;
            string result = "#result" + uniqueId++;
            string forVarName = "#i" + uniqueId++;

            var valueEnclosed = InitialValue.ProtectSemantics();

            _implementation = new LetInExpression
                {
                    VariableDeclarations = new List<VariableDeclarationBase>
                        {
                            new VariableDeclarationExpression
                                {
                                    VariableName =
                                        new MemberIdentifierNode
                                            {Name = lenght},
                                    Value = Lenght
                                },
                            new VariableDeclarationExpression
                                {
                                    VariableName =
                                        new MemberIdentifierNode
                                            {Name = result},
                                    Value = new ArrayInitializationExpression
                                        {
                                            ArrayTypeName =
                                                ArrayTypeName,
                                            Lenght =
                                                new VariableAccessExpression
                                                    {
                                                        VariableName =
                                                            lenght
                                                    }
                                        }
                                }
                        },
                    Body = new ListSemiExpression
                        {
                            new ForLoopExpression
                                {
                                    VariableName = new IdentifierNode {Name = forVarName},
                                    From = new IntLiteralExpression(0),
                                    To = new BinaryOperationExpression
                                        {
                                            Operator = TigerOperator.Subtract,
                                            Left = new VariableAccessExpression
                                                {
                                                    VariableName = lenght
                                                },
                                            Right = new IntLiteralExpression(1)
                                        },
                                    Body = new AssignExpression
                                        {
                                            Left = new ArrayIndexExpression
                                                {
                                                    Indexes = new[]
                                                        {
                                                            new VariableAccessExpression
                                                                {
                                                                    VariableName
                                                                        =
                                                                        forVarName
                                                                }
                                                        },
                                                    Target =
                                                        new VariableAccessExpression
                                                            {
                                                                VariableName =
                                                                    result
                                                            }
                                                },
                                            Right = valueEnclosed
                                        }
                                },
                            new VariableAccessExpression {VariableName = result}
                        }
                };

            _implementation.CheckSemantics(astHelper);
        }
        public void variable_initialization_with_undefined_function()
        {
            // let var x:=Foo() in x and
            var target = new LetInExpression
                {
                    VariableDeclarations = new List<VariableDeclarationBase>
                        {
                            new VariableDeclarationExpression
                                {
                                    VariableName =
                                        new MemberIdentifierNode {Name = "x"},
                                    VariableTypeName = new TypeReferenceNode(),
                                    Value = new FunctionInvokeExpression
                                        {
                                            FunctionName =
                                                new IdentifierNode
                                                    {Name = "Foo"}
                                        }
                                }
                        },
                    Body = new ListSemiExpression
                        {
                            new VariableAccessExpression {VariableName = "x"}
                        }
                };
            AstHelper helper = Mother.CreateRuntime();

            try
            {
                target.Compile(helper);
            }
            catch (SemanticException e)
            {
                var error = e.Errors.First() as FunctionNotDefinedError;
                Assert.IsNotNull(error);
            }
        }