示例#1
0
        public ktValue GetObjectFor(ktToken Token)
        {
            ktValue Value = ktValue.Null;

            if (Token.Type != ktTokenType.Id)
            {
                throw new ktError("ktBlock::GetObjectFor: Excpected an id-token but got (" + Token.ToString() + ")", ktERR.UNEXP);
            }
#if Debug
	ktDebug.Log( "GOF:" + Token.ToString() + "!" );
#endif
            /*try {
				Value = GetVariable( Token.Value );
			} catch (Exception) {
				ktClass Class = GetClass( Token.Value );

				Value = new ktValue( Token.Value, Class.Name, Class, true, true );
			}*/
            try
            {
             //   ktDebug.Log("GOF - Var");
                Value = GetVariable(Token.Value);
                return Value;
            }
            catch (ktError Err)
            {
                if ((Err.ErrorNumber != ktERR.NOTDEF) &&
                    (Err.ErrorNumber != ktERR.NOTFOUND))
                {
                    throw Err;
                }
            }
            catch (NullReferenceException) { }
            try
            {
//                ktDebug.Log("GOF - Class");
                ktClass Class = GetClass(Token.Value);
                Value = Class.GetProperty("_"); //m_Main.MakeValueOf( "ktClass", Class );
                return Value;
            }
            catch (ktError Err)
            {
                if ((Err.ErrorNumber != ktERR.NOTDEF) &&
                    (Err.ErrorNumber != ktERR.NOTFOUND))
                {
                    throw Err;
                }
            }
            catch (NullReferenceException) { }
            try
            {
//                ktDebug.Log("GOF - Func");
                ktFunction Function = GetFunction(Token.Value);
                Value = m_Main.MakeValueOf("ktFunction", Function);
                return Value;
            }
            catch (ktError Err)
            {
                ktDebug.Log("_" + Err.ToString());
                if ((Err.ErrorNumber != ktERR.NOTDEF) &&
                    (Err.ErrorNumber != ktERR.NOTFOUND))
                {
                    throw Err;
                }
            }

            return Value;
        }
示例#2
0
        /// <summary>
        /// Parse the tokens and create a tree/* (mainly for operators)*/
        /// </summary>
        protected ktList MakeATree()
        {
            if (m_Lines == null)
            {
                m_Lines = new ktList();
                //m_Lines.Node = new ktNode( m_Name, new ktToken( ktTokenType.Program, m_Name, 0, 0 ) );
            }

            ktList Tree = MakeATree(m_LineStack, true, false);
            #if ParseDebug
            ktDebug.Log( "MAT_TREE1:" + Tree.Get_R( " ", true ) );
            #endif
            /*
            Tree = MakeATree( Tree, false, false );
            ktDebug.Log( "MAT_TREE2:" + Tree.Get_R( "\t", true ) );*/
            Tree = MakeATree(Tree, false, true);
            #if ParseDebug
            ktDebug.Log( "MAT_TREEEEEEEE:\n" + Tree.Get_R( " ", true ) );
            #endif

            /**/
            m_Lines = Tree;/*/
            m_Lines.AddList( Tree );/**/
            #if ParseDebug
            ktDebug.Log( "MAT_LINES:" + m_Lines.Get_R( " ", true ) );
            #endif

            return m_Lines;
        }

        /// <summary>
        /// React on the "token" in Word
        /// </summary>
        protected bool ReactOnToken(ktString Word, int Line, ref int CharPos)
        {
            /* In the C++/"original" version of this method, it didn't do what
             *  it's called (_React_ On Token), instead it just added the token to the list.
             *  This method, hovever, does react properly to the given token
             *	This also reduces the number of steps for scanning/parsering (creating the op. tree)
             *	from three to two (we could probably just do it all in one step, but it would be messy
             * 		and a bit complex..)
             */

            #if ParseDebug
            ktDebug.Log( "ReactOnToken(" + Word + ")" );
            ktDebug.Log( "BS:" + ktXML.FromList(m_BlockStack).AsXML() );
            #endif

            ktToken Token = new ktToken(Word, Line, CharPos);
            ktToken LastToken = null;
            ktToken LastAddedToken = null;
            ktToken.OnlyExportValue = true;

            if ((m_TokenStack != null) && (m_TokenStack.Last != null) &&
                (m_TokenStack.Last.Node != null) && (m_TokenStack.Last.Node.Value != null))
            {
                LastToken = (ktToken)m_TokenStack.Last.Node.Value;
            }
            if ((m_Tokens != null) && (m_Tokens.Last != null) &&
                (m_Tokens.Last.Node != null) && (m_Tokens.Last.Node.Value != null))
            {
                LastAddedToken = (ktToken)m_Tokens.Last.Node.Value;
            }

            if (m_CurToken != null)
            {
                // React differentely deppendig on "current token/'state'"
                switch (m_CurToken.Type)
                {
                    // If it's an comment...
                    case ktTokenType.MLComment:
                        {
                            // ... Check if the token is EOC? ...
                            if (Token.Type == ktTokenType.EOC)
                            {
                                m_CurToken = (ktToken)(m_TokenStack.Pop().Node.Value);
                            }
                            // ... ignore...
                            return true;
                        }
                    // If it's an comment...
                    case ktTokenType.OLComment:
                        {
                            // ... Check if the token is EOC? ...
                            if (Word == "\n")
                            {
                                m_CurToken = (ktToken)(m_TokenStack.Pop().Node.Value);
                            }
                            // ... ignore...
                            return true;
                        }
                    // If it's a string...
                    case ktTokenType.String:
                    case ktTokenType.StringQuot:
                    case ktTokenType.StartString:
                        {
                            // If we are at end of the string?
                            if ((Token.Type == ktTokenType.StringQuot) &&
                                (m_CurToken.Value.Last() != '\\'))
                            {
                                ktList Temp = null;

                                // Add string...
                                m_Tokens.Add("ktTString", m_CurToken);

                                // If we can, put back the previous token...
                                if ((m_TokenStack != null) && ((Temp = m_TokenStack.Pop()) != null) &&
                                    (Temp.Node != null))
                                {
                                    m_CurToken = (ktToken)Temp.Node.Value;
                                }
                                else
                                {
                                    m_CurToken = null;
                                }

                                return true;
                            }

                            m_CurToken.Value += Word;

                            return true;
                        }
                }
            }
            #if ParseDebug2
            /*Debug || */
               // if (Line == 4) {
            ktDebug.Log( "Token:" + Token.Type.ToString() + " (" + Token.Value + ")" );
            ktDebug.Log( "CurToken:" + m_CurToken.Type.ToString() );
            ktDebug.Log( "BS:" + ktXML.FromList(m_BlockStack).AsXML() );
            ktDebug.Log( "Ts:" + ((m_TokenStack == null) ? "null" : m_TokenStack.Get_R().ToString() ));
            ktDebug.Log( "LS:" + ((m_LineStack == null) ? "null" : m_LineStack.Get_R( "\t", true ).ToString() ));
            ktDebug.Log( "Ls:" + ((m_Lines == null) ? "null" : m_Lines.Get_R( "\t", true ).ToString() ));
            ktDebug.Log( "ts:" + ((m_Tokens == null) ? "null" : m_Tokens.Get_R().ToString() ));
            //            }
            #endif
            // Check the token..
            switch (Token.Type)
            {
                // Is it a whitespage??
                case ktTokenType.WhiteSpace:
                    {
                        // ... ignore...
                        return true;
                    }
                // Is it an id (var/function)?
                case ktTokenType.Id:
                    {
                        AddToken(Token);
                        return true;
                    }
                // Is it a number?
                case ktTokenType.Number:
                    {
                        AddToken(Token);
                        return true;
                    }
                // Is it a boolean (true/false)?
                case ktTokenType.Boolean:
                    {
                        AddToken(Token);
                        return true;
                    }
                // Is it an separator (. or ::)?
                case ktTokenType.Separator:
                    {
                        if ((LastAddedToken != null) && (LastAddedToken.Value == ":") &&
                                (Token.Value == ":"))
                        {
                            LastAddedToken.Value = "::";
                        }
                        else
                        {
                            AddToken(Token);
                        }
                        return true;
                    }
                // Is it an operator?????
                case ktTokenType.Operator:
                case ktTokenType.AssignmentOperator:
                    {
                        // We should perhaps do some more here??
                        AddToken(Token);
                        return true;
                    }
                // Is it an const/hard?????
                case ktTokenType.Const:
                case ktTokenType.Hard:
                    {
                        AddToken(Token);
                        return true;
                    }
                // Create a new object?
                case ktTokenType.New:
                    {
                        AddToken(Token);
                        return true;
                    }
                // Null?
                case ktTokenType.Null:
                    {
                        AddToken(Token);
                        return true;
                    }
                // Is it a If-statement?
                case ktTokenType.If:
                    {
                        AddToken(Token);
                        return true;
                    }
                // Is it a quot (")?
                case ktTokenType.StringQuot:
                    {
                        // No Token stack??
                        if (m_TokenStack == null)
                        {
                            // Create it...
                            m_TokenStack = new ktList();
                        }
                        // Add the current token to the stack
                        m_TokenStack.Add(m_CurToken);
                        // And Replace it with this "string token"
                        m_CurToken = new ktToken(ktTokenType.String, "", m_CurLine, m_CharPos);

                        return true;
                    }
                // Is it an start par. [(]?
                case ktTokenType.StartPar:
                    {
                        // No Token stack??
                        if (m_TokenStack == null)
                        {
                            // Create it...
                            m_TokenStack = new ktList();
                        }
                        // No Line Stack?
                        if (m_LineStack == null)
                        {
                            // Create it...
                            m_LineStack = new ktList();
                        }
                        // Add the current token to the stack
                        m_TokenStack.Add(m_CurToken);
                        // And Replace it with this "list token"
                        m_CurToken = new ktToken(ktTokenType.List, "ktList", m_CurLine, m_CharPos);

                        // Put the current ""line"" onto the stack
                        m_LineStack.AddList(m_Tokens);
                        // Create a new list...
                        m_Tokens = new ktList();
                        m_Tokens.Node = new ktNode("ktList", m_CurToken);

                        // Put the current ""list"" onto the stack
                        m_LineStack.AddList(m_Tokens);
                        // Create a new list...
                        m_Tokens = new ktList();
                        m_Tokens.Node = new ktNode("ktTStatement",
                                                    new ktToken(ktTokenType.Statement, "", m_CurLine, m_CharPos));

                        return true;
                    }
                // Is it an comma (,)?
                case ktTokenType.Comma:
                    {
                        if (m_CurToken.Type != ktTokenType.List)
                        {
                            throw new ktError("Unexpected " + ktToken.TokenToString(Token.Type) + " on line " +
                                                Line.ToString() + " by character " + CharPos.ToString() + ".", ktERR.UNEXP);
                        }

                        // get the last list
                        ktList List = m_LineStack.Last;

                        // Put the current ""statement"" onto the stack
                        List.AddList(m_Tokens);

                        // Create a new list...
                        m_Tokens = new ktList();
                        m_Tokens.Node = new ktNode("ktStatement",
                                                new ktToken(ktTokenType.Statement, "", m_CurLine, m_CharPos));
                        return true;
                    }
                // Is it an end par. [)]?
                case ktTokenType.EndPar:
                    {
                        if (m_CurToken.Type != ktTokenType.List)
                        {
                            throw new ktError("Unexpected " + ktToken.TokenToString(Token.Type) + " on line " +
                                                Line.ToString() + " by character " + CharPos.ToString() + ".", ktERR.UNEXP);
                        }

                        // Get the last "statement"
                        ktList Temp = m_LineStack.Pop();

                        // Add Current "statement"/"post" to the list and then switch them
                        Temp.AddList(m_Tokens);
                        m_Tokens = Temp;

                        // Get the "last statement" (where the list was "defined")
                        Temp = m_LineStack.Pop();
                        Temp.AddList(m_Tokens);
                        // "Switch"
                        m_Tokens = Temp;

                        // If we can, put back the previous token...
                        if ((m_TokenStack != null) && ((Temp = m_TokenStack.Pop()) != null) &&
                            (Temp.Node != null))
                        {
                            m_CurToken = (ktToken)Temp.Node.Value;
                        }
                        else
                        {
                            m_CurToken = null;
                        }
                        return true;
                    }

                // Is it an End-of-line (semicolon). (;)?
                case ktTokenType.EOL:
                    {
                        // Should we use m_Lines instead???
                        if (m_LineStack == null)
                        {
                            m_LineStack = new ktList();
                        }

                        // KacTalk special, runnable arguments!
                        // If we are in a list and finds a semicolon, it means
                        //  that that "post" should be treated as "runnable"
                        if (m_CurToken.Type == ktTokenType.List)
                        {
                            ktToken T = (ktToken)m_Tokens.Node.Value;

                            // Mark as "runnable"
                            T.Name = m_Tokens.Node.Name = "ktTRunStatement";
                            T.Type = ktTokenType.RunStatement;

                            // NOTE: basically the same as for comma (,) (see above)

                            // get the last list
                            ktList List = m_LineStack.Last;

                            // Put the current ""statement"" onto the stack
                            List.AddList(m_Tokens);

                            // Create a new list...
                            m_Tokens = new ktList();
                            m_Tokens.Node = new ktNode("ktTStatement",
                                                    new ktToken(ktTokenType.Statement, "", m_CurLine, m_CharPos));
                            return true;
                        }

                        ((ktToken)m_Tokens.Node.Value).Type = ktTokenType.Line;
                        ((ktToken)m_Tokens.Node.Value).Name = m_Tokens.Node.Name = "ktTLine";
                        m_LineStack.AddList(m_Tokens);

                        // Create a new list...
                        m_Tokens = new ktList();
                        m_Tokens.Node = new ktNode("ktTStatement_",
                                                    new ktToken(ktTokenType.Statement, "", m_CurLine, m_CharPos));

                        return true;
                    }

                // Is it a block ({)!?
                case ktTokenType.Block:
                    {
                        // No Token stack??
                        if (m_TokenStack == null)
                        {
                            // Create it...
                            m_TokenStack = new ktList();
                        }
                        /*/ No Line Stack?
                        if (m_LineStack == null) {
                            // Create it...
                            m_LineStack = new ktList();
                        } else {
                            if (m_BlockStack == null) {
                                m_BlockStack = new ktList();
                            }
                            m_BlockStack.AddList( m_LineStack );
                        }*/
            #if ParseDebug
               ktDebug.Log("BLOCK!!!");
            #endif
                        if (m_BlockStack == null)
                        {
            #if ParseDebug
            ktDebug.Log( "NEW_BLOCK_STACK" );
            #endif
                            m_BlockStack = new ktList();
                            if (m_MainBlock == null)
                            {
                                m_MainBlock = new ktBlock();
                                m_MainBlock.SetMain(this);
                            }
                            m_BlockStack.Add(m_MainBlock);
                        }

                        // Put the current ""line"" onto the stack
                        m_LineStack.AddList(m_Tokens);
                        LastBlock.SetLines(m_LineStack);

                        ktBlock Block = new ktBlock(new ktList());
                        Block.SetMain(this);

                        m_BlockStack.Add("ktTBlock", Block);

                        m_LineStack = new ktList();

                        // Add the current token to the stack
                        m_TokenStack.Add(m_CurToken);
                        // And Replace it with this "block token"
                        m_CurToken = new ktToken(ktTokenType.Block, "ktTBlock", m_CurLine, m_CharPos);

                        // Create a new list...
                        m_Tokens = new ktList();
                        m_Tokens.Node = new ktNode("ktTStatement.",
                                                    new ktToken(ktTokenType.Statement, "", m_CurLine, m_CharPos));

                        // Start a "new block"
                        /*m_LineStack = new ktList();

                        // Put the current ""line"" onto the stack
                        m_LineStack.AddList( m_Tokens );
                        // Create a new block...
                        m_Tokens = new ktList();
                        m_Tokens.Node = new ktNode( "ktTBlock", m_CurToken );

                        // Put the current ""list"" onto the stack
                        m_LineStack.AddList( m_Tokens );
                        // Create a new list...
                        m_Tokens = new ktList();
                        m_Tokens.Node = new ktNode( "ktTLine",
                                                    new ktToken( ktTokenType.Line, "", m_CurLine, m_CharPos ) );*/

                        return true;
                    }
                // Is it an end of a block (})?
                case ktTokenType.EOB:
                case ktTokenType.End:
                    {
            #if ParseDebug
            ktDebug.Log( "EOB;EOB;EOB;EOB;EOB;EOB;EOB;EOB;EOB;EOB;EOB;EOB;EOB;" );
            ktDebug.Log("CurrToken:" + m_CurToken.ToString());
            #endif
                        if ((m_CurToken.Type != ktTokenType.Block) ||
                                (m_BlockStack == null) || (m_BlockStack.Last == null) ||
                                (m_BlockStack.Last.Node == null) || (m_BlockStack.Last.Node.Value == null))
                        {
                            throw new ktError("Unexpected " + ktToken.TokenToString(Token.Type) + " on line " +
                                                Line.ToString() + " by character " + CharPos.ToString() + ".", ktERR.UNEXP);
                        }

            #if ParseDebug2
            ktDebug.Log( "ROT::B::BLS:" + ktXML.FromList(m_BlockStack).AsXML()  );
            #endif
                        ktBlock Block = (ktBlock)(m_BlockStack.Pop().Node.Value);
            #if ParseDebug2
            ktDebug.Log( "ROT::B::BLS2:" + ktXML.FromList(m_BlockStack).AsXML() );

            ktDebug.Log( "ROT::B::Tokens:" + m_Tokens.Get_R() );
            ktDebug.Log( "ROT::B::LineStack:" + m_LineStack.Get_R( "\t", true ) );
            #endif

                        MakeATree();
            #if ParseDebug2
            ktDebug.Log( "ROT::B::Lines:" + m_Lines.Get_R() );
            #endif
                        Block.Lines = MakeAOpTree();
            #if ParseDebug2
            ktDebug.Log( "ROT::Block:" + Block.ToString() );
            ktDebug.Log( "ROT::Block:Lines:" + Block.Lines.Get_R() );
            #endif

                        m_LineStack.Clear();
                        m_LineStack = LastBlockLines;
                        //					m_Lines.Clear();
                        m_Lines = null;
            #if ParseDebug2
            ktDebug.Log( "ROT::B::LineStack22:" + m_LineStack.Get_R( "\t", true ) );
            #endif
                        /*Block.Lines = MakeATree( MakeATree( m_LineStack, true, false ), false, true );
                    Block.Lines = MakeAOpTree( MakeAOpTree( MakeAOpTree( Block.Lines, 1 ), 2 ), 3 )*/
                        m_Tokens.Node.Name = "ktTBlock";
                        ((ktToken)m_Tokens.Node.Value).Name = "ktTBlock";
                        ((ktToken)m_Tokens.Node.Value).Type = ktTokenType.Block;

                        // Add Current "statement"/"post" to the block and then switch them
                        //Temp.AddList( m_Tokens );
                        //m_Tokens = Temp;

                        ktList Temp = null;

                        if (LastBlockLines == null)
                        {
                            throw new ktError("No Last Block Lines!", ktERR.MISSING);
                        }
                        LastBlockLines.Add("ktTBlock", new ktToken(Block, m_CurToken.LineNo, m_CurToken.CharPos));
                        // "Switch"
                        //m_Tokens = Temp;
            #if ParseDebug2
            ktDebug.Log( "ROT::B::LastBlockLines:" + LastBlockLines.Get_R( "\t", true ) );
            #endif

                        // If we can, put back the previous token...
                        if ((m_TokenStack != null) && ((Temp = m_TokenStack.Pop()) != null) &&
                            (Temp.Node != null))
                        {
                            m_CurToken = (ktToken)Temp.Node.Value;
                        }
                        else
                        {
                            m_CurToken = null;
                        }
                        return true;
                    }

                // Comments...
                // One line comment??
                //	(bash only indicates comments if it's the first thing on the line...)
                case ktTokenType.Bash:
                    {
                        // If it's the first thing on the line!?
                        if (m_CharPos <= 1)
                        {
                            // Indicate one line comment...
                            // No Token stack??
                            if (m_TokenStack == null)
                            {
                                // Create it...
                                m_TokenStack = new ktList();
                            }
                            // Add the current token to the stack
                            m_TokenStack.Add(m_CurToken);
                            // And Replace it with this "commeent token"
                            m_CurToken = new ktToken(ktTokenType.OLComment, "ktTOLComment", m_CurLine, m_CharPos);
                            return true;
                            // Not First at the line...
                        }
                        else
                        {
                            // What should we do??
                            // Exception, AddToken or somee magic for ids!???

                            // Unexpected...
                            throw new ktError("Unexpeected " + ktToken.TokenToString(Token.Type) + " on line " +
                                                m_CurLine.ToString() + " by character " + m_CharPos.ToString(), ktERR.UNEXP);
                        }
                    }
                // One line comment
                case ktTokenType.OLComment:
                    {
                        // No Token stack??
                        if (m_TokenStack == null)
                        {
                            // Create it...
                            m_TokenStack = new ktList();
                        }
                        // Add the current token to the stack
                        m_TokenStack.Add(m_CurToken);
                        // And Replace it with this "commeent token"
                        m_CurToken = new ktToken(ktTokenType.OLComment, "ktTOLComment", m_CurLine, m_CharPos);
                        return true;
                    }
                // Multi line comment
                case ktTokenType.MLComment:
                    {
                        // No Token stack??
                        if (m_TokenStack == null)
                        {
                            // Create it...
                            m_TokenStack = new ktList();
                        }
                        // Add the current token to the stack
                        m_TokenStack.Add(m_CurToken);
                        // And Replace it with this "Comment token"
                        m_CurToken = new ktToken(ktTokenType.MLComment, "ktTMLComment", m_CurLine, m_CharPos);
                        return true;
                    }
                // If it's an End of comment
                case ktTokenType.EOC:
                    {
                        // Unexpected...
                        throw new ktError("Unexpeected " + ktToken.TokenToString(Token.Type) + " on line " +
                                            m_CurLine.ToString() + " by character " + m_CharPos.ToString(), ktERR.UNEXP);
                    }
            }

            // To see if we missed any tokens...
            ktToken.OnlyExportValue = false;
            ktDebug.Log("IGNmORED:" + Token.Export() + "\n");

            return true;
        }