Example #1
0
        /// <summary>
        /// Take source code and generates the corresponding <see cref="Algorithm"/>
        /// </summary>
        /// <param name="path"> lib_path to your source code</param>
        /// <param name="print_src"> printTrace the generated purged code</param>
        /// <param name="pretty_print"> pretty_print the <see cref="RawInstruction"/>s (useful to check nested-instruction priorities)</param>
        /// <param name="default_name"> give a name to your <see cref="Algorithm"/></param>
        /// <returns> the generated <see cref="Algorithm"/></returns>
        public static Algorithm algorithmFromSrcCode(string path, bool print_src = false, bool pretty_print = false, string default_name = "no-name-given")
        {
            // read code
            Context.setStatus(Context.StatusEnum.read_purge);
            Context.setInfo(path);
            Dictionary <int, string> lines = Parser.readSourceCode(path);

            Context.reset();

            if (print_src)
            {
                StringUtils.printStringList(lines.Select(pair => pair.Value).ToList(), true);
            }

            // extract macros
            Context.setStatus(Context.StatusEnum.macro_preprocessing);
            Context.setInfo(null);

            List <(string, string)> macros = Parser.getMacroPreprocessorValues(lines.Select(pair => pair.Value).ToList());

            foreach (var pair in macros)
            {
                Parser.handleMacro(pair.Item1, pair.Item2);
            }

            Context.reset();

            // Parse code into RawInstructions
            List <RawInstruction> raw_instructions = RawInstruction.code2RawInstructions(lines);

            Parser.print("raw_instructions done");

            // Pretty-printTrace code
            foreach (RawInstruction instruction in raw_instructions)
            {
                if (pretty_print)
                {
                    instruction.prettyPrint();
                }
            }

            // Build instructions from the RawInstructions
            List <Instruction> instructions = RawInstruction.buildInstructions(raw_instructions);

            string    algo_name = default_name;
            Algorithm algo      = new Algorithm(algo_name, instructions);

            return(algo);
        }
Example #2
0
        /// <summary>
        /// Execute the input lines
        /// </summary>
        /// <param name="lines"> input lines</param>
        /// <param name="clear_lines"> clear the input lines at the end of execution</param>
        private static void executeLines(List <string> lines, bool clear_lines = true)
        {
            // execute line here
            try
            {
                int temp_i = 0;
                List <RawInstruction> raw_instructions = RawInstruction.code2RawInstructions(lines.ToDictionary(_ => temp_i++, x => x));
                List <Instruction>    instructions     = RawInstruction.buildInstructions(raw_instructions);
                foreach (Instruction instr in instructions)
                {
                    instr.execute();
                }
            }
            catch (Exception e)
            {
                Global.stdoutWriteLine(e);
            }

            if (clear_lines)
            {
                lines.Clear();
            }
        }