Пример #1
0
 public VarFormalParameter(Identifier identifier, TypeDenoter type,
                           SourcePosition position)
     : base(position)
 {
     _identifier = identifier;
     _type       = type;
 }
Пример #2
0
        /**
         * Creates a small AST to represent the declaration of a standard type.
         *
         * @param id
         *          the name of the type
         * @param typedenoter
         *          the type of the declaration
         * @return a TypeDeclaration for the given identifier to the given type
         */
        static TypeDeclaration DeclareStdType(string id, TypeDenoter typedenoter)
        {
            var ident   = new Identifier(id);
            var binding = new TypeDeclaration(ident, typedenoter);

            return(binding);
        }
Пример #3
0
 public VarDeclaration(Identifier identifier, TypeDenoter type, SourcePosition position)
     : base(position)
 {
     Compiler.WriteDebuggingInfo($"Creating {this.GetType().Name}");
     Identifier = identifier;
     Type       = type;
 }
Пример #4
0
 public InitDeclaration(Identifier identifier, TypeDenoter type, Expression expression, SourcePosition position)
     : base(position, type)
 {
     Compiler.WriteDebuggingInfo($"Creating {this.GetType().Name}");
     Identifier = identifier;
     Expression = expression;
 }
 public BinaryOperatorDeclaration(Operator op, TypeDenoter firstArgument, TypeDenoter secondArgument, TypeDenoter result) : base(SourcePosition.Empty)
 {
     Operator       = op;
     FirstArgument  = firstArgument;
     SecondArgument = secondArgument;
     Result         = result;
 }
Пример #6
0
        public TypeDenoter VisitBinaryExpression(BinaryExpression ast, Void arg)
        {
            TypeDenoter e1Type  = ast.LeftExpression.Visit(this, null);
            TypeDenoter e2Type  = ast.RightExpression.Visit(this, null);
            Declaration binding = ast.Operation.Visit(this, null);

            if (binding is BinaryOperatorDeclaration bbinding)
            {
                if (bbinding.FirstArgument == StandardEnvironment.AnyType)
                {
                    CheckAndReportError(e1Type.Equals(e2Type), "incompatible argument types for \"%\"", ast.Operation, ast);
                }
                else
                {
                    CheckAndReportError(e1Type.Equals(bbinding.FirstArgument), "wrong argument type for \"%\"", ast.Operation, ast.LeftExpression);
                    CheckAndReportError(e2Type.Equals(bbinding.SecondArgument), "wrong argument type for \"%\"", ast.Operation, ast.RightExpression);
                }
                ast.Type = bbinding.Result;
            }
            else
            {
                ReportUndeclaredOrError(binding, ast.Operation, "\"%\" is not a binary operator");
                ast.Type = StandardEnvironment.ErrorType;
            }
            return(ast.Type);
        }
Пример #7
0
        public object visitIdentifierPE(IdentifierPE expr, object arg)
        {
            expr.Type = null;
            TypeDenoter vType = (TypeDenoter)expr.visit(this, null);

            return(vType);
        }
Пример #8
0
        public TypeDenoter VisitTernaryExpression(TernaryExpression ast, Void arg)
        {
            TypeDenoter e2Type = ast.LeftExpression.Visit(this, null);
            TypeDenoter e3Type = ast.RightExpression.Visit(this, null);
            TypeDenoter e1Type = ast.Condition.Visit(this, null);

            if (e1Type is BoolTypeDenoter)
            {
                if (e1Type.Equals(true))
                {
                    ast.Type = e2Type;
                }
                else
                {
                    ast.Type = e3Type;
                    CheckAndReportError(e2Type.Equals(e3Type), "incompatible argument type for \"%\"", ast.Type.ToString(), ast.RightExpression.Position);
                }
            }
            else
            {
                ReportError("\"%\" is not a valid boolean", e1Type);
            }

            return(ast.Type);
        }
Пример #9
0
        public TypeDenoter VisitTernaryExpression(TernaryExpression ast, Void arg)
        {
            TypeDenoter e1Type = ast.Condition.Visit(this, null);
            TypeDenoter e2Type = ast.LeftExpression.Visit(this, null);
            TypeDenoter e3Type = ast.RightExpression.Visit(this, null);

            if (e1Type == StandardEnvironment.BooleanType)
            {
                if (e2Type == e3Type)
                {
                    ast.Type = StandardEnvironment.AnyType;
                }
                else
                {
                    CheckAndReportError(e2Type.Equals(e3Type), "ternary expression member \"%\" must have the same type", ast.LeftExpression);
                    CheckAndReportError(e3Type.Equals(e2Type), "ternary expression member \"%\" must have the same type", ast.RightExpression);
                    ast.Type = StandardEnvironment.ErrorType;
                }
            }
            else
            {
                CheckAndReportError(e1Type.Equals(StandardEnvironment.BooleanType), "argument type \"%\" is not a boolean", ast.Condition);
                ast.Type = StandardEnvironment.ErrorType;
            }

            return(ast.Type);
        }
 public UnaryOperatorDeclaration(Operator op, TypeDenoter argument, TypeDenoter result)
     : base(SourcePosition.Empty)
 {
     Operator = op;
     Argument = argument;
     Result   = result;
 }
Пример #11
0
 public FuncDeclaration(Identifier identifier, FormalParameterSequence formals, TypeDenoter type, Expression expression, SourcePosition position)
     : base(position)
 {
     Identifier = identifier;
     Formals    = formals;
     Type       = type;
     Expression = expression;
 }
Пример #12
0
        /**
         * Creates a small AST to represent the declaration of a binary operator. This
         * declaration summarizes the operator's type information.
         *
         * @param op
         *          the spelling of the operator
         * @param arg1Type
         *          the type of the first argument of the binary operator
         * @param arg2Type
         *          the type of the second argument of the binary operator
         * @param resultType
         *          the type of the result
         * @return a BinaryOperatorDeclaration of the given operator with the given
         *         argument types and returning the given result type
         */
        static BinaryOperatorDeclaration DeclareStdBinaryOp(string op, TypeDenoter arg1Type,
                                                            TypeDenoter arg2Type, TypeDenoter resultType)
        {
            var opAst   = new Operator(op, SourcePosition.Empty);
            var binding = new BinaryOperatorDeclaration(opAst, arg1Type, arg2Type, resultType);

            return(binding);
        }
Пример #13
0
        /**
         * Creates a small AST to represent the declaration of a unary operator. This
         * declaration summarizes the operator's type information.
         *
         * @param op
         *          the spelling of the operator
         * @param argType
         *          the type of the argument of the unary operator
         * @param resultType
         *          the type of the result
         * @return a UnaryOperatorDeclaration of the given operator with the given
         *         argument type and returning the given result type
         */
        static UnaryOperatorDeclaration DeclareStdUnaryOp(string op, TypeDenoter argType,
                                                          TypeDenoter resultType)
        {
            var opAst   = new Operator(op);
            var binding = new UnaryOperatorDeclaration(opAst, argType, resultType);

            return(binding);
        }
Пример #14
0
        /**
         * Creates a small AST to represent the declaration of a standard function.
         *
         * @param id
         *          the name of the function
         * @param fps
         *          the formal parameter sequence for the function
         * @param resultType
         *          the type of the function result
         * @return a FuncDeclaration for the given identifier with the given formal
         *         parameters and returning the given result type
         */
        static FuncDeclaration DeclareStdFunc(string id, FormalParameterSequence fps,
                                              TypeDenoter resultType)
        {
            var ident   = new Identifier(id);
            var binding = new FuncDeclaration(ident, fps, resultType);

            return(binding);
        }
Пример #15
0
 public FuncFormalParameter(Identifier identifier, FormalParameterSequence formals,
                            TypeDenoter type, SourcePosition position)
     : base(position)
 {
     _identifier = identifier;
     _formals    = formals;
     _type       = type;
 }
        public Void VisitWhileCommand(WhileCommand ast, Void arg)
        {
            System.Console.WriteLine("Visiting WhileCommand");
            TypeDenoter expressionType = ast.Expression.Visit(this, null);

            CheckAndReportError(expressionType.Equals(StandardEnvironment.BooleanType), "expression type must be boolean", ast);
            return(ast.Command.Visit(this, null));
        }
        public Void VisitAssignCommand(AssignCommand ast, Void arg)
        {
            TypeDenoter identifierType = ast.Identifier.Visit(this, null).Type;
            TypeDenoter expressionType = ast.Expression.Visit(this, null);

            CheckAndReportError(ast.Identifier.IsVariable, "LHS of assignment is not a variable", ast.Identifier);
            CheckAndReportError(expressionType.Equals(identifierType), "assignment incompatibilty", ast);
            return(null);
        }
Пример #18
0
        /**
         * Creates a small AST to represent the declaration of a standard constant.
         *
         * @param id
         *          the name of the constant
         * @param constType
         *          the type of the constant declaration
         * @return a ConstDeclaration for the given identifier to the given type
         */
        static ConstDeclaration DeclareStdConst(string id, TypeDenoter constType)
        {
            var ident = new Identifier(id);
            // constExpr used only as a placeholder for constType
            var constExpr = new EmptyExpression();

            constExpr.Type = constType;
            var binding = new ConstDeclaration(ident, constExpr);

            return(binding);
        }
        public Void VisitWhileCommand(WhileCommand ast, Void arg)
        {
            TypeDenoter expressionType = ast.Expression.Visit(this, null);

            if (expressionType is BoolTypeDenoter == false)
            {
                ReportError("\"%\" is not a boolean type", ast);
            }
            ast.Command.Visit(this, null);
            return(null);
        }
Пример #20
0
        public Void VisitWhileCommand(WhileCommand ast, Void arg)
        {
            TypeDenoter wHile = ast.Expression.Visit(this, null);

            if (!(wHile is BoolTypeDenoter))
            {
                ReportError("\"%\" is not a procedure identifier", ast);
            }

            ast.Command.Visit(this, null);
            return(null);
        }
Пример #21
0
        /**
         * Parses the single declaration, and constructs an AST to represent its
         * phrase structure.
         *
         * @return a {@link triangle.compiler.syntax.trees.declarations.Declaration}
         *
         * @throws SyntaxError
         *           a syntactic error
         *
         */
        Declaration ParseSingleDeclaration()
        {
            Compiler.WriteDebuggingInfo("Parsing Single Declaration");
            Location startLocation = tokens.Current.Start;

            switch (tokens.Current.Kind)
            {
            case TokenKind.Const:
            {
                AcceptIt();

                Identifier identifier = ParseIdentifier();

                Accept(TokenKind.Is);

                Expression     expression          = ParseExpression();
                SourcePosition declarationPosition = new SourcePosition(startLocation, tokens.Current.Finish);
                return(new ConstDeclaration(identifier, expression, declarationPosition));
            }

            case TokenKind.Var:
            {
                AcceptIt();

                Identifier identifier = ParseIdentifier();
                Accept(TokenKind.Colon);

                TypeDenoter denoter = ParseTypeDenoter();

                if (tokens.Current.Kind == TokenKind.Becomes)
                {
                    AcceptIt();

                    Expression expression = ParseExpression();

                    SourcePosition declarationPosition = new SourcePosition(startLocation, tokens.Current.Finish);
                    return(new InitDeclaration(identifier, denoter, expression, declarationPosition));
                }
                else
                {
                    SourcePosition declarationPosition = new SourcePosition(startLocation, tokens.Current.Finish);
                    return(new VarDeclaration(identifier, denoter, declarationPosition));
                }
            }

            default:
            {
                RaiseSyntacticError("\"%\" cannot start a declaration", tokens.Current);
                return(null);
            }
            }
        }
Пример #22
0
        // This is just for the standard environment

        public Void VisitFuncDeclaration(FuncDeclaration ast, Void arg)
        {
            ast.Type = ast.Type.Visit(this, null);
            idTable.Enter(ast.Identifier, ast);
            CheckAndReportError(!ast.Duplicated, "identifier \"%\" already declared", ast.Identifier, ast);
            idTable.OpenScope();
            ast.Formals.Visit(this, null);
            TypeDenoter expressionType = ast.Expression.Visit(this, null);

            idTable.CloseScope();
            CheckAndReportError(ast.Type.Equals(expressionType), "body of function \"%\" has wrong type", ast.Identifier, ast.Expression);
            return(null);
        }
Пример #23
0
        public Void VisitIfCommand(IfCommand ast, Void arg)
        {
            TypeDenoter iF = ast.Expression.Visit(this, null);

            if (!(iF is BoolTypeDenoter))
            {
                ReportError("\"%\" is not a procedure identifier", ast);
            }

            ast.TrueCommand.Visit(this, null);
            ast.FalseCommand.Visit(this, null);
            return(null);
        }
        // Actual Parameters
        // Always returns null. Uses the given FormalParameter.
        public Void VisitValueParameter(ValueParameter ast, FormalParameter arg)
        {
            TypeDenoter expressionType = ast.Expression.Visit(this, null);

            if (arg is ConstFormalParameter param)
            {
                CheckAndReportError(expressionType.Equals(param.Type), "wrong type for const actual parameter", ast.Expression);
            }
            else
            {
                ReportError("const actual parameter not expected here", ast);
            }
            return(null);
        }
Пример #25
0
        public TypeDenoter VisitUnaryExpression(UnaryExpression ast, Void arg)
        {
            TypeDenoter expressionType = ast.Expression.Visit(this, null);
            Declaration binding        = ast.Operator.Visit(this, null);

            if (binding is UnaryOperatorDeclaration ubinding)
            {
                CheckAndReportError(expressionType.Equals(ubinding.Argument), "wrong argument type for \"%\"", ast.Operator);
                ast.Type = ubinding.Result;
            }
            else
            {
                ReportUndeclaredOrError(binding, ast.Operator, "\"%\" is not a unary operator");
                ast.Type = StandardEnvironment.ErrorType;
            }
            return(ast.Type);
        }
Пример #26
0
        public TypeDenoter VisitTernaryExpression(TernaryExpression ast, Void arg)
        {
            TypeDenoter condition = ast.Condition.Visit(this, null);
            TypeDenoter e2Type    = ast.LeftExpression.Visit(this, null);
            TypeDenoter e3Type    = ast.RightExpression.Visit(this, null);

            if (condition is BoolTypeDenoter)
            {
                if (e2Type.GetType() == e3Type.GetType())
                {
                    ast.Type = e3Type;
                }
                else
                {
                    CheckAndReportError(e2Type.Equals(e3Type), "icompatible arguement types for \"%\"", ast.Type.ToString(), ast.Position);
                }
            }
            else
            {
                ReportError("\"%\" incompatible boolean", condition);
            }

            return(ast.Type);
        }
Пример #27
0
 protected Declaration(SourcePosition pos, TypeDenoter type) : base(pos)
 {
     Compiler.WriteDebuggingInfo($"Creating {this.GetType().Name}");
     Type = type;
 }
Пример #28
0
 public VarFormalParameter(TypeDenoter type)
     : this(Identifier.Empty, type, SourcePosition.Empty)
 {
 }
Пример #29
0
 public FuncDeclaration(Identifier identifier, FormalParameterSequence formals, TypeDenoter type)
     : this(identifier, formals, type, new EmptyExpression(), SourcePosition.Empty)
 {
 }
Пример #30
0
 public ConstFormalParameter(TypeDenoter typeDenoter)
     : this(Identifier.Empty, typeDenoter, SourcePosition.Empty)
 {
 }