Class that describes the current parse context (available functions, constants, variables, ...).
Beispiel #1
0
 /// <summary>
 /// Creates a new query context.
 /// </summary>
 /// <param name="context">The context to reference.</param>
 /// <param name="input">The input to parse</param>
 public QueryContext(ParseContext context, String input)
     : this(context)
 {
     _input = input;
     _parser = new ParseEngine(this, context);
     _functionBuffer = new Dictionary<String, IFunction>();
 }
Beispiel #2
0
 public override PluginInfo Init(PluginInitContext context)
 {
     context_ = context;
     yampContext = Parser.PrimaryContext;
     Parser.InteractiveMode = false;
     Parser.UseScripting = false;
     var info = new PluginInfo();
     info.Keyword = "*";
     return info;
 }
Beispiel #3
0
 /// <summary>
 /// Creates a new context with a custom parent (nested, i.e. more local layer).
 /// </summary>
 /// <param name="parentContext">
 /// The parent context for the new context.
 /// </param>
 public ParseContext(ParseContext parentContext)
 {
     _isReadOnly = false;
     _variables = new Dictionary<String, Value>();
     _functions = new Dictionary<String, IFunction>(StringComparer.OrdinalIgnoreCase);
     _constants = new Dictionary<String, IConstants>(StringComparer.OrdinalIgnoreCase);
     _defaultProperties = new Dictionary<String, IDictionary<String, Value>>();
     _parent = parentContext;
     _precision = parentContext.Precision;
     _displayStyle = parentContext._displayStyle;
 }
Beispiel #4
0
 /// <summary>
 /// Creates a new instance of the parse engine.
 /// </summary>
 /// <param name="query">The query context to consider.</param>
 /// <param name="context">The parser context to use.</param>
 public ParseEngine(QueryContext query, ParseContext context)
 {
     _context = context;
     _query = query;
     _characters = query.Input.ToCharArray();
     _errors = new List<YAMPParseError>();
     _statements = new List<Statement>();
     _markers = Marker.None;
     _currentLine = 1;
     _currentColumn = 1;
 }
Beispiel #5
0
 ParseContext(IDictionary<String, Value> shadowVariables)
 {
     _answer = "$";
     _isReadOnly = true;
     _variables = new ReadOnlyDictionary<String, Value>(shadowVariables);
     _functions = new Dictionary<String, IFunction>(StringComparer.OrdinalIgnoreCase);
     _constants = new Dictionary<String, IConstants>(StringComparer.OrdinalIgnoreCase);
     _defaultProperties = new Dictionary<String, IDictionary<String, Value>>();
     _parent = null;
     _precision = 6;
     _displayStyle = DisplayStyle.Default;
     _elements = new Elements(this);
     _elements.RegisterAssembly(this, Assembly.GetExecutingAssembly());
 }
Beispiel #6
0
        public Value Function(StringValue code)
        {
            var c = new ParseContext(Context);
            var q = new QueryContext(c, code.Value);
            var p = new ParseEngine(q, c).Parse();

            if (p.CanRun)
            {
                foreach (var statement in p.Statements)
                {
                    return statement.Interpret(new Dictionary<String, Value>());
                }
            }

            return new StringValue();
        }
Beispiel #7
0
        /// <summary>
        /// Gives an overview over the included functions and constants within the context.
        /// </summary>
        /// <param name="context">The context to investigate.</param>
        /// <returns>The list with help topics.</returns>
        public static List<HelpTopic> Overview(ParseContext context)
        {
            var topics = new List<HelpTopic>();
            var functions = context.AllFunctions;
            var constants = context.AllConstants;

            foreach (var function in functions)
            {
                AddTypeToTopics(function.Key, "Function", function.Value, topics);
            }

            foreach (var constant in constants)
            {
                AddTypeToTopics(constant.Key, "Constant", constant.Value, topics);
            }

            return topics;
        }
Beispiel #8
0
 public LinfitFunction(ParseContext context)
     : base(context)
 {
 }
Beispiel #9
0
        /// <summary>
        /// Scans for a function entry.
        /// </summary>
        /// <param name="engine">The current parse engine.</param>
        /// <returns>The created expression.</returns>
        public override Expression Scan(ParseEngine engine)
        {
            var start = engine.Pointer;
            var kw = new FunctionKeyword(engine.CurrentLine, engine.CurrentColumn, engine.Query);
            engine.Advance(Token.Length).Skip();

            if (engine.Pointer == engine.Characters.Length)
            {
                kw.Length = engine.Pointer - start;
                engine.AddError(new YAMPFunctionNameMissing(engine), kw);
                return kw;
            }

            kw.name = engine.Elements.FindExpression<SymbolExpression>().Scan(engine) as SymbolExpression;

            if (kw.name == null)
            {
                engine.AddError(new YAMPFunctionNameMissing(engine), kw);
                return kw;
            }

            engine.Skip();

            if (engine.Pointer == engine.Characters.Length)
            {
                kw.Length = engine.Pointer - start;
                engine.AddError(new YAMPFunctionArgumentsMissing(engine), kw);
                return kw;
            }

            kw.arguments = engine.Elements.FindExpression<BracketExpression>().Scan(engine) as BracketExpression;

            if (engine.Pointer == engine.Characters.Length)
            {
                kw.Length = engine.Pointer - start;
                engine.AddError(new YAMPFunctionBodyMissing(engine), kw.arguments);
                return kw;
            }

            kw.Body = engine.ParseStatement();
            kw.Length = engine.Pointer - start;

            if (kw.Body.Container.Expressions.Length == 1 && kw.Body.Container.Expressions[0] is GroupExpression)
            {
                var container = (GroupExpression)kw.Body.Container.Expressions[0];
                var context = new ParseContext(engine.Context.Parent);
                var input = container.Scope.Input;
                container.Scope = new QueryContext(context, input);
            }
            else
            {
                engine.AddError(new YAMPFunctionBodyMissing(engine), kw.arguments);
                return kw;
            }

            if (kw.arguments == null)
            {
                engine.AddError(new YAMPFunctionArgumentsMissing(engine), kw);
                return kw;
            }
            else if (kw.arguments.HasContent && !kw.arguments.IsSymbolList)
            {
                engine.AddError(new YAMPFunctionArgumentsSymbols(engine), kw.arguments);
                return kw;
            }

            return kw;
        }
Beispiel #10
0
        /// <summary>
        /// Formats a given double value with the rules of the context.
        /// </summary>
        /// <param name="context">The context with the rules.</param>
        /// <param name="value">The double precision value.</param>
        /// <returns>The string representation.</returns>
        public static String Format(ParseContext context, Double value)
        {
            if (Double.IsPositiveInfinity(value))
            {
                return "inf";
            }
            else if (Double.IsNegativeInfinity(value))
            {
                return "-inf";
            }
            else if (Double.IsNaN(value))
            {
                return "nan";
            }

            var sign = Math.Sign(value);
            var v = value * sign;
            var t = v;
            var u = v >= 1 ? (Int32)Math.Log10(v) + 1 : 0;
            var l = 0;

            while (Math.Floor(t) != t)
            {
                t *= 10.0;
                l++;
            }

            switch (context.DefaultDisplayStyle)
            {
                case DisplayStyle.Scientific:
                    return ScientificFormat(context, v, sign, u, l);

                case DisplayStyle.Engineering:
                    return EngineeringFormat(context, v, sign, u, l);

                case DisplayStyle.Default:
                default:
                    return StandardFormat(context, v, sign, u, l);
            }
        }
Beispiel #11
0
 static String StandardFormat(ParseContext context, Double v, Int32 sign, Int32 U, Int32 L)
 {
     return Compose(context, v * sign, Math.Min(context.Precision, L), 0);
 }
Beispiel #12
0
 /// <summary>
 /// Uses the standard string representation.
 /// </summary>
 /// <param name="context">The context to use.</param>
 /// <returns>The string representation.</returns>
 public override String ToString(ParseContext context)
 {
     return ToString(context, 0);
 }
Beispiel #13
0
 public HelpFunction(ParseContext context)
     : base(context)
 {
 }
Beispiel #14
0
        /// <summary>
        /// Method used by YAMP to set a value to an object.
        /// </summary>
        /// <param name="context">The context where this is happening.</param>
        /// <param name="argument">The key of the value to retrieve.</param>
        /// <param name="value">The value to set.</param>
        /// <returns>The value that has been set.</returns>
        public Value Perform(ParseContext context, Value argument, Value value)
        {
            var key = argument as StringValue;

            if (key == null)
            {
                throw new YAMPArgumentWrongTypeException(argument.Header, "String", String.Empty);
            }

            return SetValue(key, value);
        }
Beispiel #15
0
 static Calculator()
 {
     yampContext = Parser.PrimaryContext;
     Parser.InteractiveMode = false;
     Parser.UseScripting = false;
 }
Beispiel #16
0
 public TimeFunction(ParseContext context)
     : base(context)
 {
 }
Beispiel #17
0
 SubPlotValue AddSubPlot(ParseContext context, int row, int column, PlotValue plot, int rowSpan = 1, int columnSpan = 1)
 {
     AddSubPlot(row, column, plot, rowSpan, columnSpan);
     context.ChangeLastPlotTo(this);
     return this;
 }
Beispiel #18
0
        /// <summary>
        /// Sets a subplot by specifying some indices and the value.
        /// </summary>
        /// <param name="context">The context where this is happening.</param>
        /// <param name="indices">The indices to set it (1-dim or 2-dim).</param>
        /// <param name="values">The subplot to set.</param>
        /// <returns>The current instance.</returns>
        public Value Perform(ParseContext context, Value indices, Value values)
        {
            if (values is PlotValue)
            {
                if (indices is ArgumentsValue)
                {
                    var av = (ArgumentsValue)indices;

                    if (av.Length != 2)
                        throw new YAMPArgumentNumberException("SubPlot", av.Length, 2);

                    var rowIndex = 0;
                    var rowSpan = 0;
                    var colIndex = 0;
                    var colSpan = 0;

                    InspectIndex(av[1], out rowIndex, out rowSpan);
                    InspectIndex(av[2], out colIndex, out colSpan);
                    return AddSubPlot(context, rowIndex, colIndex, (PlotValue)values, rowSpan, colSpan);
                }
                else if (indices is ScalarValue)
                {
                    var index = ((ScalarValue)indices).IntValue;

                    if (index < 1 || index > Rows * Columns)
                        throw new YAMPIndexOutOfBoundException(index, 1, Rows * Columns);

                    var rowIndex = (index - 1) % Rows + 1;
                    var colIndex = (index - 1) / Columns + 1;
                    return AddSubPlot(context, rowIndex, colIndex, (PlotValue)values);
                }

                throw new YAMPWrongTypeSuppliedException(indices.Header, "Scalar");
            }

            throw new YAMPWrongTypeSuppliedException(values.Header, "Plot");
        }
Beispiel #19
0
        /// <summary>
        /// Gets a (sub) plot from this container.
        /// </summary>
        /// <param name="context">The context from which this is going on.</param>
        /// <param name="argument">The indices as arguments.</param>
        /// <returns>The subplot.</returns>
        public Value Perform(ParseContext context, Value argument)
        {
            if (argument is ScalarValue)
            {
                var index = ((ScalarValue)argument).IntValue;

                if (index < 1 || index > subplots.Count)
                    throw new YAMPIndexOutOfBoundException(index, 1, subplots.Count);

                return subplots[index - 1].Plot;
            }
            else if (argument is ArgumentsValue)
            {
                var av = (ArgumentsValue)argument;

                if (av.Length != 2)
                    throw new YAMPArgumentNumberException("SubPlot", av.Length, 2);

                var row = 1;
                var column = 1;

                if (av[1] is ScalarValue)
                    row = ((ScalarValue)av[1]).IntValue;
                else
                    throw new YAMPArgumentWrongTypeException(av[1].Header, "Scalar", "SubPlot");

                if (av[2] is ScalarValue)
                    column = ((ScalarValue)av[2]).IntValue;
                else
                    throw new YAMPArgumentWrongTypeException(av[2].Header, "Scalar", "SubPlot");

                if (row < 1 || row > Rows)
                    throw new YAMPIndexOutOfBoundException(row, 1, Rows);
                else if (column < 1 || column > Columns)
                    throw new YAMPIndexOutOfBoundException(column, 1, Columns);

                for (var i = subplots.Count - 1; i >= 0; i--)
                {
                    if (subplots[i].Row <= row && subplots[i].Row + subplots[i].RowSpan - 1 >= row &&
                        subplots[i].Column <= column && subplots[i].Column + subplots[i].ColumnSpan - 1 >= column)
                        return subplots[i].Plot;
                }

                return this;
            }

            throw new YAMPWrongTypeSuppliedException(argument.Header, "Scalar");
        }
Beispiel #20
0
 /// <summary>
 /// Performs the function in the given context.
 /// </summary>
 /// <param name="context">The context where the function is executed.</param>
 /// <param name="argument">The argument of the function.</param>
 /// <returns>The evaluted value.</returns>
 public override Value Perform(ParseContext context, Value argument)
 {
     var function = GetType().GetConstructor(new [] { typeof(ParseContext) }).Invoke(new [] { context });
     return ((SystemFunction)function).Perform(argument);
 }
Beispiel #21
0
 public CplotFunction(ParseContext context)
     : base(context)
 {
 }
Beispiel #22
0
 public Value Perform(ParseContext context, Value argument)
 {
     return _execution(argument);
 }
Beispiel #23
0
        /// <summary>
        /// Returns the string content of this instance.
        /// </summary>
        /// <param name="context">The context of the invocation.</param>
        /// <returns>The value of the object.</returns>
        public override String ToString(ParseContext context)
        {
            var sb = new StringBuilder().AppendLine("{");

            foreach (var element in _values)
            {
                var name = element.Key;
                var value = Intend(element.Value.ToString(context));
                sb.Append(Intendation);
                sb.Append(name);
                sb.Append(" = ");
                sb.Append(value);
                sb.AppendLine(",");
            }

            return sb.Append('}').ToString();
        }
Beispiel #24
0
 /// <summary>
 /// Returns a string representation of the value.
 /// </summary>
 /// <param name="context">The calling context.</param>
 /// <returns>The string representation.</returns>
 public virtual String ToString(ParseContext context)
 {
     return String.Empty;
 }
Beispiel #25
0
 public EvalFunction(ParseContext context)
     : base(context)
 {
 }
Beispiel #26
0
        static string Compose(ParseContext context, Double value, Int32 decimals, Int32 exponent)
        {
            if (exponent == 0)
            {
                return String.Format(numberFormat, "{0:F" + decimals + "}", value);
            }
            else if (context.CustomExponent)
            {
                return String.Format(numberFormat, "{0:F" + decimals + "}{1}", value, ToSuperScript(exponent));
            }

            return String.Format(numberFormat, "{0:F" + decimals + "}e{1}", value, exponent);
        }
Beispiel #27
0
        /// <summary>
        /// Use this string representation if you have a global exponent like
        /// everything is already e5 or similar. In this case the values get
        /// multiplied with e-5 for the output.
        /// </summary>
        /// <param name="context">The parse context to use.</param>
        /// <param name="exponent">The global exponent that is in use.</param>
        /// <returns>The string representation for this global exponent.</returns>
        public String ToString(ParseContext context, Int32 exponent)
        {
            var f = Math.Pow(10, -exponent);
            var re = Format(context, _real * f);

            if (Math.Abs(_imag) >= epsilon)
            {
                var im = Format(context, Math.Abs(_imag) * f) + "i";

                if (Math.Abs(_real) < epsilon)
                {
                    return (_imag < 0.0 ? "-" : String.Empty) + im;
                }

                return String.Format("{0} {2} {1}", re, im, _imag < 0.0 ? "-" : "+");
            }

            return re;
        }
Beispiel #28
0
        static String EngineeringFormat(ParseContext context, Double value, Int32 sign, Int32 U, Int32 L)
        {
            var v = sign * value;

            if (U > 3)
            {
                var pwr = 3 * ((U - 1) / 3);
                return Compose(context, v * Math.Pow(10.0, -pwr), context.Precision, pwr);
            }
            else if (U == 0 && L > 0)
            {
                var f = 0;

                while (Math.Floor(v) == 0)
                {
                    f++;
                    v *= 10.0;
                }

                var t = f / 3;
                var t3 = t * 3 - f;

                if (t3 != 0)
                {
                    t++;
                    v *= Math.Pow(10.0, t3 + 3);
                }

                return Compose(context, v, context.Precision, -3 * t);
            }

            return Compose(context, v, context.Precision, 0);
        }
Beispiel #29
0
 /// <summary>
 /// Gets the length of the output in the given parse context.
 /// </summary>
 /// <param name="context">The query context to consider.</param>
 /// <returns>The length of the string representation</returns>
 public Int32 GetLengthOfString(ParseContext context)
 {
     return ToString(context).Length;
 }
Beispiel #30
0
        static String ScientificFormat(ParseContext context, Double value, Int32 sign, Int32 U, Int32 L)
        {
            var v = sign * value;

            if (U > 1)
            {
                return Compose(context, v * Math.Pow(10.0, 1 - U), context.Precision, U - 1);
            }
            else if (U == 0 && L > 0)
            {
                var f = 0;

                while (Math.Floor(v) == 0)
                {
                    v *= 10.0;
                    f++;
                }

                return Compose(context, v, context.Precision, -f);
            }

            return Compose(context, v, context.Precision, 0);
        }