示例#1
0
        public Node ParameterDeclaration()
        {
            var paramList = new ParameterDeclarationList();

            while (CurrentToken == TokenCategory.IDENTIFIER)
            {
                var  tempList = new VariableDeclarationList();
                Node idToken  = new Identifier()
                {
                    AnchorToken = Expect(TokenCategory.IDENTIFIER)
                };

                tempList.Add(idToken);

                while (CurrentToken == TokenCategory.COMMA)
                {
                    Expect(TokenCategory.COMMA);
                    tempList.Add(new Identifier()
                    {
                        AnchorToken = Expect(TokenCategory.IDENTIFIER)
                    });
                }
                Expect(TokenCategory.DECLARATION);
                var tipo = Type();
                //declarationList.Add(Type());
                foreach (var node in tempList)
                {
                    tipo.Add(node);
                }
                paramList.Add(tipo);
                Expect(TokenCategory.ENDLINE);
            }

            return(paramList);
        }
 public ForVarIterationStatement(VariableDeclarationList variableDeclarations, AbstractExpression?conditionExpression, AbstractExpression?endExpression, Statement doStatement, bool isStrictMode) : base(isStrictMode)
 {
     this.doStatement          = doStatement;
     this.variableDeclarations = variableDeclarations;
     this.conditionExpression  = conditionExpression;
     this.endExpression        = endExpression;
 }
 public virtual void VisitVariableDeclarationList(VariableDeclarationList node, TContext context)
 {
     this.Visit(node.Type, context);
     foreach (var n in node.Declarations)
     {
         this.Visit(n, context);
     }
 }
示例#4
0
        public void ReadonlyNodeCollection_IsNotEditable()
        {
            var subject = new VariableDeclarationList();

            subject.MakeReadonly();

            Action action = () => subject.Declarations.Add(new VariableDeclaration());

            action.Should().Throw <InvalidOperationException>();
        }
        public override void VisitVariableDeclarationList(VariableDeclarationList node, CloningAstVisitorContext context)
        {
            var result = new VariableDeclarationList()
            {
                Type = this.CloneNode(node.Type, context)
            };

            this.CloneNodeCollection(node.Declarations, result.Declarations, context);
            context.Result = result;
        }
示例#6
0
        /*
         * LL(1) Grammar:
         *      PROGRAM ::= ("const" CONST_DECL+)? ("var" VAR_DECL+)? PROC_DECL* "program" STATEMENT* "end" ";"
         *      CONST_DECL ::= IDENTIFIER ":=" LITERAL ";"
         *      VAR_DECL ::= IDENTIFIER ("," IDENTIFIER)* ":" TYPE ";"
         *      LITERAL ::= SIMPLE_LITERAL | LIST
         *      SIMPLE_LITERAL ::= INTEGER_LITERAL | STRING_LITERAL | TRUE | FALSE
         *      TYPE ::= SIMPLE_TYPE | LIST_TYPE
         *      SIMPLE_TYPE ::= "integer" | "string" | "boolean"
         *      LIST_TYPE ::= "list" "of" SIMPLE_TYPE
         *      LIST ::= "{" (SIMPLE_LITERAL ("," SIMPLE_LITERAL )*)? "}"
         *      PROC_DECL ::= "procedure" IDENTIFIER "(" PARAM_DECL* ")" (":" TYPE)? ";" ("const" CONST_DECL+)? ("var" VAR_DECL+)? "begin" STATEMENT* "end" ";"
         *      PARAM_DECL ::= IDENTIFIER ("," IDENTIFIER)* ":" TYPE ";"
         *      STATEMENT ::= (IDENTIFIER (ASS_STAT | CALL_STAT) ) | IF_STAT | LOOP_STAT | FOR_STAT | RET_STAT | EXIT_STAT
         *      ASS_STAT ::= ("[" EXPR "]")? ":=" EXPR ";"
         *      CALL_STAT ::= "(" (EXPR ("," EXPR)*)? ")" ";"
         *      IF_STAT ::= "if" EXPR "then" STATEMENT* ("elseif" EXPR "THEN" STATEMENT*)* ("else" STATEMENT*)? "end" ";"
         *      LOOP_STAT ::= "loop" STATEMENT* "end" ";"
         *      FOR_STAT ::= "for" IDENTIFIER "in" EXPR "do" STATEMENT* "end" ";"
         *      RET_STAT ::= "return" EXPR? ";"
         *      EXIT_STAT ::= "exit" ";"
         *      EXPR ::= LOGIC_EXPR
         *      LOGIC_EXPR ::= REL_EXPR (LOGIC_OP REL_EXPR)*
         *      LOGIC_OP ::= "and" | "or" | "xor"
         *      REL_EXPR ::= SUM_EXPR (REL_OP SUM_EXPR)*
         *      REL_OP ::= "=" | "<>" | "<" | ">" | "<=" | ">="
         *      SUM_EXPR ::= MUL_EXPR (SUM_OP MUL_EXPR)*
         *      SUM_OP ::= "+" | "-"
         *      MUL_EXPR ::= UN_EXPR (MUL_OP UN_EXPR)*
         *      MUL_OP ::=  "*" | "div" | "rem"
         *      UN_EXPR ::= ("not" UN_EXPR) | ("-" UN_EXPR) | SIMP_EXPR
         *      SIMP_EXPR ::= ("(" Expression ")" | (IDENTIFIER CALL?) | LITERAL ) ("[" EXPR "]")?
         *      CALL ::= "(" (EXPR ("," EXPR)*)? ")"
         */

        public Node Program()
        {
            var result = new Program();

            var constantDeclarationList = new ConstantDeclarationList();

            if (CurrentToken == TokenCategory.CONST)
            {
                Expect(TokenCategory.CONST);
                do
                {
                    constantDeclarationList.Add(ConstantDeclaration());
                } while (CurrentToken == TokenCategory.IDENTIFIER);
            }
            result.Add(constantDeclarationList);

            var variableDeclarationList = new VariableDeclarationList();

            if (CurrentToken == TokenCategory.VAR)
            {
                Expect(TokenCategory.VAR);
                do
                {
                    variableDeclarationList.Add(VariableDeclaration());
                } while (CurrentToken == TokenCategory.IDENTIFIER);
            }
            result.Add(variableDeclarationList);

            var procedureDeclarationList = new ProcedureDeclarationList();

            if (CurrentToken == TokenCategory.PROCEDURE)
            {
                do
                {
                    procedureDeclarationList.Add(ProcedureDeclaration());
                } while (CurrentToken == TokenCategory.PROCEDURE);
            }
            result.Add(procedureDeclarationList);

            Expect(TokenCategory.PROGRAM);

            var statementList = new StatementList();

            while (firstOfStatement.Contains(CurrentToken))
            {
                statementList.Add(Statement());
            }
            result.Add(statementList);

            Expect(TokenCategory.END);
            Expect(TokenCategory.SEMICOLON);
            Expect(TokenCategory.EOF);

            return(result);
        }
示例#7
0
        private Struct ParseStruct()
        {
            Accept(Token.TokenType.KEBAB);
            var structName = new UserCreatableID(Accept(Token.TokenType.USER_CREATABLE_ID));

            Accept(Token.TokenType.LEFT_SQUARE);
            VariableDeclarationList variableDeclarationList = ParseVariableDeclarationList();

            Accept(Token.TokenType.RIGHT_SQUARE);
            return(new Struct(structName, variableDeclarationList));
        }
示例#8
0
        public Node Program()
        {
            var constantList  = new ConstantDeclarationList();
            var varList       = new VariableDeclarationList();
            var procedureList = new ProcedureDeclarationList();
            var stmtlist      = new StatementList();

            //IF-> do while because it's one or more times
            if (firstOfDeclaration.Contains(CurrentToken) && CurrentToken == TokenCategory.CONST)
            {
                constantList.AnchorToken = Expect(TokenCategory.CONST);

                do
                {
                    constantList.Add(ConstantDeclaration());
                } while (CurrentToken == TokenCategory.IDENTIFIER);
            }

            if (firstOfDeclaration.Contains(CurrentToken) && CurrentToken == TokenCategory.VAR)
            {
                varList.AnchorToken = Expect(TokenCategory.VAR);

                do
                {
                    varList.Add(VariableDeclaration());
                } while (CurrentToken == TokenCategory.IDENTIFIER);
            }

            //just while because it's zero or more times
            while (firstOfDeclaration.Contains(CurrentToken) && CurrentToken == TokenCategory.PROCEDURE)
            {
                procedureList.Add(ProcedureDeclaration());
            }

            Expect(TokenCategory.PROGRAM);

            while (firstOfStatement.Contains(CurrentToken))
            {
                stmtlist.Add(Statement());
            }

            Expect(TokenCategory.END);
            Expect(TokenCategory.SEMICOLON);

            return(new Program()
            {
                constantList,
                varList,
                procedureList,
                stmtlist
            });
        }
示例#9
0
文件: Checker.cs 项目: Laegas/CMC
        public object VisitVariableDeclarationList(VariableDeclarationList variableDeclarationList, object o)
        {
            variableDeclarationList.VariableDeclarations.ForEach(item => {
                var count = variableDeclarationList.VariableDeclarations.Count(i => i.Name == item.Name);
                if (count != 1)
                {
                    throw new Exception("Duplicate name in struct definition");
                }

                item.Visit(this);
            });
            return(null);
        }
示例#10
0
        public void ParentNodeReadonlyMode_WillPropagateToChildNodes()
        {
            var subjectIdentifier = new Identifier();
            var subject           = new VariableDeclarationList();
            var declaration       = new VariableDeclaration();

            declaration.Name = subjectIdentifier;
            subject.Declarations.Add(declaration);

            subject.MakeReadonly();

            Action action = () => subjectIdentifier.Name = "NewValue";

            action.Should().Throw <InvalidOperationException>();
        }
 private void InferType(VariableDeclarationList node)
 {
     if (node.Type == null)
     {
         if (node.Declarations.Count > 0)
         {
             VariableDeclarationNode variableNode = (node.Declarations[0] as VariableDeclarationNode);
             if (variableNode.Type == null)
             {
                 this.Visit(variableNode);
             }
             node.SetType(variableNode.Type, false);
         }
         else
         {
             node.SetType(NodeHelper.CreateNode(NodeKind.AnyKeyword));
         }
     }
 }
        public override void VisitVariableDeclarationList(VariableDeclarationList node, AstPrinterContext context)
        {
            this.Visit(node.Type, context);
            if (node.Declarations.Count > 0)
            {
                context.Write(" ");
                var i = 0;
                foreach (var n in node.Declarations)
                {
                    this.Visit(n, context);
                    i++;
                    if (i < node.Declarations.Count)
                    {
                        context.Write(", ");
                    }
                }
            }

            context.Write(";");
        }
示例#13
0
        public CSharpSyntaxNode Convert(VariableDeclarationList node)
        {
            bool isVar = false;
            Node type  = node.Type;

            if (type.Kind == NodeKind.AnyKeyword && node.Declarations.Count > 0)
            {
                VariableDeclarationNode variableNode = node.Declarations[0] as VariableDeclarationNode;
                if (variableNode.Initializer != null && variableNode.Initializer.Kind != NodeKind.NullKeyword)
                {
                    isVar = true;
                }
            }

            TypeSyntax csType = isVar ? SyntaxFactory.IdentifierName("var") : node.Type.ToCsNode <TypeSyntax>();

            return(SyntaxFactory
                   .VariableDeclaration(csType)
                   .AddVariables(node.Declarations.ToCsNodes <VariableDeclaratorSyntax>()));
        }
示例#14
0
        public Node VariableDeclaration()
        {
            var declarationList = new VariableDeclarationList();

            declarationList.AnchorToken = Expect(TokenCategory.VAR);
            do
            {
                var tempList        = new VariableDeclarationList();
                var firstIdentifier = new Identifier()
                {
                    AnchorToken = Expect(TokenCategory.IDENTIFIER)
                };
                tempList.Add(firstIdentifier);

                //declarationList.Add(firstIdentifier);
                while (CurrentToken == TokenCategory.COMMA)
                {
                    Expect(TokenCategory.COMMA);
                    tempList.Add(new Identifier()
                    {
                        AnchorToken = Expect(TokenCategory.IDENTIFIER)
                    });
                }
                Expect(TokenCategory.DECLARATION);
                var tipo = Type();
                Console.WriteLine("PERIQUEAR" + tipo);
                //declarationList.Add(Type());
                foreach (var node in tempList)
                {
                    tipo.Add(node);
                }
                declarationList.Add(tipo);
                Expect(TokenCategory.ENDLINE);
            } while (CurrentToken == TokenCategory.IDENTIFIER);
            return(declarationList);
        }
示例#15
0
 public object VisitVariableDeclarationList(VariableDeclarationList variableDeclarationList, object o)
 {
     return(null);
 }
示例#16
0
        public TypeG Visit(VariableDeclarationList node)
        {
            foreach (var n in node)
            {
                TypeG tipo = Visit((dynamic)n);
                foreach (var i in n)
                {
                    var     variableName  = i.AnchorToken.Lexeme;
                    dynamic variableValue = false;

                    switch (tipo)
                    {
                    case TypeG.BOOLEAN:
                        variableValue = false;
                        break;

                    case TypeG.INTEGER:
                        variableValue = 0;
                        break;

                    case TypeG.STRING:
                        variableValue = "";
                        break;

                    case TypeG.INTEGER_LIST:
                        variableValue = new int[] { 0 };
                        break;

                    case TypeG.BOOLEAN_LIST:
                        variableValue = new bool[] { false };
                        break;

                    case TypeG.STRING_LIST:
                        variableValue = new string[] { "" };
                        break;

                    default:
                        throw new Exception($"Type {tipo} wasn't found");
                    }

                    if (CurrentContext.context == "GLOBAL")
                    {
                        if (GloabalDeclaratonT.Contains(variableName))
                        {
                            throw new SemanticError(
                                      "Duplicated variable (" + CurrentContext.context + "): " + variableName,
                                      n[0].AnchorToken);
                        }
                        else
                        {
                            GloabalDeclaratonT[variableName] =
                                new GlobalDeclarationType(variableName, TypeG.INTEGER, variableValue, TypeG.VAR);
                        }
                    }
                    else if (CurrentContext.context == "LOCAL")
                    {
                        if (ListLocalDeclarationTable[CurrentContext.index].Contains(variableName))
                        {
                            throw new SemanticError(
                                      "Duplicated variable: " + variableName,
                                      n[0].AnchorToken);
                        }
                        else
                        {
                            ListLocalDeclarationTable[CurrentContext.index][variableName] = new LocalDeclarationType(variableName, TypeG.INTEGER, variableValue, -1, TypeG.VAR);
                        }
                    }
                }
            }
            //VisitChildren(node);
            return(TypeG.VOID);
        }
示例#17
0
        public Node ProcedureDeclaration()
        {
            var result = new ProcedureDeclaration()
            {
                AnchorToken = Expect(TokenCategory.PROCEDURE)
            };

            result.Add(new Identifier()
            {
                AnchorToken = Expect(TokenCategory.IDENTIFIER)
            });

            Expect(TokenCategory.PARENTHESIS_OPEN);

            var parameterList = new ParameterDeclarationList();

            if (CurrentToken == TokenCategory.IDENTIFIER)
            {
                while (CurrentToken == TokenCategory.IDENTIFIER)
                {
                    parameterList.Add(VariableDeclaration());
                }
            }
            result.Add(parameterList);

            Expect(TokenCategory.PARENTHESIS_CLOSE);

            var type = new TypeNode();

            if (CurrentToken == TokenCategory.COLON)
            {
                Expect(TokenCategory.COLON);

                if (CurrentToken != TokenCategory.LIST)
                {
                    type.Add(SimpleType());
                }

                else if (CurrentToken == TokenCategory.LIST)
                {
                    type.Add(ListType());
                }
            }
            result.Add(type);

            Expect(TokenCategory.SEMICOLON);

            var constantList = new ConstantDeclarationList();

            if (firstOfDeclaration.Contains(CurrentToken) && CurrentToken == TokenCategory.CONST)
            {
                constantList.AnchorToken = Expect(TokenCategory.CONST);

                do
                {
                    constantList.Add(ConstantDeclaration());
                } while (CurrentToken == TokenCategory.IDENTIFIER);
            }
            result.Add(constantList);

            var variableList = new VariableDeclarationList();

            if (firstOfDeclaration.Contains(CurrentToken) && CurrentToken == TokenCategory.VAR)
            {
                variableList.AnchorToken = Expect(TokenCategory.VAR);

                do
                {
                    variableList.Add(VariableDeclaration());
                } while (CurrentToken == TokenCategory.IDENTIFIER);
            }
            result.Add(variableList);

            Expect(TokenCategory.BEGIN);

            var statementList = new StatementList();

            if (firstOfStatement.Contains(CurrentToken))
            {
                while (firstOfStatement.Contains(CurrentToken))
                {
                    statementList.Add(Statement());
                }
            }
            result.Add(statementList);


            Expect(TokenCategory.END);
            Expect(TokenCategory.SEMICOLON);
            return(result);
        }
示例#18
0
        public Node ProcedureDeclaration()
        {
            var result = new ProcedureDeclaration();

            Expect(TokenCategory.PROCEDURE);
            result.AnchorToken = Expect(TokenCategory.IDENTIFIER);
            Expect(TokenCategory.LEFT_PAR);

            var parameterDeclarationList = new ParameterDeclarationList();

            while (CurrentToken == TokenCategory.IDENTIFIER)
            {
                parameterDeclarationList.Add(ParameterDeclaration());
            }
            result.Add(parameterDeclarationList);

            Expect(TokenCategory.RIGHT_PAR);

            if (CurrentToken == TokenCategory.COLON)
            {
                Expect(TokenCategory.COLON);
                result.Add(Type());
            }

            Expect(TokenCategory.SEMICOLON);

            if (CurrentToken == TokenCategory.CONST)
            {
                var constantDeclarationList = new ConstantDeclarationList()
                {
                    AnchorToken = Expect(TokenCategory.CONST)
                };
                do
                {
                    constantDeclarationList.Add(ConstantDeclaration());
                } while (CurrentToken == TokenCategory.IDENTIFIER);

                result.Add(constantDeclarationList);
            }

            if (CurrentToken == TokenCategory.VAR)
            {
                var variableDeclarationList = new VariableDeclarationList()
                {
                    AnchorToken = Expect(TokenCategory.VAR)
                };
                do
                {
                    variableDeclarationList.Add(VariableDeclaration());
                } while (CurrentToken == TokenCategory.IDENTIFIER);

                result.Add(variableDeclarationList);
            }

            Expect(TokenCategory.BEGIN);

            var statementList = new StatementList();

            while (firstOfStatement.Contains(CurrentToken))
            {
                statementList.Add(Statement());
            }
            result.Add(statementList);

            Expect(TokenCategory.END);
            Expect(TokenCategory.SEMICOLON);

            return(result);
        }
        public override AstNode VisitDeclaratorlist([NotNull] GLSL_ES300Parser.DeclaratorlistContext context)
        {
            VariableDeclarationList declaratorList = new VariableDeclarationList();
            var currentContext = context;

            while (currentContext != null)
            {
                var dec = currentContext.declarator();
                if (dec != null)
                {
                    if (dec.fully_specified_type() != null)
                    {
                        declaratorList.Type = (TypeSpecifier)this.Visit(dec.fully_specified_type());
                    }

                    VariableDeclaration declaration = new VariableDeclaration();
                    if (dec.Identifier() != null)
                    {
                        declaration.Name = new Identifier()
                        {
                            Name = dec.Identifier().Symbol.Text
                        };

                        if (dec.LeftBracket() != null)
                        {
                            declaration.ArraySpecifier = new ArraySpecifier();
                            if (dec.constant_expression() != null)
                            {
                                declaration.ArraySpecifier.ArraySizeExpression = (Expression)this.Visit(dec.constant_expression());
                            }
                        }
                        if (dec.initializer() != null)
                        {
                            declaration.Initializer = (Expression)this.Visit(dec.initializer().assignment_expression());
                        }

                        declaratorList.Declarations.Add(declaration);
                    }

                    currentContext = null;
                }
                else
                {
                    VariableDeclaration declaration = new VariableDeclaration();
                    if (currentContext.Identifier() != null)
                    {
                        declaration.Name = new Identifier()
                        {
                            Name = currentContext.Identifier().Symbol.Text
                        };
                    }
                    if (currentContext.LeftBracket() != null)
                    {
                        declaration.ArraySpecifier = new ArraySpecifier();
                        if (currentContext.constant_expression() != null)
                        {
                            declaration.ArraySpecifier.ArraySizeExpression = (Expression)this.Visit(currentContext.constant_expression());
                        }
                    }
                    if (currentContext.initializer() != null)
                    {
                        declaration.Initializer = (Expression)this.Visit(currentContext.initializer().assignment_expression());
                    }

                    currentContext = currentContext.declaratorlist();
                    declaratorList.Declarations.Add(declaration);
                }
            }

            declaratorList.Declarations.Reverse();
            return(declaratorList);
        }
示例#20
0
 public VariableStatement(VariableDeclarationList variableDeclarations, bool isStrictMode) : base(isStrictMode)
 {
     this.variableDeclarations = variableDeclarations;
 }
示例#21
0
 //-----------------------------------------------------------
 private string Visit(VariableDeclarationList node, Table table)
 {
     return(VisitChildren(node, table));
 }
示例#22
0
 public Type Visit(VariableDeclarationList node)
 {
     VisitChildren(node);
     return(Type.VOID);
 }
 //-----------------------------------------------------------
 private Type Visit(VariableDeclarationList node, Table table)
 {
     VisitChildren(node, table);
     return(Type.VOID);
 }