Beispiel #1
0
        /// <summary>
        /// Ported from void process_option(const string& whence, const expr_t::func_t& opt,
        /// </summary>
        public static void ProcessOption(string whence, ExprFunc opt, Scope scope, string arg, string name)
        {
            try
            {
                CallScope args = new CallScope(scope);

                args.PushBack(NValue.Get(whence));
                if (!String.IsNullOrEmpty(arg))
                {
                    args.PushBack(NValue.Get(arg));
                }

                opt(args);
            }
            catch (CountError)
            {
                throw; // DM - error_count is not std::exception and may pass by "catch" block
            }
            catch (Exception)
            {
                if (!String.IsNullOrEmpty(name) && name.StartsWith("-"))
                {
                    throw new Exception(String.Format("While parsing option '{0}'", name));
                }
                else
                {
                    throw new Exception(String.Format("While parsing environent variable '{0}'", name));
                }
            }
        }
Beispiel #2
0
        public static CallScope Create(string parm)
        {
            CallScope callScope = new CallScope(new EmptyScope());

            callScope.PushBack(NValue.StringValue(parm));
            return(callScope);
        }
Beispiel #3
0
 public Value Handler(CallScope args)
 {
     if (WantsArg)
     {
         if (args.Size < 2)
         {
             throw new InvalidOperationException(String.Format("No argument provided for {0}", Desc));
         }
         if (args.Size > 2)
         {
             throw new InvalidOperationException(String.Format("To many arguments provided for {0}", Desc));
         }
         if (args[0].Type != ValueTypeEnum.String)
         {
             throw new InvalidOperationException(String.Format("Context argument for {0} not a string", Desc));
         }
         On(args[0].AsString, args[1].AsString);
     }
     else
     {
         if (args.Size < 1)
         {
             throw new InvalidOperationException(String.Format("No argument provided for {0}", Desc));
         }
         if (args[0].Type != ValueTypeEnum.String)
         {
             throw new InvalidOperationException(String.Format("Context argument for {0} not a string", Desc));
         }
         On(args[0].AsString);
     }
     return(NValue.Get(true));
 }
Beispiel #4
0
        public static void ProcessOption(string whence, ExprFunc opt, Scope scope, string arg, string name)
        {
            try
            {
                CallScope args = new CallScope(scope);

                args.PushBack(NValue.Get(whence));
                if (!String.IsNullOrEmpty(arg))
                {
                    args.PushBack(NValue.Get(arg));
                }

                opt(args);
            }
            catch
            {
                if (!String.IsNullOrEmpty(name) && name.StartsWith("-"))
                {
                    throw new Exception(String.Format("While parsing option '{0}'", name));
                }
                else
                {
                    throw new Exception(String.Format("While parsing environent variable '{0}'", name));
                }
            }
        }
Beispiel #5
0
        public Value PushCommand(CallScope args)
        {
            // Make a copy at position 2, because the topmost report object has an
            // open output stream at this point.  We want it to get popped off as
            // soon as this command terminate so that the stream is closed cleanly.
            Report temp = ReportStack.Pop();

            ReportStack.Push(new Report(temp));
            ReportStack.Push(temp);
            return(Value.Get(true));
        }
Beispiel #6
0
        public Value FnLotTag(CallScope args)
        {
            Amount amt = args.Get <Amount>(0, false);

            if (amt.HasAnnotation && amt.Annotation.Tag != null)
            {
                return(Value.Get(amt.Annotation.Tag));
            }
            else
            {
                return(Value.Empty);
            }
        }
Beispiel #7
0
 public Value FnAccount(CallScope args)
 {
     if (args[0].Type == ValueTypeEnum.String)
     {
         return(Value.ScopeValue(Journal.FindAccount(args[0].AsString)));
     }
     else if (args[0].Type == ValueTypeEnum.Mask)
     {
         return(Value.ScopeValue(Journal.FindAccountRe(args[0].AsMask.ToString())));
     }
     else
     {
         return(null);
     }
 }
Beispiel #8
0
        // operator()
        public Value Call(CallScope args)
        {
            if (!args.IsEmpty)
            {
                args.PushFront(NValue.StringValue("?expr"));
                return(Handler(args));
            }

            if (WantsArg)
            {
                return(NValue.StringValue(Value));
            }
            else
            {
                return(NValue.Get(Handled));
            }
        }
Beispiel #9
0
        public void ExecuteCommand(IEnumerable <string> args, bool atRepl)
        {
            Session.FlushOnNextDataFile = true;

            // Process the command verb, arguments and options
            if (atRepl)
            {
                args = ReadCommandArguments(Report, args);
                if (!args.Any())
                {
                    return;
                }
            }

            string verb = args.First();

            args = args.Skip(1); // DM - skip the first item that is equal to verb = *arg++;

            // Look for a precommand first, which is defined as any defined function
            // whose name starts with "ledger_precmd_".  The difference between a
            // precommand and a regular command is that precommands ignore the journal
            // data file completely, nor is the user's init file read.
            //
            // Here are some examples of pre-commands:
            //
            //   parse STRING       ; show how a value expression is parsed
            //   eval STRING        ; simply evaluate a value expression
            //   format STRING      ; show how a format string is parsed
            //
            // If such a command is found, create the output stream for the result and
            // then invoke the command.

            ExprFunc  command;
            bool      isPrecommand = false;
            BindScope boundScope   = new BindScope(this, Report);

            command = LookForPrecommand(boundScope, verb);
            if (!command.IsNullOrEmpty())
            {
                isPrecommand = true;
            }

            // If it is not a pre-command, then parse the user's ledger data at this
            // time if not done already (i.e., if not at a REPL).  Then patch up the
            // report options based on the command verb.

            if (!isPrecommand)
            {
                if (!atRepl)
                {
                    Session.ReadJournalFiles();
                }

                Report.NormalizeOptions(verb);

                command = LookForCommand(boundScope, verb);
                if (command.IsNullOrEmpty())
                {
                    throw new LogicError(String.Format(LogicError.ErrorMessageUnrecognizedCommand, verb));
                }
            }

            // Create the output stream (it might be a file, the console or a PAGER
            // subprocess) and invoke the report command.  The output stream is closed
            // by the caller of this function.

            Report.OutputStream = FileSystem.OutputStreamInitialize(
                Report.OutputHandler.Handled ? Report.OutputHandler.Str() : String.Empty,
                Report.PagerHandler.Handled ? Report.PagerHandler.Str() : String.Empty);

            // Now that the output stream is initialized, report the options that will
            // participate in this report, if the user specified --options

            if (OptionsHandler.Handled)
            {
                Report.OutputStream.Write(ReportOptions(Report));
            }

            // Create an argument scope containing the report command's arguments, and
            // then invoke the command.  The bound scope causes lookups to happen
            // first in the global scope, and then in the report scope.

            CallScope commandArgs = new CallScope(boundScope);

            foreach (string arg in args)
            {
                commandArgs.PushBack(Value.Get(arg));
            }

            var info = Logger.Current.InfoContext(TimerName.Command)?.Message("Finished executing command").Start(); // INFO_START

            command(commandArgs);
            info?.Finish(); // INFO_FINISH
        }
Beispiel #10
0
 public Value PopCommand(CallScope args)
 {
     PopReport();
     return(Value.Get(true));
 }
Beispiel #11
0
 public static string JoinArgs(CallScope args)
 {
     return(String.Join(" ", args.ArgsList));
 }
Beispiel #12
0
 public Value FnStr(CallScope args)
 {
     return(Value.StringValue(args[0].AsString));
 }
Beispiel #13
0
 public Value FnInt(CallScope args)
 {
     return(Value.Get(args[0].AsLong));
 }
Beispiel #14
0
 public Value FnMax(CallScope args)
 {
     return(args[1].IsGreaterThan(args[0]) ? args[1] : args[0]);
 }
Beispiel #15
0
 public Value FnMin(CallScope args)
 {
     return(args[1].IsLessThan(args[0]) ? args[1] : args[0]);
 }