<commit_before>﻿using Exporter;
using System;
using System.Collections.Generic;

namespace Crayon
{
    public class FlagParser
    {
        private static readonly string READABLE_BYTE_CODE = "readablebytecode";
        private static readonly string LIBRARY_DEP_TREE = "showLibDepTree";
        private static readonly string SHOW_PERFORMANCE_MARKERS = "showperf";
        private static readonly string BUILD_TARGET = "target";
        private static readonly string BUILD_FILE = "buildfile";
        private static readonly string VM = "vm";
        private static readonly string VM_DIR = "vmdir";
        private static readonly string CBX = "cbx";
        private static readonly string GEN_DEFAULT_PROJ = "genDefaultProj";
        private static readonly string GEN_DEFAULT_PROJ_ES = "genDefaultProjES";
        private static readonly string GEN_DEFAULT_PROJ_JP = "genDefaultProjJP";
        private static readonly string SHOW_LIB_STACK = "showLibStack";

        // don't use the output directory specified by the build file. Use this arg instead.
        private static readonly string OVERRIDE_OUTPUT_DIR = "overrideOutputDir";

        // don't compile resources or generate byte code. Simply generate compile errors.
        private static readonly string ERROR_CHECK_ONLY = "errorCheckOnly";

        // enable compile time output.
        private static readonly string JSON_OUTPUT = "jsonOutput";

        // Use prefixes for each line of output
        // LIBS: library dependency tree (generated by "-librarydeptree")
        // COMP: compile time information
        // STATUS: give some sort of status change. Options: COMPILE-START, COMPILE-END, RUN-START, RUN-ABORTED, RUN-END
        // STDOUT: standard output
        // STDERR: standard error output
        // STACKTRACE: stack trace information
        private static readonly string USE_OUTPUT_PREFIXES = "useOutputPrefixes";

        // Enable the debugger when running a CBX project.
        private static readonly string DEBUGGER = "Debugger";

        private static readonly HashSet<string> ATOMIC_FLAGS = new HashSet<string>() {
            CBX,
            DEBUGGER,
            ERROR_CHECK_ONLY,
            JSON_OUTPUT,
            LIBRARY_DEP_TREE,
            READABLE_BYTE_CODE,
            SHOW_LIB_STACK,
            SHOW_PERFORMANCE_MARKERS,
            USE_OUTPUT_PREFIXES,
        };

        private static readonly HashSet<string> ONE_ARG_FLAGS = new HashSet<string>()
        {
            BUILD_TARGET,
            BUILD_FILE, // this will be implicitly applied to the first argument that ends in .build and has no flag associated with it.
            OVERRIDE_OUTPUT_DIR,
            VM,
            VM_DIR,

            GEN_DEFAULT_PROJ,
            GEN_DEFAULT_PROJ_ES,
            GEN_DEFAULT_PROJ_JP,
        };

        private static readonly Dictionary<string, string> ALIASES = new Dictionary<string, string>()
        {
            { "genDefaultProject", GEN_DEFAULT_PROJ },
            { "genDefaultProjectES", GEN_DEFAULT_PROJ_ES },
            { "genDefaultProjectJP", GEN_DEFAULT_PROJ_JP },
        };

        public static ExportCommand Parse(string[] args)
        {
            Dictionary<string, string> output = new Dictionary<string, string>();
            int i;

            // TODO: I need to fix the TODO below regarding having an unambiguous format.
            // For now, do a first-pass scrape of showLibStack.
            List<string> argsMutable = new List<string>(args);
            for (i = 0; i < argsMutable.Count; ++i)
            {
                if (argsMutable[i].StartsWith("-CR:"))
                {
                    string name = argsMutable[i].Substring("-CR:".Length);
                    if (ATOMIC_FLAGS.Contains(name))
                    {
                        output[name] = "";
                        argsMutable.RemoveAt(i--);
                    }
                    else if (ONE_ARG_FLAGS.Contains(name) && i + 1 < argsMutable.Count)
                    {
                        string value;
                        if (i + 1 == argsMutable.Count || argsMutable[i + 1].StartsWith("-CR:"))
                        {
                            value = "";
                        }
                        else
                        {
                            value = argsMutable[i + 1];
                            argsMutable.RemoveAt(i + 1);
                        }
                        output[name] = value;
                        argsMutable.RemoveAt(i--);
                    }
                    else
                    {
                        throw new InvalidOperationException("Unrecognized compiler flag: " + name);
                    }
                }
            }
            args = argsMutable.ToArray();

            // TODO: change this. This is a hack.
            // Ideally, the format will change to something that is not ambiguous.
            // For now, just check to see if the first argument ends with ".build" and
            // that the second argument is not "-target".
            // Current idea is to have -export Foo.build instead of just Foo.build
            // However, this will make all currently written documentation and tutorials
            // correct, at least for 0.2.1.
            if (args.Length > 0 && args[0].ToLower().EndsWith(".build") && !output.ContainsKey(BUILD_TARGET))
            {
                if (args.Length == 1 || !(args[1].ToLower() == "-target" && args[1].ToLower() == "-CR:target"))
                {
                    string[] directRunArgs = new string[args.Length - 1];
                    Array.Copy(args, 1, directRunArgs, 0, directRunArgs.Length);
                    ExportCommand runCommand = new ExportCommand()
                    {
                        IsDirectCbxRun = true,
                        DirectRunArgs = directRunArgs,
                        BuildFilePath = args[0],
                        DirectRunShowLibStack = output.ContainsKey(SHOW_LIB_STACK),
                    };

                    ParseAdditionalArgs(runCommand, output);

                    return runCommand;
                }
            }

            i = 0;
            while (i < args.Length)
            {
                string arg = args[i];
                if (arg.StartsWith("-"))
                {<commit_msg>logic for detecting if a command line command is for export or for running was incorrect and had an always-true expression in an if statement to make it run every time.<commit_after>﻿using Exporter;
using System;
using System.Collections.Generic;

namespace Crayon
{
    public class FlagParser
    {
        private static readonly string READABLE_BYTE_CODE = "readablebytecode";
        private static readonly string LIBRARY_DEP_TREE = "showLibDepTree";
        private static readonly string SHOW_PERFORMANCE_MARKERS = "showperf";
        private static readonly string BUILD_TARGET = "target";
        private static readonly string BUILD_FILE = "buildfile";
        private static readonly string VM = "vm";
        private static readonly string VM_DIR = "vmdir";
        private static readonly string CBX = "cbx";
        private static readonly string GEN_DEFAULT_PROJ = "genDefaultProj";
        private static readonly string GEN_DEFAULT_PROJ_ES = "genDefaultProjES";
        private static readonly string GEN_DEFAULT_PROJ_JP = "genDefaultProjJP";
        private static readonly string SHOW_LIB_STACK = "showLibStack";

        // don't use the output directory specified by the build file. Use this arg instead.
        private static readonly string OVERRIDE_OUTPUT_DIR = "overrideOutputDir";

        // don't compile resources or generate byte code. Simply generate compile errors.
        private static readonly string ERROR_CHECK_ONLY = "errorCheckOnly";

        // enable compile time output.
        private static readonly string JSON_OUTPUT = "jsonOutput";

        // Use prefixes for each line of output
        // LIBS: library dependency tree (generated by "-librarydeptree")
        // COMP: compile time information
        // STATUS: give some sort of status change. Options: COMPILE-START, COMPILE-END, RUN-START, RUN-ABORTED, RUN-END
        // STDOUT: standard output
        // STDERR: standard error output
        // STACKTRACE: stack trace information
        private static readonly string USE_OUTPUT_PREFIXES = "useOutputPrefixes";

        // Enable the debugger when running a CBX project.
        private static readonly string DEBUGGER = "Debugger";

        private static readonly HashSet<string> ATOMIC_FLAGS = new HashSet<string>() {
            CBX,
            DEBUGGER,
            ERROR_CHECK_ONLY,
            JSON_OUTPUT,
            LIBRARY_DEP_TREE,
            READABLE_BYTE_CODE,
            SHOW_LIB_STACK,
            SHOW_PERFORMANCE_MARKERS,
            USE_OUTPUT_PREFIXES,
        };

        private static readonly HashSet<string> ONE_ARG_FLAGS = new HashSet<string>()
        {
            BUILD_TARGET,
            BUILD_FILE, // this will be implicitly applied to the first argument that ends in .build and has no flag associated with it.
            OVERRIDE_OUTPUT_DIR,
            VM,
            VM_DIR,

            GEN_DEFAULT_PROJ,
            GEN_DEFAULT_PROJ_ES,
            GEN_DEFAULT_PROJ_JP,
        };

        private static readonly Dictionary<string, string> ALIASES = new Dictionary<string, string>()
        {
            { "genDefaultProject", GEN_DEFAULT_PROJ },
            { "genDefaultProjectES", GEN_DEFAULT_PROJ_ES },
            { "genDefaultProjectJP", GEN_DEFAULT_PROJ_JP },
        };

        public static ExportCommand Parse(string[] args)
        {
            Dictionary<string, string> output = new Dictionary<string, string>();
            int i;

            // TODO: I need to fix the TODO below regarding having an unambiguous format.
            // For now, do a first-pass scrape of showLibStack.
            List<string> argsMutable = new List<string>(args);
            for (i = 0; i < argsMutable.Count; ++i)
            {
                if (argsMutable[i].StartsWith("-CR:"))
                {
                    string name = argsMutable[i].Substring("-CR:".Length);
                    if (ATOMIC_FLAGS.Contains(name))
                    {
                        output[name] = "";
                        argsMutable.RemoveAt(i--);
                    }
                    else if (ONE_ARG_FLAGS.Contains(name) && i + 1 < argsMutable.Count)
                    {
                        string value;
                        if (i + 1 == argsMutable.Count || argsMutable[i + 1].StartsWith("-CR:"))
                        {
                            value = "";
                        }
                        else
                        {
                            value = argsMutable[i + 1];
                            argsMutable.RemoveAt(i + 1);
                        }
                        output[name] = value;
                        argsMutable.RemoveAt(i--);
                    }
                    else
                    {
                        throw new InvalidOperationException("Unrecognized compiler flag: " + name);
                    }
                }
            }
            args = argsMutable.ToArray();

            // TODO: change this. This is a hack.
            // Ideally, the format will change to something that is not ambiguous.
            // For now, just check to see if the first argument ends with ".build" and
            // that the second argument is not "-target".
            // Current idea is to have -export Foo.build instead of just Foo.build
            // However, this will make all currently written documentation and tutorials
            // incorrect, at least for 0.2.1.
            if (args.Length > 0 && args[0].ToLower().EndsWith(".build") && !output.ContainsKey(BUILD_TARGET))
            {
                if (args.Length == 1 || (args[1] != "-target" && args[1] != "-CR:target"))
                {
                    string[] directRunArgs = new string[args.Length - 1];
                    Array.Copy(args, 1, directRunArgs, 0, directRunArgs.Length);
                    ExportCommand runCommand = new ExportCommand()
                    {
                        IsDirectCbxRun = true,
                        DirectRunArgs = directRunArgs,
                        BuildFilePath = args[0],
                        DirectRunShowLibStack = output.ContainsKey(SHOW_LIB_STACK),
                    };

                    ParseAdditionalArgs(runCommand, output);

                    return runCommand;
                }
            }

            i = 0;
            while (i < args.Length)
            {
                string arg = args[i];
                if (arg.StartsWith("-"))
                {
{'fraction_non_alphanumeric': 0.08026418026418027, 'fraction_numerical': 0.0029526029526029526, 'mean_word_length': 1.5881761512165695, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5975203', 'n_tokens_mistral': 3253, 'n_tokens_neox': 3053, 'n_words': 944}