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; }
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; }
/// <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; }
public ktValue RunLine(ktList Line) { ktValue Ret = ktValue.Null; ktToken Token = null; ktToken LToken = null; if (Line == null) { throw new ktError("ktBlock::RunLine() : Didn't get a line to run!", ktERR.NOTSET); } //#if Debug ktDebug.Log( "RL:" + Line.Get_R( " ", true ) ); //#endif /*if (Token.LineNo == 3) { //ktDebug.Log("L3T: "); ktDebug.Log("L3: " + Line.Get_R(" ", true)); }*/ try { Token = (ktToken)Line.Node.Value; if (Line.GetCount() == 0) { if (Line.Node != null) { if (Token.Type == ktTokenType.String) { Ret = new ktValue(":ktString", "ktString", MakeObjectOf("ktString", Token.Value), true, true); } else if (Token.Type == ktTokenType.Number) { Ret = new ktValue(":ktInt", "ktInt", MakeObjectOf("ktInt", Token.Value), true, true); } else if (Token.Type == ktTokenType.Float) { Ret = new ktValue(":ktFloat", "ktFloat", MakeObjectOf("ktFloat", Token.Value), true, true); } else if (Token.Type == ktTokenType.Id) { Ret = GetVariable(Token.Value); } } else { //throw new ktError( "ktBlock::RunLine() : Didn't get a line to run!", ktERR.NOTSET ); return Ret; } } switch (Token.Type) { case ktTokenType.If: { bool r = CheckLogicStatement(Line.First,Token, ref Ret); if (r) { // m_skipNextLine = true; ktList Next = Line.First.Next; ktToken IfToken = (ktToken)Next.Node.Value; switch (IfToken.Type) { case ktTokenType.Block: { IfToken.Block.SetParent(this); Ret = IfToken.Block.Run(); break; } default: { Ret = RunLine(Next); break; } }; } return Ret; } case ktTokenType.For: { break; } case ktTokenType.Foreach: { break; } case ktTokenType.While: { break; } } Line.Reset(); foreach (ktList TL in Line) { if ((TL.Node == null) || (TL.Node.Value == null)) { continue; } LToken = Token; Token = (ktToken)TL.Node.Value; switch (Token.Type) { case ktTokenType.CompStatement: { Ret = HandleCompound(TL); break; } case ktTokenType.AssignmentOperator: { Ret = HandleOperator(TL); break; } case ktTokenType.Operator: { Ret = HandleOperator(TL); break; } /*case ktTokenType.If: { break; }*/ case ktTokenType.Number: case ktTokenType.String: case ktTokenType.Id: { ktList L = new ktList(); L.AddList(TL); Ret = HandleStatement(L); break; } default: { throw new ktError("ktBlock::RunLine(): Unexpected " + Token.Name + " on line " + Token.LineNo.ToString() + " (by character " + Token.CharPos.ToString() + ")", ktERR.UNEXP); } } #if Debug ktDebug.Log( "New Line!!!!!!!!!!!!" ); #endif } } catch (ktError Err) { //ktDebug.Log("SL:SC:" + Err.ToString() + ";" + Line.Get_R()); if (Token == null) { if (LToken != null) { Token = LToken; } else if (Line.Node != null) { Token = (ktToken)Line.Node.Value; } else if (Line.FirstNode != null) { Token = (ktToken)Line.FirstNode.Value; } else if (Line.LastNode != null) { Token = (ktToken)Line.LastNode.Value; } } if (Token != null) { Err.SetLine(Token.LineNo); Err.SetChar(Token.CharPos); ktDebug.Log(Err.ToString()); } else ktDebug.Log("NOOOOOOOO!!"); throw Err; } #if Debug ktDebug.Log( "EORL" ); #endif /* if (Ret == null) { Ret = Value; }*/ return Ret; }
/// <summary> /// Handle a operator in the tree /// </summary> private bool HandleOperator(ktToken PrevToken, ktList Prev, ktToken Token, ktList L, ref ktList Tree, int RunNumb, out bool SkipNext, bool OneSided = false ) { ktList Next = null; ktList Temp = null; SkipNext = false; // Create a new "subtree"(/operator) and assign appropriate data Temp = new ktList(); Temp.Node = L.Node; ((ktToken)Temp.Node.Value).RunnedStep = RunNumb; // If the left part of the "operator tree" hasn't been parsed on this level ... if (PrevToken.RunnedStep < RunNumb) { // ... parse it! Prev = MakeAOpTree(Prev, RunNumb); } else { Prev = new ktList(Prev); } // Add the first part to the op tree Temp.AddList(Prev); // Is it a operator with two "sides" if (!OneSided) { // Parse the right part of the "operator tree" Next = MakeAOpTree(L.Next, RunNumb); // And add it to the op tree Temp.AddList(Next); SkipNext = true; } /*ktDebug.Log("HO: Temp:\n" + Temp.Get_R()); ktDebug.Log("HO: Tree:\n" + Tree.Get_R());*/ // Remove the last leaf(/item) in the tree (it will now become a leaf under this operator) Tree.Pop(false); // .Pop() is more efficient than .Remove() and 'false' as an argument means that it will "fail" gracefully in the event of an empty list! //ktDebug.Log("HO: TreeP:\n" + Tree.Get_R()); // Add the new subtree/operator to the main tree Tree.AddList(Temp); //ktDebug.Log("HO: TreeA:\n" + Tree.Get_R()); // Hey! That went well! return true; }
/// <summary> /// Get the rest of the line/statement... /// </summary> public ktList GetTheRest(ktList List) { ktList Rest = new ktList(), Curr = null, Next = null; ktToken Token = null; if (List == null) { return null; } if ((List.Node != null) && (List.Node.Value != null)) { Token = (ktToken)List.Node.Value; Token = new ktToken(ktTokenType.Statement, "", Token.LineNo, Token.CharPos); } else { Token = new ktToken(ktTokenType.Statement, "", m_CurLine, m_CharPos); } Rest.Node = new ktNode("ktTStatement", Token); Curr = List.CurrentNode; while (Curr != null) { Next = Curr.Next; Rest.AddList(Curr); Curr = Next; } return Rest; }
/// <summary> /// Handle a "compound satement" /// </summary> /// <param name="Token"></param> /// <param name="L"></param> /// <param name="Tree"></param> /// <param name="List"></param> /// <param name="SkipNext"></param> /// <param name="ReturnTree"></param> /// <returns></returns> private bool HandleCompStatement(ktToken Token, ktList L, ref ktList Tree, ref ktList List, out bool SkipNext, out bool ReturnTree, int RunNumb = 1) { ktList First = null, Second = null, SecondChild = null, Temp = null; ktToken FirstToken = null, SecondToken = null, SecondChildToken = null; SkipNext = false; ReturnTree = false; ktDebug.Log("HandleComp:\n" + L.Get_R()); First = L.First; Second = L.First.Next; SecondChild = Second.First; FirstToken = (ktToken)First.Node.Value; SecondToken = (ktToken)Second.Node.Value; if (FirstToken.Type != ktTokenType.Id) { First = MakeAOpTree(First, RunNumb); } if (SecondChild != null) { SecondChildToken = (ktToken)SecondChild.Node.Value; if (SecondChildToken.Type == ktTokenType.List) { SecondChild = MakeAOpTree(SecondChild, RunNumb); Second.Clear(); Second.AddList(SecondChild); } } else if (SecondToken.Type != ktTokenType.Id) { Second = MakeAOpTree(Second, RunNumb); } Temp = new ktList(); Temp.Node = L.Node; ((ktToken)Temp.Node.Value).RunnedStep = RunNumb; Temp.AddList(First); Temp.AddList(Second); Tree.AddList(Temp); /*ktDebug.Log("HandleComp__:\n" + Temp.Get_R()); ktDebug.Log("HandleComp Tree:\n" + Tree.Get_R());*/ return true; }
private bool HandleAssignmentOperator(ktToken PrevToken, ktList Prev, ktToken Token, ktList L, ref ktList Tree, ref ktList List, out bool SkipNext, out bool ReturnTree) { ktList Next = null; ktList Temp = null; SkipNext = false; ReturnTree = false; /*ktDebug.Log("Tree::\n" + Tree.Get_R()); ktDebug.Log("Prev: T:\n" + Prev.Node.Value.ToString()); ktDebug.Log("Prev::\n" + Prev.Get_R());*/ // Remove the last leaf(/item) in the tree (it will now become a leaf under this operator) Tree.Pop(false); // .Pop() is more efficient than .Remove() and 'false' as an argument means that it will "fail" gracefully in the event of an empty list! // Take a step forward List.MoveNext(); // If the previous token has childs if (Prev.Count > 0) { if ( (((ktToken)Prev.First.Node.Value).Type == ktTokenType.Id) && (Prev.First.Next != null) && (((ktToken)Prev.First.Next.Node.Value).Type == ktTokenType.Id) ) { Prev.Node = new ktNode("ktTVarStatement", new ktToken(ktTokenType.VarStatement, "", ((ktToken)Prev.Node.Value).LineNo, ((ktToken)Prev.Node.Value).CharPos)); ; } else { // Parse the left part of the op tree Prev = MakeAOpTree(Prev, 1); } } // Get the right part of the op tree Next = GetTheRest(List); // Parse the right part of the tree Next = MakeAOpTree(Next, 1); // Create a new "subtree"(/operator) and assign appropriate data Temp = new ktList(); Temp.Node = L.Node; ((ktToken)Temp.Node.Value).RunnedStep = 1; // Add the parts of op tree Temp.AddList(Prev); Temp.AddList(Next); // Add the operator to the tree Tree.AddList(Temp); // ktDebug.Log("Tree::\n" + Tree.Get_R()); ktDebug.WrapLevel--; ReturnTree = true; // Hey! That went well! return true; }
/// <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; }
/// <summary> /// Parse the tokens and create a optree /// </summary> protected ktList MakeAOpTree(ktList List, int RunNumb) { ktList Tree = new ktList(), Prev = null, Temp = null; ktToken Token = null, PrevToken = null; bool result = false, SkipNext = false, ReturnTree = false; // ktDebug.Log("MAOT!! [" + RunNumb + "]\n" + List.Get_R("\t") ); // null is null.. if (List == null) { return null; } ktDebug.WrapLevel++; // Same, same, same... Tree.Node = List.Node; if ((Tree.Node == null) || (Tree.Node.Value == null) || (Tree.Node.Value.GetType() != typeof(ktToken))) { if (Tree.Node == null) { Tree.Node = new ktNode("ktBlock"); } Tree.Node.Value = new ktToken(ktTokenType.Block, "ktBlock", 0, 0); } ((ktToken)Tree.Node.Value).RunnedStep = RunNumb; //#if ParseDebug ktDebug.Log("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\nMAOT(" + RunNumb + "):" + List.Get_R()); //#endif List.Reset(); foreach (ktList L in List) { SkipNext = false; // Check so we don't try to work on nothing! if ((L.Node != null) && (L.Node.Value != null)) { // Get Token and previous... Token = (ktToken)L.Node.Value; Prev = Tree.Last; // Get previous token if possible if ((Prev != null) && (Prev.Node != null) && (Prev.Node.Value != null)) { PrevToken = (ktToken)Prev.Node.Value; } else { PrevToken = new ktToken(ktTokenType.NULLTOKEN, ":null", 0, 0); } // Nothing here?? } // Nothing there? ... else { // ... Lets continue! continue; } //ktDebug.Log("LLLLLLLLLL:::\n" + L.Get_R()); //ktDebug.Log("LiLiLiLiLiLiLiLiLiLi:::\n" + List.Get_R()); //#if ParseDebug3 ktDebug.Log("opTT:" + Token.Type.ToString() + "(" + Token.Value + ")"); ktDebug.Log("opPTT:" + PrevToken.Type.ToString() + "(" + PrevToken.Value + ")"); //#endif // On run/pass 1 if (RunNumb == 1) { // Handle run 1 result = MakeAOpTree_Run1(PrevToken, Prev, Token, L, ref Tree, ref List, out SkipNext, out ReturnTree); } // On run/pass 2 else if (RunNumb == 2) { result = MakeAOpTree_Run2(PrevToken, Prev, Token, L, ref Tree, ref List, out SkipNext, out ReturnTree); } // On run/pass 3 else if (RunNumb == 3) { result = MakeAOpTree_Run3(PrevToken, Prev, Token, L, ref Tree, ref List, out SkipNext, out ReturnTree); } // If nothing was done ... if (!result) { if ((Token.Type == ktTokenType.Id) || (Token.Type == ktTokenType.Null) || (Token.Type == ktTokenType.Number) || (Token.Type == ktTokenType.Float) || (Token.Type == ktTokenType.Boolean) || (Token.Type == ktTokenType.String)/* || (Token.Type == ktTokenType.If)*/ ) { //ktDebug.Log("Constant etc!!(" + Token.Value + ")"); Temp = new ktList(); Temp.Node = L.Node; ((ktToken)Temp.Node.Value).RunnedStep = RunNumb; Tree.AddList(Temp); } else if (Token.Type == ktTokenType.If) { //ktDebug.Log("IF-TOKEN!"); Tree.AddList(new ktList(L)); } else { if ((Token.Type == ktTokenType.Statement) && (L.Count == 0)) { //ktDebug.Log("MAOT(" + RunNumb + "): SKIP(Statement)"); continue; } Temp = MakeAOpTree(L, RunNumb); ktDebug.Log("MAOT(" + RunNumb + "): TEMP:\n" + Temp.Get_R()); if ((Token.Type == ktTokenType.Statement) && (((ktToken)Temp.FirstNode.Value).Type == ktTokenType.If)) { Temp = Temp.First; } if ((Token.Type == ktTokenType.Line) && (!PrevToken.HasBlock) && ((PrevToken.Type == ktTokenType.If) || (PrevToken.Type == ktTokenType.Else) || (PrevToken.Type == ktTokenType.ElseIf) )) { /*ktDebug.Log("ISLINE:PREVIF!"); ktDebug.Log("TEMP: " + Temp.Get_R());*/ Prev.AddList(Temp); } else { Tree.AddList(Temp); } } } //ktDebug.Log("MAOT(" + RunNumb + "): TREE:\n" + Tree.Get_R()); // Should we return the tree we have created?? if (ReturnTree) { // Return the tree and be done with this part! return Tree; } // Should we skip this one? if (SkipNext) { SkipNext = false; List.MoveNext(); } continue; // OLD CODE FOR REFERENCE: if ((RunNumb == 1) && (Token.Type == ktTokenType.Block)) { if (((PrevToken.Type == ktTokenType.If) || (PrevToken.Type == ktTokenType.Else) || (PrevToken.Type == ktTokenType.ElseIf) ) && (!PrevToken.HasBlock)) { /*Tree.Last.AddList(new ktList(L)); ((ktToken)Tree.Last.Node.Value).HasBlock = true;*/ Prev.AddList(new ktList(L)); PrevToken.HasBlock = true; } else { Tree.AddList(new ktList(L)); } } else if ((Token.Type == ktTokenType.Id) || (Token.Type == ktTokenType.Null) || (Token.Type == ktTokenType.Number) || (Token.Type == ktTokenType.Float) || (Token.Type == ktTokenType.Boolean) || (Token.Type == ktTokenType.String)/* || (Token.Type == ktTokenType.If)*/ ) { //ktDebug.Log("Constant etc!!(" + Token.Value + ")"); Temp = new ktList(); Temp.Node = L.Node; ((ktToken)Temp.Node.Value).RunnedStep = RunNumb; if (L.GetCount() != 0) { Temp.AddList(MakeAOpTree(L, RunNumb)); } Tree.AddList(Temp); } else if (Token.Type == ktTokenType.If) { //ktDebug.Log("IF-TOKEN!"); Tree.AddList(new ktList(L)); } else if ((RunNumb == 1) && (PrevToken.Type == ktTokenType.If) && (!PrevToken.HasBlock) && (RunNumb == 1) && ((Token.Type == ktTokenType.Statement) || (Token.Type == ktTokenType.List)) ) { ktList ifL = Tree.Pop(); Temp = new ktList(); Temp.Node = ifL.Node; ((ktToken)Temp.Node.Value).RunnedStep = RunNumb; Temp.AddList(MakeAOpTree(L, RunNumb)); #if ParseDebug ktDebug.Log("MAOT(" + RunNumb + "): IF-statement: " + Temp.Get_R()); #endif Tree.AddList(Temp); #if ParseDebug ktDebug.Log("MAOT(" + RunNumb + "): IF TREE: " + Tree.Get_R()); #endif } } #if ParseDebug ktDebug.Log( "MAOTREE(" + RunNumb + "):\n" + Tree.Get_R( "\t", true ) ); ktDebug.WrapLevel--; if (this.m_enabledDebug && (this.m_enabledAt == ktDebug.WrapLevel)) { ktDebug.D.Disable(); } #endif return Tree; }
/// <summary> /// Add a token to the tokenlist /// </summary> protected bool AddToken(ktToken Token) { /**/ if (m_Tokens == null) { m_Tokens = new ktList(); } return m_Tokens.Add(Token.Name, Token); /*/ if (LastBlockLines == null) { if (LastBlock == null) { throw new ktError( "No Last Block!", ktERR.MISSING ); } LastBlock.SetLines( new ktList() ); } return LastBlockLines.Add( Token.Name, Token );/**/ } /// <summary> /// Handle "separators" /// </summary> protected void HandleSep(ktList First, ktList Second, ref ktList Tree, ktToken Token) { ktDebug.Log("HASEP: T:" + Token.ToString() + "; F:" + First.ToString()); if ((First == null) || (First.Node == null) || (First.Node.Value == null) || (Second == null) || (Second.Node == null) || (Second.Node.Value == null)) { throw new ktError("Unexpected ktTSeparator (" + Token.Value + ") on line " + Token.LineNo.ToString() + " by character " + Token.CharPos.ToString() + ".", ktERR.UNEXP); } ktList Temp = null; ktList Last = Tree.Last; ktToken FirstToken = (ktToken)First.Node.Value; ktToken SecondToken = (ktToken)Second.Node.Value; ktToken LastToken = null; ktDebug.Log( "HASEP:" + SecondToken.ToString() );/* if ((Token.Value == ":") && (SecondToken.Value == ":") ) { //T } else */ //ktDebug.Log( "HASEP:: FT:" + FirstToken.ToString() + "; ST:" + SecondToken.ToString() ); if ( (FirstToken.Type == ktTokenType.Id) && ( SecondToken.Type == ktTokenType.AssignmentOperator ) && ( Token.Value == ":" ) ) { LastToken = new ktToken(ktTokenType.AssignmentOperator, ":=", FirstToken.LineNo, FirstToken.CharPos, ktToken.TokenToString(ktTokenType.AssignmentOperator)); Tree.Remove((uint)(Tree.Count - 1)); Tree.AddList(Temp); ktDebug.Log("TREE:" + Tree.Get_R("\t", true)); return; } else if ((FirstToken.Type != ktTokenType.Id) && (FirstToken.Type != ktTokenType.List) && (FirstToken.Type != ktTokenType.CompStatement) && (FirstToken.Type != ktTokenType.Boolean) && (FirstToken.Type != ktTokenType.Float) && (FirstToken.Type != ktTokenType.Number) && (FirstToken.Type != ktTokenType.String) || ( (SecondToken.Type != ktTokenType.Id) && (SecondToken.Type != ktTokenType.CompStatement) && (SecondToken.Type != ktTokenType.Number) )) { throw new ktError("Unexpected ktTSeparator (" + Token.Value + ") on line " + Token.LineNo.ToString() + " by character " + Token.CharPos.ToString() + ".", ktERR.UNEXP); } #if DeepDebug #if ControlDebug Console.ReadLine(); #endif #endif if ((Last != null) && (Last.Node != null) && (Last.Node.Value != null) && (Last.Node.Value.GetType() == typeof(ktToken))) { LastToken = (ktToken)Last.Node.Value; } //#if ParseDebug2 ktDebug.Log( "FIRST(" + FirstToken.ToString() + "):\n" + First.Get_R( "\t", true ) + "\nSECOND(" + SecondToken.ToString() + "):\n" + Second.Get_R( "\t", true ) ); ktDebug.Log( "TREE:\n" + Tree.Get_R( "\t", true ) ); if (LastToken != null) { ktDebug.Log( "LAST(" + LastToken.ToString() + "):\n" + Last.Get_R( "\t", true ) ); } //#endif if ((FirstToken.Type == ktTokenType.Number) && (SecondToken.Type == ktTokenType.Number)) { //((ktToken)Tree.Last.Node.Value).Value += "." + SecondToken.Value; Tree.Remove((uint)(Tree.Count - 1)); FirstToken.Value += "." + SecondToken.Value; FirstToken.Type = ktTokenType.Float; FirstToken.Name = "ktTFloat"; Tree.Add(FirstToken); } else if ((Last != null) && (LastToken.Type == ktTokenType.CompStatement)) { Tree.Pop(); Temp = new ktList(); Temp.Node = new ktNode("ktTCompStatement", new ktToken(ktTokenType.CompStatement, Token.Value, Token.LineNo, Token.CharPos)); Temp.AddList(new ktList(Last)); Temp.AddList(new ktList(Second)); #if DeepDebug ktDebug.Log( "LCT:" + Temp.Get_R( "\t", true ) ); #endif Tree.AddList(Temp); } else { Tree.Pop(); Temp = new ktList(); Temp.Node = new ktNode("ktTCompStatement", new ktToken(ktTokenType.CompStatement, Token.Value, Token.LineNo, Token.CharPos)); Temp.AddList(new ktList(First)); Temp.AddList(new ktList(Second)); //#if DeepDebug ktDebug.Log( "T:" + Temp.Get_R( "\t", true ) ); //#endif Tree.AddList(Temp); } #if DeepDebug ktDebug.Log( "=========================================" ); #if ControllDebug Console.ReadLine(); #endif #endif }