Exemplo n.º 1
0
        public IEnumerable <MemoryItem> GetMemory()
        {
            var variables = _scope.GetVariableNames();

            foreach (var variable in variables)
            {
                if (!variable.StartsWith("_") && !_functionTypes.Where(t => t.Name == variable).Any())
                {
                    object var = _scope.GetVariable(variable);

                    var result = new MemoryItem
                    {
                        VariableName = variable,
                        TypeName     = var?.GetType()?.Name,
                        Value        = StringFormatter.DisplayString(var, false, true, TrigonometryMode)
                    };
                    yield return(result);
                }
            }
        }
Exemplo n.º 2
0
        public Task <CalculatorResult> Calculate(string commandLine)
        {
            return(Task.Run(() =>
            {
                try
                {
                    _linebuffer.Clear();

                    if (string.IsNullOrEmpty(commandLine))
                    {
                        return new CalculatorResult(Status.ResultOk, "0", string.Empty, 0.0d);
                    }

                    var processed = _preprocessor.Process(commandLine);

                    ScriptSource source = _engine.CreateScriptSourceFromString(processed, SourceCodeKind.AutoDetect);

                    object result = source.Execute(_scope);

                    if (result != null)
                    {
                        _scope.SetVariable("ans", result);
                        return new CalculatorResult(Status.ResultOk,
                                                    StringFormatter.DisplayString(result, PreferPrefixes, GroupByThousands, TrigonometryMode),
                                                    _linebuffer.ToString(),
                                                    result);
                    }
                    else
                    {
                        return new CalculatorResult(Status.NoResult, string.Empty, _linebuffer.ToString(), null);
                    }
                }
                catch (Exception ex)
                {
                    return new CalculatorResult(Status.ResultError, ex.Message, _linebuffer.ToString(), null);
                }
            }));
        }