﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StillOneScriptCore.Parser.Syntaxer.SkeletonLeafs.FunctionLeafs
{
    public class MainLeaf : StillOneScriptCore.Parser.Syntaxer.BaseLeaf
    {
        public MainLeaf(SyntaxerContext context)
            : base(context)
        {
        }

        protected override bool BeginToken()
        {
            mResult = new ScriptCodeDom.Skeletons.Function();

            mMainCodeBlock = new ScriptCodeDom.Statements.CodeBlockStatement();

            mResult.Statement = mMainCodeBlock;

            return true;
        }

        protected override ResultOfProcess ProcessToken(Lexer.Token token)
        {
            this.Context.Recovery(token);

            var tmpExpressionStatementLeaf = new Syntaxer.StatementLeafs.ExpressionStatementLeaf(this.Context);

            var tmpRez = tmpExpressionStatementLeaf.Run();

            var tmpResultNode = tmpExpressionStatementLeaf.Result;

            if (mCurrStatement == null)
            {
                mCurrStatement = tmpResultNode;

                mMainCodeBlock.FirstStatement = mCurrStatement;
            }
            else
            {
                mCurrStatement.NextStatement = tmpResultNode;

                mCurrStatement = tmpResultNode;
            }

            return tmpRez;
        }

        protected override bool EndLeaf()
        {
            return true;
        }

        private ScriptCodeDom.Statements.BaseStatement mCurrStatement = null;

        private ScriptCodeDom.Statements.CodeBlockStatement mMainCodeBlock = null;

        private ScriptCodeDom.Skeletons.Function mResult = null;

        public ScriptCodeDom.Skeletons.Function Result
        {
            get
            {
                return mResult;
            }
        }
    }
}
