private List <BytecodeFound> IsFor(List <TokensFound> tokens, int indented = 0) { var bytecodeFoundList = new List <BytecodeFound>(); ForLevel.Push(tokens[indented].Line); OperationsName.OperationsNameList.TryGetValue(tokens[indented].Token, out string opName); bytecodeFoundList.Add(CreateBytecodeFoundObject(opName)); OperationsName.OperationsNameList.TryGetValue(tokens[indented + 3].Token, out opName); bytecodeFoundList.Add(CreateBytecodeFoundObject(opName, tokens[indented + 3].Lexema)); OperationsName.OperationsNameList.TryGetValue(tokens[indented + 5].Token, out opName); bytecodeFoundList.Add(CreateBytecodeFoundObject(opName, tokens[indented + 5].Lexema)); var bytecode = new BytecodeFound() { OpName = "CALL_FUNCTION", FriendlyInterpretation = "1 positional, 0 keyword pair", Argument = "" }; bytecodeFoundList.Add(bytecode); bytecode = new BytecodeFound() { OpName = "GET_ITER", Argument = "" }; bytecodeFoundList.Add(bytecode); bytecode = new BytecodeFound() { OpName = "FOR_ITER", Argument = "" }; bytecodeFoundList.Add(bytecode); OperationsName.OperationsNameList.TryGetValue(tokens[indented + 1].Token, out opName); bytecodeFoundList.Add(CreateBytecodeFoundObject(opName, tokens[indented + 1].Lexema)); return(bytecodeFoundList); }
private List <BytecodeFound> GetObjects(int line) { var tokens = TokensList.FindAll(x => x.Line.Equals(line)); var list = new List <BytecodeFound>(); bool hasPlaceJumpForward = false; int index = 0; while (tokens[index].Token.Equals("TOKEN.INDENT")) { index++; IdentLevel.Push(line); if (tokens[index].Token.Equals("TOKEN.ELSE")) { return(list); } } while (tokens[index].Token.Equals("TOKEN.DEDENT")) { index++; if (IdentLevel.Count > 0) { IdentLevel.Pop(); } string nextToken; if (line + 1 <= LastLine.Line) { if ((nextToken = TokensList.FindAll(x => x.Line.Equals(line + 1)).FirstOrDefault().Token.ToString()) != null) { if (nextToken.Equals("TOKEN.DEDENT") && IdentLevel.Count > 0) { IdentLevel.Pop(); } } } if (tokens[index].Token.Equals("TOKEN.ELSE") || tokens[index].Token.Equals("TOKEN.DEF")) { return(list); } int actualLine = tokens[index].Line; InsertIntoLineArgumentsList(actualLine, IdentLevel.Count); if (tokens[index].Token.Equals("TOKEN.IF") || tokens[index].Token.Equals("TOKEN.ELIF")) { InsertIntoLineArgumentsList(actualLine, IdentLevel.Count, tokens[index].Token); if (!hasPlaceJumpForward) { list.Add(new BytecodeFound { OpName = "JUMP_FORWARD", Argument = "" }); hasPlaceJumpForward = true; } else { list.Add(new BytecodeFound { OpName = "JUMP_ABSOLUTE", Argument = "" }); } } else if (WhileLevel.Count > 0 && WhileLevel.Count != WhileCounter) { InsertIntoLineArgumentsList(actualLine, IdentLevel.Count); if (WhileLevel.Count == 1) { list.Add(new BytecodeFound { OpName = "JUMP_ABSOLUTE", Argument = "", Line = line - 1 }); } list.Add(new BytecodeFound { OpName = "POP_BLOCK_WHILE", Line = line - 1 }); WhileLevel.Pop(); WhileCounter++; } else if (ForLevel.Count > 0 && ForLevel.Count != ForCounter) { InsertIntoLineArgumentsList(actualLine, IdentLevel.Count); if (ForLevel.Count == 1) { list.Add(new BytecodeFound { OpName = "JUMP_ABSOLUTE", Argument = "", Line = line - 1 }); } list.Add(new BytecodeFound { OpName = "POP_BLOCK_FOR", Line = line - 1 }); ForLevel.Pop(); ForCounter++; } else if (!tokens[index].Token.Equals("TOKEN.DEDENT") && !tokens[index].Token.Equals("TOKEN.EOF")) { InsertIntoLineArgumentsList(actualLine, IdentLevel.Count); list.Add(new BytecodeFound { OpName = "JUMP_FORWARD", Argument = "" }); } else if (!tokens[index - 1].Token.Equals("TOKEN.DEDENT") && !tokens[index].Token.Equals("TOKEN.EOF")) { InsertIntoLineArgumentsList(actualLine, IdentLevel.Count); list.Add(new BytecodeFound { OpName = "JUMP_ABSOLUTE", Argument = "" }); } } if (tokens[index].Token.Equals("TOKEN.ELSE") || tokens[index].Token.Equals("TOKEN.DEF")) { return(list); } else if (tokens[index].Token.Equals("TOKEN.ID") && tokens[index + 1].Token.Equals("TOKEN.IGUAL")) { IsAssign(tokens, index).ForEach(bytecode => list.Add(bytecode)); return(list); } else if (tokens[index].Token.Equals("TOKEN.IF") || tokens[index].Token.Equals("TOKEN.ELIF")) { IsIf(tokens).ForEach(bytecode => list.Add(bytecode)); return(list); } else if (tokens[index].Token.Equals("TOKEN.WHILE")) { IsWhile(tokens, index).ForEach(bytecode => list.Add(bytecode)); return(list); } else if (tokens[index].Token.Equals("TOKEN.FOR")) { IsFor(tokens, index).ForEach(bytecode => list.Add(bytecode)); return(list); } else if (tokens[index].Token.Equals("TOKEN.ID") && tokens[index + 1].Token.Equals("TOKEN.PARENTESES_ESQUERDO")) { IsFunction(tokens, index).ForEach(bytecode => list.Add(bytecode)); return(list); } else if (tokens[index].Token.Equals("TOKEN.RETURN")) { IsReturn(tokens, index).ForEach(bytecode => list.Add(bytecode)); return(list); } else if (tokens[index].Token.Equals("TOKEN.EOF")) { var endProgram = new BytecodeFound() { Argument = "0", OpName = "LOAD_CONST", FriendlyInterpretation = "None" }; list.Add(endProgram); OperationsName.OperationsNameList.TryGetValue(tokens[index].Token, out string opName); list.Add(CreateBytecodeFoundObject(opName)); return(list); } return(list); }