Exemple #1
0
        protected internal override void CheckSemantics(AstHelper astHelper)
        {
            VariableName.CheckSemantics(astHelper);
            VariableTypeName.CheckSemantics(astHelper);

            AstHelper childScope = VariableTypeName.HasType
                                       ? astHelper.CreateChild(expecting: true,
                                                               expectedType: VariableTypeName.ReferencedType)
                                       : astHelper.CreateChild(expecting: true);

            Value.CheckSemantics(childScope);

            bool cantInferType = astHelper.Errors.Check(new CanNotInferTypeError(Type, Value.Type, Start));

            astHelper.Errors.Check(new NotAssignableError(Type, Value.Type, Start));
            bool memberDeclared = astHelper.Errors.Check(
                new MemberWithSameNameAlreadyDefinedError(VariableName.Name, astHelper, Start));

            if (!memberDeclared)
            {
                if (!cantInferType)
                {
                    Pointer = MAst.Parameter(Type, VariableName.Name);
                }
                else
                {
                    // if the type can't be inferred then register the variable with null type
                    Pointer = MAst.Parameter(typeof(Null), VariableName.Name);
                }

                astHelper.Variables.Add(VariableName.Name, Pointer);
            }
        }
        protected internal override void CheckSemantics(AstHelper astHelper)
        {
            VariableName.CheckSemantics(astHelper);
            VariableTypeName.CheckSemantics(astHelper);

            AstHelper childScope = VariableTypeName.HasType
                                       ? astHelper.CreateChild(expecting: true,
                                                               expectedType: VariableTypeName.ReferencedType)
                                       : astHelper.CreateChild(expecting: true);

            Value.CheckSemantics(childScope);

            bool cantInferType = astHelper.Errors.Check(new CanNotInferTypeError(Type, Value.Type, Start));
            astHelper.Errors.Check(new NotAssignableError(Type, Value.Type, Start));
            bool memberDeclared = astHelper.Errors.Check(
                new MemberWithSameNameAlreadyDefinedError(VariableName.Name, astHelper, Start));

            if (!memberDeclared)
            {
                if (!cantInferType)
                {
                    Pointer = MAst.Parameter(Type, VariableName.Name);
                }
                else
                {
                    // if the type can't be inferred then register the variable with null type
                    Pointer = MAst.Parameter(typeof (Null), VariableName.Name);
                }

                astHelper.Variables.Add(VariableName.Name, Pointer);
            }
        }
Exemple #3
0
        public static MAst Compile(AstHelper runtime, ICharStream stream)
        {
            var lexer  = new TigerLexer(stream);
            var tokens = new CommonTokenStream(lexer);
            var parser = new TigerParser(tokens);
            ProgramExpression programExpression = parser.parse();

            if (parser.NumberOfSyntaxErrors > 0)
            {
                IEnumerable <string> errors = from e in parser.Errors
                                              select e.ToString();

                throw new SyntaxException(errors);
            }

            AstHelper helper = runtime.CreateChild(function: true, variables: true, types: true);

            programExpression.CheckSemantics(helper);

            if (helper.Errors.HasErrors)
            {
                throw new SemanticException(helper.Errors);
            }

            return(programExpression.Transform());
        }
        protected internal override void CheckSemantics(AstHelper astHelper)
        {
            FunctionName.CheckSemantics(astHelper);

            // initialize the scopes for each one of the arguments
            var scopes = new AstHelper[Args.Count()];
            for (int i = 0; i < scopes.Length; scopes[i++] = astHelper.CreateChild(expecting: true))
            {
            }

            foreach (var item in Args.Zip(scopes, (arg, scope) => new {arg, scope}))
                item.arg.CheckSemantics(item.scope);

            Type[] argTypes = (from arg in Args select arg.Type).ToArray();

            if (astHelper.Errors.Check(new FunctionNotDefinedError(FunctionName.Name, argTypes, astHelper.Functions,
                                                                   Start))) return;

            _pointer = astHelper.Functions[FunctionName.Name, argTypes];

            // SPEC: The following are legal: function f(p:rec) = f(nil)
            // for that reason the expected type for each one of the arguments is the
            // type of the function argument (in the definition)
            foreach (var item in _pointer.ArgTypes.Zip(scopes, (type, scope) => new {type, scope}))
                item.scope.Expecting.Type = item.type;
        }
        protected internal override void CheckSemantics(AstHelper astHelper)
        {
            FunctionName.CheckSemantics(astHelper);

            // initialize the scopes for each one of the arguments
            var scopes = new AstHelper[Args.Count()];

            for (int i = 0; i < scopes.Length; scopes[i++] = astHelper.CreateChild(expecting: true))
            {
            }

            foreach (var item in Args.Zip(scopes, (arg, scope) => new { arg, scope }))
            {
                item.arg.CheckSemantics(item.scope);
            }

            Type[] argTypes = (from arg in Args select arg.Type).ToArray();

            if (astHelper.Errors.Check(new FunctionNotDefinedError(FunctionName.Name, argTypes, astHelper.Functions,
                                                                   Start)))
            {
                return;
            }

            _pointer = astHelper.Functions[FunctionName.Name, argTypes];

            // SPEC: The following are legal: function f(p:rec) = f(nil)
            // for that reason the expected type for each one of the arguments is the
            // type of the function argument (in the definition)
            foreach (var item in _pointer.ArgTypes.Zip(scopes, (type, scope) => new { type, scope }))
            {
                item.scope.Expecting.Type = item.type;
            }
        }
        protected internal override void CheckSemantics(AstHelper astHelper)
        {
            Test.CheckSemantics(astHelper);

            // the if-then-else expression is expected to have a static type
            // for that reason it defines a new Expecting.Type scope
            AstHelper helper = astHelper.CreateChild(expecting: true);

            Then.CheckSemantics(helper);
            Else.CheckSemantics(helper);

            astHelper.Errors.Check(new ConditionalNotEquivalentTypes(Then.Type, Else.Type, Start));

            if (astHelper.Errors.Check(new CanNotInferTypeError(Then.Type, Else.Type, Start)))
            {
                return;
            }

            if (Then.Type == typeof(Null))
            {
                helper.Expecting.Type = Else.Type;
            }
            if (Else.Type == typeof(Null))
            {
                helper.Expecting.Type = Then.Type;
            }
        }
        public static MAst Compile(AstHelper runtime, ICharStream stream)
        {
            var lexer = new TigerLexer(stream);
            var tokens = new CommonTokenStream(lexer);
            var parser = new TigerParser(tokens);
            ProgramExpression programExpression = parser.parse();

            if (parser.NumberOfSyntaxErrors > 0)
            {
                IEnumerable<string> errors = from e in parser.Errors
                                             select e.ToString();

                throw new SyntaxException(errors);
            }

            AstHelper helper = runtime.CreateChild(function: true, variables: true, types: true);
            programExpression.CheckSemantics(helper);

            if (helper.Errors.HasErrors)
            {
                throw new SemanticException(helper.Errors);
            }

            return programExpression.Transform();
        }
        protected internal override void CheckSemantics(AstHelper astHelper)
        {
            base.CheckSemantics(astHelper);

            foreach (var item in Args.Zip(Constructor.GetParameters().Select(parameter => parameter.ParameterType), (arg, type) => new {arg, type}))
            {
                AstHelper helper = astHelper.CreateChild(expecting: true, expectedType: item.type);
                item.arg.CheckSemantics(helper);
            }
        }
        protected internal override void CheckSemantics(AstHelper astHelper)
        {
            base.CheckSemantics(astHelper);

            foreach (var item in Args.Zip(Constructor.GetParameters().Select(parameter => parameter.ParameterType), (arg, type) => new { arg, type }))
            {
                AstHelper helper = astHelper.CreateChild(expecting: true, expectedType: item.type);
                item.arg.CheckSemantics(helper);
            }
        }
        protected internal override void CheckSemantics(AstHelper astHelper)
        {
            base.CheckSemantics(astHelper);

            AstHelper helper = astHelper.CreateChild(expecting: true, expectedType: typeof (bool));
            _innerExpression.CheckSemantics(helper);

            // only void expressions can't be converted to bool
            astHelper.Errors.Check(new NotBoolConvertibleError(_innerExpression.Type, Start));
        }
        protected internal override void CheckSemantics(AstHelper astHelper)
        {
            base.CheckSemantics(astHelper);

            AstHelper helper = astHelper.CreateChild(expecting: true, expectedType: typeof(bool));

            _innerExpression.CheckSemantics(helper);

            // only void expressions can't be converted to bool
            astHelper.Errors.Check(new NotBoolConvertibleError(_innerExpression.Type, Start));
        }
        /// <summary>
        /// Comverts the expression into the corresponding 
        /// <see cref="System.Linq.Expressions.Expression"/>
        /// </summary>
        /// <param name="runtime"></param>
        /// <returns></returns>
        /// <exception cref="SemanticException">if there is some semantic error in the tree</exception>
        public MAst Compile(AstHelper runtime)
        {
            AstHelper helper = runtime.CreateChild(function: true, variables: true, types: true);
            CheckSemantics(helper);

            if (helper.Errors.HasErrors)
            {
                throw new SemanticException(helper.Errors);
            }

            return Transform();
        }
Exemple #13
0
        protected internal override void CheckSemantics(AstHelper astHelper)
        {
            base.CheckSemantics(astHelper);

            Left.CheckSemantics(astHelper);

            AstHelper helper = astHelper.CreateChild(expecting: true, expectedType: Left.Type);

            Right.CheckSemantics(helper);

            astHelper.Errors.Check(new NotAssignableError(Left.Type, Right.Type, Start));
        }
        protected internal override void CheckSemantics(AstHelper astHelper)
        {
            base.CheckSemantics(astHelper);

            Left.CheckSemantics(astHelper);

            AstHelper helper = astHelper.CreateChild(expecting: true, expectedType: Left.Type);

            Right.CheckSemantics(helper);

            astHelper.Errors.Check(new NotAssignableError(Left.Type, Right.Type, Start));
        }
        /// <summary>
        /// Comverts the expression into the corresponding
        /// <see cref="System.Linq.Expressions.Expression"/>
        /// </summary>
        /// <param name="runtime"></param>
        /// <returns></returns>
        /// <exception cref="SemanticException">if there is some semantic error in the tree</exception>
        public MAst Compile(AstHelper runtime)
        {
            AstHelper helper = runtime.CreateChild(function: true, variables: true, types: true);

            CheckSemantics(helper);

            if (helper.Errors.HasErrors)
            {
                throw new SemanticException(helper.Errors);
            }

            return(Transform());
        }
        protected internal override void CheckSemantics(AstHelper astHelper)
        {
            base.CheckSemantics(astHelper);

            _breakLabel = MAst.Label();
            _continueLabel = MAst.Label();

            Test.CheckSemantics(astHelper);

            AstHelper child = astHelper.CreateChild(loop: true, breakLabel: _breakLabel, continueLabel: _continueLabel);
            Body.CheckSemantics(child);

            astHelper.Errors.Check(new WhileBodyReturnsValueError(Body.Type, Start));
        }
Exemple #17
0
        protected internal override void CheckSemantics(AstHelper astHelper)
        {
            AstHelper helper = astHelper.CreateChild(expecting: true);

            base.CheckSemantics(astHelper);

            // semantic for this items is checked before having a type for them
            // so after the array type is determined they must be checked
            var lateCheckItems = new List <Expression>();

            foreach (Expression initializer in Initializers)
            {
                initializer.CheckSemantics(helper);

                if (helper.Expecting.IsExpectingType)
                {
                    helper.Errors.Check(new NotAssignableError(helper.Expecting.Type, initializer.Type, Start));
                }
                else
                {
                    // the array type isn't determined yet
                    if (initializer.Type != typeof(Null))
                    {
                        helper.Expecting.Type = initializer.Type;
                    }
                    else
                    {
                        lateCheckItems.Add(initializer);
                    }
                }
            }

            if (helper.Expecting.IsExpectingType)
            {
                InnerType = helper.Expecting.Type;

                // check that all types are assignable to the array type
                foreach (Expression lateCheckItem in lateCheckItems)
                {
                    helper.Errors.Check(new NotAssignableError(helper.Expecting.Type, lateCheckItem.Type, Start));
                }
            }
            else
            {
                // if the array doesn't have a type assign null to it
                InnerType = typeof(Null);
            }

            helper.Errors.Check(new CanNotInferArrayTypeError(Initializers.Select(i => i.Type), Start));
        }
Exemple #18
0
        protected internal override void CheckSemantics(AstHelper astHelper)
        {
            base.CheckSemantics(astHelper);

            _breakLabel    = MAst.Label();
            _continueLabel = MAst.Label();

            Test.CheckSemantics(astHelper);

            AstHelper child = astHelper.CreateChild(loop: true, breakLabel: _breakLabel, continueLabel: _continueLabel);

            Body.CheckSemantics(child);

            astHelper.Errors.Check(new WhileBodyReturnsValueError(Body.Type, Start));
        }
        protected internal override void CheckSemantics(AstHelper astHelper)
        {
            _returnLabel = MAst.Label(Type);
            AstHelper helper = astHelper.CreateChild(variables: true, returning: true, returnLabel: _returnLabel);

            // add the variables to the child scope
            foreach (FunctionParameterExpression functionParameterExpression in Parameters)
            {
                functionParameterExpression.AddToScope(helper);
            }

            Body.CheckSemantics(helper);

            astHelper.Errors.Check(new TypeMismatchedError(Type, Body.Type, Start));
        }
        protected internal override void CheckSemantics(AstHelper astHelper)
        {
            // SPEC: The comparison operators do not associate, e.g., a=b=c is erroneous, but a=(b=c) is legal.
            if (!Operator.IsComparison()) throw new InvalidOperationException();

            AstHelper helper = astHelper.CreateChild(expecting: true);

            Left.CheckSemantics(helper);
            Right.CheckSemantics(helper);

            // SPEC: Nil must be used in a context were its actual record type can be determined
            // SPEC: Thus the following are ilegal: var a := nil, if nil = nil then ...
            astHelper.Errors.Check(new CanNotInferTypeError(Left.Type, Right.Type, Start));

            astHelper.Errors.Check(new OperatorNotDefinedError(Left.Type, Right.Type, Operator, Start));
        }
Exemple #21
0
        protected internal override void CheckSemantics(AstHelper astHelper)
        {
            AstHelper helper = astHelper.CreateChild(function: true, variables: true, types: true);

            // add all type builders to the scope in case of recursive type calls
            foreach (IAddToScope typeDeclarationNode in TypeDeclarations.OfType <IAddToScope>())
            {
                typeDeclarationNode.AddToScope(helper);
            }

            var error = new CircularTypeReferencesError(TypeDeclarations, Start);

            astHelper.Errors.Check(error);
            // define other types
            foreach (TypeDeclarationNode typeDeclarationNode in error.OrderedTypes)
            {
                typeDeclarationNode.CheckSemantics(helper);
            }

            // add all functions to the scope in case of recursive calls
            foreach (FunctionDefinitionExpression functionDefinitionExpression in FunctionDefinitions)
            {
                functionDefinitionExpression.AddToScope(helper);
            }

            foreach (VariableDeclarationBase variableDeclarationExpression in VariableDeclarations)
            {
                variableDeclarationExpression.CheckSemantics(helper);
            }

            foreach (FunctionDefinitionExpression functionDefinitionExpression in FunctionDefinitions)
            {
                functionDefinitionExpression.CheckSemantics(helper);
            }

            Body.CheckSemantics(helper);

            IEnumerable <ParameterExpression> variables = from variable in VariableDeclarations select variable.Pointer;
            IEnumerable <ParameterExpression> functions = from function in FunctionDefinitions select function.Pointer;

            // calculate the closure that will be used when we're generating the expression
            _closure = variables.Union(functions);
        }
Exemple #22
0
        protected internal override void CheckSemantics(AstHelper astHelper)
        {
            // SPEC: The comparison operators do not associate, e.g., a=b=c is erroneous, but a=(b=c) is legal.
            if (!Operator.IsComparison())
            {
                throw new InvalidOperationException();
            }

            AstHelper helper = astHelper.CreateChild(expecting: true);

            Left.CheckSemantics(helper);
            Right.CheckSemantics(helper);

            // SPEC: Nil must be used in a context were its actual record type can be determined
            // SPEC: Thus the following are ilegal: var a := nil, if nil = nil then ...
            astHelper.Errors.Check(new CanNotInferTypeError(Left.Type, Right.Type, Start));

            astHelper.Errors.Check(new OperatorNotDefinedError(Left.Type, Right.Type, Operator, Start));
        }
        protected internal override void CheckSemantics(AstHelper astHelper)
        {
            AstHelper helper = astHelper.CreateChild(expecting: true);

            base.CheckSemantics(astHelper);

            // semantic for this items is checked before having a type for them
            // so after the array type is determined they must be checked
            var lateCheckItems = new List<Expression>();

            foreach (Expression initializer in Initializers)
            {
                initializer.CheckSemantics(helper);

                if (helper.Expecting.IsExpectingType)
                    helper.Errors.Check(new NotAssignableError(helper.Expecting.Type, initializer.Type, Start));
                else
                {
                    // the array type isn't determined yet
                    if (initializer.Type != typeof (Null))
                        helper.Expecting.Type = initializer.Type;
                    else
                        lateCheckItems.Add(initializer);
                }
            }

            if (helper.Expecting.IsExpectingType)
            {
                InnerType = helper.Expecting.Type;

                // check that all types are assignable to the array type
                foreach (Expression lateCheckItem in lateCheckItems)
                    helper.Errors.Check(new NotAssignableError(helper.Expecting.Type, lateCheckItem.Type, Start));
            }
            else
            {
                // if the array doesn't have a type assign null to it
                InnerType = typeof (Null);
            }

            helper.Errors.Check(new CanNotInferArrayTypeError(Initializers.Select(i => i.Type), Start));
        }
        protected internal override void CheckSemantics(AstHelper astHelper)
        {
            Test.CheckSemantics(astHelper);

            // the if-then-else expression is expected to have a static type
            // for that reason it defines a new Expecting.Type scope
            AstHelper helper = astHelper.CreateChild(expecting: true);

            Then.CheckSemantics(helper);
            Else.CheckSemantics(helper);

            astHelper.Errors.Check(new ConditionalNotEquivalentTypes(Then.Type, Else.Type, Start));

            if (astHelper.Errors.Check(new CanNotInferTypeError(Then.Type, Else.Type, Start)))
                return;

            if (Then.Type == typeof (Null))
                helper.Expecting.Type = Else.Type;
            if (Else.Type == typeof (Null))
                helper.Expecting.Type = Then.Type;
        }
        protected internal override void CheckSemantics(AstHelper astHelper)
        {
            _returnLabel = MAst.Label(Type);
            AstHelper helper = astHelper.CreateChild(variables: true, returning: true, returnLabel: _returnLabel);

            // add the variables to the child scope
            foreach (FunctionParameterExpression functionParameterExpression in Parameters)
                functionParameterExpression.AddToScope(helper);

            Body.CheckSemantics(helper);

            astHelper.Errors.Check(new TypeMismatchedError(Type, Body.Type, Start));
        }
        protected internal override void CheckSemantics(AstHelper astHelper)
        {
            AstHelper helper = astHelper.CreateChild(function: true, variables: true, types: true);

            // add all type builders to the scope in case of recursive type calls
            foreach (IAddToScope typeDeclarationNode in TypeDeclarations.OfType<IAddToScope>())
                typeDeclarationNode.AddToScope(helper);

            var error = new CircularTypeReferencesError(TypeDeclarations, Start);
            astHelper.Errors.Check(error);
            // define other types
            foreach (TypeDeclarationNode typeDeclarationNode in error.OrderedTypes)
                typeDeclarationNode.CheckSemantics(helper);

            // add all functions to the scope in case of recursive calls
            foreach (FunctionDefinitionExpression functionDefinitionExpression in FunctionDefinitions)
                functionDefinitionExpression.AddToScope(helper);

            foreach (VariableDeclarationBase variableDeclarationExpression in VariableDeclarations)
                variableDeclarationExpression.CheckSemantics(helper);

            foreach (FunctionDefinitionExpression functionDefinitionExpression in FunctionDefinitions)
                functionDefinitionExpression.CheckSemantics(helper);

            Body.CheckSemantics(helper);

            IEnumerable<ParameterExpression> variables = from variable in VariableDeclarations select variable.Pointer;
            IEnumerable<ParameterExpression> functions = from function in FunctionDefinitions select function.Pointer;
            // calculate the closure that will be used when we're generating the expression
            _closure = variables.Union(functions);
        }