Exemplo n.º 1
0
 private ProgramNode(
     IEnumerable <IDeclarationNode> declarations,
     SymT symT,
     IDictionary <string, IEntityType> scopeTypeTable
     )
 {
     Declarations   = declarations.ToList();
     SymbolTable    = symT;
     ScopeTypeTable = scopeTypeTable;
 }
Exemplo n.º 2
0
        public static Either <ParseException, ProgramNode> Parse(List <IToken> tokens)
        {
            Console.WriteLine("ProgramNode");

            var declarations = new List <IDeclarationNode>();
            var typeTable    = new Dictionary <string, IEntityType>();
            var symT         = new SymT();

            var result = new ProgramNode(declarations, symT, typeTable);

            while (tokens.Count > 0)
            {
                var maybeRoutineDeclaration = RoutineDeclarationNode.Parse(tokens, symT, result);
                if (maybeRoutineDeclaration.IsRight)
                {
                    tokens = maybeRoutineDeclaration.RightToList()[0].First;

                    var routineDeclaration = maybeRoutineDeclaration.RightToList()[0].Second;
                    declarations.Add(routineDeclaration);
                    typeTable.TryAdd(routineDeclaration.Identifier.Lexeme, routineDeclaration.ToRoutineType());
                    continue;
                }

                var maybeVariableDeclaration = VariableDeclarationNode.Parse(tokens, symT, result);
                if (maybeVariableDeclaration.IsRight)
                {
                    tokens = maybeVariableDeclaration.RightToList()[0].First;
                    var varDecl = maybeVariableDeclaration.RightToList()[0].Second;
                    declarations.Add(varDecl);
                    typeTable.TryAdd(varDecl.Identifier.Lexeme, varDecl.ToVariableType());
                    continue;
                }

                var maybeTypeDeclaration = TypeDeclarationNode.Parse(tokens, symT, result);
                if (maybeTypeDeclaration.IsRight)
                {
                    tokens = maybeTypeDeclaration.RightToList()[0].First;
                    var typeDecl = maybeTypeDeclaration.RightToList()[0].Second;
                    declarations.Add(typeDecl);
                    typeTable.TryAdd(typeDecl.Identifier.Lexeme, typeDecl.ToTypeAliasType());
                    continue;
                }

                return(maybeTypeDeclaration.LeftToList()[0]);
            }

            Console.WriteLine("Program is interprited Successfully");
            return(new ProgramNode(declarations, symT, typeTable));
            //return Either<ParseException, ProgramNode>.Bottom;
        }
Exemplo n.º 3
0
 public SymT(SymT parent0)
 {
     elements = new List <SymTableNode>();
     parent   = parent0;
 }
Exemplo n.º 4
0
        public static Either <ParseException, Pair <List <IToken>, ParameterNode> > Parse(List <IToken> tokens, SymT symT, IScopedTable <IEntityType, string> parentTypeTable)
        {
            Console.WriteLine("ParameterNode");
            if (tokens.Count < 3)
            {
                return(NotAParameterException);
            }

            if (!(tokens[0] is IdentifierToken))
            {
                return(NotAParameterException);
            }

            IdentifierToken name = (IdentifierToken)tokens[0];

            tokens = tokens.Skip(1).ToList();

            if (!(tokens[0] is IsKeywordToken))
            {
                return(NotAParameterException);
            }

            tokens = tokens.Skip(1).ToList();

            var maybeType = TypeNode.Parse(tokens, symT, parentTypeTable);

            if (maybeType.IsLeft)
            {
                return(maybeType.LeftToList()[0]);
            }

            ITypeNode type = maybeType.RightToList()[0].Second;

            tokens = maybeType.RightToList()[0].First;

            return(new Pair <List <IToken>, ParameterNode>(tokens, new ParameterNode(name, type)));
        }
Exemplo n.º 5
0
        public static Either <ParseException, Pair <List <IToken>, BodyNode> > Parse(List <IToken> tokens, SymT symT, IScopedTable <IEntityType, string> parentTypeTable)
        {
            Console.WriteLine("BodyNode");

            ImmutableArray <IAstNode> elements = ImmutableArray <IAstNode> .Empty;
            var typeTable = new Dictionary <string, IEntityType>();
            var result    = new BodyNode(ImmutableArray <IAstNode> .Empty, typeTable, parentTypeTable);

            while (tokens.Count > 0)
            {
                if (tokens[0] is NewLineSymbolToken || tokens[0] is CommentToken ||
                    tokens[0] is SemicolonSymbolToken)
                {
                    tokens = tokens.Skip(1).ToList();
                }
                else
                {
                    break;
                }
            }

            while (true)
            {
                var maybeSimpleDeclaration1 = VariableDeclarationNode.Parse(tokens, symT, result);
                if (maybeSimpleDeclaration1.IsRight)
                {
                    tokens = maybeSimpleDeclaration1.RightToList()[0].First;
                    var varDecl = maybeSimpleDeclaration1.RightToList()[0].Second;
                    elements = elements.Add(varDecl);
                    typeTable.TryAdd(varDecl.Identifier.Lexeme, varDecl.ToVariableType());
                    while (tokens.Count > 0)
                    {
                        if (tokens[0] is NewLineSymbolToken || tokens[0] is CommentToken ||
                            tokens[0] is SemicolonSymbolToken)
                        {
                            tokens = tokens.Skip(1).ToList();
                        }
                        else
                        {
                            break;
                        }
                    }
                    continue;
                }

                var maybeSimpleDeclaration2 = TypeDeclarationNode.Parse(tokens, symT, result);
                if (maybeSimpleDeclaration2.IsRight)
                {
                    tokens = maybeSimpleDeclaration2.RightToList()[0].First;
                    var typeDecl = maybeSimpleDeclaration2.RightToList()[0].Second;
                    elements = elements.Add(typeDecl);
                    typeTable.TryAdd(typeDecl.Identifier.Lexeme, typeDecl.ToTypeAliasType());
                    while (tokens.Count > 0)
                    {
                        if (tokens[0] is NewLineSymbolToken || tokens[0] is CommentToken ||
                            tokens[0] is SemicolonSymbolToken)
                        {
                            tokens = tokens.Skip(1).ToList();
                        }
                        else
                        {
                            break;
                        }
                    }
                    continue;
                }

                var maybeStatement = StatementNode.Parse(tokens, symT, result);
                if (maybeStatement.IsRight)
                {
                    tokens   = maybeStatement.RightToList()[0].First;
                    elements = elements.Add(maybeStatement.RightToList()[0].Second);
                    while (tokens.Count > 0)
                    {
                        if (tokens[0] is NewLineSymbolToken || tokens[0] is CommentToken ||
                            tokens[0] is SemicolonSymbolToken)
                        {
                            tokens = tokens.Skip(1).ToList();
                        }
                        else
                        {
                            break;
                        }
                    }
                    continue;
                }

                var maybeExpression = StatementNode.Parse(tokens, symT, result);
                if (maybeExpression.IsRight)
                {
                    tokens   = maybeExpression.RightToList()[0].First;
                    elements = elements.Add(maybeExpression.RightToList()[0].Second);
                    while (tokens.Count > 0)
                    {
                        if (tokens[0] is NewLineSymbolToken || tokens[0] is CommentToken ||
                            tokens[0] is SemicolonSymbolToken)
                        {
                            tokens = tokens.Skip(1).ToList();
                        }
                        else
                        {
                            break;
                        }
                    }
                    continue;
                }

                break;
            }


            if (tokens.Count < 1)
            {
                return(NotABodyException);
            }
            if (!(tokens[0] is ReturnKeywordToken))
            {
                return(NotABodyException);
            }
            tokens = tokens.Skip(1).ToList();
            var maybeExpression1 = ExpressionNode.Parse(tokens, result);

            if (maybeExpression1.IsRight)
            {
                while (tokens.Count > 0)
                {
                    if (tokens[0] is NewLineSymbolToken || tokens[0] is CommentToken ||
                        tokens[0] is SemicolonSymbolToken)
                    {
                        tokens = tokens.Skip(1).ToList();
                    }
                    else
                    {
                        break;
                    }
                }
                tokens = maybeExpression1.RightToList()[0].First;

                result.Elements             = elements;
                result.ReturnExpressionNode = maybeExpression1.RightToList()[0].Second;
                var res = new Pair <List <IToken>, BodyNode>(tokens, result);

                return(res);
            }



            result.Elements = elements;
            return(new Pair <List <IToken>, BodyNode>(tokens, result));
        }