Ejemplo n.º 1
0
        public HrmpProgram Parse(string filepath)
        {
            HrmpProgram program = null;

            using (StreamReader file = new StreamReader(filepath))
            {
                string      lastLabel   = null;
                CommandList commandList = new CommandList();

                string line;
                while ((line = file.ReadLine()) != null)
                {
                    ICommand command = ParseLine(line);
                    if (command == null)
                    {
                        // Not a command, check if it is a label for a jump.
                        if (line.EndsWith(":"))
                        {
                            // If there was a previous label, save the current commandList to that ExecutionNode.
                            if (lastLabel != null && program != null)
                            {
                                ExecutionNode en = new ExecutionNode(lastLabel, commandList);
                                program.AddExecutionNode(en);
                            }
                            // Else, create the main ExecutionNode and create the HrmpProgram.
                            else
                            {
                                ExecutionNode en = new ExecutionNode("main", commandList);
                                program = new HrmpProgram(en);
                            }

                            // Clear the current commandList.
                            commandList.Clear();

                            // Save the label for the next ExecutionNode.
                            line      = line.Remove(line.Count() - 1);
                            lastLabel = line;
                        }

                        continue;
                    }
                    // Else, it is a command.
                    else
                    {
                        commandList.Add(command);
                    }
                }

                // End of file, so add the current commandList.
                // If there was a previous label, save the current commandList to that ExecutionNode.
                if (lastLabel != null && program != null)
                {
                    ExecutionNode en = new ExecutionNode(lastLabel, commandList);
                    program.AddExecutionNode(en);
                }
                // Else, create the main ExecutionNode and create the HrmpProgram.
                else
                {
                    ExecutionNode en = new ExecutionNode("main", commandList);
                    program = new HrmpProgram(en);
                }

                file.Close();
            }

            return(program);
        }
        public HrmpProgram Parse(string filepath)
        {
            HrmpProgram program = null;

            using (StreamReader file = new StreamReader(filepath))
            {
                string lastLabel = null;
                CommandList commandList = new CommandList();

                string line;
                while ((line = file.ReadLine()) != null)
                {
                    ICommand command = ParseLine(line);
                    if (command == null)
                    {
                        // Not a command, check if it is a label for a jump.
                        if (line.EndsWith(":"))
                        {
                            // If there was a previous label, save the current commandList to that ExecutionNode.
                            if (lastLabel != null && program != null)
                            {
                                ExecutionNode en = new ExecutionNode(lastLabel, commandList);
                                program.AddExecutionNode(en);
                            }
                            // Else, create the main ExecutionNode and create the HrmpProgram.
                            else
                            {
                                ExecutionNode en = new ExecutionNode("main", commandList);
                                program = new HrmpProgram(en);
                            }

                            // Clear the current commandList.
                            commandList.Clear();

                            // Save the label for the next ExecutionNode.
                            line = line.Remove(line.Count() - 1);
                            lastLabel = line;
                        }

                        continue;
                    }
                    // Else, it is a command.
                    else
                    {
                        commandList.Add(command);
                    }
                }

                // End of file, so add the current commandList.
                // If there was a previous label, save the current commandList to that ExecutionNode.
                if (lastLabel != null && program != null)
                {
                    ExecutionNode en = new ExecutionNode(lastLabel, commandList);
                    program.AddExecutionNode(en);
                }
                // Else, create the main ExecutionNode and create the HrmpProgram.
                else
                {
                    ExecutionNode en = new ExecutionNode("main", commandList);
                    program = new HrmpProgram(en);
                }

                file.Close();
            }

            return program;
        }