Esempio n. 1
0
        public override void Compile()
        {
            DoNothingNode firstDN = new DoNothingNode();
            DoNothingNode middleDN = new DoNothingNode();
            DoNothingNode lastDN = new DoNothingNode();

            Compiled.Add(firstDN);

            CompiledCondition condition = new CompiledCondition();

            while(CurrentToken.Value.TokenType != TokenType.ELLIPSISOPEN)
            {
                CurrentToken = CurrentToken.Next;
            }

            condition.CurrentToken = CurrentToken.Next.Next;
            condition.Compile();
            CurrentToken = condition.CurrentToken;

            Compiled.InsertAfter(condition.Compiled);

            ConditionalJumpNode jump = new ConditionalJumpNode();
            jump.NextOnTrue = middleDN;
            jump.NextOnFalse = lastDN;

            Compiled.Add(jump);
            Compiled.Add(middleDN);

            while (CurrentToken.Value.TokenType != TokenType.BRACKETOPEN)
            {
                CurrentToken = CurrentToken.Next;
            }

            CurrentToken = CurrentToken.Next;

            while (CurrentToken.Value.TokenType != TokenType.BRACKETCLOSE)
            {
                AbstractCompiled compiled = CompilerFactory.getInstance().GetCompiled(CurrentToken);
                compiled.CurrentToken = CurrentToken;
                compiled.Compile();
                Compiled.InsertAfter(compiled.Compiled);
                CurrentToken = CurrentToken.Next;
            }

            Compiled.Add(new JumpNode(firstDN));

            Compiled.Add(lastDN);
        }
        public override void Compile(ref LinkedListNode<Token> currentToken)
        {
            int ifLevel = currentToken.Value.getLevel();
            _condition.addLast(new DoNothing());

            List<TokenExpectation> expected = new List<TokenExpectation>()
            {
                new TokenExpectation(ifLevel, Token.Type.If),
                new TokenExpectation(ifLevel, Token.Type.EllipsisOpen),
                new TokenExpectation(ifLevel + 1, Token.Type.Any), // Condition
                new TokenExpectation(ifLevel, Token.Type.EllipsisClose),
                new TokenExpectation(ifLevel, Token.Type.BracketsOpen),
                new TokenExpectation(ifLevel + 1, Token.Type.Any), // Body
                new TokenExpectation(ifLevel, Token.Type.BracketsClose)
            };

            foreach (var expectation in expected)
            {
                if (expectation.Level == ifLevel)
                {
                    if (currentToken.Value.tokentype != expectation.TokenType)
                        throw new Exception(String.Format("Unexpected end of statement, expected {0}", expectation.TokenType));
                    else
                    {
                        if (currentToken.Next != null)
                            currentToken = currentToken.Next;
                    }
                }
                else if (expectation.Level >= ifLevel)
                {
                    if (_condition.getSize() < 2) // We komen eerst de conditie tegen, deze vullen we daarom eerst.
                    {
                        var compiledCondition = new CompiledCondition();
                        compiledCondition.Compile(ref currentToken);
                        _condition.addLast(compiledCondition.compiled);
                    }
                    else
                    {
                        while (currentToken.Value.getLevel() > ifLevel) // Zolang we in de body zitten mag de factory hiermee aan de slag. Dit is niet onze zaak.
                        {
                            if (currentToken.Value.tokentype != Token.Type.Semicolon)
                            {
                                var compiledBodyPart = CompilerFactory.getInstance().CreateCompiledStatement(currentToken);
                                compiledBodyPart.Compile(ref currentToken);
                                _body.addLast(compiledBodyPart.compiled);
                            }
                            else
                                currentToken = currentToken.Next;
                        }
                    }
                }
            }

            _compiledStatement.addLast(new DoNothing());
            _compiledStatement.addLast(_condition);
            _compiledStatement.addLast(_conditionalJump);
            _compiledStatement.addLast(_body);
            _compiledStatement.addLast(new DoNothing());

            _conditionalJump.setOnTrue(_body.getFirst());
            _conditionalJump.setOnFalse(_compiledStatement.getLast());

            compiled.addLast(_compiledStatement);
        }