Esempio n. 1
0
 private BodyNode(ImmutableArray <IAstNode> elements, ExpressionNode returnExpressionNode, IDictionary <string, IEntityType> scopeTypeTable, IScopedTable <IEntityType, string> parentTypeTable)
 {
     Elements             = elements;
     ReturnExpressionNode = returnExpressionNode;
     ScopeTypeTable       = scopeTypeTable;
     ParentTypeTable      = parentTypeTable;
 }
Esempio n. 2
0
 private ExpressionNode(RelationNode relation1, List <RelationNode> relations, List <IToken> tokens, IScopedTable <IEntityType, string> parentTypeTable)
 {
     Relation1       = relation1;
     Relations       = relations;
     Tokens          = tokens;
     ParentTypeTable = parentTypeTable;
 }
Esempio n. 3
0
 private SimpleNode(FactorNode factor, List <IToken> tokens, List <FactorNode> factors, IScopedTable <IEntityType, string> parentTypeTable)
 {
     Factor          = factor;
     Tokens          = tokens;
     Factors         = factors;
     ParentTypeTable = parentTypeTable;
 }
Esempio n. 4
0
 private FactorNode(SummandNode summand, List <IToken> tokens, List <SummandNode> summands, IScopedTable <IEntityType, string> parentTypeTable)
 {
     Summand         = summand;
     Tokens          = tokens;
     Summands        = summands;
     ParentTypeTable = parentTypeTable;
 }
Esempio n. 5
0
 private RelationNode(SimpleNode simple1, IToken token, SimpleNode simple2, IScopedTable <IEntityType, string> parentTypeTable)
 {
     Simple1         = simple1;
     Token           = token;
     Simple2         = simple2;
     ParentTypeTable = parentTypeTable;
 }
 private ModifiablePrimaryNode(IdentifierToken identifier, List <IdentifierToken> identifiers, List <ExpressionNode> expressions, IScopedTable <IEntityType, string> parentTypeTable)
 {
     Identifier      = identifier;
     Identifiers     = identifiers;
     Expressions     = expressions;
     ParentTypeTable = parentTypeTable;
 }
Esempio n. 7
0
 private VariableDeclarationNode(IdentifierToken identifier, Option <TypeNode> type, Option <ExpressionNode> expression, IScopedTable <IEntityType, string> parentTypeTable)
 {
     Identifier      = identifier;
     Type            = type;
     Expression      = expression;
     ParentTypeTable = parentTypeTable;
 }
Esempio n. 8
0
 private ForLoopNode(IdentifierToken identifier, RangeNode range, BodyNode body, SymT symT, IDictionary <string, IEntityType> scopeTypeTable, IScopedTable <IEntityType, string> parentTypeTable)
 {
     Identifier      = identifier;
     Range           = range;
     Body            = body;
     SymbolTable     = symT;
     ScopeTypeTable  = scopeTypeTable;
     ParentTypeTable = parentTypeTable;
 }
 private RoutineDeclarationNode(IdentifierToken identifier, IEnumerable <ParameterNode> parameters, Option <TypeNode> returnType, BodyNode body, SymT symT, IDictionary <string, IEntityType> scopeTypeTable, IScopedTable <IEntityType, string> parentTypeTable)
 {
     Identifier      = identifier;
     Parameters      = parameters.ToImmutableArray();
     ReturnType      = returnType;
     Body            = body;
     ScopeTypeTable  = scopeTypeTable;
     ParentTypeTable = parentTypeTable;
     SymbolTable     = symT;
 }
Esempio n. 10
0
 private StatementNode(AssignmentNode assignment, IScopedTable <IEntityType, string> parentTypeTable)
 {
     Assignment      = assignment;
     ParentTypeTable = parentTypeTable;
 }
        public static Either <ParseException, Pair <List <IToken>, TypeDeclarationNode> > Parse(List <IToken> tokens, SymT symT, IScopedTable <IEntityType, string> parentTypeTable)
        {
            Console.WriteLine("TypeDeclarationNode");
            if (tokens.Count < 3)
            {
                return(NotATypeDeclarationException);
            }
            if (!(tokens[0] is TypeKeywordToken) ||
                !(tokens[1] is IdentifierToken) ||
                !(tokens[2] is IsKeywordToken))
            {
                return(NotATypeDeclarationException);
            }


            IdentifierToken identifier = (IdentifierToken)tokens[1];

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

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

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

            if (symT.Contain(identifier))
            {
                // TODO: return an exception instead

                Console.WriteLine("Repeating identifier in the same scope");
            }
            else
            {
                symT.Add(identifier);
            }

            return(new Pair <List <IToken>, TypeDeclarationNode>(tokens, new TypeDeclarationNode(
                                                                     identifier, maybeType.RightToList()[0].Second, parentTypeTable)));
        }
 private TypeDeclarationNode(IdentifierToken identifier, TypeNode type, IScopedTable <IEntityType, string> parentTypeTable)
 {
     Identifier      = identifier;
     Type            = type;
     ParentTypeTable = parentTypeTable;
 }
Esempio n. 13
0
        public static Either <ParseException, Pair <List <IToken>, AssignmentNode> > Parse(List <IToken> tokens, IScopedTable <IEntityType, string> parentTypeTable)
        {
            Console.WriteLine("AssignmentNode");
            var maybeModifiablePrimary = ModifiablePrimaryNode.Parse(tokens, parentTypeTable);

            if (maybeModifiablePrimary.IsLeft)
            {
                return(maybeModifiablePrimary.LeftToList()[0]);
            }
            tokens = maybeModifiablePrimary.RightToList()[0].First;

            if (tokens.Count < 1)
            {
                return(NotAnAssignmentNodeException);
            }

            if (!(tokens[0] is AssignmentOperatorToken))
            {
                return(NotAnAssignmentNodeException);
            }
            tokens = tokens.Skip(1).ToList();

            var maybeExpression = ExpressionNode.Parse(tokens, parentTypeTable);

            if (maybeExpression.IsLeft)
            {
                return(maybeExpression.LeftToList()[0]);
            }
            tokens = maybeExpression.RightToList()[0].First;

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

            return(new Pair <List <IToken>, AssignmentNode>(tokens, new AssignmentNode(
                                                                maybeModifiablePrimary.RightToList()[0].Second, maybeExpression.RightToList()[0].Second)));
        }
        public static Either <ParseException, Pair <List <IToken>, RoutineDeclarationNode> > Parse(List <IToken> tokens, SymT symT, IScopedTable <IEntityType, string> parentTypeTable)
        {
            var NewSymT = new SymT(symT);

            Console.WriteLine("RoutineDeclarationNode");
            if (tokens.Count <= 3)
            {
                return(NotARoutineException);
            }

            var maybeRoutine    = tokens[0];
            var maybeIdentifier = tokens[1];
            var maybeLp         = tokens[2];

            if (
                !(maybeRoutine is RoutineKeywordToken) ||
                !(maybeIdentifier is IdentifierToken) ||
                !(maybeLp is LeftParenthSymbolToken)
                )
            {
                return(NotARoutineException);
            }

            IdentifierToken identifier = (IdentifierToken)maybeIdentifier;

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

            var parameters = new List <ParameterNode>();
            var typeTable  = new Dictionary <string, IEntityType>();

            while (true)
            {
                var maybeParameter = ParameterNode.Parse(tokens, NewSymT, parentTypeTable);

                if (maybeParameter.IsLeft)
                {
                    break;
                }

                var parameter = maybeParameter.RightToList()[0].Second;
                parameters.Add(parameter);
                typeTable.TryAdd(parameter.Name.Lexeme, new VariableType(parameter.Type.ToTypeRepresentation()));
                tokens = maybeParameter.RightToList()[0].First;

                if (tokens.Count < 1)
                {
                    return(NotARoutineException);
                }

                if (tokens[0] is ComaSymbolToken)
                {
                    tokens = tokens.Skip(1).ToList();
                    continue;
                }

                break;
            }

            if (tokens[0] is RightParenthSymbolToken)
            {
                tokens = tokens.Skip(1).ToList();
            }
            else
            {
                return(NotARoutineException);
            }

            if (tokens.Count < 2)
            {
                return(NotARoutineException);
            }

            Either <ParseException, Pair <List <IToken>, TypeNode> > maybeType = new ParseException("Dummy");

            if (tokens[0] is ColonSymbolToken)
            {
                tokens    = tokens.Skip(1).ToList();
                maybeType = TypeNode.Parse(tokens, symT, parentTypeTable);
                if (maybeType.IsLeft)
                {
                    return(NotARoutineException);
                }
                tokens = maybeType.RightToList()[0].First;
            }

            if (tokens.Count < 1)
            {
                return(NotARoutineException);
            }
            if (!(tokens[0] is IsKeywordToken))
            {
                return(NotARoutineException);
            }

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

            var result = new RoutineDeclarationNode(
                identifier, parameters,
                maybeType.Map <TypeNode>(pr => pr.Second).ToOption(),
                null,
                NewSymT,
                typeTable,
                parentTypeTable
                );
            var maybeBody = BodyNode.Parse(tokens, NewSymT, result);

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

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

            if (tokens.Count < 1)
            {
                return(NotARoutineException);
            }
            if (!(tokens[0] is EndKeywordToken))
            {
                return(NotARoutineException);
            }


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

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

            result.Body = maybeBody.RightToList()[0].Second;
            return(new Pair <List <IToken>, RoutineDeclarationNode>(tokens, result));
        }
Esempio n. 15
0
        public static Either <ParseException, Pair <List <IToken>, StatementNode> > Parse(List <IToken> tokens, SymT symT, IScopedTable <IEntityType, string> parentTypeTable)
        {
            Console.WriteLine("StatementNode");

            var maybeAssignment = AssignmentNode.Parse(tokens, parentTypeTable);

            if (maybeAssignment.IsRight)
            {
                tokens = maybeAssignment.RightToList()[0].First;
                return(new Pair <List <IToken>, StatementNode>(tokens, new StatementNode(
                                                                   maybeAssignment.RightToList()[0].Second,
                                                                   parentTypeTable)));
            }

            var maybeRoutineCall = RoutineCallNode.Parse(tokens, symT, parentTypeTable);

            if (maybeRoutineCall.IsRight)
            {
                tokens = maybeRoutineCall.RightToList()[0].First;
                return(new Pair <List <IToken>, StatementNode>(tokens, new StatementNode(
                                                                   maybeRoutineCall.RightToList()[0].Second,
                                                                   parentTypeTable)));
            }

            var maybeWhileLoop = WhileLoopNode.Parse(tokens, symT, parentTypeTable);

            if (maybeWhileLoop.IsRight)
            {
                tokens = maybeWhileLoop.RightToList()[0].First;
                return(new Pair <List <IToken>, StatementNode>(tokens, new StatementNode(
                                                                   maybeWhileLoop.RightToList()[0].Second,
                                                                   parentTypeTable)));
            }

            var maybeForLoop = ForLoopNode.Parse(tokens, symT, parentTypeTable);

            if (maybeForLoop.IsRight)
            {
                tokens = maybeForLoop.RightToList()[0].First;
                return(new Pair <List <IToken>, StatementNode>(tokens, new StatementNode(
                                                                   maybeForLoop.RightToList()[0].Second,
                                                                   parentTypeTable)));
            }

            var maybeIfStatement = IfStatementNode.Parse(tokens, symT, parentTypeTable);

            if (maybeIfStatement.IsRight)
            {
                tokens = maybeIfStatement.RightToList()[0].First;
                return(new Pair <List <IToken>, StatementNode>(tokens, new StatementNode(
                                                                   maybeIfStatement.RightToList()[0].Second,
                                                                   parentTypeTable)));
            }

            return(NotAStatementException);
        }
Esempio n. 16
0
 private StatementNode(ForLoopNode forLoop, IScopedTable <IEntityType, string> parentTypeTable)
 {
     ForLoop         = forLoop;
     ParentTypeTable = parentTypeTable;
 }
Esempio n. 17
0
        public static Either <ParseException, Pair <List <IToken>, ForLoopNode> > Parse(List <IToken> tokens, SymT symT, IScopedTable <IEntityType, string> parentTypeTable)
        {
            Console.WriteLine("ForLoopNode");
            SymT NewSymT = new SymT(symT);

            if (tokens.Count < 1)
            {
                return(NotAForLoopException);
            }
            if (!(tokens[0] is ForKeywordToken))
            {
                return(NotAForLoopException);
            }
            tokens = tokens.Skip(1).ToList();

            if (tokens.Count < 1)
            {
                return(NotAForLoopException);
            }
            if (!(tokens[0] is IdentifierToken))
            {
                return(NotAForLoopException);
            }
            IdentifierToken identifier = (IdentifierToken)tokens[0];
            var             typeTable  = new Dictionary <string, IEntityType>();

            typeTable.TryAdd(
                identifier.Lexeme,
                new VariableType(new PrimitiveTypeRepresentation(new IntegerTypeNode()))
                );
            tokens = tokens.Skip(1).ToList();

            var result = new ForLoopNode(
                identifier, null, null, NewSymT, typeTable, parentTypeTable);
            var maybeRange = RangeNode.Parse(tokens, result);

            if (NewSymT.Contain(identifier))
            {
                // TODO: return an exception instead
                Console.WriteLine("Repeating identifier in the same scope");
            }
            else
            {
                NewSymT.Add(identifier);
            }

            if (maybeRange.IsLeft)
            {
                return(maybeRange.LeftToList()[0]);
            }
            RangeNode range = maybeRange.RightToList()[0].Second;

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

            if (tokens.Count < 1)
            {
                return(NotAForLoopException);
            }
            if (!(tokens[0] is LoopKeywordToken))
            {
                return(NotAForLoopException);
            }
            tokens = tokens.Skip(1).ToList();

            var maybeBody = BodyNode.Parse(tokens, NewSymT, result);

            if (maybeBody.IsLeft)
            {
                return(maybeBody.LeftToList()[0]);
            }
            BodyNode body = maybeBody.RightToList()[0].Second;

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

            if (tokens.Count < 1)
            {
                return(NotAForLoopException);
            }
            if (!(tokens[0] is EndKeywordToken))
            {
                return(NotAForLoopException);
            }
            tokens = tokens.Skip(1).ToList();

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

            result.Range = range;
            result.Body  = body;
            return(new Pair <List <IToken>, ForLoopNode>(tokens, result));
        }
Esempio n. 18
0
        public static Either <ParseException, Pair <List <IToken>, SummandNode> > Parse(List <IToken> tokens, IScopedTable <IEntityType, string> parentTypeTable)
        {
            Console.WriteLine("SummandNode");
            var maybePrimary = PrimaryNode.Parse(tokens, parentTypeTable);

            if (maybePrimary.IsRight)
            {
                tokens = maybePrimary.RightToList()[0].First;
                while (tokens.Count > 0)
                {
                    if (tokens[0] is NewLineSymbolToken || tokens[0] is CommentToken)
                    {
                        tokens = tokens.Skip(1).ToList();
                    }
                    else
                    {
                        break;
                    }
                }
                return(new Pair <List <IToken>, SummandNode>(tokens, new SummandNode(maybePrimary.RightToList()[0].Second, parentTypeTable)));
            }


            if (tokens.Count < 1)
            {
                return(NotASummandException);
            }
            if (!(tokens[0] is LeftParenthSymbolToken))
            {
                return(NotASummandException);
            }
            tokens = tokens.Skip(1).ToList();



            var maybeExpression = ExpressionNode.Parse(tokens, parentTypeTable);

            if (maybeExpression.IsLeft)
            {
                return(maybeExpression.LeftToList()[0]);
            }
            tokens = maybeExpression.RightToList()[0].First;



            if (tokens.Count < 1)
            {
                return(NotASummandException);
            }
            if (!(tokens[0] is RightParenthSymbolToken))
            {
                return(NotASummandException);
            }
            tokens = tokens.Skip(1).ToList();

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



            return(new Pair <List <IToken>, SummandNode>(tokens, new SummandNode(maybeExpression.RightToList()[0].Second, parentTypeTable)));
        }
        public static Either <ParseException, Pair <List <IToken>, ModifiablePrimaryNode> > Parse(List <IToken> tokens, IScopedTable <IEntityType, string> parentTypeTable)
        {
            Console.WriteLine("ModifiablePrimaryNode");

            if (tokens.Count < 1)
            {
                return(NotAModifiablePrimaryException);
            }
            if (!(tokens[0] is IdentifierToken))
            {
                return(NotAModifiablePrimaryException);
            }
            IdentifierToken identifier = (IdentifierToken)tokens[0];

            tokens = tokens.Skip(1).ToList();
            var identifiers = new List <IdentifierToken>();
            var expressions = new List <ExpressionNode>();

            while (true)
            {
                if (tokens.Count < 1)
                {
                    return(new Pair <List <IToken>, ModifiablePrimaryNode> (tokens, new ModifiablePrimaryNode(identifier, parentTypeTable)));
                }

                if (tokens[0] is DotSymbolToken)
                {
                    tokens = tokens.Skip(1).ToList();
                    if (tokens.Count < 1)
                    {
                        return(NotAModifiablePrimaryException);
                    }
                    if (!(tokens[0] is IdentifierToken))
                    {
                        return(NotAModifiablePrimaryException);
                    }
                    identifiers.Add((IdentifierToken)tokens[0]);
                    tokens = tokens.Skip(1).ToList();
                }

                else if (tokens[0] is LeftBracketSymbolToken)
                {
                    tokens = tokens.Skip(1).ToList();
                    var maybeExpression = ExpressionNode.Parse(tokens, parentTypeTable);
                    if (maybeExpression.IsLeft)
                    {
                        return(maybeExpression.LeftToList()[0]);
                    }
                    tokens = maybeExpression.RightToList()[0].First;
                    if (tokens.Count < 1)
                    {
                        return(NotAModifiablePrimaryException);
                    }
                    if (!(tokens[0] is RightBracketSymbolToken))
                    {
                        return(NotAModifiablePrimaryException);
                    }
                    expressions.Add(maybeExpression.RightToList()[0].Second);
                    tokens = tokens.Skip(1).ToList();
                }
                else
                {
                    break;
                }
            }

            while (tokens.Count > 0)
            {
                if (tokens[0] is NewLineSymbolToken || tokens[0] is CommentToken ||
                    tokens[0] is SemicolonSymbolToken)
                {
                    tokens = tokens.Skip(1).ToList();
                }
                else
                {
                    break;
                }
            }
            return(new Pair <List <IToken>, ModifiablePrimaryNode> (tokens, new ModifiablePrimaryNode(identifier, identifiers, expressions, parentTypeTable)));;
        }
 private ModifiablePrimaryNode(IdentifierToken identifier, IScopedTable <IEntityType, string> parentTypeTable)
 {
     Identifier      = identifier;
     ParentTypeTable = parentTypeTable;
 }
Esempio n. 21
0
        public static Either <ParseException, Pair <List <IToken>, RangeNode> > Parse(List <IToken> tokens, IScopedTable <IEntityType, string> parentTypeTable)
        {
            Console.WriteLine("RangeNode");
            if (tokens.Count < 1)
            {
                return(NotARangeException);
            }
            if (!(tokens[0] is InKeywordToken))
            {
                return(NotARangeException);
            }
            tokens = tokens.Skip(1).ToList();

            Boolean reverse = false;

            if (tokens.Count >= 1)
            {
                if (tokens[0] is ReverseKeywordToken)
                {
                    reverse = true;
                    tokens  = tokens.Skip(1).ToList();
                }
            }

            var maybeExpression1 = ExpressionNode.Parse(tokens, parentTypeTable);

            if (maybeExpression1.IsLeft)
            {
                return(maybeExpression1.LeftToList()[0]);
            }
            tokens = maybeExpression1.RightToList()[0].First;

            if (tokens.Count < 1)
            {
                return(NotARangeException);
            }
            if (!(tokens[0] is RangeSymbolToken))
            {
                return(NotARangeException);
            }
            tokens = tokens.Skip(1).ToList();

            var maybeExpression2 = ExpressionNode.Parse(tokens, parentTypeTable);

            if (maybeExpression2.IsLeft)
            {
                return(maybeExpression2.LeftToList()[0]);
            }
            tokens = maybeExpression2.RightToList()[0].First;

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

            return(new Pair <List <IToken>, RangeNode>(tokens, new RangeNode(
                                                           maybeExpression1.RightToList()[0].Second,
                                                           maybeExpression2.RightToList()[0].Second,
                                                           reverse)));
        }
Esempio n. 22
0
        public static Either <ParseException, Pair <List <IToken>, RecordTypeNode> > Parse(List <IToken> tokens, SymT symT, IScopedTable <IEntityType, string> parentTypeTable)
        {
            Console.WriteLine("RecordTypeNode");
            var declarations = new List <VariableDeclarationNode>();

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

            while (true)
            {
                var maybeVariableDeclaration = VariableDeclarationNode.Parse(tokens, symT, parentTypeTable);
                if (maybeVariableDeclaration.IsLeft)
                {
                    break;
                }
                declarations.Add(maybeVariableDeclaration.RightToList()[0].Second);
                tokens = maybeVariableDeclaration.RightToList()[0].First;
            }

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

            return(new Pair <List <IToken>, RecordTypeNode>(tokens, new RecordTypeNode(declarations, symT)));
        }
Esempio n. 23
0
 private StatementNode(RoutineCallNode routineCall, IScopedTable <IEntityType, string> parentTypeTable)
 {
     RoutineCall     = routineCall;
     ParentTypeTable = parentTypeTable;
 }
Esempio n. 24
0
 private SummandNode(ExpressionNode expression, IScopedTable <IEntityType, string> parentTypeTable)
 {
     Expression      = expression;
     ParentTypeTable = parentTypeTable;
 }
Esempio n. 25
0
 private StatementNode(WhileLoopNode whileLoop, IScopedTable <IEntityType, string> parentTypeTable)
 {
     WhileLoop       = whileLoop;
     ParentTypeTable = parentTypeTable;
 }
Esempio n. 26
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));
        }
Esempio n. 27
0
 private StatementNode(IfStatementNode ifStatement, IScopedTable <IEntityType, string> parentTypeTable)
 {
     IfStatement     = ifStatement;
     ParentTypeTable = parentTypeTable;
 }
Esempio n. 28
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)));
        }
Esempio n. 29
0
        public static Either <ParseException, Pair <List <IToken>, TypeNode> > Parse(List <IToken> tokens, SymT symT, IScopedTable <IEntityType, string> parentTypeTable)
        {
            Console.WriteLine("TypeNode");
            var maybePrimitiveType = PrimitiveTypeNode.Parse(tokens);

            if (maybePrimitiveType.IsLeft)
            {
                var maybeArrayType = ArrayTypeNode.Parse(tokens, symT, parentTypeTable);
                if (maybeArrayType.IsLeft)
                {
                    SymT NewSymT         = new SymT(symT);
                    var  maybeRecordType = RecordTypeNode.Parse(tokens, NewSymT, parentTypeTable);

                    if (maybeRecordType.IsLeft)
                    {
                        if (tokens.Count < 1)
                        {
                            return(NotATypeException);
                        }
                        if (!(tokens[0] is IdentifierToken))
                        {
                            return(NotATypeException);
                        }
                        IdentifierToken identifier = (IdentifierToken)tokens[0];
                        if (!(symT.ContainRec(identifier)))
                        {
                            // TODO: return an exception instead
                            Console.Write("No such identifier");
                        }
                        tokens = tokens.Skip(1).ToList();
                        return(new Pair <List <IToken>, TypeNode> (tokens, new TypeNode(identifier)));
                    }
                    tokens = maybeRecordType.RightToList()[0].First;
                    return(new Pair <List <IToken>, TypeNode> (tokens, new TypeNode(
                                                                   maybeRecordType.RightToList()[0].Second)));
                }
                tokens = maybeArrayType.RightToList()[0].First;
                return(new Pair <List <IToken>, TypeNode> (tokens, new TypeNode(
                                                               maybeArrayType.RightToList()[0].Second)));
            }

            tokens = maybePrimitiveType.RightToList()[0].First;
            return(new Pair <List <IToken>, TypeNode> (tokens, new TypeNode(
                                                           maybePrimitiveType.RightToList()[0].Second)));
        }
 private RoutineDeclarationNode(IDictionary <string, IEntityType> scopeTypeTable, IScopedTable <IEntityType, string> parentTypeTable)
 {
     ScopeTypeTable  = scopeTypeTable;
     ParentTypeTable = parentTypeTable;
 }