예제 #1
0
        public ktDelegateFunction(ktString Name, ktFunction_Double_Delegate Delegate)
            : base(Name, null, null, ktValue.Null)
        {
            m_Arguments = new ktList();
            m_Arguments.Add(new ktValue("D", "double",
                                              kacTalk.Main.GetClass("double"),
                                              true, true));

            m_Delegate = delegate(ktList Args)
            {
                if ((Args == null) || (Args.FirstNode == null) ||
                    (Args.FirstNode.Value == null))
                {
                    throw new ktError("Didn't get an argument for '" +
                                      Name + "'!", ktERR.MISSING);
                }
                //ktDebug.Log( "ktDF::DOUBLE(" +Args.FirstNode.Value.ToString() + ")" );
                ktString S_In = new ktString(Args.FirstNode.Value.ToString());
                double D_In = 0.0;
                double D_Out = 0.0;

                try
                {
                    if (System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator == ",")
                    {
                        S_In.Replace(".", ",", true);
                    }

                    D_In = S_In.ToDouble();
                }
                catch (Exception E)
                {
                    if (E.GetType() == typeof(System.FormatException) && !S_In.IsEmpty())
                    {
                        throw new ktError("ktDouble::CreateObject: Cant make '" + S_In + "' into an double", ktERR.WRONGTYPE);
                    }
                }
                ktDebug.Log("D_In: " + D_In.ToString());
                D_Out = Delegate(D_In);
                ktDebug.Log("D_Out: " + D_Out.ToString());

                return new ktValue("return", "double",
                                   kacTalk.Main.MakeObjectOf("double", D_Out),
                                   true, true);
            };
        }
예제 #2
0
        public ktList Split(ktString Input, int Count)
        {
            // If we have a pattern?!
            if ((m_Pattern.IsEmpty()) || (m_P == null))
            {
                // Set error (throw exeption??) and return false
                throw new ktError("ktRegExp::Split(): The pattern is not set!", ktERR.NOTSET);
            }

            ktList Parts = new ktList();
            string[] OrgParts = m_P.Split(Input, Count);

            foreach (string Part in OrgParts)
            {
                Parts.Add(new ktString(Part));
            }

            return Parts;
        }
예제 #3
0
        public override ktList GetMethods()
        {
            ktList List = new ktList();

            List.Add("Export", CreateObject("0"));

            return List;
        }
예제 #4
0
        /// <summary>
        /// Parse the tokens and create a tree /*(mainly for operators)**/
        /// </summary>
        protected ktList MakeATree(ktList List, bool FirstRun, bool ThirdRun)
        {
            //ktDebug.Log( "MAT!" + List.Get_R( "\t", true ));
            ktList Tree = new ktList(), Prev = null, Next = null, Temp = null, Temp2 = null;
            ktToken Token = null, PrevToken = null;

            /*/ 	If the list are the "root"
            if (List == m_LineStack) {
                Tree.Node = new ktNode( "ktTBlock",  new ktToken( ktTokenType.Block, "", 0, 0 )  );
            } else/**/
            {
                Tree.Node = List.Node;
            }

            List.Reset();
            foreach (ktList L in List)
            {
                //ktDebug.Log( "\nLLL: "+ List.Get_R() /*+ " :" +L.Export() */);
                if ((L.Node != null) && (L.Node.Value != null))
                {
                    Token = (ktToken)L.Node.Value;
                    Prev = Tree.Last;
                    if ((Prev != null) && (Prev.Node != null) && (Prev.Node.Value != null))
                    {
                        PrevToken = (ktToken)Prev.Node.Value;
                    }
                    else
                    {
                        PrevToken = new ktToken(ktTokenType.NULLTOKEN, ":null", 0, 0);
                    }
                }
                else
                {
                    continue;
                }
            //#if ParseDebug
            ktDebug.Log( "TT:" + Token.Type.ToString() + "(" + Token.Value + ")" );
            ktDebug.Log( "PTT:" + PrevToken.Type.ToString() + "(" + PrevToken.Value + ")" );
            //#endif
                if ((FirstRun) && (
                        ((PrevToken.Type == ktTokenType.Const) || (PrevToken.Type == ktTokenType.Hard)) ||
                        ((Token.Type == ktTokenType.Const) ||  (Token.Type == ktTokenType.Hard)) ||
                        ((PrevToken.Type == ktTokenType.VarStatement) && (
                            (Token.Type != ktTokenType.AssignmentOperator)/* ||
                            (Token.Type != ktTokenType.)*/
                         )
                        )))
                {
                    //ktDebug.Log( "CONSTCONSTCONSTCONSTCONSTCONSTCONSTCONSTCONST" );
                    if (PrevToken.Type == ktTokenType.VarStatement)
                    {
                        if ((Token.Type != ktTokenType.Id) && (Token.Type != ktTokenType.Hard) &&
                            (Token.Type != ktTokenType.Const))
                        {
                            throw new ktError("Unexpected " + Token.Name + " on line " + Token.LineNo.ToString() +
                                              " by character " + Token.CharPos.ToString() + "!", ktERR.UNEXP);
                        }
                        Prev.Add(Token);
                    }
                    else
                    {
                        Temp = new ktList();
                        Temp.Node = new ktNode("ktTVarStatement", new ktToken(ktTokenType.VarStatement, "", Token.LineNo, Token.CharPos));

                        Temp.Add(Token);
                        //						Temp.AddList( MakeATree( L, FirstRun ) );

                        Tree.AddList(Temp);
                    }
                }
                else if (
                   (
                      ((PrevToken.Type == ktTokenType.Id) && (ThirdRun)) ||
                      ((/*!*/FirstRun) && (
                          (PrevToken.Type == ktTokenType.CompStatement) &&
                              (
                                  (PrevToken.Value == ".") ||
                                  (PrevToken.Value == "::")
                              )
                      )
                   )) && (
                      (Token.Type == ktTokenType.List) ||
                      (Token.Type == ktTokenType.Statement) ||
                    //				    	(Token.Type == ktTokenType.CompStatement) ||
                      (Token.Type == ktTokenType.Const) ||
                      (Token.Type == ktTokenType.Hard) ||
                      (Token.Type == ktTokenType.String) ||
                      (Token.Type == ktTokenType.Boolean) ||
                      (Token.Type == ktTokenType.Number) ||
                      (Token.Type == ktTokenType.Float) ||
                      (Token.Type == ktTokenType.Null) ||
                      (Token.Type == ktTokenType.Id) // ?? Not shure on how this actually will work!?
                   ))
                {
                    ktDebug.Log("Tree:" + Tree.Get_R());
                    Tree.Pop(false);
             /*   ktDebug.Log("TToxen:" + Token.ToString());
            ktDebug.Log("PToxen:" + PrevToken.ToString());*/
                    if ( (PrevToken.Value == ".") ) {
                        if (Token.Type == ktTokenType.List)
                        {
                            Temp = MakeATree(L, FirstRun, ThirdRun);

                            Prev.Last.AddList(Temp);
                        }
                        else
                        {
                            Prev.Last.AddList(new ktList(L));
                        }
                        ((ktToken)Prev.Node.Value).Value = "§";
                        Tree.AddList(Prev);

                        //((ktToken)Tree.Node.Value).Value = "§";

                        ktDebug.Log("Tree:" + Tree.Get_R());
                    }
                    else if ((Prev.Prev == null) || ((Prev.Prev.Node != null) && (Prev.Prev.Node.Value != null) && (((ktToken)Prev.Prev.Node.Value).Type != ktTokenType.Id)))
                    {
                        Temp = new ktList();
                        Temp.Node = new ktNode("ktCompStatement", new ktToken(ktTokenType.CompStatement, "F", PrevToken.LineNo, PrevToken.CharPos));

                        if (Token.Type == ktTokenType.List)
                        {
                            Temp2 = MakeATree(L, FirstRun, ThirdRun);
                            //Temp2.Node.Value = Token;

                            Temp.AddList(Prev);  // Add function name
                            Temp.AddList(Temp2); // Add arguments
            #if ParseDebug
                            ktDebug.Log("FComp_List:\n" + Temp.Get_R());
                            ktDebug.Log("FComp_List T2:\n" + Temp2.Get_R());
            #endif
                        }
                        else
                        {
                            Temp.AddList(Prev); // Add function name
                            Temp.AddList(new ktList(L));    // Add argument
            #if ParseDebug
                            ktDebug.Log("FComp_NoList:\n" + Temp.Get_R());
            #endif
                        }
                        Tree.AddList(Temp);
            #if ParseDebug
                        ktDebug.Log("FComp Tree:\n" + Tree.Get_R());
            #endif
                    }
                    else
                    {
                        Temp = new ktList();
                        Temp.Node = new ktNode("ktTCompStatement", new ktToken(ktTokenType.CompStatement, "§", PrevToken.LineNo, PrevToken.CharPos));
            //ktDebug.Log("§§§§§§§§§§§§§§§§§§§§§§§§§§§");
                        if (Token.Type == ktTokenType.List)
                        {
                            Temp2 = new ktList();
                            Temp2.Node = new ktNode("ktTCompStatement", new ktToken(ktTokenType.CompStatement, ".", PrevToken.LineNo, PrevToken.CharPos));

                            Temp2.AddList(Prev.Last); // Add Method
                            Temp2.AddList(MakeATree(L, FirstRun, ThirdRun)); // Add Arguments

                            Temp.AddList(Prev.First); // Add object
                            Temp.AddList(Temp2); // Add Method call

                            Tree.AddList(Temp);
            #if Debug
                            ktDebug.Log("L_Temp2:\n" + Temp2.Get_R());
                            ktDebug.Log("L_Temp:\n" + Temp.Get_R());
                            ktDebug.Log("L_Tree:\n" + Tree.Get_R());
            #endif
                        }
                        else
                        {
                            //Temp.AddList(Prev);
                            //Temp.AddList(MakeATree(L, FirstRun, ThirdRun));
                            Prev.AddList(MakeATree(L, FirstRun, ThirdRun));
                           /* ktDebug.Log("Temp:" + Temp.Get_R());
                            ktDebug.Log("Prev:" + Prev.Get_R());*/
                            //  Tree.AddList(Temp);
                            Tree.AddList(Prev);
                            //ktDebug.Log("Tree:" + Tree.Get_R());
                        }
                    }
                }
                else if (
                          (ThirdRun) &&
                          (
                              (PrevToken.Type == ktTokenType.Id) ||
                              (PrevToken.Type == ktTokenType.CompStatement)
                          ) &&
                          (Token.Type == ktTokenType.CompStatement)
                   )
                {
            #if ParseDebug2
            ktDebug.Log( "PREV:"+ PrevToken.Value );
            ktDebug.Log( "LLLL:"+ L.Get_R() );
            ktDebug.Log( "NNN:"+ ((Next == null) ? "nulL" : Next.Get_R().ToString()) );
            #endif
                    Tree.Remove((uint)(Tree.Count - 1));

                    Temp = new ktList();
                    Temp.Node = new ktNode("ktTCompStatement", new ktToken(ktTokenType.CompStatement, "$", PrevToken.LineNo, PrevToken.CharPos));

                    Temp.AddList(Prev);
                    Tree.AddList(new ktList(L));

                    Tree.AddList(Temp);
                }
                else if ((Token.Type == ktTokenType.Line) ||
                  (Token.Type == ktTokenType.List) ||
                  (Token.Type == ktTokenType.Statement) ||
                  (Token.Type == ktTokenType.RunStatement) /*||
                    (Token.Type == ktTokenType.New)*/)
                {
            #if ParseDebug2
            ktDebug.Log( "\nMAT::MAT::MAT::MAT::MAT::MAT::MAT::MAT::\n" +L.Get_R() );
            #endif
                    /*if ((!FirstRun) && (PrevToken != null) && (
                        (PrevToken.Type == ktTokenType.Id) ||
                        (PrevToken.Type == ktTokenType.CompStatement) ||
                        (PrevToken.Type == ktTokenType.Statement))) {
                            Tree.Remove( (uint)(Tree.Count - 1) );

                            Temp = new ktList( );
                            Temp.Node = new ktNode( "ktTCompStatement", new ktToken( ktTokenType.CompStatement, Token.Value, Token.LineNo, Token.CharPos ) );

                            Temp.AddList( Prev );
                            Temp.AddList( MakeATree( L, FirstRun ) );

                            Tree.AddList( Temp );
                    } else {*/
                    Tree.AddList(MakeATree(L, FirstRun, ThirdRun));
                    /*}*/
                }
                else if ((Token.Type == ktTokenType.Separator) && FirstRun)
                {
                    Prev = L.Prev;
                    Next = L.Next;

                    //					Tree.Remove( (uint)(Tree.Count - 1) );

                    if ((Next != null) && (Next.Node != null) && (Next.Node.Value != null) &&
                            (Token.Value == ":") &&
                             (((ktToken)Next.Node.Value).Type == ktTokenType.Null))
                    {
                        Token = (ktToken)Next.Node.Value;
                        Token.Value = ":null";

                        Tree.Add("ktTNull", Token);

                        List.MoveNext();
                        List.MoveNext();
                        continue;
                    }
            #if ParseDebug2
            ktDebug.Log( "MakeATree calling HandleSep!" );
            #endif
                    HandleSep(Prev, Next, ref Tree, Token);

                    List.MoveNext();
                }
                else if ((FirstRun) && (
                          (Token.Type == ktTokenType.Operator) ||
                          (Token.Type == ktTokenType.AssignmentOperator)
                      ) && (
                          (PrevToken.Type == ktTokenType.Operator) ||
                          (PrevToken.Type == ktTokenType.AssignmentOperator)
                      ))
                {
                    if ( (Token.Value == "=") || (PrevToken.Value == "=") )
                    {
                        switch (PrevToken.Value)
                        {
                            case "=": case "!": case ">": case "<":
                                {
                                    //   PrevToken.Value = "==";
                                    if (PrevToken.Value == "=")
                                        PrevToken.Value.Append(Token.Value);
                                    else
                                        PrevToken.Value.Append("=");
                                    PrevToken.Type = ktTokenType.ComparisonOperator;
                                    Prev.Node.Name = PrevToken.Name = ktToken.TokenToString(ktTokenType.ComparisonOperator);
                                    break;
                                }
                            case "+": case "-": case "*": case "/": case "%":
                            case "&": case "|": case "~": case "^":
                                {
                                    if (PrevToken.Value == "=")
                                        PrevToken.Value.Append(Token.Value);
                                    else
                                        PrevToken.Value.Append("=");
                                    PrevToken.Type = ktTokenType.AssignmentOperator;
                                    Prev.Node.Name = PrevToken.Name = ktToken.TokenToString(ktTokenType.AssignmentOperator);
                                    break;
                                }
                            default:
                                {
                                    throw new ktError("Unknown operator '" + PrevToken.Value + Token.Value +
                                                        "' on line " + PrevToken.LineNo.ToString() +
                                                        " by character " + PrevToken.CharPos.ToString() + "!",
                                                      ktERR.UNKNOWN);
                                }
                        }
                    } else if ( PrevToken.Value == Token.Value )
                    {
                        PrevToken.Value.Append(Token.Value);
                        PrevToken.Type = ktTokenType.Operator;
                        Prev.Node.Name = PrevToken.Name = ktToken.TokenToString(ktTokenType.Operator);
                    }
                    else
                    {
                        throw new ktError("Unknown operator '" + PrevToken.Value + Token.Value +
                                            "' on line " + PrevToken.LineNo.ToString() +
                                            " by character " + PrevToken.CharPos.ToString() + "!",
                                          ktERR.UNKNOWN);
                    }
                    /*Prev = L.Prev;
                    Next = L.Next;

                    HandleSep( Prev, Next, ref Tree, Token );

                    List.MoveNext();*/
                }
                else if ((ThirdRun) && (PrevToken.Type == ktTokenType.New))
                {
            #if ParseDebug2
            ktDebug.Log( "NEW_PREV:"+ PrevToken.Value );
            ktDebug.Log( "NEW_LLLL:"+ L.Get_R() );
            ktDebug.Log( "NEW_NNN:" + ((Next == null) ? "null" : Next.Get_R().ToString()) );
            #endif
                    Tree.Remove((uint)(Tree.Count - 1));

                    Temp = new ktList();
                    Temp.Node = new ktNode("ktTNewStatment", PrevToken);

                    //					Temp.AddList( Prev );
                    Temp.AddList(GetTheRest(List));

                    Tree.AddList(Temp);
                    break;
                }
                else
                {
                    Tree.AddList(new ktList(L));
                }
            }
            //#if ParseDebug2
            ktDebug.Log( "+++++++++++++++++++++" + Tree.Get_R( "\t", true ) + "-----------__");
            //#endif

            if (List == m_LineStack)
            {
                Tree = MakeATree(Tree, false, false);
            }

            return Tree;
        }
예제 #5
0
        public ktList GetArguments(ktList Arguments)
        {
            ktList Args = null;
            ktToken Token = null;

#if Debug
	ktDebug.Log( "GAGAGAGAGAGA:" + Arguments.Get_R( "\t", true ) );
#endif
            if (Arguments == null)
            {
                return null;
            }
            else if ((Arguments.Node != null) && (Arguments.Node.Value != null) &&
                      ((Token = (ktToken)(Arguments.Node.Value)).Type == ktTokenType.CompStatement))
            {
                Args = new ktList();

                Args.Add(HandleCompound(Arguments).CopyValue());

                return Args;
            }
            else if (((Arguments.First != null) && (Arguments.First.Node != null) &&
                       (Arguments.First.Node.Value != null) &&
                       ((Token = (ktToken)(Arguments.First.Node.Value)).Type == ktTokenType.CompStatement)
                      ) || ((Arguments.Node != null) && (Arguments.Node.Value != null) &&
                       ((Token = (ktToken)(Arguments.Node.Value)).Type == ktTokenType.CompStatement)
                      )
                    )
            {
#if Debug
	ktDebug.Log( "GACSGACSGACSGACSGACSGACS:" + Arguments.Get_R() );
#endif
                ktList Stat = new ktList();
                Stat.Node = new ktNode("ktStatement", new ktToken(ktTokenType.Statement, "ktStatement",
                                            Token.LineNo, Token.CharPos));

                Args = new ktList();
                Args.Node = new ktNode("ktList", new ktToken(ktTokenType.List, "ktList", Token.LineNo,
                                            Token.CharPos));
                Args.AddList(Stat);
                Stat.AddList(Arguments);

                return GetArguments(Args);
            }
#if Debug
	ktDebug.Log( "gQAGAGAGAGAGAGAGGAgA::::" + Arguments.Get_R( "\t", true )  );
#endif

            if (Arguments.GetCount() == 0)
            {
                if (Arguments.Node == null)
                {
                    return null;
                }

                Token = (ktToken)Arguments.Node.Value;
                //ktDebug.Log( "GAGA111:" + Token.Name );
                Args = new ktList();

                if (Token.Type == ktTokenType.RunStatement)
                {
                    Args.Add(TokenToValue(Token, null));
                }
                else
                {
                    Args.Add(TokenToValue(Token, null).CopyValue());
                }

            }
            else
            {
                ktValue Value = null;

                Arguments.Reset();
                foreach (ktList L in Arguments)
                {
                    if ((L.Node == null) || (L.Node.Value == null))
                    {
                        continue;
                    }

                    if (Args == null)
                    {
                        Args = new ktList();
                    }

                    Token = (ktToken)L.Node.Value;

                    if (Token.Type == ktTokenType.RunStatement)
                    {
                        Value = TokenToValue(Token, L);
                    }
                    else
                    {
                        Value = TokenToValue(Token, L).CopyValue();
                    }

                    if (Value != null)
                    {
                        Args.Add(Value);
                    }
                }
            }
#if Debug
	ktDebug.Log( "EOFGA" );
	ktDebug.Log( Args.Get_R( "\t", true ) );
#endif
            return Args;
        }
예제 #6
0
        public virtual ktList GetMethods()
        {
            ktList Methods = new ktList();

            if (m_Methods == null)
            {
                return Methods;
            }

            m_Methods.Reset();
            foreach (ktList ML in m_Methods)
            {
                if ((ML.Node == null) || (ML.Node.Value == null))
                {
                    continue;
                }

                Methods.Add(ML.Node.Name, ((ktFunction)ML.Node.Value).Return);
            }

            Methods.Add("Export", CreateObject());

            return Methods;
        }
예제 #7
0
 public void CopyTo(ktList List)
 {
     foreach (ktRE_Group G in this)
     {
         List.Add(G);
     }
 }
예제 #8
0
        protected ktValue HandleAssignment(ktToken Operator, ktList ListTarget, ktToken Target, ktValue AssValue)
        {
            ktValue Value = ktValue.Null;
            ktClass Class = null;
            ktList Args = null;

            AssValue.HardType = false;
#if Debug
	ktDebug.Log(  "ASSSVALUE:" + Value.Export() + ";" );
#endif

            try
            {
                if (Target.Type == ktTokenType.Id)
                {
                    Value = GetVariable(Target.Value);
                }
                else if (Target.Type == ktTokenType.CompStatement)
                {
                    Value = HandleCompound(ListTarget);
#if Debug
	ktDebug.Log(  "ASCP:" + Value.Export() + ";" );
#endif
                }
            }
            catch (ktError Err)
            {
                if ((Err.ErrorNumber == ktERR.NOTFOUND) || (Err.ErrorNumber == ktERR.NOTDEF))
                {
                    if (Operator.Value == "=")
                    {
                        SetVariable(Target.Value, true, AssValue, true, true);
                        return AssValue;
                    }
                    else
                    {
                        throw new ktError("You can't use a complex assignment operator (" +
                                            Operator.Value + ") when creating a variable!",
                                          ktERR.UNEXP);
                    }
                }
                else
                {
                    throw Err;
                }
            }
            //ktDebug.Log("HARD:" + Value.HardType);
            if (Value.Constant)
            {
                throw new ktError("You are trying to assign a value to " + Target.Value +
                                    ", which is a constant!", ktERR.CONST);
            }
            else if (Value.IsNull())
            {
                SetVariable(Target.Value, true, AssValue, true, true);
                return AssValue;
            }/*
            else if (!Value.HardType)
            {
                SetVariable(Target.Value, Value = AssValue, true, true);
            }*/

            try
            {
                Class = (ktClass)Value.Value;

                Args = new ktList();
                Args.Add(AssValue);

                ArrayList MethNames = new ArrayList(4);
                MethNames.Add("operator" + Operator.Value);
                MethNames.Add("op" + Operator.Value);

                switch (Operator.Value)
                {
                    case "=":
                        {
                            MethNames.Add("_assign");
                            break;
                        }
                    case "+=":
                        {
                            MethNames.Add("_append");
                            break;
                        }
                }

                foreach (ktString Meth in MethNames)
                {
                    try
                    {
                        Value.SetTheValue(Class.RunMethod(Meth, Args));
#if Debug
	ktDebug.Log( "AFTREEEEOP:" + Value.Export() + ";" );
#endif
                        return Value;
                    }
                    catch (ktError Err)
                    {
//                        ktDebug.Log("ASSMETHERR:" + Err.ToString());
                        if ((Err.ErrorNumber != ktERR._404) &&
                            (!Err.Message.StartsWith("Couldn't find the method ")))
                        {
                            throw Err;
                        }
                    }
                }
                throw new ktError("-", ktERR._404);
            }
            catch (ktError Err)
            {
#if Debug
	ktDebug.Log( "##############" + Err.ToString() + "\n" + Err.StackTrace );
#endif
                if (Err.ErrorNumber != ktERR._404)
                {
                    throw Err;
                }
                else if (Operator.Value == "=")
                {
                    SetVariable(Target.Value, Value = AssValue, true, true);
                }
                else if (Class == null)
                {
                    throw new ktError("The variable '" + Target.Value + "' is not set", ktERR.NOTSET);
                }
                else
                {
                    throw new ktError("The operator '" + Operator.Value + "' isn't handled by the class " +
                                        Class.Name, ktERR.NOTIMP);
                }
            }

            return Value;
        }
예제 #9
0
 public void CopyTo(ktList List)
 {
     foreach (ktRE_Capture G in this)
     {
         List.Add(G);
     }
 }
예제 #10
0
        public ktRE_GroupCollection(ktRE_GroupCollection GC)
            : this()
        {
            m_List = new ktList();

            foreach (ktRE_Group G in GC)
            {
                m_List.Add(G);
            }
        }
예제 #11
0
        public ktRE_CaptureCollection(ktRE_CaptureCollection GC)
            : this()
        {
            m_List = new ktList();

            foreach (ktRE_Capture G in GC)
            {
                m_List.Add(G);
            }
        }
예제 #12
0
        /// <summary>
        /// Scan/Parse the script into tokens
        /// </summary>
        public bool Scan(ktString Script, ktString Name)
        {
            bool Ret = true;

            Script.Replace("\r\n", "\n",true);

            // Initiate the lineno.
            m_CurLine = 1;
            m_CharPos = 1;
              // .. and name
            m_Name = Name;

            if (m_Tokens != null)
            {
                m_Tokens.Clear();
                m_Tokens = null;
            }

            // Init...
            ktString Line = new ktString();
            int Pos = 0;
            Script.Trim();
            ktRegEx RE = new ktRegEx(ktToken.Separators);

            // ... the block-stack
            if (m_BlockStack != null)
            {
                m_BlockStack.Clear();
                m_BlockStack = null;
            }
            m_BlockStack = new ktList();
            // ... the token-list
            if (m_Tokens != null)
            {
                m_Tokens.Clear();
                m_Tokens = null;
            }
            m_Tokens = new ktList();
            m_Tokens.Node = new ktNode("ktTStatement", new ktToken(ktTokenType.Statement, "", 0, 0));
            // ... the token-stack
            if (m_TokenStack != null)
            {
                m_TokenStack.Clear();
                m_TokenStack = null;
            }
            m_TokenStack = new ktList();
            // ... the lines (??)
            if (m_Lines != null)
            {
                m_Lines.Clear();
                m_Lines = null;
            }
            m_Lines = new ktList();
            m_Lines.Node = new ktNode(Name, m_CurToken = new ktToken(ktTokenType.Program, Name, 0, 0));
            // ... the line-stack
            if (m_LineStack != null)
            {
                m_LineStack.Clear();
                m_LineStack = null;
            }
            m_LineStack = new ktList();

            if (m_MainBlock == null)
            {
                m_MainBlock = new ktBlock(new ktList());
            }
            else
            {
                if (m_MainBlock.Lines != null)
                {
                    m_MainBlock.Lines.Clear();
                }
                m_MainBlock.ClearKTSymbols();
            }
            m_MainBlock.SetMain(this);

            m_BlockStack.Add("ktTBlock", m_MainBlock);

            /* In the "original scanner" (C++), we took one line (terminated by \n)
             * 	at a time and worked on, now we just go through the line.
             * And We hope that it will be much "leaner"!
             */
            // Go on until the there's nothing left...
            while (!Script.IsEmpty())
            {
                // Get the position for the next separator
                Pos = RE.Find(Script);

                // If there was none...
                if (Pos < 0)
                {
                    // Take it to the end...
                    Pos = Script.Len();
                }
                else if (Pos == 0)
                {
                    Pos++;
                }
                // Get the next "token"
                Line = Script.SubStr(0, Pos);

                // If it's the start of a comment
                if ((Line == "/") && (Script.StartsWith("//") || Script.StartsWith("/*")))
                {
                    Line = Script.SubStr(0, 2);
                    Pos++;
                }
                else if ((Line == "*") && (Script.StartsWith("*/")))
                {
                    Line = "*/";
                    Pos++;
                }

                ReactOnToken(Line, m_CurLine, ref m_CharPos);

                if (Line == "\n")
                {
                    m_CurLine++;
                    m_CharPos = 1;
                }
                else
                {
                    m_CharPos += Line.Len();
                }

                // Remove the "token", we just worked on...
                Script.Remove(0, Pos);
            }

            #if ParseDebug || DebugXML
            ktDebug.Log("XML111111:");
            ktDebug.Log(ktXML.FromList(m_TokenStack).AsXML());
            ktDebug.Log("==================");
            ktDebug.Log(ktXML.FromList(m_Tokens).AsXML());
            #endif
            if (!m_Tokens.IsEmpty())
            {
                if (m_AllowMissingEOL)
                {
                    ((ktToken)m_Tokens.Node.Value).Type = ktTokenType.Line;
                    ((ktToken)m_Tokens.Node.Value).Name = m_Tokens.Node.Name = "ktTLine";
                    m_LineStack.AddList(m_Tokens);

                    m_Tokens = null;
                }
                else
                {
                    throw new ktError("Expected a ktTEOL at line " + m_CurLine.ToString() + " but didn't find one!",
                                      ktERR.MISSING);
                }
            }
            if (m_BlockStack.Count > 1)
            {
                throw new ktError("Expecting ktTEOB (}) at " + m_CharPos.ToString() + ", line " +
                                    m_CurLine.ToString() + ".", ktERR.MISSING);
            }

            //ktToken.OnlyExportValue = false;
            //ktDebug.Log( m_LineStack.Get_R(  ) );
            //ktToken.OnlyExportValue = false;
            #if ParseDebug || DebugXML
            ktDebug.Log( "XML:" );
            ktDebug.Log( ktXML.FromList(m_LineStack).AsXML() );
            ktDebug.Log( "==================" );
            ktDebug.Log( ktXML.FromList(m_Lines).AsXML() );
            #endif

            /*			ktDebug.Log( "?+++++\n" + m_Tokens.Get_R( "\t", true ) );
            ktDebug.Log( "?+++++\n" + m_CurToken.Export
            if (m_CurToken != null) {
                if (m_CurToken.Type == ktTokenType.List) {
                    throw new ktError( "Expected a ktTEndPar at line " + m_CurLine.ToString() + " but didn't find one!",
                                      ktERR.MISSING );
                } else if (m_CurToken.Type == ktTokenType.String) {
                    throw new ktError( "Expected a ktTStringQuot at line " + m_CurLine.ToString() + " but didn't find one!",
                                      ktERR.MISSING );
                } else if (m_CurToken.Type == ktTokenType.Block) {
                    throw new ktError( "Expected a ktTEndOfBlock at line " + m_CurLine.ToString() + " but didn't find one!",
                                      ktERR.MISSING );
                }
            } else if ((m_Tokens != null) && (!m_Tokens.IsEmpty())) {
                throw new ktError( "Expected a ktTEOL at line " + m_CurLine.ToString() + " but didn't find one!",
                                  ktERR.MISSING );
            }
            */
            //			MakeATree( );
            //			MakeAOpTree( );

            if ((m_BlockStack == null) || (m_BlockStack.IsEmpty()))
            {
                return Ret;
            }

            ktBlock Block = (ktBlock)(m_BlockStack.Pop().Node.Value);
            #if ParseDebug
            ktDebug.Log( "BLOCK1:" + ktXML.FromList(Block.Lines).AsXML() );r
            #endif

            MakeATree();
            #if ParseDebug
            ktDebug.Log("BLOCK2:" + ktXML.FromList(m_Lines).AsXML());
            #endif
            Block.Lines = MakeAOpTree();

            m_LineStack.Clear();
            m_LineStack = null;

            // Add Current "statement"/"post" to the block and theń switch them
            //Temp.AddList( m_Tokens );
            //m_Tokens = Temp;
            #if ParseDebug
            ktDebug.Log( "BLOCK:" + ((Block == m_MainBlock) ? "MAIN":"NOT_MAIN") );
            #endif
            /*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 ) );*/
            #if ParseDebug || DebugXML
            ktDebug.Log( "XML_After Tree:" + ktXML.FromList(Block.Lines).AsXML() );
            ktDebug.Log( "XML_After Tree:" + Block.Lines.Get_R( "\t", true ) );
            #endif
            //ktDebug.Log( m_Lines.Export() );

            return Ret;
        }
예제 #13
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;
        }
예제 #14
0
        protected void CallCallback(ktString Method)
        {
            if (Method.IsEmpty() || (m_Callback == null))
            {
                return;
            }

            ktList Args = new ktList();
            Args.Add("Name", Name);
            Args.Add("Value", this);

            try
            {
                m_Callback.RunMethod("_" + Method, Args);
            }
            catch (ktError Err)
            {
                if ((Err.ErrorNumber != ktERR._404) && (Err.ErrorNumber != ktERR.NOTDEC))
                {
                    throw Err;
                }
            }
        }
예제 #15
0
        public static ktList Replace(ktString Pattern, ktString Haystack, ktRE_MatchEvaluator[] Evaluators)
        {
            ktList Results = new ktList();

            foreach (ktRE_MatchEvaluator Evaluator in Evaluators)
            {
                Results.Add(Replace(Pattern, Haystack, Evaluator));
            }

            return Results;
        }
예제 #16
0
        protected ktValue HandleOperator(ktList OpTree)
        {
            ktValue Value = ktValue.Null;
            ktValue SecondValue = ktValue.Null;

            if (OpTree == null)
            {
                ktDebug.Log("NOOOOOOOHO!!O!!!!!!!");
                return Value;
            }
#if Debug
	ktDebug.Log( "HOHOHOHOHOHOHOHOHOHOHOHOHOHO:" + OpTree.Get_R( " ", true ) );
#endif

            ktToken Op = null;
            ktToken First = null;
            ktToken Last = null;

            if ((OpTree.Node != null) && (OpTree.Node.Value != null))
            {
                Op = (ktToken)(OpTree.Node.Value);
            }
            if ((OpTree.First != null) && (OpTree.First.Node != null))
            {
                if (OpTree.First.Node.Value is ktToken)
                {
                    First = (ktToken)(OpTree.First.Node.Value);
                }
                else if (OpTree.First.Node.Value is ktValue)
                {
                    Value = (ktValue)(OpTree.First.Node.Value);
                    First = new ktToken(ktTokenType.NULLTOKEN, "Value", 0, 0);
                }
            }
            if ((OpTree.Last != null) && (OpTree.Last.Node != null))
            {
                Last = (ktToken)(OpTree.Last.Node.Value);
            }

            if ((First == null) && (Last == null) || (Op == null))
            {
                return Value;
            }

            if (Op.Type == ktTokenType.Operator)
            {
                ktClass Class = null;

                if (First.Type == ktTokenType.NULLTOKEN)
                {
                    //ktDebug.Log( "000000000000000000000000000000" );
                }
                else
                {
                    Value = TokenToValue(First, OpTree.First);
                }

                SecondValue = TokenToValue(Last, OpTree.Last);

                if (Value.IsNull() || SecondValue.IsNull())
                {
                    throw new ktError("The " + (Value.IsNull() ? "left-hand" : "right-hand") +
                                  " operand can not be a null value!", ktERR.NULL);
                }

                try
                {
                    //ktDebug.Log("111:" + ((ktClass)Value.Value).Name + "!!!!222:" + ((ktClass)SecondValue.Value).Name + "#####");
                    Class = (ktClass)(Value.Value);
                    //ktDebug.Log( "?????????????????????????????????????" );
                    ktList Args = null;
                    if (((ktClass)SecondValue.Value).Name == "ktList")
                    {
                        ktDebug.Log("!!!!!!!!!");
                        Args = (ktList)(((ktClass)SecondValue.Value).GetProperty("___").Value);
                    }
                    else
                    {
                        Args = new ktList();
                        Args.Add(SecondValue);
                    }

                    ArrayList MethNames = new ArrayList(4);
                    MethNames.Add("operator" + Op.Value.ToString());
                    MethNames.Add("op" + Op.Value.ToString());

                    switch (Op.Value)
                    {
                        case "+":
                            {
                                MethNames.Add("_add");
                                break;
                            }
                        case "-":
                            {
                                MethNames.Add("_subtract");
                                break;
                            }
                        case "*":
                            {
                                MethNames.Add("_multiply");
                                MethNames.Add("_times");
                                break;
                            }
                        case "/":
                            {
                                MethNames.Add("_divide");
                                break;
                            }
                        case "^":
                            {
                                if (kacTalk.Main.MathMode)
                                {
                                    MethNames.Add("_pow");
                                }
                                break;
                            }
                    }

                    foreach (string Meth in MethNames)
                    {
                        try
                        {
                            Value = Class.RunMethod(Meth, Args);
#if Debug
ktDebug.Log( "AFTREOP:" + Value.Export() + ";" );
#endif
                            return Value;
                        }
                        catch (ktError Err)
                        {
//                            ktDebug.Log("ASSMETHERR:" + Err.ToString());
                            if ((Err.ErrorNumber != ktERR._404) &&
                                (!Err.Message.StartsWith("Couldn't find the method ")))
                            {
                                throw Err;
                            }
                        }
                    }
                    throw new ktError("-", ktERR._404);
                }
                catch (ktError Err)
                {
#if Debug
ktDebug.Log( "##############" + Err.ToString() + "\n" + Err.StackTrace ); //ktDebug.Log
#endif
                    if (Err.ErrorNumber != ktERR._404)
                    {
                        throw Err;
                    }
                    else if (Class == null)
                    {
                        throw new ktError("The variable '" + Value.Value + "' is not set", ktERR.NOTSET);
                    }
                    else if (Class.Name == "ktList")
                    {
                        ktList L = new ktList();
                        L.Node = OpTree.Node;
                        ktDebug.Log("HANDLELIST:::" + ((ktList)(Class.GetProperty("_FirstObject").Value)).Get_R() + " ==");
                        L.AddList((ktList)(Class.GetProperty("_FirstObject").Value));
                        L.AddList(OpTree.Last);

                        return HandleOperator(L);
                    }
                    else
                    {
                        throw new ktError("The operator '" + Op.Value + "' isn't handled by the class " +
                                            Class.Name, ktERR.NOTIMP);
                    }
                }

                //	ktDebug.Log( "111:" + Value.Value.Export( ) + "!!!!222:" + SecondValue.Value.Export() + "#####" );
                //break;
            }
            else if (Op.Type == ktTokenType.AssignmentOperator)
            {
                Value = TokenToValue(Last, OpTree.Last);
                if ((First.Type == ktTokenType.Id) || (First.Type == ktTokenType.CompStatement))
                {
                    Value.Constant = false;
#if Debug
ktDebug.Log( "HO:AO: " + First.Value + ";" );
#endif
                    Value = HandleAssignment(Op, OpTree.First, First, Value);
                    //						SetVariable( First.Value, Value, true, true );
                }
                else if (First.Type == ktTokenType.VarStatement)
                {
                    if (Op.Value != "=")
                    {
                        throw new ktError("Expected a simple assignment operator (=) on line " + Op.LineNo.ToString() +
                                          " by character " + Op.CharPos.ToString() + ", but found " +
                                          Op.Value + "!", ktERR.UNEXP);
                    }
                    Value = HandleCompAssignment(OpTree.First, Value);
                }
                else
                {
                    throw new ktError("Can't assign a value to an " + First.Name + " (" + First.Value + ") on line " + Op.LineNo.ToString() +
                                        ", character " + Op.CharPos.ToString() + "!", ktERR.UNKNOWN);
                }

                //break;
            }
            else
            {
                throw new ktError("Unrecognized operator (" + Op.Value + ") on line " + Op.LineNo.ToString() +
                                    ", character " + Op.CharPos.ToString() + "!", ktERR.UNKNOWN);
            }
#if Debug
	ktDebug.Log( "END OF HO!!!" );
#endif
            return Value;
        }
예제 #17
0
        public static ktList Replace(ktString Pattern, ktString Haystack, ktString[] Replacements)
        {
            ktList Results = new ktList();

            foreach (ktString Replacement in Replacements)
            {
                Results.Add(Replace(Pattern, Haystack, Replacement));
            }

            return Results;
        }
예제 #18
0
        protected ktValue HandleCompAssignment(ktList Target, ktValue Value)
        {
            bool Constant = false, Hard = false;
            ktString Name = new ktString(), ClassName = null;
            ktToken Token = null;
            ktClass Class = null;
#if Debug
	ktDebug.Log( "HCAHCAHCAHCAHCAHCAHCAHCAHCAHCAHCAHCAHCAHCA" );
#endif
            ktDebug.Log("HCA: V:" + Value.Export() + "\n" + Target.Get_R());
            Target.Reset();
            foreach (ktList L in Target)
            {
                if ((L.Node == null) || (L.Node.Value == null))
                {
                    continue;
                }

                Token = (ktToken)L.Node.Value;
#if Debug
	ktDebug.Log( "HCA:" + Token.ToString() );
#endif

                switch (Token.Type)
                {
                    case ktTokenType.Const:
                        {
                            if (Constant)
                            {
                                throw new ktError("Unexpected " + Token.ToString() + " on line " +
                                                      Token.LineNo.ToString() + " by character " +
                                                      Token.CharPos.ToString() + "!",
                                                  ktERR.UNEXP);
                            }

                            Constant = true;
                            break;
                        }
                    case ktTokenType.Hard:
                        {
                            if (Hard)
                            {
                                throw new ktError("Unexpected " + Token.ToString() + " on line " +
                                                      Token.LineNo.ToString() + " by character " +
                                                      Token.CharPos.ToString() + "!",
                                                  ktERR.UNEXP);
                            }

                            Hard = true;
                            break;
                        }
                    case ktTokenType.Id:
                        {
                            if (!Name.IsEmpty())
                            {
                                try
                                {
                                    Class = GetClass(Name);
                                    ClassName = Name;
                                    Hard = true;
                                } catch (Exception) {
                                    throw new ktError("Unexpected " + Token.ToString() + " (" +
                                                          Token.Value + ") on line " +
                                                          Token.LineNo.ToString() + " by character " +
                                                          Token.CharPos.ToString() + "!",
                                                      ktERR.UNEXP);
                                }
                            }

                            Name = Token.Value;
                            break;
                        }
                }
            }

            Value.Constant = Constant;
            Value.HardType = Hard;
            if (!Value.IsNull())
            {
                ((ktClass)Value.Value).HardType = Hard;
                ((ktClass)Value.Value).IsConstant = Constant;
            }

            if (Class != null)
            {
                ktList Arg = new ktList();
                ktClass Obj = Class.CreateObject();
                Obj.HardType = Hard;
                Obj.IsConstant = Constant;

                if (!Value.IsNull())
                {
                    Arg.Add(Value);
                    Obj.RunMethod("Assign", Arg);
                }

                Value = new ktValue(Name, ClassName, Obj, Hard, Constant); 
            }

            SetVariable(Name, true, Value, true, true);

            return Value;
        }
예제 #19
0
        public static ktList Replace(ktList Replacements, ktString Haystack)
        {
            ktList Results = new ktList();
            ktString Pattern = new ktString();
            ktString Replacement = new ktString();
            ktRE_MatchEvaluator Ev = null;
            ktNode Node;
            ktString Result = "";
            object First = null, Second = null;

            Replacements.Reset();
            foreach (ktList Post in Replacements)
            {
                if (Post.IsEmpty())
                {
                    if ((Post.Node != null) && (Post.Node.Value != null))
                    {
                        Pattern = Post.Node.Name;
                        if ((Post.Node.Value.GetType() == typeof(ktString)) ||
                            (Post.Node.Value.GetType() == typeof(string)))
                        {
                            Replacement = Post.Node.Value.ToString();
                            Results.Add(Replace(Pattern, Haystack, Replacement));
                        }
                        else if (Post.Node.Value.GetType() == typeof(ktRE_MatchEvaluator))
                        {
                            Ev = (ktRE_MatchEvaluator)Post.Node.Value;
                            Results.Add(Replace(Pattern, Haystack, Ev));
                        }
                        else if (Post.Node.Value.GetType() == typeof(ktRegEx))
                        {
                            //ktRegEx RE = (ktRegEx)Post.Node.Value;
                            if (Pattern.IsEmpty())
                            {
                                //Results.Add( RE.Replace( Haystack, Ev ) );
                            }
                            else
                            {
                                //Results.Ad‭d( Replace( Pattern, Haystack, Ev ) );
                            }
                        }
                    }
                }
                else if (Post.Count == 2)
                {
                    if ((Node = Post.GetNode(0)) != null)
                    {
                        First = Node.Value;
                        if ((Node = Post.GetNode(1)) != null)
                        {
                            Second = Node.Value;
                        }
                    }
                    if (Second != null)
                    {
                        if (First.GetType() == typeof(ktRegEx))
                        {
                            Result = HandleRegExFirst((ktRegEx)First, Second, Haystack);
                            Results.Add(Result);
                        }
                        else if (First.GetType() == typeof(ktString[]))
                        {
                            ktString[] Patterns = (ktString[])First;

                            foreach (ktString P in Patterns)
                            {
                                Result = HandleStringFirst(P, Second, Haystack);
                                Results.Add(Result);
                            }
                        }
                        else if (First.GetType() == typeof(ktList))
                        {
                            ktList Patterns = (ktList)First;

                            foreach (ktList P in Patterns)
                            {
                                if ((P.Node != null) && (P.Node.Value != null))
                                {
                                    Pattern = P.Node.Value.ToString();
                                    Result = HandleStringFirst(Pattern, Second, Haystack);
                                    Results.Add(Result);
                                }
                            }
                        }
                        else /*if ((First.GetType() == typeof( ktString )) ||
                            (First.GetType() == typeof( string )))*/
                        {
                            Result = HandleStringFirst(First.ToString(), Second, Haystack);
                            Results.Add(Result);
                        }
                    }
                }
            }

            return Results;
        }
예제 #20
0
        public virtual ktList GetContexts(bool JustNames)
        {
            ktList Contexts = new ktList();
            Contexts.Node = new ktNode("Contexts of module  '" + m_Name + "'");

            if (m_Contexts == null)
            {
                return Contexts;
            }

            m_Contexts.Reset();
            foreach (ktList CL in m_Contexts)
            {
                if (CL.Node == null)
                {
                    continue;
                }

                if (JustNames)
                {
                    Contexts.Add(CL.Node.Name);
                }
                else if (CL.Node.Value != null)
                {
                    Contexts.Add((ktContext)CL.Node.Value);
                }
            }

            return Contexts;
        }
예제 #21
0
        /// Search...
        // Find all occurence of a pattern in the given value/string
        public ktList FindAllIn(ktString Value, bool AsObject)
        {
            // If we have a pattern?!
            if ((m_Pattern.IsEmpty()) || (m_P == null))
            {
                // Set error (throw exeption??) and return false
                throw new ktError("ktRegExp::FindAllIn(): The pattern is not set!", ktERR.NOTSET);
            }

            ktList List = new ktList();

            MatchCollection Found = m_P.Matches(Value);
            foreach (Match M in Found)
            {
                if (AsObject)
                {
                    List.Add(new ktRE_Match(M));
                }
                else
                {
                    List.Add(M.ToString());
                }
            }

            return List;
        }
예제 #22
0
        public virtual ktList GetProperties()
        {
            ktList Props = new ktList();

            if (m_Properties == null)
            {
                return Props;
            }

            m_Properties.Reset();
            foreach (ktList PL in m_Properties)
            {
                if ((PL.Node == null) || (PL.Node.Value == null))
                {
                    continue;
                }

                Props.Add(PL.Node.Name, ((ktValue)PL.Node.Value).Type);
            }

            return Props;
        }
예제 #23
0
        public ktList GetGroupNumbers()
        {
            // If we have a pattern?!
            if ((m_Pattern.IsEmpty()) || (m_P == null))
            {
                // Set error (throw exeption...) and return false
                throw new ktError("ktRegExp::GetGroupNumbers(): The pattern is not set!", ktERR.NOTSET);
            }

            ktList Names = new ktList();

            foreach (int Group in m_P.GetGroupNumbers())
            {
                Names.Add(Group);
            }

            return Names;
        }
예제 #24
0
        public override ktList GetProperties()
        {
            ktValue This = new ktValue(m_Name, "int", this, true, false);
            ktList List = new ktList();

            List.Add("_", This);
            List.Add("this", This);
            List.Add("_this", This);
            List.Add("object", This);
            List.Add("_object", This);

            List.Add("MathMode", This);

            return List;
        }
예제 #25
0
        public virtual ktList GetClasses(bool JustNames)
        {
            ktList Classes = new ktList();
            Classes.Node = new ktNode("Classes");

            if (m_Classes == null)
            {
                return Classes;
            }

            m_Classes.Reset();
            foreach (ktList CL in m_Classes)
            {
                if (CL.Node == null)
                {
                    continue;
                }

                if (JustNames)
                {
                    Classes.Add(CL.Node.Name);
                }
                else if (CL.Node.Value != null)
                {
                    Classes.Add((ktClass)CL.Node.Value);
                }
            }

            return Classes;
        }