Exemple #1
0
 /// <summary>
 /// Creates new instance with default Functions/Types/Scope.
 /// </summary>
 public Context()
 {
     Types = new RegisteredTypes();
     ExternalFunctions = new ExternalFunctions();
     Words = new RegisteredWords();
     Plugins = new RegisteredPlugins();
     PluginsMeta = new MetaPluginContainer();
     Symbols = new Symbols();
     Memory = new Memory();
     Limits = new Limits(this);
     TokenAliases = new Dictionary<string, Token>();
     var stack = new CallStack(Limits.CheckCallStack);
     Callbacks = new Callbacks();
     State = new LangState(stack);
     Units = new Units();
     Methods = new RegisteredMethods();
     Plugins.Init();
 }
Exemple #2
0
        /// <summary>
        /// Evaluates a math expression of 2 units.
        /// </summary>
        /// <param name="node">The AST node the evaluation is a part of.</param>
        /// <param name="left">The unit on the left</param>
        /// <param name="right">The unit on the right</param>
        /// <param name="op">The math operator</param>
        /// <param name="units"></param>
        /// <returns></returns>
        public static LObject CalcUnits(AstNode node, LUnit left, LUnit right, Operator op, Units units)
        {
            double baseUnitsValue = 0;
            if (op == Operator.Multiply)
            {
                baseUnitsValue = left.BaseValue * right.BaseValue;
            }
            else if (op == Operator.Divide)
            {
                baseUnitsValue = left.BaseValue / right.BaseValue;
            }
            else if (op == Operator.Add)
            {
                baseUnitsValue = left.BaseValue + right.BaseValue;
            }
            else if (op == Operator.Subtract)
            {
                baseUnitsValue = left.BaseValue - right.BaseValue;
            }
            else if (op == Operator.Modulus)
            {
                baseUnitsValue = left.BaseValue % right.BaseValue;
            }

            var relativeValue = units.ConvertToRelativeValue(baseUnitsValue, left.SubGroup, null);
            var result = new LUnit();
            result.BaseValue = baseUnitsValue;
            result.Group = left.Group;
            result.SubGroup = left.SubGroup;
            result.Value = relativeValue;

            return new LClass(result);
        }