Esempio n. 1
0
        private List <BytecodeFound> IsWhile(List <TokensFound> tokens, int indented = 0)
        {
            var bytecodeFoundList = new List <BytecodeFound>();

            Size = 1;
            WhileLevel.Push(tokens[indented].Line);

            OperationsName.OperationsNameList.TryGetValue(tokens[indented].Token, out string opName);
            bytecodeFoundList.Add(CreateBytecodeFoundObject(opName));

            for (int index = indented + 2; index < tokens.Count; index += Size)
            {
                Size = 0;
                var list = GetBytecodeFound(tokens, index);
                if (list != null)
                {
                    list.ForEach(bytecode =>
                    {
                        Size++;
                        bytecodeFoundList.Add(bytecode);
                    });
                }
                else
                {
                    Size++;
                }
            }

            bytecodeFoundList.Add(CreateBytecodeFoundObject("POP_JUMP_IF_FALSE"));

            return(bytecodeFoundList);
        }
Esempio n. 2
0
        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);
        }