Ejemplo n.º 1
0
        /// <summary>
        /// Process the <see cref="Event"/>
        /// </summary>
        /// <param name="event_"> <see cref="Event"/> that should be processed</param>
        public override void update(Event event_)
        {
            // blocked context ?
            if (Context.isFrozen() && !Global.getSetting("allow tracing in frozen context"))
            {
                printTrace("Context is blocked in function tracer update call. Normal behaviour ?");
                return;
            }
            // checks
            Debugging.assert(event_ != null);
            // extract
            events.Push(event_);

            printTrace("event stack size: " + events.Count);

            Alteration alter    = event_.alter;
            Variable   affected = alter.affected;

            Debugging.assert(affected != null);
            Debugging.assert(affected.isTraced());
            affected.tracer.update(new Event(new Alteration(traced_func, affected, event_.alter.main_value, event_.alter.minor_values)));  //! just push event_ !

            printTrace("updated all in function tracer update");
        }
Ejemplo n.º 2
0
        /// <summary>
        /// This function should be called at the end of every <see cref="Instruction.execute"/> call that is susceptible of
        /// changing the value of a <see cref="Variable"/>. It works in 3 steps:
        /// <para/>* Checks for any new assigned <see cref="Variable"/>s in the <see cref="Global._variable_stack"/> (if found: adds to the <see cref="Global.usable_variables"/>)
        /// <para/>* Update/Process all the awaiting <see cref="Event"/>s (<seealso cref="update"/>)
        /// <para/>* Check for any new <see cref="Event"/> which has not been processed by the <see cref="Global.tracer_update_handler_function"/>
        /// </summary>
        /// <param name="add_call_info"> stands for "additional calling information". If it has any value, it will be printed by the <see cref="Debugging.print"/> function for debugging</param>
        public static void updateTracers(string add_call_info = "")
        {
            if (Global.getSetting("test mode"))
            {
                return;
            }
            printTrace("updating tracers");
            // printTrace alter info about the call if needed
            if (add_call_info != "")
            {
                printTrace(add_call_info);
            }
            // check for new usable variables
            foreach (var pair in Global.getCurrentDict().Where(pair => !Global.usable_variables.Contains(pair.Key)))
            {
                printTrace("checking potential var ", pair.Value is NullVar ? StringConstants.Types.NULL_TYPE : pair.Value.getName());
                if (pair.Value is NullVar || !pair.Value.assigned)
                {
                    continue;
                }
                // new usable variable !
                printTrace("var ", pair.Value.getName(), " is usable (non-null & assigned)");
                Global.usable_variables.Add(pair.Key);
            }

            // checking tracers
            checkAllAwaitingEvents();

            // check tracer event stacks
            printTrace("checking variable tracers event stack counts");
            foreach (VarTracer tracer in Global.var_tracers)
            {
                // unread tracers updates
                while (tracer.last_stack_count != tracer.getStackCount())
                {
                    printTrace("stack count changed for ", tracer.getVar().getName(), " from ", tracer.last_stack_count, " to ", tracer.getStackCount());
                    //Global.stdoutWriteLine("call graphical function " + StringUtils.varList2String(tracer.getVar().getRawValue()) + " & call event: " + tracer.peekEvent().ToString());

                    // traced functions have already been processed. checking awaiting stacks
                    int diff = tracer.getStackCount() - tracer.last_stack_count;
                    Debugging.assert(diff > 0); // will run forever

                    tracer.last_stack_count++;  //! here
                }

                // awaiting tracer stacks
                while (tracer._awaiting_events.Count != 0)
                {
                    printTrace("awaiting stacks: ", tracer._awaiting_events.Count);
                    Alteration alter = tracer._awaiting_events.Peek().alter;
                    callUpdateHandler(alter);
                    tracer.update(tracer._awaiting_events.Pop());
                }
            }

            // update data_tree
            printTrace("Updating Global.data_tree");
            // ReSharper disable once InvertIf
            if (Global.getSetting("update data tree"))
            {
                Debugging.assert(Global.data_tree != null,
                                 new AquilaExceptions.RuntimeError("Tried to update data_tree, but it is null"));
                Global.data_tree = Global.data_tree.update();
            }
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Create a new <see cref="Event"/>
 /// </summary>
 /// <param name="alter"></param>
 public Event(Alteration alter)
 {
     _status    = Context.getStatus();
     _info      = Context.getInfo();
     this.alter = alter;
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Process the input from the interpreter.
        /// If the input is one of the interactive-mode-only commands, the command should not
        /// be executed by the interpreter but will be executed by this function manually.
        /// </summary>
        /// <param name="input"> command line from the interactive-mode</param>
        /// <returns> should the command be executed ?</returns>
        internal static bool processInterpreterInput(string input)
        {
            switch (input)
            {
            case "help":
            {
                Global.stdoutWriteLine("All existing interactive-mode-only commands:");
                // ReSharper disable once RedundantExplicitArrayCreation
                foreach (string command in new string[]
                    {
                        "help", "exit", "reset_env", "clear",                                                  // base interactive-mode
                        "exec", "exec_info",                                                                   // exec
                        "debug", "trace_debug", "parse_debug",                                                 // debugging (enable/disable)
                        "eval %expr",                                                                          // expr
                        "var %var_name", "vars", "$%var_name",                                                 // variables
                        // ReSharper disable once StringLiteralTypo
                        "funcs", "df_funcs",                                                                   // functions
                        "type",                                                                                // ?
                        "trace_info", "trace_uniq_stacks", "rewind %n %var_name", "peek_trace $%traced_value", // trace
                        "get_context", "set_status", "set_info_null", "reset_context",                         // context
                        "scope_info",                                                                          // scope
                        "show-settings",                                                                       // settings
                    })
                {
                    Global.stdoutWriteLine("  -> " + command);
                }

                return(false);
            }

            case "clear":
                if (!Global.getSetting("redirect debug stout & stderr"))
                {
                    Console.Clear();
                }
                return(false);

            case "vars":
            {
                Global.stdoutWriteLine("Global Variables:");
                foreach (var pair in Global.getGlobalVariables())
                {
                    Global.stdoutWriteLine("\t" + pair.Key + " : " + pair.Value);
                }
                int i = 0;
                Global.stdoutWriteLine("Local Scope Variables:");
                foreach (var dict in Global.getCurrentDictList())
                {
                    i++;
                    foreach (var pair in dict)
                    {
                        Global.stdoutWrite(new string('\t', i));
                        Global.stdoutWrite(pair.Key + " : ");
                        Global.stdoutWriteLine(pair.Value.ToString());
                    }
                }

                return(false);
            }

            // ReSharper disable once StringLiteralTypo
            case "funcs":
            {
                Global.stdoutWriteLine("User defined functions:");
                foreach (var pair in Functions.user_functions)
                {
                    Global.stdoutWriteLine(" * " + pair.Key + (pair.Value.isRec() ? " : [rec] " : " : ") + "(" + pair.Value.func_args.Count + ") -> " + pair.Value.getType());
                }

                return(false);
            }

            case "df_funcs":
                Global.stdoutWriteLine("Default (predefined) functions:");
                foreach (var pair in Functions.functions_dict)
                {
                    MethodInfo method = pair.Value.GetMethodInfo();
                    Global.stdoutWriteLine(" * " + pair.Key + " [?] : (" + method.GetParameters().Length + ") -> " + method.ReturnType);
                }

                return(false);

            case "debug":
                Global.setSetting("debug", !Global.getSetting("debug"));
                return(false);

            case "trace_debug":
                Global.setSetting("trace debug", !Global.getSetting("trace debug"));
                return(false);

            case "parse_debug":
                Global.setSetting("parse debug", !Global.getSetting("parse debug"));
                return(false);

            case "trace_info":
            {
                Global.stdoutWriteLine("var tracers:");
                foreach (VarTracer tracer in Global.var_tracers)
                {
                    Global.stdoutWriteLine(" - var     : " + (tracer.getTracedObject() as Variable).getName());
                    Global.stdoutWriteLine(" - stack   : " + tracer.getStackCount());
                    Global.stdoutWriteLine(" - updated : " + tracer.last_stack_count);
                }
                Global.stdoutWriteLine("func tracers:");
                foreach (FuncTracer tracer in Global.func_tracers)
                {
                    Global.stdoutWriteLine(" - func  : " + tracer.getTracedObject());
                    Global.stdoutWriteLine(" - stack : " + tracer.getStackCount());
                }

                return(false);
            }

            case "trace_uniq_stacks":
                Global.var_tracers[0].printEventStack();
                return(false);

            case "reset_env":
                Global.resetEnv();
                Global.stdoutWriteLine(" [!] Global Environment reset");

                return(false);

            case "get_context":
            {
                int status = Context.getStatus();
                Context.StatusEnum status_quote = (Context.StatusEnum)status;
                object             info         = Context.getInfo();
                bool blocked = Context.isFrozen();
                bool enabled = Global.getSetting("fail on context assertions");
                Global.stdoutWriteLine("status  : " + status);
                Global.stdoutWriteLine("quote   : " + status_quote);
                Global.stdoutWriteLine("info    : " + info);
                Global.stdoutWriteLine("blocked : " + blocked);
                Global.stdoutWriteLine("asserts : " + enabled);

                return(false);
            }

            case "set_info_null":
                Context.setInfo(null);
                return(false);

            case "reset_context":
                Context.reset();
                return(false);

            case "type":
                Global.stdoutWriteLine("type of NullVar: " + typeof(NullVar));
                Global.stdoutWriteLine("type of Variable: " + typeof(Variable));
                Global.stdoutWriteLine("type of Integer: " + typeof(Integer));

                return(false);

            case "scope_info":
                Global.stdoutWriteLine("local scope depth: " + Global.getLocalScopeDepth());
                Global.stdoutWriteLine("main scope depth: " + Global.getMainScopeDepth());

                return(false);

            case "show-settings":
                foreach (var pair in Global.getSettings())
                {
                    Global.stdoutWriteLine($" - {pair.Key} : {pair.Value}");
                }

                return(false);
            }

            if (input[0] == '$' && Global.variableExistsInCurrentScope(input.Substring(1)))
            {
                Global.stdoutWriteLine(Global.variableFromName(input.Substring(1)).ToString());
                return(false);
            }

            if (input.StartsWith("set_status "))
            {
                int status = int.Parse(input.Substring(11));
                Context.setStatus((Context.StatusEnum)status);

                return(false);
            }

            if (input.StartsWith("eval "))
            {
                Variable var_ = Expression.parse(input.Substring(5));
                Global.stdoutWriteLine(var_.ToString());
                return(false);
            }

            if (input.StartsWith("var "))
            {
                string var_name = input.Substring(4);
                var_name = StringUtils.normalizeWhiteSpaces(var_name);
                if (var_name == "")
                {
                    return(false);
                }
                Variable var_ = Global.variableFromName(var_name);
                Global.stdoutWriteLine("name     : " + var_.getName());
                Global.stdoutWriteLine("type     : " + var_.getTypeString());
                Global.stdoutWriteLine("const    : " + var_.isConst());
                Global.stdoutWriteLine("assigned : " + var_.assigned);
                Global.stdoutWriteLine("value    : " + var_);
                Global.stdoutWriteLine("traced   : " + var_.isTraced());
                Global.stdoutWriteLine("^ mode   : " + var_.trace_mode);
                if (var_ is NumericalValue value)
                {
                    Global.stdoutWriteLine("*source  : " + StringUtils.dynamic2Str(value.source_vars));
                }
                return(false);
            }

            if (input.StartsWith("rewind"))
            {
                string[] splitted = input.Split(' ');
                if (splitted.Length == 3)
                {
                    if (!int.TryParse(splitted[1], out int n))
                    {
                        Global.stdoutWriteLine("cannot read n");
                        return(false);
                    }

                    string var_name = splitted[2];

                    Variable var_ = Expression.parse(var_name);
                    if (!var_.isTraced())
                    {
                        Global.stdoutWriteLine("Variable is not traced! Use the \"trace\" instruction to start tracing variables");
                        return(false);
                    }

                    var_.tracer.rewind(n);
                    return(false);
                }

                Global.stdoutWriteLine("split count does not match 3");
                return(false);
            }

            if (input.StartsWith("peek_trace"))
            {
                if (input.Length < 11)
                {
                    return(false);
                }
                Variable var_ = Expression.parse(input.Substring(11));
                if (!var_.isTraced())
                {
                    Global.stdoutWriteLine("Variable is not traced! Use the \"trace\" instruction to start tracing variables");
                    return(false);
                }

                Alteration alter = var_.tracer.peekEvent().alter;
                Global.stdoutWriteLine("name         : " + alter.name);
                Global.stdoutWriteLine("variable     : " + alter.affected.getName());
                Global.stdoutWriteLine("main value   : " + StringUtils.dynamic2Str(alter.main_value));
                Global.stdoutWriteLine("num minor    : " + alter.minor_values.Length);
                Global.stdoutWriteLine("minor values : " + StringUtils.dynamic2Str(alter.main_value));
                Global.stdoutWriteLine("stack count  : " + var_.tracer.getStackCount());
                Global.stdoutWriteLine("updated      : " + var_.tracer.last_stack_count);

                return(false);
            }

            if (input.StartsWith("#"))
            {
                Global.stdoutWriteLine("Handling macro parameter");
                if (!input.Contains(' '))
                {
                    Parser.handleMacro(input.Substring(1), null);
                    return(false);
                }

                int index = input.IndexOf(' ');
                // extract the key & value pair
                string value = input.Substring(index);
                input = input.Substring(1, index);
                // purge pair
                value = StringUtils.normalizeWhiteSpaces(value);
                input = StringUtils.normalizeWhiteSpaces(input);
                Parser.handleMacro(input, value);
                return(false);
            }

            return(true);
        }